Single Source Shortest Paths Dijkstra

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

SINGLE SOURCE SHORTEST PATHS: DIJKSTRAS ALGORITHM

Consider a complete directed graph of 4 nodes, where the vertices are vi for i between 1 and 4 and the weight of an edge (vi,vj) is 2i+j, for I not = j.. Obtain shortest paths from node 1 to all the other nodes using Dijkstras single source shortest path algorithm. What is the time complexity of your algorithm? Discuss. What happens when some of the weights of the edges are negative?

COMPLEXITY T(n) =O(n2)

WORKS ONLY WHEN EDGE WEIGHTS ARE POSITIVE

PICTORIAL VIEW OF THE GRAPH

4 1 9 5 10 7 11 3 10 8 4 6 7 8 5 2

Initialisation Iteration initial Path S --Vertex selected 1 1 4 1-2 5 1-3 6 1-4 1 2 3 4

Shortest from node 1 is now node 2

Iteration initial

S ---

Vertex selected 1

1over 1

2over 4 1-2

3 5 1-3 5 1-3

4 6 1-4 6 1-4

{1}

( 1 to 2) to 3 is 11, so (1 to 3) remains shortest. (1 to 2) to 4 is 12, so (1 to 4) remains shortest. Least distance is now to node 3, of length 5. Iteration initial S --Vertex selected 1 1 1 2 {1} {1,2} 2 3 4 1-2 5 1-3 5 1-3 6 1-4 6 1-4 6 1-4 1over 2over 3 4

(1 to 3) to 4 is 15 so 6 remains shortest above. Iteration S Vertex selected 1over 2over 3 4

initial

---

4 1-2

5 1-3 5 1-3

6 1-4 6 1-4 6 1-4

1 2

{1} {1,2}

2 3

You might also like