# Write a greedy algorithm to generate shortest path problem

If our denominations list is as above, then [6, 3, 0, 0, 0, 0, 0] represents taking 6 1p coins and 3 2p coins, but 0 of all other coins.

We move down one. We maintain two sets, one set contains vertices included in shortest path tree, other set includes vertices not yet included in shortest path tree. Greedy Algorithms are sometimes globally optimal.

First, we need to define the problem. We want to loop backwards, from largest to smallest. Following subgraph shows vertices and their distance values, only the vertices with finite distance values are shown.

### Dijkstras shortest path algorithm example

We'll start with the denominations. To get around this, you would either have to create currency where this doesn't work or to brute-force the solution. One possible and common answer to this question is to find a path with the minimum expected travel time. Enumerate means "for loop through this list, but keep the position in another variable". At every step of the algorithm, we find a vertex which is in the other set set of not yet included and has a minimum distance from the source. This algorithm works quite well in real life. We use a boolean array sptSet[] to represent the set of vertices included in SPT. From earlier, we saw these algorithms are globally optimal:. Reversed x reverses x and lets us loop backwards.

Vertex 6 is picked.

Rated 8/10
based on 59 review

Download