CS323 Sylabus

### Dijkstra's Algorithm

• Minimum cost paths from a vertex to all other vertices

• Consider:

Problem:

 Compute the minimum cost paths from a node (e.g., node B) to any other node in the network

• Examples:

• Shortest paths from node A to all other nodes:

• Shortest paths from node B to all other nodes:

• Information available to the algorithm: (distance matrix)

 ``` linkcost[i][j] = Link costs stored in an adjacency matrix linkcost[][]: A B C D E ----+--------------------- A | - 5 10 * * * = infinite B | 5 - 3 11 * C | 10 3 - 2 * D | * 11 2 - 3 E | * * * 3 - ```

• Overview Dijkstra's Algorithm: a greedy search algorithm

• Synopsis:

 The Dijkstra's algorithm gradually builds a short path tree using links in the network Initially, the tree contains only the source nodes (as the root of the tree. It adds one more node in each iteration to the minimum cost spanning tree

• Reached Set:

• It partitions the nodes in the network into 2 sets:

 Reached set - where the minimum cost path to that node has been found          UnReached set - where the minimum cost path is not yet fixed

Initially:

 Reached set = { SourceNode } UnReached set = all the other nodes

• The Dijkstra's Algorithm....

• Notations used in the algorithm:

 S = source node (the node performing the computation) N = the nodes set (all nodes) ReachSet = set of nodes that are reached with known minimum cost path        D(S, n) = current minimum cost (distance) from S to node n

• Psuedo code:

 ``` /* ------------------------ Initialization ------------------------ */ ReachSet := {S} (S is the node that performs the computation) for ( all other nodes n ≠ S ) do { D(S,n) = linkcost(S,n); // Min. cost S -> n } /* ----------------------- Find min. cost path ----------------------- */ while ( ReachSet ≠ N ) do { Let: m ∈ (N - ReachSet) that has smallest value for D(S,m) ReachSet := ReachSet ∪ {m} // shortest path to m has been found /* ------------------------------------------------------ Find possible "short cut" through m ------------------------------------------------------ */ for ( each node n ∈ (N - ReachSet) ) do { Check for possible better route to n via m: D(S,n) S <-------> n \ / \ / D(S,m) \ / linkcost(m,n) \ / \ / m D(S,m) + linkcost(m,n) < D(S,n) ??? } } ```

• Dijkstra's Algorithm:

 ``` /* ------------------------ Initialization step ------------------------ */ ReachSet := {S} (S is the node that performs the computation) for ( each node n in N - {S} ) do { D(S,n) := linkcost(S,n); // Min. cost S -> n Add (S,n) to candidate edges // Candidate edges may be part of shortest path } /* ----------------------- Computation loop ----------------------- */ while ( ReachSet != N ) do { /* ----------------------------------------- Inclusion step: add a node to Reach Set ----------------------------------------- */ Find the node m in (N - ReachSet) that has smallest value for D(S,m) Output edge (x,m) to min. cost spanning tree; // Output next edge in Min Cost tree CandEdges = CandEdges - {(n,m)}; // Remove this edge from Candidate set ReachSet := ReachSet ∪ {m} /* ------------------------------------------------------ Update step: adjust min. cost to the UnReached nodes ------------------------------------------------------ */ for ( each node n in (N - ReachSet) ) do { /* ------------------------------------------------------- Pictorially: D(S,n) S <-------> n \ / \ / D(S,m) \ / linkcost(m,n) \ / m We are trying to find a "short cut" to n through m !!! -------------------------------------------------------- */ if ( D(S,m)+linkcost(m,n) < D(S,n) ) { D(S,n) = D(S,m)+linkcost(m,n); Delete (x,n) from candidate edges Add (m,n) to candidate edges // A better direction to reach n is through m... } } } ```

• Example Dijkstra's Algorithm

• Example network:

• Information on the graph is represented by the follow distance matrix:

 ``` Distance matrix To: From A B C D E ------+--------------------------------- A | * 5 10 * * B | 5 * 3 11 * C | 10 3 * 2 * D | * 11 2 * 3 E | * * * 3 * ```

• We will show the computation of the shortest paths from source node B

• Initialization step:

 ``` ReachSet := {B} Candidate edegs: D(B,A) = 5; (B,A) D(B,C) = 3; (B,C) D(B,D) = 11; (B,D) D(B,E) = *; (= infinite) (B,E) (It's OK to include a bogus edge) ```

Graphically:

• Iteration 1:

• The node with the lowest cost path is C:

 ``` (m = C;) ReachSet := {B} ∪ {C}; // Add C to ReachSet /* ------------------------------------ We know D(B -> C) = 3 Check for better paths through C ------------------------------------ */ Candidate edges: D(B,A) = min( 5, 3 + 10) = 5; (B,A) D(B,D) = min( 11, 3 + 2) = 5; (B,D) ⇒ (C,D) (better !) D(B,E) = min( *, 3 + * ) = * (B,E) ```

Graphically:

• Iteration 2:

• The node with the lowest cost path is A (ties broken arbitrarily):

 ``` (m = A;) ReachSet := {B,C} ∪ {A}; // Add A to ReachSet /* ------------------------------------ We know D(B -> A) = 5 Check for better paths through A ------------------------------------ */ Candidate edges: D(B,D) = min( 5, 5 + *) = 5; (C,D) (unchanged) D(B,E) = min( *, 5 + * ) = * (B,E) ```

Graphically:

• Iteration 3:

• The node with the lowest cost path is D:

 ``` (m = D;) ReachSet := {A,B,C} ∪ {D}; // Add D to ReachSet /* ------------------------------------ We know D(B -> D) = 5 Check for better paths through A ------------------------------------ */ Candidate edges: D(B,E) = min( *, 5 + 3 ) = 8 (B,E) ⇒ (D,E) (better !); ```

Graphically:

• Iteration 4:

• The node with the lowest cost path is E:

 ``` (m = E;) ReachSet := {A,B,C,D} ∪ {E}; // Add E to ReachSet /* ------------------------------------ We know D(B -> E) = 8 Check for better paths through E ------------------------------------ */ (No more nodes to be processed...) ```

Final result:

• ReachSet == N - Done

• Result: shortest path edges (listed in the order of inclusion)

 ``` (B,C) (B,A) (C,D) (D,E) ```