950 likes | 1.1k Views
Advanced Algorithm Design and Analysis . Jiaheng Lu Renmin University of China www.jiahenglu.net. Directed Acyclic Graphs. A directed acyclic graph or DAG is a directed graph with no directed cycles:. DFS and DAGs.
E N D
Advanced Algorithm Design and Analysis Jiaheng Lu Renmin University of China www.jiahenglu.net
Directed Acyclic Graphs • A directed acyclic graph or DAG is a directed graph with no directed cycles:
DFS and DAGs • Argue that a directed graph G is acyclic iff a DFS of G yields no back edges: • Forward: if G is acyclic, will be no back edges • Trivial: a back edge implies a cycle • Backward: if no back edges, G is acyclic • Argue contrapositive: G has a cycle a back edge • Let v be the vertex on the cycle first discovered, and u be the predecessor of v on the cycle • When v discovered, whole cycle is white • Must visit everything reachable from v before returning from DFS-Visit() • So path from uv is yellowyellow, thus (u, v) is a back edge
Topological Sort • Topological sort of a DAG: • Linear ordering of all vertices in graph G such that vertex u comes before vertex v if edge (u, v) G • Real-world example: getting dressed
Getting Dressed Underwear Socks Watch Pants Shoes Shirt Belt Tie Jacket
Getting Dressed Underwear Socks Watch Pants Shoes Shirt Belt Tie Jacket Socks Underwear Pants Shoes Watch Shirt Belt Tie Jacket
Topological Sort Algorithm Topological-Sort() { Run DFS When a vertex is finished, output it Vertices are output in reverse topological order } • Time: O(V+E) • Correctness: Want to prove that (u,v) G uf > vf
Correctness of Topological Sort • Claim: (u,v) G uf > vf • When (u,v) is explored, u is yellow • v = yellow (u,v) is back edge. Contradiction (Why?) • v = white v becomes descendent of u vf < uf (since must finish v before backtracking and finishing u) • v = black v already finished vf < uf
Minimum Spanning Tree • Problem: given a connected, undirected, weighted graph: 6 4 5 9 14 2 10 15 3 8
Minimum Spanning Tree • Problem: given a connected, undirected, weighted graph, find a spanning tree using edges that minimize the total weight 6 4 5 9 14 2 10 15 3 8
Minimum Spanning Tree • Which edges form the minimum spanning tree (MST) of the below graph? A 6 4 5 9 H B C 14 2 10 15 G E D 3 8 F
Minimum Spanning Tree • Answer: A 6 4 5 9 H B C 14 2 10 15 G E D 3 8 F
Minimum Spanning Tree • MSTs satisfy the optimal substructure property: an optimal tree is composed of optimal subtrees • Let T be an MST of G with an edge (u,v) in the middle • Removing (u,v) partitions T into two trees T1 and T2 • Claim: T1 is an MST of G1 = (V1,E1), and T2 is an MST of G2 = (V2,E2) (Do V1 and V2 share vertices? Why?) • Proof: w(T) = w(u,v) + w(T1) + w(T2)(There can’t be a better tree than T1 or T2, or T would be suboptimal)
Minimum Spanning Tree • Thm: • Let T be MST of G, and let A T be subtree of T • Let (u,v) be min-weight edge connecting A to V-A • Then (u,v) T
Minimum Spanning Tree • Thm: • Let T be MST of G, and let A T be subtree of T • Let (u,v) be min-weight edge connecting A to V-A • Then (u,v) T
Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);
Prim’s Algorithm 6 4 9 5 MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 14 2 10 15 3 8 Run on example graph
Prim’s Algorithm 6 4 9 5 MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 14 2 10 15 3 8 Run on example graph
Prim’s Algorithm 6 4 9 5 MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 14 2 10 15 r 0 3 8 Pick a start vertex r
Prim’s Algorithm 6 4 9 5 MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 14 2 10 15 u 0 3 8 Red vertices have been removed from Q
Prim’s Algorithm 6 4 9 5 MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 14 2 10 15 u 0 3 8 3 Red arrows indicate parent pointers
Prim’s Algorithm 6 4 9 5 MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 14 14 2 10 15 u 0 3 8 3
Prim’s Algorithm 6 4 9 5 MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 14 14 2 10 15 0 3 8 3 u
Prim’s Algorithm 6 4 9 5 MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 14 14 2 10 15 0 8 3 8 3 u
Prim’s Algorithm 6 4 9 5 MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 10 14 2 10 15 0 8 3 8 3 u
Prim’s Algorithm 6 4 9 5 MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 10 14 2 10 15 0 8 3 8 3 u
Prim’s Algorithm 6 4 9 5 MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 10 2 14 2 10 15 0 8 3 8 3 u
Prim’s Algorithm 6 4 9 5 MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 10 2 14 2 10 15 0 8 15 3 8 3 u
Prim’s Algorithm u 6 4 9 5 MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 10 2 14 2 10 15 0 8 15 3 8 3
Prim’s Algorithm u 6 4 9 5 MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 10 2 9 14 2 10 15 0 8 15 3 8 3
Prim’s Algorithm u 4 6 4 9 5 MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 10 2 9 14 2 10 15 0 8 15 3 8 3
Prim’s Algorithm u 4 6 4 9 5 MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 5 2 9 14 2 10 15 0 8 15 3 8 3
Prim’s Algorithm u 4 6 4 9 5 MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 5 2 9 14 2 10 15 0 8 15 3 8 3
Prim’s Algorithm u 4 6 4 9 5 MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 5 2 9 14 2 10 15 0 8 15 3 8 3
Prim’s Algorithm u 4 6 4 9 5 MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 5 2 9 14 2 10 15 0 8 15 3 8 3
Prim’s Algorithm 4 6 4 9 5 MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 5 2 9 u 14 2 10 15 0 8 15 3 8 3
Review: Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); What is the hidden cost in this code?
Review: Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; DecreaseKey(v, w(u,v));
Review: Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; DecreaseKey(v, w(u,v)); How often is ExtractMin() called? How often is DecreaseKey() called?
Review: Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); What will be the running time?A: Depends on queue binary heap: O(E lg V) Fibonacci heap: O(V lg V + E)
Single-Source Shortest Path • Problem: given a weighted directed graph G, find the minimum-weight path from a given source vertex s to another vertex v • “Shortest-path” = minimum weight • Weight of path is sum of edges • E.g., a road map: what is the shortest path from Chapel Hill to Charlottesville?
Binary Heap A special kind of binary tree. It has two properties that are not generally true for other trees: Completeness The tree is complete, which means that nodes are added from top to bottom, left to right, without leaving any spaces. A binary tree is completely full if it is of height, h, and has 2h+1-1 nodes. Heapness The item in the tree with the highest priority is at the top of the tree, and the same is true for every subtree.
Binary Heap Binary tree of height, h, is completeiff it is empty or its left subtree is complete of height h-1 and its right subtree is completely full of height h-2 or its left subtree is completely full of height h-1 and its right subtree is complete of height h-1.
Binary Heap In simple terms: • A heap is a binary tree in which every parent is greater than its child(ren). • A Reverse Heap is one in which the rule is “every parent is less than the child(ren)”.
Binary Heap To build a heap, data is placed in the tree as it arrives. Algorithm: • add a new node at the next leaf position • use this new node as the current position • While new data is greater than that in the parent of the current node: ● move the parent down to the current node ● make the parent (now vacant) the current node ● Place data in current node
Binary Heap New nodes are always added on the deepest level in an orderly way. The tree never becomes unbalanced. There is no particular relationship among the data items in the nodes on any given level, even the ones that have the same parent A heap is not a sorted structure. Can be regarded as partially ordered. A given set of data can be formed into many different heaps (depends on the order in which the data arrives.)
Binary Heap Example: Data arrives to be heaped in the order: 54, 87, 27, 67, 19, 31, 29, 18, 32, 56, 7, 12, 31
87 87 54 87 87 87 27 27 27 27 54 54 67 87 54 67 54 54 67 19 Binary Heap 54, 87, 27, 67, 19, 31, 29, 18, 32, 56, 7, 12, 31 54
87 87 67 67 27 27 19 19 54 54 Binary Heap 54, 87, 27, 67, 19, 31, 29, 18, 32, 56, 7, 12, 31 31
87 87 67 67 31 31 27 27 29 29 19 19 54 54 Binary Heap 54, 87, 27, 67, 19, 31, 29, 18, 32, 56, 7, 12, 31 18 32 etc…