Graph Algorithms Introduction • Terminology – V, E, directed, adjacent, path, simple path, cycle, DAG v2 v1 v4 v7 v3 v5 v6 v8 Introduction • Terminology – V, E, directed, adjacent, path, simple path, cycle, DAG v2 v1 v4 v7 v3 v5 v6 v8 Introduction • Terminology – connected, strongly connected, weakly connected v5 v7 v5 v8 v7 v8 Representation – Adjacency Matrix v3 v2 v1 v5 v4 v8 v7 v1 v1 v2 v3 v4 v5 v6 V7 v8 v2 v3 v6 v4 v5 v6 v7 V8 Representation – Adjacency Matrix v3 v2 v1 v5 v4 v8 v7 v1 v1 v2 1 1 v5 1 1 v7 V8 1 1 1 1 1 1 1 1 v6 1 1 v6 v8 v4 1 v5 v7 v3 1 v3 v4 v2 v6 1 1 1 1 1 1 1 1 Representation – Adjacency List v2 v1 v4 v7 v1 v2 v4 v7 Ø v2 v1 v3 v4 v5 v3 v2 v6 Ø v4 v1 v2 v7 v5 v2 v6 v7 v6 v3 v5 Ø v7 v1 v4 v5 Ø Ø v8 v8 Ø Ø v3 v5 v6 v8 Topological Ordering • find vertex with no incoming edges • print it • remove it and its edges v2 v1 v4 v7 v3 v5 v6 v8 Topological Ordering • v1 v2 v1 v4 v7 v3 v5 v6 v8 v2 v4 v7 v3 v5 v6 v8 Topological Ordering • v1, v2, v3, v7 v2 v4 v3 v3 v5 v6 v8 v7 v5 v4 v6 v8 v7 v5 v4 v5 v6 v6 v4 v8 v7 v8 Topological Ordering • v1, v2, v3, v7, v4, v5, v6, v8 v5 v6 v5 v4 v8 • Complexity? • Improvements? v6 v6 v8 v8 Topological Ordering v2 v1 v4 v3 v5 v7 v6 v8 • Complexity – V2 • Improvements – as edges are removed, enqueue vertices with 0 indegree – v1, v2, v7, v3, v4, v5, v6, v8 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? Breadth-first Search • Level-order traversal Unweighted Shortest Path v2 v1 v4 v7 v3 v5 v6 v8 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 Unweighted Shortest Path v2 v1 v4 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) v3 v5 v6 v8 Unweighted Shortest Path v2 v1 v4 v3 v5 v6 v8 v7 queue – v1 v1 v2 v3 v4 v5 v6 v7 v8 dv pv 0 v1 Unweighted Shortest Path v2 v1 v4 v3 v5 v8 v7 queue – v1 queue – v2, v4, v7 queue – v4, v7, v3, v5 queue – v7, v3, v5 queue – v3, v5, v8 queue – v5, v8, v6 v6 dv pv v1 0 v1 v2 1 v1 v3 2 v2 v4 1 v1 v5 2 v2 v6 3 v3 v7 1 v1 v8 2 v7 Unweighted Shortest Path v2 v1 v4 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) v3 v5 v6 v8 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 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 Dijkstra’s2 Algorithm v3 v2 10 v1 1 6 7 7 2 v7 known dv pv v1 F 0 0 v2 F I 0 v3 F I 0 v4 F I 0 v5 F I 0 v6 F I 0 v7 F I 0 v8 F I 0 5 v5 v4 3 4 v6 9 v8 2 Dijkstra’s2 Algorithm v3 v2 10 v1 1 6 7 2 known dv pv v1 T 0 0 v2 F 10 v3 F v4 v6 9 7 v7 5 v5 v4 3 4 v8 2 known dv pv v1 T 0 0 v1 v2 F 10 v1 I 0 v3 F I 0 F 7 v1 v4 F 5 v7 v5 F I 0 v5 F 10 v7 v6 F I 0 v6 F I 0 v7 F 3 v1 v7 T 3 v1 v8 F I 0 v8 F 5 v7 Dijkstra’s2 Algorithm v3 v2 10 v1 1 6 7 2 known dv pv v1 T 0 0 v2 F 10 v3 F v4 v6 9 7 v7 5 v5 v4 3 4 v8 2 known dv pv v1 T 0 0 v1 v2 F 10 v1 I 0 v3 F I 0 T 5 v7 v4 T 5 v7 v5 F 10 v7 v5 F 10 v7 v6 F I 0 v6 F I 0 v7 T 3 v1 v7 T 3 v1 v8 F 5 v7 v8 T 5 v7 Dijkstra’s2 Algorithm v3 v2 10 v1 1 6 7 2 known dv pv v1 T 0 0 v2 T 10 v3 F v4 v6 9 7 v7 5 v5 v4 3 4 v8 2 known dv pv v1 T 0 0 v1 v2 T 10 v1 12 0 v3 F 12 v2 T 5 v7 v4 T 5 v7 v5 F 10 v7 v5 T 10 v7 v6 F I 0 v6 F 15 v5 v7 T 3 v1 v7 T 3 v1 v8 T 5 v7 v8 T 5 v7 Dijkstra’s2 Algorithm v3 v2 10 v1 1 6 7 2 known dv pv v1 T 0 0 v2 T 10 v3 T v4 v6 9 7 v7 5 v5 v4 3 4 v8 2 known dv pv v1 T 0 0 v1 v2 T 10 v1 12 v2 v3 T 12 v2 T 5 v7 v4 T 5 v7 v5 T 10 v7 v5 T 10 v7 v6 F 15 v5 v6 T 15 v5 v7 T 3 v1 v7 T 3 v1 v8 T 5 v7 v8 T 5 v7 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) Negative Edge Costs v2 3 v1 1 -3 5 v5 v4 -6 10 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) 4 7 7 v7 v3 v6 9 v8 2 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? Max-Flow2 Algorithm v3 v2 10 src v1 1 4. sink 5 v5 v4 7 2 v7 1. 2. 3. 6 7 3 4 v6 9 v8 2 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 Max-Flow2 Algorithm v3 v2 10 src 1 6 sink 7 v1 v4 v7 3 8 v1 1 2 3 2 3 4 sink 2 v5 2 v7 2 v3 6 7 v4 v6 v8 v2 src 3 9 4 2 2 2 v5 2 3 4 9 v8 2 3 v6 Max-Flow Algorithm 2 4 6 src 1 7 v1 v4 3 2 4 3 sink 5 v6 9 4 v8 2 2 2 v3 v2 4 2 v5 2 v7 2 v3 v2 2 src v1 v4 3 v7 sink v5 5 v6 3 v8 Algorithm Complexity • O(f * E) • Can be bad if f is large – Improve by choosing augmenting path that increases flow by largest amount 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 Prim’s Algorithm v2 3 2 v3 4 7 1 v1 7 10 v4 2 v7 5 v5 6 9 v6 v8 2 • Similar to Dijkstra’s 1. choose min distance vertex v and mark as known 2. update distance values for all adjacent vertices w 1. dw = min(dw, cv, w) Prim’s Algorithm v2 2 v3 4 3 1 v1 5 v5 v4 2 v7 v6 v8 2 Kruskal’s Algorithm v2 3 2 v3 4 7 1 v1 7 10 v4 2 v7 • 5 v5 9 6 v6 v8 2 choose min cost edge – if it doesn’t create a cycle, add it • use heap to provide O(ElogE) running time Kruskal’s Algorithm v2 3 2 v3 4 7 1 v1 4 10 v4 2 v7 5 v5 6 9 v6 v8 2 • (v2,v4), (v2,v3), (v4,v7), (v7,v8), (v1,v2), (v3,v6), (v1,v4), (v6,v5) Depth-First Search • Generalization of preorder traversal dfs(Vertex v) v.visited = true for each w adjacent to v if(!w.visited) dfs(w)
© Copyright 2026 Paperzz