Presentation is loading. Please wait.

Presentation is loading. Please wait.

Shortest Path Consider the following weighted undirected graph: 20 10

Similar presentations


Presentation on theme: "Shortest Path Consider the following weighted undirected graph: 20 10"— Presentation transcript:

1 Shortest Path Consider the following weighted undirected graph: 20 10
22 20 9 16 7 18 6 2 8 24 5 4 6 66 10 20 We want: A path 5  v1  v2  …  20 Such that cost(5v1) + cost(v1 v2) + … + cost ( 20) is minimum

2 How About Using the MST Algorithm?
20 10 22 20 9 16 7 18 6 2 8 24 5 4 6 66 10 20 Compute the MST Take the path between the two nodes that goes through the MST

3 The Fringe Given a Graph G = (V,E) and T a subset of V, the fringe of T, is defined as: Fringe(T) = { (w,x) in E : w  T and x V − T} T

4 Djikstra Algorithm: Idea
We are computing a shortest path from node u to a node v Start the algorithm with T = {u}, construct Fringe(T) At each iteration, select the (z,w) in Fringe(T) such that the path from u to w has the lowest cost Stop when v is part of T (not of the fringe!)

5 Example 20 10 22 20 16 7 9 18 6 2 8 24 5 4 6 66 10 20

6 The Dijkstra’s Algorithm
// Input: G = (G,V), nodes u and v in V //output: Shortest path from u to v T  {u}; F  Fringe({u}); ET  {} While v is not in the T do Choose (z,w) in F with the shortest path (u, u1), (u1,u2),…, (un,z) (z,w) with (u, u1), (u1,u2),…, (un,z) in ET T  T  {w} ET  ET  {(z,w)} for each {(w,x) : (w,x) in E − ET } do If no (w’,x) in F then F  F  {(w,x)} else If the path u  x going through (w,x) has less weight than the path ux going through (w’,x) then F  ( F − {(w’,x)})  {(w,x)}

7 Properties Is Dijkstra’s Algorithm greedy? Yes
Why Dijkstra’s Algorithm works? T V  T The path from u to every node in T is the minimum path w u r s v 6 20 x

8 Complexity Actual complexity is O(|E|log2 |E|)
It is easy to see that it is at most |E||V| (i.e., |E|2): the outer “while” is performed |V| times the inner for is performed at most |E| times

9 Homework 010 Write the pseudo-code for the algorithm inserting an element in a min-heap Do the same for a Heap Write the pseudo-code for isLeaf(i) (Slide 15 of class about Heaps) Change Dijkstra’s Algorithm (Slide 6) to compute the minimum distance from u to every node in the graph True or False: the subgraph (T,ET) resulting from 4 is an MST of the input graph In the graph of 2.b (Page 323). Follow Dijkstra’s Algorithm to compute the path from c to l 011 Write the pseudo-code for the algorithm deleting the element with the minimum priority value in a min-heap Do the same but deleting the one with the highest priority for a Heap Write the pseudo-code for Parent(i) (Slide 15 of class about Heaps) True or False: the subgraph (T,ET) resulting from 4 is a tree connecting all nodes of the input graph In the graph of 2.b (Page 323). Follow Dijkstra’s Algorithm to compute the path from l to a


Download ppt "Shortest Path Consider the following weighted undirected graph: 20 10"

Similar presentations


Ads by Google