1 / 38

Graph Algorithms

Graph Algorithms. Introduction. Terminology V, E, directed, adjacent, path, simple path, cycle, DAG. v3. v2. v1. v5. v6. v4. v8. v7. Introduction. Terminology V, E, directed, adjacent, path, simple path, cycle, DAG. v3. v2. v1. v5. v6. v4. v8. v7. Introduction. Terminology

raisie
Download Presentation

Graph Algorithms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Graph Algorithms

  2. Introduction • Terminology • V, E, directed, adjacent, path, simple path, cycle, DAG v3 v2 v1 v5 v6 v4 v8 v7

  3. Introduction • Terminology • V, E, directed, adjacent, path, simple path, cycle, DAG v3 v2 v1 v5 v6 v4 v8 v7

  4. Introduction • Terminology • connected, strongly connected, weakly connected v5 v5 v8 v8 v7 v7

  5. Representation – Adjacency Matrix v3 v2 v1 v5 v6 v4 v8 v7

  6. Representation – Adjacency Matrix v3 v2 v1 v5 v6 v4 v8 v7

  7. Representation – Adjacency List v3 v2 v1 v5 v6 v4 v8 v7 v1 v2 v4 v7 Ø v2 v1 v3 v4 v5 Ø v3 v2 v6 Ø v4 v1 v2 v7 Ø v5 v2 v6 v7 v8 Ø v6 v3 v5 Ø v7 v1 v4 v5 v8 Ø

  8. Topological Ordering • find vertex with no incoming edges • print it • remove it and its edges v3 v2 v1 v5 v6 v4 v8 v7

  9. Topological Ordering • v1 v3 v2 v1 v5 v6 v4 v3 v8 v2 v7 v5 v6 v4 v8 v7

  10. Topological Ordering • v1, v2, v3, v7 v3 v3 v2 v5 v5 v6 v6 v4 v4 v8 v8 v7 v7 v5 v6 v5 v6 v4 v4 v8 v8 v7

  11. Topological Ordering • v1, v2, v3, v7, v4, v5, v6, v8 • Complexity? • Improvements? v6 v5 v5 v6 v6 v4 v8 v8 v8

  12. Topological Ordering v3 • Complexity – V2 • Improvements – as edges are removed, enqueue vertices with 0 indegree • v1, v2, v7, v3, v4, v5, v6, v8 v2 v1 v5 v6 v4 v8 v7

  13. Shortest Path Algorithms • Given as input a weighted graph G=(V, E), and a distinguished vertex, s, find the shortest weighted path from s to every other vertex in G. • Unweighted – every edge has weight 1 • Applications?

  14. Breadth-first Search • Level-order traversal

  15. Unweighted Shortest Path v3 v2 v1 v5 v6 v4 v8 v7 s.dist = 0; for(currdist = 0; currdist < NUM_VERT; currdist++) for each vertex v if(!v.known && v.dist == currdist) v.known = true for each w adjacent to v if (w.dist == INFINITY) w.dist = currdist + 1 w.path = v

  16. Unweighted Shortest Path v3 v2 v1 v5 v6 v4 v8 v7 enqueue(s) s.dist = 0; while(!q.isEmpty()) v = q.dequeue() for each w adjacent to v if(w.dist == INFINITY) w.dist = v.dist + 1 w.path = v q.enqueue(w)

  17. Unweighted Shortest Path v3 v2 queue – v1 v1 v5 v6 v4 v8 v7

  18. Unweighted Shortest Path v3 v2 queue – v1 queue – v2, v4, v7 queue – v4, v7, v3, v5 queue – v7, v3, v5 queue – v3, v5, v8 queue – v5, v8, v6 v1 v5 v6 v4 v8 v7

  19. Unweighted Shortest Path v3 v2 v1 v5 v6 v4 v8 v7 enqueue(s) s.dist = 0; while(!q.isEmpty()) v = q.dequeue() for each w adjacent to v if(w.dist == INFINITY) w.dist = v.dist + 1 w.path = v q.enqueue(w) s.dist = 0; for(currdist = 0; currdist < NUM_VERT; currdist++) for each vertex v if(!v.known && v.dist == currdist) v.known = true for each w adjacent to v if (w.dist == INFINITY) w.dist = currdist + 1 w.path = v

  20. Dijkstra’s Algorithm • Weighted shortest-path first • Greedy algorithm s.dist = 0 for (;;) v = smallest unknown distance vertex if(v == NOT_A_VERTEX) break; v.known = true for each w adjacent to v if(!w.known) if(v.dist + cvw < w.dist) decrease(w.dist to v.dist+cvw) w.path = v

  21. Dijkstra’s Algorithm 2 v3 v2 4 10 6 1 5 7 v1 v5 v6 v4 9 7 2 3 v8 v7 2

  22. Dijkstra’s Algorithm 2 v3 v2 4 10 6 1 5 7 v1 v5 v6 v4 9 7 2 3 v8 v7 2

  23. Dijkstra’s Algorithm 2 v3 v2 4 10 6 1 5 7 v1 v5 v6 v4 9 7 2 3 v8 v7 2

  24. Dijkstra’s Algorithm 2 v3 v2 4 10 6 1 5 7 v1 v5 v6 v4 9 7 2 3 v8 v7 2

  25. Dijkstra’s Algorithm 2 v3 v2 4 10 6 1 5 7 v1 v5 v6 v4 9 7 2 3 v8 v7 2

  26. Running time – Dijkstra’s • Simple implementation • O(E + V2) • Improvements • Use a priority queue • Smallest unknown distance vertex log V (V times) • decrease log V (E times)

  27. Negative Edge Costs -3 v3 v2 4 3 7 1 5 7 v1 v5 v6 v4 9 -6 2 10 v8 v7 2 enqueue(s) s.dist = 0; while(!q.isEmpty()) v = q.dequeue() for each w adjacent to v if(w.dist > v.dist+cvw) w.dist = v.dist + cvw w.path = v if(w not in q) q.enqueue(w)

  28. Network Flow • Determine maximum flow from source to sink in a directed graph where each edge has given capacity • capacity cv,w is maximum flow for edge (v, w) • total flow coming in must = total flow going out • Example applications?

  29. Max-Flow Algorithm 2 v3 v2 4 10 6 • choose a path from src to sink – augmenting path • add flow equal to minimum edge on path • add reverse edges to allow algorithm to undo its decision • continue until no augmenting path can be found sink src 1 5 7 v1 v5 v6 v4 9 7 2 3 v8 v7 2

  30. Max-Flow Algorithm 2 v3 v2 4 10 6 sink src 1 2 7 v1 v5 v6 v4 3 3 9 4 2 3 v8 v7 2 2 2 v3 v2 2 2 6 8 sink src 1 2 7 v1 v5 v6 v4 3 3 9 4 2 3 v8 v7 2

  31. Max-Flow Algorithm 2 2 v3 v2 4 2 2 6 sink src 1 4 5 7 v1 v5 v6 v4 3 9 4 2 3 v8 v7 2 2 2 v3 v2 4 2 sink src 5 v1 v5 v6 v4 3 3 v8 v7

  32. Algorithm Complexity • O(f * E) • Can be bad if f is large • Improve by choosing augmenting path that increases flow by largest amount

  33. Minimum Spanning Tree • Find a tree (acyclic graph) that covers every vertex and has minimum cost • Number of edges in tree will be V-1

  34. Prim’s Algorithm 2 v3 v2 4 • Similar to Dijkstra’s • choose min distance vertex v and mark as known • update distance values for all adjacent vertices w • dw = min(dw, cv, w) 3 7 1 5 v1 7 v5 v6 v4 9 6 2 10 v8 v7 2

  35. Prim’s Algorithm 2 v3 v2 4 3 1 5 v1 v5 v6 v4 2 v8 v7 2

  36. Kruskal’s Algorithm 2 v3 v2 4 • choose min cost edge • if it doesn’t create a cycle, add it • use heap to provide O(ElogE) running time 3 7 1 5 v1 7 v5 v6 v4 9 6 2 10 v8 v7 2

  37. Kruskal’s Algorithm 2 v3 v2 4 • (v2,v4), (v2,v3), (v4,v7), (v7,v8), (v1,v2), (v3,v6), (v1,v4), (v6,v5) 3 7 1 5 v1 4 v5 v6 v4 9 6 2 10 v8 v7 2

  38. Depth-First Search • Generalization of preorder traversal dfs(Vertex v) v.visited = true for each w adjacent to v if(!w.visited) dfs(w)

More Related