21.11.2014 Views

CHAPTER 25 Weighted Graphs and Applications Objectives • To ...

CHAPTER 25 Weighted Graphs and Applications Objectives • To ...

CHAPTER 25 Weighted Graphs and Applications Objectives • To ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

(a) Before moving u to T<br />

(b) After moving u to T<br />

Let us illustrate Dijkstra’s algorithm using the graph in Figure <strong>25</strong>.10a. Suppose the source vertex is 1.<br />

Therefore, cost[1] = 0 <strong>and</strong> parent[1] = -1, <strong>and</strong> the costs for all other vertices are initially , as<br />

shown in Figure <strong>25</strong>.10b.<br />

Figure <strong>25</strong>.10<br />

The algorithm will find all shortest paths from source vertex 1.<br />

2<br />

5<br />

8<br />

1<br />

9<br />

10<br />

5<br />

6<br />

8<br />

3<br />

8<br />

4<br />

cost<br />

0<br />

0 1 2 3 4 5 6<br />

parent<br />

1<br />

2<br />

7<br />

5<br />

-1<br />

0<br />

4<br />

5<br />

0 1 2 3 4 5 6<br />

(a)<br />

(b)<br />

Now T contains {1}. Vertex 1 is the one in V-T with the smallest cost, so add 1 to T, as shown in<br />

Figure <strong>25</strong>.12 <strong>and</strong> update the cost <strong>and</strong> parent for vertices in V-T <strong>and</strong> adjacent to 1 if applicable. The cost<br />

<strong>and</strong> parent for vertices 0, 2, 3, <strong>and</strong> 6 are now updated, as shown in Figure <strong>25</strong>.12b.<br />

Figure <strong>25</strong>.12<br />

Now vertex 1 is in set T.<br />

2<br />

5<br />

8<br />

T<br />

1<br />

9<br />

10<br />

5<br />

6<br />

8<br />

3<br />

8<br />

4<br />

cost<br />

8 0 5 10 9<br />

0 1 2 3 4 5 6<br />

parent<br />

1<br />

2<br />

7<br />

5<br />

1 -1 1 1 1<br />

0<br />

4<br />

5<br />

0 1 2 3 4 5 6<br />

(a)<br />

(b)<br />

29

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!