1 / 24

Intro to Computer Algorithms Lecture 19

Intro to Computer Algorithms Lecture 19. Phillip G. Bradford Computer Science University of Alabama. Announcements. Advisory Board’s Industrial Talk Series http://www.cs.ua.edu/9IndustrialSeries.shtm 2-Dec: Mike Thomas, CIO, Gulf States Paper Next Research Colloquia:

howard
Download Presentation

Intro to Computer Algorithms Lecture 19

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. Intro to Computer Algorithms Lecture 19 Phillip G. Bradford Computer Science University of Alabama

  2. Announcements • Advisory Board’s Industrial Talk Series • http://www.cs.ua.edu/9IndustrialSeries.shtm • 2-Dec: Mike Thomas, CIO, Gulf States Paper • Next Research Colloquia: • Prof. Prof. Nenad Jukic • 17-Nov @ 11:00am • “Data Warehouses:  The Foundation of Business Intelligence”

  3. Computer Security Research Group • Meets every Friday from 11:00 to 12:00 • In 112 Houser • Computer Security, etc. • Email me to be on the mailing list!

  4. CS Story Time • Prof. Jones’ research group • See http://cs.ua.edu/StoryHourSlide.pdf

  5. Next Midterm • Tuesday before Thanksgiving ! • 25-November

  6. Outline • The Greedy Design Paradigm • More on the Greedy Method • Single-Source Shortest Paths

  7. The Greedy Paradigm • Greedy Principle: • Start with an (optimal) solution and adding locally optimal structure eventually gives a globally optimal solution

  8. Prim’s Algorithm • Spanning Tree of an undirected graph • Acyclic subgraph containing all nodes • Minimum weight spanning tree • Weighted edges • Among the least cost spanning trees • The algorithm • The inductive Proof of optimality

  9. Prim’s Algorithm • Greedily grows a MST in a local sense • Classical Greedy Approach

  10. Prim’s Algorithm • Vt{v0} • Et  empty-set • For i1 to |V|-1 do • Find min weight edge (u,v) • Where u is in Vt and v is in V-Vt • Vt Vt u {u} • Et Et u {(u,v)} • Endfor • Return Et

  11. Proof of Correctness for Prim’s Algorithm • Assume all edge weights are different • Claim: Each subtree Ti of Prim’s algorithm is a part of some MST. • Proof by Induction. • Basis: T0 is part of all MSTs • I.H. Assume Ti is part of some MST • Inductive Step: Consider Ti to Ti+1 via Prim’s algorithm

  12. Proof of Correctness for Prim’s Algorithm • I.S. Continued • Suppose, for the sake of a contradiction, that Ti+1 is not in any MST • Then Ti+1 must have a cycle • Why? • Thus, adding (u,v) to Ti gives a cycle in Ti+1. • But, this means there is a (u’,v’) in Ti which is an MST • Therefore, deleting (u’,v’) gives a cheeper MST than Ti+1, contradicting the I.H. & completing the proof

  13. How much does Prim’s Algorithm cost? • Data-structure dependent answers • If linear search for each minimal weight edge adjacent to the growing MST • Then the cost is O(|V|2). • Can we do better? • What data structure?

  14. How much does Prim’s Algorithm cost? • Using a Min-Heap • How does this work?

  15. Min-Heap Review • A[1,…,n] • A[1] is the root • Parent(i): return floor(i/2); • Left(i): return 2i; • Right(i): return 2i+1 • Min-Heap Property: • A[Parent(i)] < A[i]

  16. Min-Heap Review • Min-Heap-Insert: O(log n) • Extract-Min: O(log n)

  17. Prim’s Alg. With Min-Heap • O(|E| log |V|) • Why?

  18. Kruskal’s Algorithm • Basic idea • Uses greedy paradigm • Augmented with decrease and conquer • Sort the edge weights • Select the smallest weight edge to add to the MST that does not form a cycle • Add this edge • Continue

  19. Kruskal’s Algorithm • Sort E • Gives e1, …, e|E| • Et emptyset • ecounter  0; k0 • While ecounter < |V|-1 do • kk+1 • If (Et u {ek} is acyclic) • Et Et u {ek}; ecounter  ecounter +1; • Return Et

  20. Correctness of Kruskal’s Algorithm • What is the `hard part’? • Making sure there are no cycles • Growing trees with minimal length edges • Similar to Prim’s algorithm

  21. Union-Find Algorithms • Start with n elements each in their “own set” • Union operation • Joins two sets • Find operation • Which set does an element belong to?

  22. A Union-Find Algorithm • Initial Makeset(x) • Find(x) subset containing x • Union(x,y) joins the subsets containing x and y

  23. Union-Find Examples • Makeset to 1,…,6 • {1},{2},{3},…,{6} • Union(1,4) and Union(5,2) • {1,4},{2,5},{3},{6} • Union(3,6) and Union(4,5) • {1,2,4,5} and {3,6}

  24. Two main methods • Focus on • Running the find operation quickly • Focus on • Running the union operation quickly

More Related