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.

Slides:



Advertisements
Similar presentations
Lecture 15. Graph Algorithms
Advertisements

Chapter 23 Minimum Spanning Tree
Advanced Algorithm Design and Analysis Jiaheng Lu Renmin University of China
CSCE 411H Design and Analysis of Algorithms Set 8: Greedy Algorithms Prof. Evdokia Nikolova* Spring 2013 CSCE 411H, Spring 2013: Set 8 1 * Slides adapted.
Greed is good. (Some of the time)
1 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 20 Prof. Erik Demaine.
Minimum Spanning Trees Definition Two properties of MST’s Prim and Kruskal’s Algorithm –Proofs of correctness Boruvka’s algorithm Verifying an MST Randomized.
Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm.
More Graph Algorithms Minimum Spanning Trees, Shortest Path Algorithms.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
1 Greedy 2 Jose Rolim University of Geneva. Algorithmique Greedy 2Jose Rolim2 Examples Greedy  Minimum Spanning Trees  Shortest Paths Dijkstra.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 13 Minumum spanning trees Motivation Properties of minimum spanning trees Kruskal’s.
Minimum Spanning Tree Algorithms
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.
Shortest Paths Definitions Single Source Algorithms –Bellman Ford –DAG shortest path algorithm –Dijkstra All Pairs Algorithms –Using Single Source Algorithms.
1 7-MST Minimal Spanning Trees Fonts: MTExtra:  (comment) Symbol:  Wingdings: Fonts: MTExtra:  (comment) Symbol:  Wingdings:
1 Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
Course Review COMP171 Spring Hashing / Slide 2 Elementary Data Structures * Linked lists n Types: singular, doubly, circular n Operations: insert,
Analysis of Algorithms CS 477/677
Shortest Paths Definitions Single Source Algorithms
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 17 Union-Find on disjoint sets Motivation Linked list representation Tree representation.
CSE Algorithms Minimum Spanning Trees Union-Find Algorithm
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 11 Instructor: Paul Beame.
Binomial Heaps Jyh-Shing Roger Jang ( 張智星 ) CSIE Dept, National Taiwan University.
Heapsort CIS 606 Spring Overview Heapsort – O(n lg n) worst case—like merge sort. – Sorts in place—like insertion sort. – Combines the best of both.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2001 Lecture 11 Tuesday, 12/4/01 Advanced Data Structures Chapters.
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.
CS112A1 Spring 2008 Practice Final. ASYMPTOTIC NOTATION: a)Show that log(n) and ln(n) are the same in terms of Big-Theta notation b)Show that log(n+1)
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.
Definition: Given an undirected graph G = (V, E), a spanning tree of G is any subgraph of G that is a tree Minimum Spanning Trees (Ch. 23) abc d f e gh.
Theory of Computing Lecture 10 MAS 714 Hartmut Klauck.
MST Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
Graph Dr. Bernard Chen Ph.D. University of Central Arkansas.
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.
1 Minimum Spanning Trees. Minimum- Spanning Trees 1. Concrete example: computer connection 2. Definition of a Minimum- Spanning Tree.
Disjoint Sets Data Structure. Disjoint Sets Some applications require maintaining a collection of disjoint sets. A Disjoint set S is a collection of sets.
Union-find Algorithm Presented by Michael Cassarino.
1 Minimum Spanning Trees (some material adapted from slides by Peter Lee, Ananda Guna, Bettina Speckmann)
Runtime O(VE), for +/- edges, Detects existence of neg. loops
November 13, Algorithms and Data Structures Lecture XII Simonas Šaltenis Aalborg University
1 Binomial Tree Binomial tree. n Recursive definition: B k-1 B0B0 BkBk B0B0 B1B1 B2B2 B3B3 B4B4.
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.
Chapter 23: Minimum Spanning Trees: A graph optimization problem Given undirected graph G(V,E) and a weight function w(u,v) defined on all edges (u,v)
F d a b c e g Prim’s Algorithm – an Example edge candidates choosen.
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.
Data StructuresData Structures Priority Queue. Recall Queues FIFO:First-In, First-Out Some contexts where this seems right? Some contexts where some things.
David Luebke 1 3/1/2016 CS 332: Algorithms Dijkstra’s Algorithm Disjoint-Set Union.
Lecture 12 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
November 22, Algorithms and Data Structures Lecture XII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Lecture ? The Algorithms of Kruskal and Prim
Introduction to Algorithms
Shortest Paths and Minimum Spanning Trees
Minimum Spanning Trees
CSE 373 Data Structures and Algorithms
Algorithms and Data Structures Lecture XII
CS200: Algorithm Analysis
Minimum Spanning Tree Algorithms
Shortest Paths and Minimum Spanning Trees
CSE 373: Data Structures and Algorithms
The Minimum Cost Spanning Tree Problem
CSE 373: Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
Presentation transcript:

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 operations on this set –Queries –Modifying operations We must choose a data structure to implement the dynamic set efficiently The “correct” data structure to choose is based on –Which operations need to be supported –How frequently each operation will be executed

Some Example Operations Notation –S is the data structure –k is the key of the item –x is a pointer to the item Search(S,k): returns pointer to item Insert(S,x) Delete(S,x): note we are given a pointer to item Minimum or Maximum(S): returns pointer Decrease-key(S,x) Successor or Predecessor (S,x): returns pointer Merge(S 1,S 2 )

Basic Data Structures/Containers Unsorted Arrays Sorted Array Unsorted linked list Sorted linked list Stack Queue Heap

Puzzles How can I implement a queue with two stacks? –Running time of enqueue? –Dequeue? How can I implement two stacks in one array A[1..n] so that neither stack overflows unless the total number of elements in both stacks exceeds n?

Unsorted Array Sorted Array Unsorted LL Sorted LL Heap Search Insert Delete Max/Min Pred/Succ Merge

Case Study: Dictionary Search(S,k) Insert(S,x) Delete(S,x) Is any one of the data structures listed so far always the best for implementing a dictionary? Under what conditions, if any, would each be best? What other standard data structure is often used for a dictionary?

Case Study: Priority Queue Insert(S,x) Max(S) Delete-max(S) Decrease-key(S,x) Which data structure seen so far is typically best for implementing a priority queue and why?

Case Study: Minimum Spanning Trees Input –Weighted, connected undirected graph G=(V,E) Weight (length) function w on each edge e in E Task –Compute a spanning tree of G of minimum total weight Spanning tree –If there are n nodes in G, a spanning tree consists of n-1 edges such that no cycles are formed

Prim’s algorithm A greedy approach to edge selection –Initialize connected component N to be any node v –Select the minimum weight edge connecting N to V-N –Update N and repeat Dynamic set in Prim’s algorithm –An item is a node in V-N –The value of a node is its minimum distance to any node in N –A minimum weight edge connecting N to V-N corresponds to the node with minimum value in V-N (Extract minimum) –When v is added to N, we need to update the value of the neighbors of v in V-N if they are closer to v than other nodes in N (Decrease key)

Maintain dynamic set of nodes in V-N If we started with node D, N is now {C,D} Dynamic set values of other nodes: –A, E, F: infinity –B: 4 –G: 6 Extract-min: Node B is added next to N Illustration ABC D EFG

Node B is added to N; edge (B,C) is added to T Need to update dynamic set values of A, E, F –Decrease-key operation Dynamic set values of other nodes: –A: 1 –E: 2 –F: 5 –G: 6 Extract-min: Node A is added next to N Updating Dynamic Set ABC D EFG

Node A is added to N; edge (A,B) is added to T Need to update dynamic set values of E –Decrease-key operation Dynamic set values of other nodes: –E: 2 (unchanged because 2 is smaller than 3) –F: 5 –G: 6 Updating Dynamic Set Again ABC D EFG

Dynamic Set Analysis How many objects in initial dynamic set representation of V-N? How many extract-min operations need to happen? How many decrease-key operations may occur? Given all of the above, choose a data structure and tell me the implementation cost. –Time to build initial dynamic set –Time to implement all extract-min operations –Time to implement all decrease-key operations

Kruskal’s Algorithm A greedy approach to edge selection –Initialize tree T to have no edges –Iterate through the edges starting with the minimum weight one Add the edge (u,v) to tree T if this does not create a cycle

6 ABC D EFG Example (A,B) (A,E) (B,E): cycle (B,C) (F,G) (C,G) (B,F): cycle (C,D) (D,G): cycle ABC D EFG ABC D EFG ABC D EFG

Disjoint Set Data Structure Given a universe U of objects (nodes V) –Maintain a collection of disjoint sets S i that partition U –Find-set(x): Returns set S i that contains x –Merge(S i, S j ): Returns new set S k = S i union S j Disjoint Sets and Kruskal’s algorithm –Universe U is the set of vertices V –The sets are the current connected components –When an edge (u,v) is considered, we check for a cycle by determining if u and v belong to the same set 2 calls to Find-set(x) –If we add (u,v) to T, we need to merge the 2 sets represented by u and v. Merge(S u,S v )

Analysis How do we initialize the universe? How many calls to find-set do we perform? How many calls to merge-set do we perform?

Better data structures We need mergeable data structures that still support fast searches –Binomial heaps (ch. 19) –Fibonacci heaps (ch. 20) –Disjoint set data structures (ch. 21) linked lists forests

Disjoint-set forests Representation –Each set is represented as a tree, nodes point to parent –Root element is the representative for the set, points to self or has null parent pointer –Height: maintain height of tree as an integer Operations –Makeset: make a tree with one node –Find: progress from current element to root element following links –Union: connect root of lower height tree to point to root of larger height tree Figures copied from Jeff Erickson, UIUC

Naïve implementation Figure copied from Jeff Erickson’s slides at UIUC.

union-by-rank or union-by-depth Figure copied from Jeff Erickson’s slides at UIUC. Leads to height of any tree of n nodes being at most O(lg n).

Path Compression Figure copied from Jeff Erickson’s slides at UIUC. Leads to amortized cost of α(n), the inverse ackerman function. For all practical purposes, α(n) ≤ 4.

Binomial Heaps Binomial Tree Binomial Heap –Figures copied from Dan Gildea, University of Rochester

Key idea: Union in O(lg n) time

Binomial Trees Tree B k has 2 k nodes. B k has height k. Children of the root of B k are B k-1, B k-2, …, B 0 from left to right. Max degree of an n-node binomial tree is lg n.

Binomial Heap A binomial heap of n-elements is a collection of binomial trees with the following properties: –Each binomial tree is heap-ordered (parent is less than all children) –No two binomial trees in the collection have the same size –Number of trees will be O(lg n)

Example Binomial Heap Binomial heap of 29 elements 29 = in binary.

Minimum Operation Where does the minimum have to be? How can we find minimum in general? Running time?

Union of 2 Binomial Heaps