# Write a greedy algorithm to generate shortest path algorithm

It then goes to 10p.

## Dijkstra algorithm python

We pick 1x 20p. We now need to return 1p. Enumerate means "for loop through this list, but keep the position in another variable". The algorithm needs to return change of 10p. The vertices included in SPT are shown in green colour. Now, let's see what our Greedy algorithm does. It is optimal locally, but sometimes it isn't optimal globally. Our Greedy algorithm failed because it didn't look at 15p. Is Greedy Optimal? How to implement the above algorithm? Counting Change Using Greedy Imagine you're a vending machine. The algorithm for doing this is: Pick 3 denominations of coins. Does Greedy Always Work? Let's take it. Your algorithm needs to follow this property: At that exact moment in time, what is the optimal choice to make?

Vertex 6 is picked. We now need to return 10p. How to implement the above algorithm? It chooses 1 10p, and now our return is 0 we stop the algorithm. We maintain two sets, one set contains vertices included in shortest path tree, other set includes vertices not yet included in shortest path tree.

The vertex 0 is picked, include it in sptSet. There's no 30p coin in pound sterlinghow do you calculate how much change to return? In our example when we start the loop. Greedy Algorithms are sometimes globally optimal.