MA/CSSE 473 Day 38 Finish Disjoint Sets Dijkstra.

Slides:



Advertisements
Similar presentations
Chapter 5: Tree Constructions
Advertisements

Chapter 9 Greedy Technique. Constructs a solution to an optimization problem piece by piece through a sequence of choices that are: b feasible - b feasible.
Lecture 15. Graph Algorithms
Advanced Algorithm Design and Analysis Jiaheng Lu Renmin University of China
Disjoint Sets CS2 -- 9/8/2010. Disjoint Sets A disjoint set contains a set of sets such that in each set, an element is designated as a marker for the.
Comp 122, Spring 2004 Greedy Algorithms. greedy - 2 Lin / Devi Comp 122, Fall 2003 Overview  Like dynamic programming, used to solve optimization problems.
Greed is good. (Some of the time)
CS38 Introduction to Algorithms Lecture 5 April 15, 2014.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Graphs.
1 Greedy 2 Jose Rolim University of Geneva. Algorithmique Greedy 2Jose Rolim2 Examples Greedy  Minimum Spanning Trees  Shortest Paths Dijkstra.
Union-Find Problem Given a set {1, 2, …, n} of n elements. Initially each element is in a different set. {1}, {2}, …, {n} An intermixed sequence of union.
CHAPTER 3 Data Structures. Algorithm Initializing a Stack This algorithm initializes a stack to empty. An empty stack has t = -1. Input Parameters:
CSE 780 Algorithms Advanced Algorithms Minimum spanning tree Generic algorithm Kruskal’s algorithm Prim’s algorithm.
Minimum Spanning Trees Definition Algorithms –Prim –Kruskal Proofs of correctness.
3 -1 Chapter 3 The Greedy Method 3 -2 The greedy method Suppose that a problem can be solved by a sequence of decisions. The greedy method has that each.
1 7-MST Minimal Spanning Trees Fonts: MTExtra:  (comment) Symbol:  Wingdings: Fonts: MTExtra:  (comment) Symbol:  Wingdings:
CPSC 311, Fall CPSC 311 Analysis of Algorithms Disjoint Sets Prof. Jennifer Welch Fall 2009.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
Dynamic Sets and Data Structures Over the course of an algorithm’s execution, an algorithm may maintain a dynamic set of objects The algorithm will perform.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 17 Union-Find on disjoint sets Motivation Linked list representation Tree representation.
Lecture 9 Disjoint Set ADT. Preliminary Definitions A set is a collection of objects. Set A is a subset of set B if all elements of A are in B. Subsets.
Lecture 16: Union and Find for Disjoint Data Sets Shang-Hua Teng.
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
Chapter 9: Graphs Spanning Trees Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
Minimal Spanning Trees What is a minimal spanning tree (MST) and how to find one.
Midwestern State University Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm 1.
1 Binomial heaps, Fibonacci heaps, and applications.
Trees Featuring Minimum Spanning Trees HKOI Training 2005 Advanced Group Presented by Liu Chi Man (cx)
9/10/10 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne Adam Smith Algorithm Design and Analysis L ECTURE 8 Greedy Graph.
MA/CSSE 473 Day 36 Kruskal proof recap Prim Data Structures and detailed algorithm.
Theory of Computing Lecture 10 MAS 714 Hartmut Klauck.
© The McGraw-Hill Companies, Inc., Chapter 3 The Greedy Method.
2IL05 Data Structures Fall 2007 Lecture 13: Minimum Spanning Trees.
Spring 2015 Lecture 11: Minimum Spanning Trees
D ESIGN & A NALYSIS OF A LGORITHM 06 – D ISJOINT S ETS Informatics Department Parahyangan Catholic University.
Minimum Spanning Trees CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Computer Algorithms Submitted by: Rishi Jethwa Suvarna Angal.
The single-source shortest path problem (SSSP) input: a graph G = (V, E) with edge weights, and a specific source node s. goal: find a minimum weight (shortest)
Disjoint Sets Data Structure (Chap. 21) A disjoint-set is a collection  ={S 1, S 2,…, S k } of distinct dynamic sets. Each set is identified by a member.
Disjoint Sets Data Structure. Disjoint Sets Some applications require maintaining a collection of disjoint sets. A Disjoint set S is a collection of sets.
CSCE 411H Design and Analysis of Algorithms Set 7: Disjoint Sets Prof. Evdokia Nikolova* Spring 2013 CSCE 411H, Spring 2013: Set 7 1 * Slides adapted from.
Disjoint-sets Abstract Data Type (ADT) that contains nonempty pairwise disjoint sets: For all sets X and Y in the container, – X != ϴ and Y != ϴ – And.
Union-Find  Application in Kruskal’s Algorithm  Optimizing Union and Find Methods.
MA/CSSE 473 Days Answers to student questions Prim's Algorithm details and data structures Kruskal details.
CHAPTER 8 THE DISJOINT SET ADT §1 Equivalence Relations 【 Definition 】 A relation R is defined on a set S if for every pair of elements (a, b), a, b 
MA/CSSE 473 Day 34 MST details: Kruskal's Algorithm Prim's Algorithm.
CS6045: Advanced Algorithms Sorting Algorithms. Heap Data Structure A heap (nearly complete binary tree) can be stored as an array A –Root of tree is.
Lecture 12 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
CSE 589 Applied Algorithms Spring 1999 Prim’s Algorithm for MST Load Balance Spanning Tree Hamiltonian Path.
Proof of correctness of Dijkstra’s algorithm: Basically, we need to prove two claims. (1)Let S be the set of vertices for which the shortest path from.
MA/CSSE 473 Day 37 Student Questions Kruskal Data Structures and detailed algorithm Disjoint Set ADT 6,8:15.
CSE 373, Copyright S. Tanimoto, 2001 Up-trees - 1 Up-Trees Review of the UNION-FIND ADT Straight implementation with Up-Trees Path compression Worst-case.
MA/CSSE 473 Day 37 Student Questions Kruskal data structures
MA/CSSE 473 Day 36 Student Questions More on Minimal Spanning Trees
Disjoint Sets Data Structure
MA/CSSE 473 Days Answers to student questions
COMP 6/4030 ALGORITHMS Prim’s Theorem 10/26/2000.
Lecture 12 Algorithm Analysis
Greedy Algorithms / Minimum Spanning Tree Yin Tat Lee
MA/CSSE 473 Day 36 Kruskal proof recap
MA/CSSE 473 Day 33 Student Questions Change to HW 13
Lecture 12 Algorithm Analysis
Minimum Spanning Tree Algorithms
CSE 373: Data Structures and Algorithms
Disjoint Sets DS.S.1 Chapter 8 Overview Dynamic Equivalence Classes
Disjoint Sets Data Structure (Chap. 21)
Lecture 12 Algorithm Analysis
Chapter 9: Graphs Spanning Trees
Presentation transcript:

MA/CSSE 473 Day 38 Finish Disjoint Sets Dijkstra

MA/CSSE 473 Day 38 HW 14 –Only do problems 1, 2, 3, and 9 –(Fall, 2010 vs. Fall, 2012) Fill out the Course evaluation form –If everybody in a section does it, everyone in that section gets 5 bonus points on the Final Exam –I can't see who has completed the evaluation, but I can see how many Final Exam Monday evening, O269 Student Questions Finish disjoint set discussion

Final Exam Format Same as previous exams. You may bring up to four pages of notes. –Double-sided –Hopefully that is enough for you to write down details of things you understand but don't want to memorize. Material from –Chapters 1-9 –Excerpts from Weiss and Dasgupta that I posted on ANGEL –HW 1-14 –All class days

Recap: Disjoint Set Representation Each disjoint set is a tree, with the "marked" element as its root Efficient representation of the trees: –an array called parent –parent[i] contains the index of i’s parent. –If i is a root, parent[i]=i

Using this representation makeset(i): findset(i): mergetrees(i,j): –assume that i and j are the marked elements from different sets. union(i,j): –assume that i and j are elements from different sets def makeset1(i): parent[i] = i def findset1(i): while i != parent[i]: i = parent[i] return i def mergetrees1(i,j): parent[i] = j def union1(i,j): mergetrees1(findset1(i), findset1(j))

def mergetrees2(i,j): if height[i] < height[j]): parent[i] = j elif height[i] > height[j]: parent[j] = i else: parent[i] = j height[j] = height[j] + 1 Can we keep the trees from growing so fast? Make the shorter tree the child of the taller one What do we need to add to the representation? rewrite makeset, mergetrees. findset & union are unchanged. What can we say about the maximum height of a k-node tree? def makeset2(i): parent[i] = i height[i] = 0

Theorem: max height of a k-node tree T produced by these algorithms is  lg k  Base case… Induction hypothesis… Induction step: –Let T be a k-node tree –T is the union of two trees: T 1 with k 1 nodes and height h 1 T 2 with k 2 nodes and height h 2 –What can we about the heights of these trees? –Case 1: h 1 ≠h 2. Height of T is –Case 2: h 1 =h 2. WLOG Assume k 1 ≥k 2. Then k 2 ≤k/2. Height of tree is 1 + h2 ≤ …

Worst-case running time Again, assume n makeset operations, followed by m union/find operations. If m > n If m < n

Speed it up a little more Path compression: Whenever we do a findset operation, change the parent pointer of each node that we pass through on the way to the root so that it now points directly to the root. Replace the height array by a rank array, since the number now is only an upper bound for the height. Look at makeset, findset, mergetrees (on next slides)

Makeset This algorithm represents the set {i} as a one-node tree and initializes its rank to 0. def makeset3(i): parent[i] = i rank[i] = 0

Findset This algorithm returns the root of the tree to which i belongs and makes every node on the path from i to the root (except the root itself) a child of the root. def findset(i): root = i while root != parent[root]: root = parent[root] j = parent[i] while j != root: parent[i] = root i = j j = parent[i] return root

Mergetrees This algorithm receives as input the roots of two distinct trees and combines them by making the root of the tree of smaller rank a child of the other root. If the trees have the same rank, we arbitrarily make the root of the first tree a child of the other root. def mergetrees(i,j) : if rank[i] < rank[j]: parent[i] = j elif rank[i] > rank[j]: parent[j] = i else: parent[i] = j rank[j] = rank[j] + 1

Analysis It's complicated! R.E. Tarjan proved (1975)*: –Let t = m + n –Worst case running time is Ѳ(t α(t, n)), where α is a function with an extremely slow growth rate. –Tarjan's α: α(t, n) ≤ 4 for all n ≤ Thus the amortized time for each operation is essentially constant time. * According to Algorithmsby R. Johnsonbaugh and M. Schaefer, 2004, Prentice-Hall, pages

Dijkstra's Algorithm For a selected vertex, (call it start) in a connected, weighted graph G, –find a shortest (minimum total weight) path from start to each other vertex Assumption: All weights are positive

Dijkstra’s algorithm approach The shortest path from vertex v to vertex w is either –empty, if v = w, or –obtained by adding an edge (u, w) to the end of a shortest path from v to u.

Dijkstra’s algorithm start-up Begin with a subgraph that consists of only the starting vertex. Among all edges (start, v), –choose the one with the smallest weight, –add it (along with the edge that connects it) to our subgraph. –Clearly (start, v) is the shortest path from start to v.

Dijkstra’s algorithm continues Loop: Among all vertices not in a path yet, and which are also adjacent to a vertex that is in the path, –choose one with minimal path length from start, –and add it to the path, –along with the edge that connects it to a path. Which previously- studied algorithm that we have seen does this closely resemble? Example: g = AdjancencyListGraph( [[1, [(2, 4), (3, 2), (5, 3)]], [2, [(1, 4), (4, 5)]], [3, [(1, 2), (5, 6), (4, 1), (6,3)]], [4, [(2, 5), (6,6), (3,1)]], [5, [(1, 3), (3,6), (6,2)]], [6, [(5, 2), (3, 3), (4, 6)]] ])

Dijkstra’s algorithm data structures A parent array as in Prim’s algorithm. A minheap that stores “unpathed” nodes as keys and minimum path lengths (as extensions of a minimum path that has already been discovered) as weights. –Use an indirect binary heap, just as we did for Prim. A cost array that stores the minimum path length to each vertex –Not strictly necessary; could reconstruct form adjacency list and parent array.

Dijkstra’s algorithm details