1 / 94

Advanced Algorithm Design and Analysis

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.

lucio
Télécharger la présentation

Advanced Algorithm Design and Analysis

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. Advanced Algorithm Design and Analysis Jiaheng Lu Renmin University of China www.jiahenglu.net

  2. Directed Acyclic Graphs • A directed acyclic graph or DAG is a directed graph with no directed cycles:

  3. 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 uv is yellowyellow, thus (u, v) is a back edge

  4. 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

  5. Getting Dressed Underwear Socks Watch Pants Shoes Shirt Belt Tie Jacket

  6. Getting Dressed Underwear Socks Watch Pants Shoes Shirt Belt Tie Jacket Socks Underwear Pants Shoes Watch Shirt Belt Tie Jacket

  7. 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  uf > vf

  8. Correctness of Topological Sort • Claim: (u,v)  G  uf > vf • When (u,v) is explored, u is yellow • v = yellow  (u,v) is back edge. Contradiction (Why?) • v = white  v becomes descendent of u  vf < uf (since must finish v before backtracking and finishing u) • v = black  v already finished  vf < uf

  9. Minimum Spanning Tree • Problem: given a connected, undirected, weighted graph: 6 4 5 9 14 2 10 15 3 8

  10. 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

  11. 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

  12. Minimum Spanning Tree • Answer: A 6 4 5 9 H B C 14 2 10 15 G E D 3 8 F

  13. 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)

  14. 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

  15. 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

  16. 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);

  17. 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

  18. 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

  19. 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

  20. 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

  21. 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

  22. 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

  23. 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

  24. 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

  25. 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

  26. 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

  27. 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

  28. 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

  29. 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

  30. 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

  31. 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

  32. 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

  33. 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

  34. 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

  35. 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

  36. 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

  37. 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?

  38. 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));

  39. 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?

  40. 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)

  41. 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?

  42. 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.

  43. 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.

  44. 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)”.

  45. 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

  46. 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.)

  47. Binary Heap Example: Data arrives to be heaped in the order: 54, 87, 27, 67, 19, 31, 29, 18, 32, 56, 7, 12, 31

  48. 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

  49. 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

  50. 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…

More Related