# Main example for priority queue using dijkstra's algorithm. #15857

Merged
merged 1 commit into from Jul 22, 2014

## Conversation

Projects
None yet
5 participants
Contributor

### treeman commented Jul 21, 2014

 I wanted to have a slightly larger example compared to the method examples, but I'm unsure how it worked out. Feedback would nice.

### steveklabnik reviewed Jul 21, 2014

 //! #[deriving(Eq, PartialEq)] //! struct State { //! cost: uint, //! pos: uint

#### steveklabnik Jul 21, 2014

Member

bikeshed alert: wonder if this shouldn't be a full `position` instead. It's not that much longer.

### steveklabnik reviewed Jul 21, 2014

 //! pos: uint //! } //! //! // The priority queue depends on `Ord`

#### steveklabnik Jul 21, 2014

Member

This should probably have a period at the end.

### steveklabnik reviewed Jul 21, 2014

 //! //! // The priority queue depends on `Ord` //! // Explicitly implement the trait so the queue becomes a min-heap //! // instead of a max-heap

Member

Same here.

### steveklabnik reviewed Jul 21, 2014

 //! } //! } //! //! // Currently `PartialOrd` needs to be implemented as well

#### steveklabnik Jul 21, 2014

Member

Words like 'currently' shouldn't be in the docs. Everything is current. I know it might change in the future, but then this has to change anyway.

Member

### steveklabnik commented Jul 21, 2014

 I really like this idea.
Contributor Author

### treeman commented Jul 22, 2014

 Great! Thanks for the pointers.

### huonw reviewed Jul 22, 2014

 //! //! // Examine the frontier with lower cost nodes first (min-heap) //! while !pq.is_empty() { //! let State { position: u, cost: curr_cost } = pq.pop().unwrap();

#### huonw Jul 22, 2014

Member

This could be

```loop {
let State { position, cost } = match pq.pop() {
None => break, // empty
Some(s) => s
};

// ...
}```

### huonw reviewed Jul 22, 2014

 //! // The graph is represented as an adjecency list where each index, //! // corresponding to a node value, has a list of outgoing edges. //! // Chosen for it's efficiency. //! let graph = vec!(

#### huonw Jul 22, 2014

Member

These can/should be `vec![...]` (rather than `vec!(...)`).

### alexcrichton reviewed Jul 22, 2014

 //! if position == goal { return cost }; //! //! // Important as we may have already found a better way //! if cost > dist[position] { continue };

#### alexcrichton Jul 22, 2014

Member

This semicolon (and the one above) could be removed.

#### steveklabnik Jul 22, 2014

Member

I'd even argue should, it's usually the style

### alexcrichton reviewed Jul 22, 2014

 //! fn shortest_path(adj_list: &Vec>, start: uint, goal: uint) -> uint { //! // dist[node] = current shortest distance from `start` to `node` //! let mut dist: Vec = Vec::new(); //! dist.grow(adj_list.len(), &uint::MAX);

#### alexcrichton Jul 22, 2014

Member

You could combine these two with `let mut dist = Vec::from_elem(adj_list.len(), uint::MAX);`

Member

### alexcrichton commented Jul 22, 2014

 This is a fantastic example, nice job! This looks good to go with two minor comments and some squashings, thanks!
``` Main example for priority queue using dijkstra's algorithm. ```
``` 94500b8 ```
Contributor Author

### treeman commented Jul 22, 2014

 Cool, thanks!
Contributor

### bors commented on `94500b8`Jul 22, 2014

 saw approval from alexcrichton at https://github.com/treeman/rust/commit/94500b84d4355e322efafc4f21c9d29ffc676826
Contributor

### bors replied Jul 22, 2014

 merging treeman/rust/doc-dijkstra-example = 94500b8 into auto
Contributor

### bors replied Jul 22, 2014

 treeman/rust/doc-dijkstra-example = 94500b8 merged ok, testing candidate = 853f53e
Contributor

Contributor

### bors replied Jul 22, 2014

 saw approval from alexcrichton at https://github.com/treeman/rust/commit/94500b84d4355e322efafc4f21c9d29ffc676826
Contributor

### bors replied Jul 22, 2014

 merging treeman/rust/doc-dijkstra-example = 94500b8 into auto
Contributor

### bors replied Jul 22, 2014

 treeman/rust/doc-dijkstra-example = 94500b8 merged ok, testing candidate = 2ffccb7
Contributor

Contributor

### bors replied Jul 22, 2014

 fast-forwarding master to auto = 2ffccb7

### bors added a commit that referenced this pull request Jul 22, 2014

``` auto merge of #15857 : treeman/rust/doc-dijkstra-example, r=alexcrichton ```
```I wanted to have a slightly larger example compared to the method examples, but I'm unsure how it worked out.

Feedback would nice.```
``` 853f53e ```

### bors added a commit that referenced this pull request Jul 22, 2014

``` auto merge of #15857 : treeman/rust/doc-dijkstra-example, r=alexcrichton ```
```I wanted to have a slightly larger example compared to the method examples, but I'm unsure how it worked out.

Feedback would nice.```
``` 2ffccb7 ```

### bors merged commit `94500b8` into rust-lang:master Jul 22, 2014 2 checks passed

#### 2 checks passed

continuous-integration/travis-ci The Travis CI build passed
Details
default all tests passed