380 likes | 493 Views
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
E N D
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 • connected, strongly connected, weakly connected v5 v5 v8 v8 v7 v7
Representation – Adjacency Matrix v3 v2 v1 v5 v6 v4 v8 v7
Representation – Adjacency Matrix v3 v2 v1 v5 v6 v4 v8 v7
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 Ø
Topological Ordering • find vertex with no incoming edges • print it • remove it and its edges v3 v2 v1 v5 v6 v4 v8 v7
Topological Ordering • v1 v3 v2 v1 v5 v6 v4 v3 v8 v2 v7 v5 v6 v4 v8 v7
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
Topological Ordering • v1, v2, v3, v7, v4, v5, v6, v8 • Complexity? • Improvements? v6 v5 v5 v6 v6 v4 v8 v8 v8
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
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 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
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)
Unweighted Shortest Path v3 v2 queue – v1 v1 v5 v6 v4 v8 v7
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
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
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’s Algorithm 2 v3 v2 4 10 6 1 5 7 v1 v5 v6 v4 9 7 2 3 v8 v7 2
Dijkstra’s Algorithm 2 v3 v2 4 10 6 1 5 7 v1 v5 v6 v4 9 7 2 3 v8 v7 2
Dijkstra’s Algorithm 2 v3 v2 4 10 6 1 5 7 v1 v5 v6 v4 9 7 2 3 v8 v7 2
Dijkstra’s Algorithm 2 v3 v2 4 10 6 1 5 7 v1 v5 v6 v4 9 7 2 3 v8 v7 2
Dijkstra’s Algorithm 2 v3 v2 4 10 6 1 5 7 v1 v5 v6 v4 9 7 2 3 v8 v7 2
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 -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)
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-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
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
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
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 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
Prim’s Algorithm 2 v3 v2 4 3 1 5 v1 v5 v6 v4 2 v8 v7 2
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
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
Depth-First Search • Generalization of preorder traversal dfs(Vertex v) v.visited = true for each w adjacent to v if(!w.visited) dfs(w)