240 likes | 338 Views
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:
E N D
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: • Prof. Prof. Nenad Jukic • 17-Nov @ 11:00am • “Data Warehouses: The Foundation of Business Intelligence”
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!
CS Story Time • Prof. Jones’ research group • See http://cs.ua.edu/StoryHourSlide.pdf
Next Midterm • Tuesday before Thanksgiving ! • 25-November
Outline • The Greedy Design Paradigm • More on the Greedy Method • Single-Source Shortest Paths
The Greedy Paradigm • Greedy Principle: • Start with an (optimal) solution and adding locally optimal structure eventually gives a globally optimal solution
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
Prim’s Algorithm • Greedily grows a MST in a local sense • Classical Greedy Approach
Prim’s Algorithm • Vt{v0} • Et empty-set • For i1 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
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
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
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?
How much does Prim’s Algorithm cost? • Using a Min-Heap • How does this work?
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]
Min-Heap Review • Min-Heap-Insert: O(log n) • Extract-Min: O(log n)
Prim’s Alg. With Min-Heap • O(|E| log |V|) • Why?
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
Kruskal’s Algorithm • Sort E • Gives e1, …, e|E| • Et emptyset • ecounter 0; k0 • While ecounter < |V|-1 do • kk+1 • If (Et u {ek} is acyclic) • Et Et u {ek}; ecounter ecounter +1; • Return Et
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
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?
A Union-Find Algorithm • Initial Makeset(x) • Find(x) subset containing x • Union(x,y) joins the subsets containing x and y
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}
Two main methods • Focus on • Running the find operation quickly • Focus on • Running the union operation quickly