6. Prove whether it is always, never, or sometimes true that the order in which the nodes are added to the MST by the Dijkstra–Prim algorithm is the same as the order in which they are encountered in a breadth-first traversal.
7. Prove whether it is always, never, or sometimes true that the order in which the nodes are added to the MST by the Dijkstra–Prim algorithm is the same as the order in which they are encountered in a depth-first traversal.
clearly not the shortest path, because you can see in Fig. 6.7(a) that there is a direct path between node A and node B that has length 2.
■ 6.5.1 Dijkstra’s Algorithm
The minimum spanning tree algorithm will not work to find the shortest path because its greedy algorithm just considered the weight of one edge at each pass. If we modify the algorithm so that it chooses the edge to the fringe that is part of the shortest entire path from the starting node, we will get the result we want. More specifically, our algorithm becomes
select a starting node
build the initial fringe from nodes connected to the starting node while we are not at the destination node do
choose the fringe node with the shortest path to the starting node add that node and its edge to the tree
update the fringe by:
adding nodes to the fringe connected to the new node for each node in the fringe do
update its edge to the one connected to the tree on the shortest path to the starting node
end for end while
Figure 6.8 shows an example execution of this algorithm. We begin with the same graph that we used for the minimum spanning tree algorithm (repro- duced here as Fig. 6.8(a)) and will look for the shortest path starting at node A and ending at node G.
A B
F G
D
C E
4 7
7 6
2
6 6 6
1
5 8
3
F D C B
A
2 4 7 5
■ FIGURE 6.8A The original graph
■ FIGURE 6.8B The shortest path is to node B
Beginning our path at node A gives us four possible edges to consider. Of those four, the edge AB is the shortest.
Node B is added to our shortest path tree (Fig. 6.8(c)), and we now look at updating the paths. Nodes E and G can now be reached, so they are added. We also look at node D and compare its direct path from node A of length 7 with the path that goes through node B, which is of length 8. Because the direct path is shorter, there is no change to the edge for node D. In looking at the options, we see that the path from node A to node C is of length 4 and is the shortest. The edge BE is shorter, but we are now considering the entire path from node A and so the length of the path to node E is actually 5.
Node C is added to the shortest path tree (Fig. 6.8(d)). In examining the graph, we see that we can get to node F through node C, but that total path length is 10, which is longer than the current path to node F and so there are no changes.
Given the situation in Fig. 6.8(d), we could choose either the path from node A to node F or the path from node A to node E that goes through node B, because they are both of length 5. The one chosen during a program execu- tion will depend on the way the data is stored. For our purposes, when presented with a choice, we will select the node that is alphabetically smaller to get Fig. 6.8(e). Because the addition of node E to the graph didn’t change any
4 7 5 2
3
8 B
F
E D C
G A
4 7
5
2 3
8
D
F
E
G C
B A
■ FIGURE 6.8C Path of length 4 to node C is the shortest of the options
■ FIGURE 6.8D The path of length 5 to either node E or node F is shortest
of the other connections, we now choose node F to get Fig. 6.8(f ). You should see that even though the selection of node F changed the edge for node D, if we had selected node F first, we would have chosen node E second.
In Fig. 6.8(f ), it should be clear that the path to node D is shorter than the path to node G. Choosing node D results in Fig. 6.8(g), and then node G is the last to be added, giving the final shortest path tree in Fig. 6.8(h). The shortest path from node A to node G has length of 10. If you look back at Fig. 6.5(h), you will see another example of the minimum spanning tree not having the shortest path, because that figure has the path from node A to node G at length 11.
4
7 5 2
3 8
D
F
G E
C
B A
4 5
2
3 8
A F 1 D
C
B G
E
■ FIGURE 6.8E The other path of length 5 to node F is next
■ FIGURE 6.8F The path of length 6 to node D is shorter than the path to node G
4 5
2
3
8
A F 1 D
C
B G
E
4 5
2
3 8 1
A F
D C
G B
E
■ FIGURE 6.8G The path to node G is the only one left
■ FIGURE 6.8H The complete shortest path tree starting at node A
In the example in Fig. 6.8, we have the full shortest-path tree for node A because our destination node was the last to be added. If we had reached node G earlier, the algorithm would have stopped at that point. There are applica- tions where we might be interested in the shortest path from one node to every other node. For example, if we have a small computer network that has relatively stable transmission rates between the nodes, we could calculate the shortest path to every other node for each computer. Then when a message needs to be sent, we would not need to do anything but access our predeter- mined shortest-path table to find the quickest way to send the message.
6.5.2
1. Execute the shortest-path algorithm on the following graphs starting at node A to create the entire shortest-path tree for each one. Count how many edges you look at in the process (if you look at one edge more than once, count it each time).
6.5.2 EXERCISES
■
A B C
H
F G
D E
4
1 1
1 2
2 2
5 4
4
4
4 2 2
2 3
3 3
3 5
6 6
3
a. b. A B
D E
F G
C
A B C
H
F G
D E
4 5
4 1
2 2
5 2 4
1
1 4
2 1
3
2 2
3 5
5
2 3
2
c. d. A
B
D E
G H C
F 2
2. Alter the algorithm in this section so that it efficiently determines the short- est path from the start node to every node in the graph, instead of just to one destination.
3. Do an analysis of the shortest-path algorithm, counting the number of times that an edge is checked for nodes added to the fringe, for updating edges to the fringe nodes, or to pick the node to move from the fringe to the mini- mum spanning tree.
4. Prove that a breadth-first traversal produces a shortest-path tree for a graph without weights.
5. Prove whether it is always, never, or sometimes true that the order in which the nodes are added to the shortest-path tree is the same as the order in which they are encountered in a breadth-first traversal.
6. Prove whether it is always, never, or sometimes true that the order in which the nodes are added to the shortest-path tree is the same as the order in which they are encountered in a depth-first traversal.