CSE 373: Data Structures and Algorithms

Slides:



Advertisements
Similar presentations
MA/CSSE 473 Day 38 Finish Disjoint Sets Dijkstra.
Advertisements

1 Disjoint Sets Set = a collection of (distinguishable) elements Two sets are disjoint if they have no common elements Disjoint-set data structure: –maintains.
1 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 20 Prof. Erik Demaine.
Andreas Klappenecker [Based on slides by Prof. Welch]
CPSC 411, Fall 2008: Set 7 1 CPSC 411 Design and Analysis of Algorithms Set 7: Disjoint Sets Prof. Jennifer Welch Fall 2008.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 17 Union-Find on disjoint sets Motivation Linked list representation Tree representation.
CSE 421 Algorithms Richard Anderson Lecture 10 Minimum Spanning Trees.
CS2420: Lecture 42 Vladimir Kulyukin Computer Science Department Utah State University.
0 Course Outline n Introduction and Algorithm Analysis (Ch. 2) n Hash Tables: dictionary data structure (Ch. 5) n Heaps: priority queue data structures.
D ESIGN & A NALYSIS OF A LGORITHM 06 – D ISJOINT S ETS Informatics Department Parahyangan Catholic University.
Dijkstra’s Algorithm. Announcements Assignment #2 Due Tonight Exams Graded Assignment #3 Posted.
Lecture X Disjoint Set Operations
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.
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.
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.
21. Data Structures for Disjoint Sets Heejin Park College of Information and Communications Hanyang University.
MA/CSSE 473 Day 37 Student Questions Kruskal Data Structures and detailed algorithm Disjoint Set ADT 6,8:15.
CSE 373: Data Structures and Algorithms
Lecture 11 Graph Algorithms
Data Structures and Algorithms I Day 19, 11/3/11 Edge-Weighted Graphs
May 12th – Minimum Spanning Trees
MA/CSSE 473 Day 36 Student Questions More on Minimal Spanning Trees
Minimum Spanning Trees
Disjoint Sets Data Structure
CSE373: Data Structures & Algorithms Lecture 19: Spanning Trees
CSE 2331/5331 Topic 9: Basic Graph Alg.
Review for Final Exam Non-cumulative, covers material since exam 2
Review for Final Exam Non-cumulative, covers material since exam 2
Greedy Algorithms / Minimum Spanning Tree Yin Tat Lee
Data Structures: Disjoint Sets
Minimum Spanning Trees
CMSC 341 Disjoint Sets Based on slides from previous iterations of this course.
CSE373: Data Structures & Algorithms Lecture 11: Implementing Union-Find Linda Shapiro Spring 2016.
Data Structures and Algorithms
Disjoint Sets with Arrays
Search Related Algorithms
CSE 373 Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
CSE 332: Data Abstractions Union/Find II
CSE 373: Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
CSE 373 Data Structures Lecture 16
CSE332: Data Abstractions Lecture 18: Minimum Spanning Trees
Sorting and recurrence analysis techniques
CSE 373: Data Structures and Algorithms
Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
CSE373: Data Structures & Algorithms Implementing Union-Find
CSE 373: Data Structures and Algorithms
Richard Anderson Winter 2009 Lecture 6
Lecture 19: Minimum Spanning Trees
Autumn 2016 Lecture 10 Minimum Spanning Trees
Lecture 20: Disjoint Sets
Lecture 18: Implementing Graphs
Lecture 21: Disjoint Sets with Arrays
Winter 2019 Lecture 10 Minimum Spanning Trees
Lecture 10 Graph Algorithms
Richard Anderson Winter 2019 Lecture 5
Lecture 21 Amortized Analysis
Lecture 23: Minimum Spanning Trees
Lecture 22: Implementing Dijkstra’s
Lecture 24: Disjoint Sets
CSE 373: Data Structures and Algorithms
Lecture 27: Array Disjoint Sets
Lecture 26: Array Disjoint Sets
Presentation transcript:

CSE 373: Data Structures and Algorithms More on disjoint sets Autumn 2018 Shrirang (Shri) Mare shri@cs.washington.edu Thanks to Kasey Champion, Ben Jones, Adam Blank, Michael Lee, Evan McCarty, Robbie Weber, Whitaker Brand, Zora Fung, Stuart Reges, Justin Hsia, Ruth Anderson, and many others for sample slides and materials ...

Today Revisiting Kruskal’s algorithm (with union/find operations) Array representation of a disjoint set Review: Answering algorithm design questions CSE 373 AU 18

Set and union Set: Collection of elements. We want to identify all elements in a set with one value, so that findSet(x) == findSet(y) if x and y in one set. In tree structure, every node has one root. So use a tree structure for our set, and identified the set as the root element Union of two elements is union of sets of those elements: Combine the two trees 2 3 5 1 4 5 CSE 373 AU 18

Improving union Problem: Trees can be unbalanced Solution: Union-by-rank! let rank(x) be a number representing the upper bound of the height of x so rank(x) ≥ height(x) Keep track of rank of all trees When unioning make the tree with larger rank the root If it’s a tie, pick one randomly and increase rank by one rank = 0 rank = 0 rank = 0 rank = 1 rank = 2 2 4 4 5 1 3 5 CSE 373 AU 18

Improving findSet() Problem: Every time we call findSet() you must traverse all the levels of the tree to find representative Solution: Path Compression Collapse tree into fewer levels by updating parent pointer of each node you visit Whenever you call findSet() update each node you touch to point directly to overallRoot rank = 3 8 10 12 9 11 6 4 5 3 2 7 13 rank = 3 findSet(5) 8 11 6 9 10 7 4 3 12 13 2 5 CSE 373 AU 18

Optimized disjoint set runtime makeSet(x) Without Optimizations With Optimizations findSet(x) union(x, y) O(1) O(1) O(n) Worst case: O(logn) O(n) Worst case: O(logn) CSE 373 AU 18

Worksheet question 1 CSE 373 AU 18

Worksheet question 1 CSE 373 AU 18

Implementation Use Nodes? In modern Java (assuming 64-bit JDK) each object takes about 32 bytes int field takes 4 bytes Pointer takes 8 bytes Overhead ~ 16 bytes Adds up to 28, but we must partition in multiples of 8 => 32 bytes Use arrays instead! Make index of the array be the vertex number Either directly to store ints or representationally We implement makeSet(x) so that we choose the representative Make element in the array the index of the parent CSE 373 AU 18

Array implementation rank = 0 rank = 3 rank = 3 1 2 3 4 5 6 7 8 9 10 1 11 2 6 12 15 3 4 5 7 10 13 14 16 17 8 9 18 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 CSE 373 AU 18

Array implementation rank = 0 rank = 3 rank = 3 1 2 3 4 5 6 7 8 9 10 1 11 2 6 12 15 3 4 5 7 10 13 14 16 17 8 9 18 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 -1 -4 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 -1 Store (rank * -1) - 1 CSE 373 AU 18

Worksheet question 2 CSE 373 AU 18

Array method implementation makeSet(x) add new value to array with a rank of -1 findSet(x) Jump into array at index/value you’re looking for, jump to parent based on element at that index, continue until you hit negative number union(x, y) findSet(x) and findSet(y) to decide who has larger rank, update element to represent new parent as appropriate CSE 373 AU 18

Graph Review Graph Definitions/Vocabulary Graph Traversals Vertices, Edges Directed/undirected Weighted Etc… Graph Traversals Breadth First Search Depth First Search Finding Shortest Path Dijkstra’s Topological Sort, Strongly connected components Minimum Spanning Trees Primm’s Kruskal’s Disjoint Sets Implementing Kruskal’s CSE 373 AU 18

Next week Monday: Guest lecture on Technical Interviews Wednesday: P vs. NP Thursday (Section): Final review session Friday: Final review session Sunday (tentative): TA lead extra review session CSE 373 AU 18

Topics covered in final - Everything we learned in the class - Final is cumulative with more focus on topics we covered after midterm. So there will be questions on the topics we covered before midterm as well. - Topics not covered in final B-Trees Java generics and Java interfaces Java Syntax Advice: - Make use of exams from previous terms. A practice exam will be posted on Monday. - Review section handouts for how to write good answers (particularly algorithm design questions) - Finish HW7 early so you can focus on Final. CSE 373 AU 18

Worksheet question 3 Suppose you are given a connected graph G. Describe how you would figure out if the graph has a cycle. (Answer in at most 3-4 sentences.) CSE 373 AU 18

Worksheet question 3 Suppose you are given a connected graph G. Describe how you would figure out if the graph has a cycle. (Answer in at most 3-4 sentences.) Run DFS but keep track of vertices in the stack. If we hit a vertex that is already in the stack, then there is a cycle in the graph. (Another solution is using topological sort, which is a similar idea, but it works only for directed graphs.) CSE 373 AU 18