# 1 Paths in Graphs Oscar Miguel Alonso M. 2 Outline The problem to be solved Breadth first search Dijkstra's Algorithm Bellman-Ford Algorithm Shortest.

## Presentation on theme: "1 Paths in Graphs Oscar Miguel Alonso M. 2 Outline The problem to be solved Breadth first search Dijkstra's Algorithm Bellman-Ford Algorithm Shortest."— Presentation transcript:

1 Paths in Graphs Oscar Miguel Alonso M

2 Outline The problem to be solved Breadth first search Dijkstra's Algorithm Bellman-Ford Algorithm Shortest path in DAGs Efficient implementation of Dijkstra (Heap) Bonus: graph diameter, all to all shortest path (Floyd algorithm)

3 Paths in graphs Find the shortest path between two nodes  Are all the edges of the same cost?  Are there edges with negative cost?  Is the graph directed?  Are there cycles of negative cost? Given a graph, which are the most distant nodes?

4 Breadth first search Idea:  You begin with one node S  Then, visit all the nodes at distance 1 from S  Then, visit the all nodes at distance 2 from S  You continue until you meet your target node or you have reached all the nodes

6 s 1 1 1

7 s 1 1 1 2

8 BFS(G,s) Input: Graph G=(V,E), node s Output: array dist[] // dist[u] is the distance from s to u Vars: Queue Q for-each u in V dist[u]=∞ dist [s]=0 Q.add(s) while(!Q.empty()) u = Q.get() for-each edge ( u, v ) in E if dist[v]==∞ Q.add(v) dist[v]=dist[u]+1 O(E)

9 s Breadth first search Application of the algorithm: see whiteboard 0 1 3 2 4

10 Dijkstra's algorithm Edge Relaxation  if(dist[u] > dist[k]+W[k][u]) dist[u] = dist[k]+W[k][u] Used in weighted graphs Do not use it if there are negative weighs!

11 Dijkstra's algorithm Idea:  Keep a list of nodes which you already know the shortest path to  Such list begin with only one node (s), and you add one node in each iteration of the algorithm  The node added is connected to one node in the list, and is the one with minimum distance to the origin from all candidates

12 0 3 1 3 1 3 2 3 Dijkstra's algorithm 0 1 2 3 4

13 0 2 3 1 3 1 3 2 3 Dijkstra's algorithm 0 1 2 3 4

14 0 3 2 3 1 3 1 3 2 3 Dijkstra's algorithm 0 1 2 3 4

15 0 3 3 2 4 3 1 3 1 3 2 3 Dijkstra's algorithm 0 1 2 3 4

16 Dijkstra's algorithm For the implementation  Keep an array indicating the minimum distance so far to each node (initialize with infinite)  Update the distance each time you add a node (relax the edges from that node)  Keep a list of the nodes not reached, instead of the ones reached  Now, instead of searching the node with minimum distance, just pick the one with lower distance

17 ∞ ∞ ∞ ∞ ∞ 3 1 3 1 3 2 3 Dijkstra's algorithm 0 1 2 3 4

18 0 3 3 2 ∞ 3 1 3 1 3 2 3 Dijkstra's algorithm 0 1 2 3 4

19 0 3 3 2 5 3 1 3 1 3 2 3 Dijkstra's algorithm 0 1 2 3 4

20 0 3 3 2 4 3 1 3 1 3 2 3 Dijkstra's algorithm 0 1 2 3 4

21 0 3 3 2 4 3 1 3 1 3 2 3 Dijkstra's algorithm 0 1 2 3 4

22 0 3 3 2 4 3 1 3 1 3 2 3 Dijkstra's algorithm 0 1 2 3 4

23 Dijkstra's algorithm DIJKSTRA(G,s) for-each i in V d[ i ] ← ∞ Q.add(i) d[s] ← 0 while not empty(queue) do u = Q.extract_min() // take node with minimum dist for-each v where (u, v) in E if d[v] > d[u] + w uv then d[v] = d[u] + w uv Note that d[v] represents the known shortest distance of v from s during the process and may not be the real shortest distance until v is marked black.

24 Dijkstra's algorithm Complexity:  Depends on Queue implementation E lg VVHeap E lg VEHeap (Lazy) VEEArray (Lazy) VEVSorted Array E + V lg VVFibonacci Heap V2V2 VArray TimeMemoryWorst Case Big-Oh

25 Bellman-Ford algorithm Useful when there are negative-weighted edges Idea: relax all the edges n times A path can be found with max n-i edge relaxations If after that, the graph allow other edge relaxation, the graph has a negative weight cycle 5 -3

26 Bellman-Ford algorithm Do n-1 times for-each (u, v) in E if d[v] > d[u] + w uv then d[v] ← d[u] + w uv Complexity = O(VE)

27 Shortest path in DAGs In DAGs the problem is easier Idea: sort the nodes (topological sort), and traverse the nodes, relaxing all their edges ShortestPath-DAG(G,s) for-each i in V d[ i ] ← ∞ d[s] ← 0 TS = TopologicalSort(G) for-each u in TS for-each v where (u, v) is and edge if d[v] > d[u] + w uv then d[v] = d[u] + w uv O(E)

28 Shortest path in DAGs

29 Heap data structure A heap is a comlete binary tree in which every parent is greater (smaller) than its child(ren). extract-min and decrease-key operations are performed in O(log(n)) constructed in O(n) stored efficiently in an array:  no need for pointers  parent of element i is in pos (i-1)/2  left son is in pos 2*i+1  left son is in pos 2*i+2 31 87 67 54 1927

30 Heap data structure 31 87 67 54 1927 31 87 67 54 19 27

31 Heap data structure Example: extract-min

32 Heap data structure

33 Heap data structure

34 Heap data structure

35 Warshall-Floyd Algorithm All to all shortest path d[i][j] is the minimum distance from vertex i to vertex j the diameter of the graph is the maximum d[i][j] after the execution of the algorithm

36 Warshall-Floyd Algorithm Floyd(G) for-each (u, v) in E d[u][v] ← w uv for-each k in V for-each i in V for-each j in V if d[i][j] > d[i][k] + d[k][j] d[i][j] ← d[i][k] + d[k][j] Time Complexity: O(V 3 )

37 THANK YOU !!

Download ppt "1 Paths in Graphs Oscar Miguel Alonso M. 2 Outline The problem to be solved Breadth first search Dijkstra's Algorithm Bellman-Ford Algorithm Shortest."

Similar presentations