CHAPTER 25 Weighted Graphs and Applications Objectives ⢠To ...
CHAPTER 25 Weighted Graphs and Applications Objectives ⢠To ...
CHAPTER 25 Weighted Graphs and Applications Objectives ⢠To ...
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
Input: a weighted graph G = (V, E) with non-negative weights<br />
Output: A shortest path tree from a source vertex s<br />
1 ShortestPathTree getShortestPath(s) {<br />
2 Let T be a set that contains the vertices whose<br />
3 paths to s are known;<br />
4 Initially T contains source vertex s with cost[s] = 0;<br />
5 for (each u in V – T)<br />
6 cost[u] = infinity;<br />
7<br />
8 while (size of T < n)<br />
{<br />
9 Find v in V – T with the smallest cost[u] + w(u, v) value<br />
10 among all u in T;<br />
11 Add v to T <strong>and</strong> set cost[v] = cost[u] + w(u, v);<br />
12 parent[v] = u;<br />
13 }<br />
14 }<br />
The algorithm uses cost[v] to store the cost of the shortest path from vertex v to the source vertex s.<br />
cost[s] is 0. Initially assign infinity to cost[v] to indicate that no path is found from v<br />
to s. Let V denote all vertices in the graph <strong>and</strong> T denote the set of the vertices whose costs<br />
are known. Initially, the source vertex s is in T. The algorithm repeatedly finds a vertex u in<br />
T <strong>and</strong> a vertex v in V – T such that cost[u] + w(u, v) is the smallest, <strong>and</strong> moves v<br />
to T. The shortest path algorithm given in the text coninously update the cost <strong>and</strong> parent for a<br />
vertex in V – T. This algorithm initializes the cost to infinity for each vertex <strong>and</strong> then<br />
changes the cost for a vertex only once when the vertex is added into T. This algorithm can<br />
be implemented using priority queues to achieve the O(|E|log|V|) time complexity. Use<br />
Listing <strong>25</strong>.9 TestShortestPath.cpp to test your new algorithm.<br />
49