470 likes | 492 Views
Lecture 13 covers two greedy algorithms: Dijkstra's Algorithm for finding shortest paths and Prim's Algorithm for minimum spanning trees. It also introduces the Floyd-Warshall algorithm and discusses directed and undirected graphs.
E N D
Data Structures and Algorithms for Information Processing Lecture 13: Graphs II Lecture 13: Graphs II
Topics • Two Greedy Algorithms: • Shortest Path • Dijkstra’s Algorithm (Main, Ch. 14) • Minimum Spanning Trees • Prim’s Algorithm (CLR, Ch. 24) • A classic Dynamic Programming Algorithm: • Floyd Warshall (CLR, Ch. 25) Lecture 13: Graphs II
Graphs Revisited • Nodes and links between them • May be linked in any pattern(unlike trees) • Vertex: a node in the graph • Edge: a connection between nodes Lecture 13: Graphs II
Vertices are labelled V0 e0 Edges are labelled V2 V1 e1 e4 e5 e2 V4 V3 e3 Undirected Graphs • Vertices drawn with circles • Edges drawn with lines Visual Layout Doesn’t Matter! Lecture 13: Graphs II
Undirected Graphs • An undirected graph is a finite set of vertices along with a finite set of edges. The empty graph is an empty set of vertices and an empty set of edges. • Each edge connects two vertices • The order of the connection is unimportant Lecture 13: Graphs II
Directed Graphs • A directed graph: finite set of vertices and edges • Each edge connects two vertices, called the source and target; the edge connects the source to the target (order is significant) • Simple Graphs: have no loops and no multiple edges Lecture 13: Graphs II
Directed Graphs Arrows are used to represent the edges; arrows start at the source vertex and point to the target vertex Useful for modelling directional phenomena, like geographical travel, or game-playing where moves are irreversible Examples: modelling states in a chess game, or Tic-Tac-Toe Lecture 13: Graphs II
Finding if a Path Exists • When problems are represented by graphs, a solution is often some path from a start node to a goal node • Example: modeling network connectivity between computers • As we’ve seen, DFS and BFS can be used to find path(a,b) Lecture 13: Graphs II
V0 6 1 3 V1 V3 2 7 V2 Minimal Paths • Weighted Graphs: each edge has an associated cost or weight Several paths from V0 to V2 One path with lowest total cost:{(V0,V3) (V3,V1) (V1,V2)} =1 + 3 + 2 = 6 Paths with fewer edges exist, but with higher cost! Lecture 13: Graphs II
Finding a Minimal Path • The shortest (minimal) path has the lowest cost, not the fewest edges. BFS will find the path with fewest edges. • Examples: Minimizing travel cost between A & B; minimizing wiring length between two points Lecture 13: Graphs II
Definitions • A weighted edge is an edge with a non-negative integer weight • The weight of a path is the total sum of the weights of its edges • If two vertices are connected by at least one path, then the shortest path is the path with the smallest weight (can be more than one) Lecture 13: Graphs II
Dijkstra Lecture 13: Graphs II
Dijkstra’s Algorithm • Given a weighted graph, find the shortest path between two vertices • Start by finding the weight of the shortest path (shortest distance) • Dijkstra’s Algorithm finds the shortest distance from the start node to every other node in the graph Lecture 13: Graphs II
Dijkstra’s Algorithm • Use an integer array called distance with one component for each vertex in the graph • Goal: Completely fill the distance array so that for each vertex v, the value of distance[v] is the weight of the shortest path from start to v (this is also the loop invariant) Lecture 13: Graphs II
Dijkstra’s Algorithm • Step 1: Fill in distance with ? (infinity) at every location, with the exception of distance[start] = 0 • Step 2: Initialize a set of vertices called allowedVertices to the empty set; a permitted path starts at start and contains only vertices in allowedVertices Lecture 13: Graphs II
Dijkstra’s Algorithm • Step 3: Loop, adding one more vertex to allowedVertices and updating the distance array • 3a: Let next be the closest vertex to the start vertex that is not yet in allowedVertices • 3b: Add the next to allowedVertices • 3c: Update distance array to maintain the invariant Lecture 13: Graphs II
start distance V0 9 2 0 0 2 ? ? ? ? ? ? ? ? 9 6 V5 V1 [0] [0] [1] [1] [2] [2] [3] [3] [4] [4] [5] [5] next 15 3 8 V3 1 3 V2 V4 7 next 0 2 10 17 ? 8 [0] [1] [2] [3] [4] [5] next Example Lecture 13: Graphs II
start V0 9 2 0 2 10 17 11 8 6 V5 V1 [0] [1] [2] [3] [4] [5] next 15 3 8 V3 0 2 10 11 11 8 1 3 [0] [1] [2] [3] [4] [5] V2 V4 next 7 0 2 10 11 11 8 [0] [1] [2] [3] [4] [5] Example Lecture 13: Graphs II
Shortest Path • We’ve computed the weight of the shortest path from start to every other node • What about the shortest path itself? (sequence of nodes from start to some v) • Need to maintain predecessor information for each node Lecture 13: Graphs II
Predecessor Information • For each vertex v, keep track of which vertex was the next vertex when distance[v] was given a new (smaller) value • Use an array called predecessor: for each vertex v, predecessor[v] is the value of next when distance[v] was last updated Lecture 13: Graphs II
start V0 9 2 0 0 2 ? ? ? ? ? ? ? 9 ? 6 V5 V1 [0] [0] [1] [1] [2] [2] [3] [3] [4] [4] [5] [5] 15 3 8 V3 1 3 V2 V4 7 next 0 2 10 17 ? 8 [0] [1] [2] [3] [4] [5] next Example w/Predecessor distance predecessor next 0 0 0 1 1 1 Lecture 13: Graphs II
start V0 9 2 0 2 10 17 11 8 6 V5 V1 [0] [1] [2] [3] [4] [5] next 15 3 8 V3 0 2 10 11 11 8 1 3 [0] [1] [2] [3] [4] [5] V2 V4 next 7 0 2 10 11 11 8 [0] [1] [2] [3] [4] [5] Example w/Predecessor 0 1 1 5 1 0 1 2 5 1 0 1 2 5 1 Lecture 13: Graphs II
Printing the Shortest Path // Printing the vertices on the shortest path // from start to v. // Print in reverse order from v to start. vertexOnPath = v; System.out.println(vertexOnPath); while (vertexOnPath != start) { vertexOnPath = predecessor[vertexOnPath]; System.out.println(vertexOnPath); } Lecture 13: Graphs II
A Digression • Write a routine that computes the n’th Fibonacci number. • Method 1: int fib(int n) { //pre: n >= 1 from Wikipedia introduced Hindu-Arabic numerals to Europe if (n == 1 || n ==2) return 1; else return fib(n-1) + fib(n-2); } Lecture 13: Graphs II
A Digression • Method 2: int fib(int n) { //pre: n >= 1 if(n == 1 || n == 2) return 1; else { first = 1; second = 1; k = 3; while (k <= n) { sum = first + second; first = second; second = sum; k = k + 1; } return sum; } Which is better, method 1 or method 2? Is recursion always bad like method 1? Dynamic programming solutions try to avoid redundant computations. Lecture 13: Graphs II
Floyd Warshall All Pairs for each vertex u in G do for each vertex v in G do cost[u,v] = c[u,v] for each vertex w in G do for each vertex u in G do for each vertex v in G do cost[u,v] = min(cost[u,v], cost[u,w] + cost[w,v] From wikipedia Compute the shortest path from each vertex to every other vertex. This is a classic dynamic programming algorithm. “programming” refers to the fact that it’s a tabular method. Lecture 13: Graphs II
Graph G 2 3 2 6 4 1 3 1 1 4 Lecture 13: Graphs II
Initial Cost matrix Lecture 13: Graphs II
After w = 1 Which is cheaper, the current cost of the path from u to v or the cost of the path from u to 1 plus the cost of the path from 1 to v? Lecture 13: Graphs II
After w = 2 Which is cheaper, the current cost of the path from u to v or the cost of the path from u to 2 plus the cost of the path from 2 to v? The cost of the current path may already involve 1. The cost of the paths from u to 2 and from 2 to v may already include the vertex 1 from the previous iteration. Lecture 13: Graphs II
After w = 3 Which is cheaper, the cost of the current path from u to v or the cost of the path from u to 3 plus the cost of the path from 3 to v? The cost of the current path may already include 1 and 2. The cost of the paths from u to 3 and from 3 to v may already include the vertex 1 and 2 from previous iterations. Lecture 13: Graphs II
After w = 4 Which is cheaper, the cost of the current path from u to v or the cost of the path from u to 4 plus the cost of the path from 4 to v? The cost of the current path may already include 1 and 2 and 3. The cost of the paths from u to 4 and from 4 to v may already include the vertices 1, 2 and 3 from previous iterations. Quiz: What’s the runtime complexity of Floyd Warshall? Lecture 13: Graphs II
Why does Floyd Warshall Work? (From CLR) • Theorem: Subpaths of shortest paths are shortest paths. • Let dijk be the weight of the shortest path from vertex i to vertex j for which all intermediate vertices are in the set {1,2,…,k}. • When k = 0, a path from vertex i to vertex j with no intermediate vertex numbered higher than 0 has no intermediate vertices at all. Such a path has at most one edge and hence dij0= wij = the weight of the edge from i to j. • dijk = wij if k = 0. = min (dij(k-1) , dik(k-1) + dkj(k-1)) if k >= 1. Lecture 13: Graphs II
Minimum Spanning Trees • Suppose we want to find the lowest cost solution to connect n vertices with (n - 1) edges • Example: Wiring pins of electrical components • Solution: Find a minimum spanning tree (MST) Lecture 13: Graphs II
Minimum Spanning Tree • Assume: • A connected, undirected graph,G = (V, E) • V is the set of vertices • E is the set of edges • for each edge (u, v) in E, we have a weight w(u, v) specifying the cost to connect u and v Lecture 13: Graphs II
Minimum Spanning Tree • Goal: • Find an acyclic subset of the edge list, T, which connects all of the vertices; • The total cost (the sum of w(u, v) for edges in T) is minimized. Lecture 13: Graphs II
8 7 b c d 4 9 2 11 14 a i 4 e 7 6 8 10 h g f 1 2 Example Total weight: 37 Unique?... No. Replacing (b,c) with (a,h) yields another ST with weight = 37. Lecture 13: Graphs II
Generic MST • Maintain a set of edges A which is always a subset of some minimal spanning tree • At each step, an edge (u,v) is found which can be added to A without violating this invariant;A U {(u,v)} is also a subset of a MST. We call (u,v) a safe edge. Lecture 13: Graphs II
Generic MST GenericMST(G,w) A = null while A does not form a spanning tree do: find an edge (u,v) that is safe for A A = A U {(u,v)} return A To implement MST, we need two things: • a rule for recognizing safe edges; • an algorithm that uses the rule to find safe edges. Lecture 13: Graphs II
Definitions • A cut(S, V-S) of an undirected graph G = (V,E) is a partition of V 8 7 S b c d 4 9 2 11 14 a i 4 e 7 6 8 10 h g f 1 2 V-S A: Lecture 13: Graphs II
Definitions • An edge (u,v)crosses the cut(S,V-S) if one of its endpoints is in S and the other is in V-S. • A cut respects the set A of edges if no edge in A crosses the cut • A crossing edge is a light edge if its weight is the minimum of any edge crossing the cut Lecture 13: Graphs II
Theorem • Let G = (V,E) be a connected, undirected graph with weight function w. Let A be a subset of E that is included in some minimum spanning tree for G; let (S, V-S) be any cut of G that respects A; and let (u,v) be a light edge crossing (S, V-S). Then (u,v) is safe for A. Lecture 13: Graphs II
Prim’s Algorithm • Greed sometimes pays off. • Edges in A always form a single tree • Start from an arbitrary root vertex r and grow until the tree spans V • At each step, a light edge connecting A to V-A is added • Store nodes in V-A in a priority queue based on a key Lecture 13: Graphs II
Prim’s Algorithm • For each vertex v, key[v] is the minimum weight of any edge connecting v to a vertex in the tree we are building • The field pi[v] names the “parent” of v in the tree we are building • A = {(v,pi[v]): v in V - {r} - Q} Lecture 13: Graphs II
Prim’s Algorithm MST-Prim(G,w,r) Q = V[G] foreach u in Q do: key[u] = ? // initialize to ‘infinity’ key[r] = 0 pi[r] = null while Q is not empty do: u = ExtractMin(Q) // find light edge; u = r first time through foreach v in Adj[u] do: if v in Q && w(u,v) < key[v] // update adjacent nodes then pi[v] = u key[v] = w(u,v) Lecture 13: Graphs II
8 7 b c d 4 9 2 11 14 a i 4 e 7 6 a b c d e f g h i 8 a b c d e f g h i 10 0 ? ? ? ? ? ? ? ? 0 4 8 7 10 4 2 7 2 h g f f nil 1 2 nil a b c f c f i c 0 4 ? ? ? ? ? 8 ? 0 4 8 7 10 4 2 1 2 a g nil a a nil a b c f c f g c 0 4 8 ? ? ? ? 8 ? 0 4 8 7 10 4 2 1 2 b h nil a b a nil a b c f c f g c 0 4 8 7 9 4 2 1 2 0 4 8 7 ? 4 ? 8 2 d c nil a b c d c f g c nil a b c c a c 0 4 8 7 9 4 2 1 2 0 4 8 7 ? 4 6 7 2 e i nil a b c d c f g c nil a b c c i i c weight parent Lecture 13: Graphs II
Prim Can Help With TSP • Given a minimal tour H*, chop off an edge to form a spanning tree ST. • c(ST) <= c(H*) • c(MST) <= c(ST) <= c(H*) • Walk the MST using each edge twice. c(walk) = 2c(MST) <= 2c(H*). • Use the triangle inequality to make the walk a tour H. This is a preorder traversal. • c(H) <= c(walk) <= 2c(H*) • So, compute MST using Prim, and create a tour. You are within twice the optimal tour. Lecture 13: Graphs II