Lecture 21: Disjoint Sets with Arrays

Slides:



Advertisements
Similar presentations
Midwestern State University Department of Computer Science Dr. Ranette Halverson CMPS 2433 CHAPTER 4 - PART 2 GRAPHS 1.
Advertisements

The Travelling Salesman Algorithm A Salesman has to visit lots of different stores and return to the starting base On a graph this means visiting every.
Graphs 2 Kevin Kauffman CS 309s. Problem We have sprinklers in our yard which need to be connected with pipe. Assuming pipes may only intersect at sprinkler.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Graph Dr. Bernard Chen Ph.D. University of Central Arkansas.
Graph Theory Hamilton Paths and Hamilton Circuits.
Representing and Using Graphs
Disneyland and Probability Zachary Kuiland MAT 119 Honors Project.
CIRCUITS, PATHS, AND SCHEDULES Euler and Königsberg.
Graphs Upon completion you will be able to:
Spanning Trees Dijkstra (Unit 10) SOL: DM.2 Classwork worksheet Homework (day 70) Worksheet Quiz next block.
CSE 373: Data Structures and Algorithms Lecture 21: Graphs V 1.
Graphs – Part III CS 367 – Introduction to Data Structures.
Hamilton Paths and Hamilton Circuits
CSE 373: Data Structures and Algorithms
Graphs Chapter 20.
Graphs Chapter 15 introduces graphs which are probably the most general and commonly-used data structure. This lecture introduces heaps, which are used.
Graphs Representation, BFS, DFS
Shortest Paths and Minimum Spanning Trees
Spanning Trees Lecture 21 CS2110 – Fall 2016.
Minimum Spanning Trees and Shortest Paths
Parallel Graph Algorithms
Minimum Spanning Trees
Discussion section #2 HW1 questions?
Data Structures and Algorithms
CSE373: Data Structures & Algorithms Lecture 11: Implementing Union-Find Linda Shapiro Spring 2016.
Graphs Representation, BFS, DFS
Data Structures and Algorithms
Data Structures and Algorithms
Disjoint Sets with Arrays
Greedy Algorithms / Dijkstra’s Algorithm Yin Tat Lee
Graphs Chapter 13.
Graphs Chapter 15 explain graph-based algorithms Graph definitions
Data Structures and Algorithms
Genome Assembly.
Graphs Chapter 11 Objectives Upon completion you will be able to:
Data Structures and Algorithms
Shortest Path.
CSE 373: Data Structures and Algorithms
Graphs.
CSE 373 Data Structures and Algorithms
Data Structures and Algorithms
Yan Shi CS/SE 2630 Lecture Notes
Shortest Path Algorithms
Shortest Paths and Minimum Spanning Trees
CSE 373 Data Structures Lecture 16
CSE 373: Data Structures and Algorithms
Chapter 15 Graphs © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
CSE 373: Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
Algorithms: Design and Analysis
Lecture 19: Minimum Spanning Trees
CS 584 Project Write up Poster session for final Due on day of final
Weighted Graphs & Shortest Paths
Lecture 20: Disjoint Sets
CSE 373 Data Structures and Algorithms
CSE 417: Algorithms and Computational Complexity
Lecture 18: Implementing Graphs
Spanning Trees Lecture 20 CS2110 – Spring 2015.
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Data Structures and Algorithms
Richard Anderson Spring 2016
Lecture 10 Graph Algorithms
Lecture 20: Implementing Graphs
Lecture 23: Minimum Spanning Trees
Lecture 22: Implementing Dijkstra’s
CSE 373: Data Structures and Algorithms
Lecture 27: Array Disjoint Sets
Lecture 27: More Graph Algorithms
Lecture 26: Array Disjoint Sets
Presentation transcript:

Lecture 21: Disjoint Sets with Arrays CSE 373: Data Structures and Algorithms CSE 373 19 wi - Kasey Champion

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 SP 18 - Kasey Champion

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 Each “node” now only takes 4 bytes of memory instead of 32 CSE 373 SP 18 - Kasey Champion

Practice rank = 2 rank = 1 rank = 0 rank = 2 1 2 3 4 5 6 7 8 9 10 11 13 4 7 9 10 12 15 14 1 11 2 8 16 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 -3 -1 -2 CSE 373 SP 18 - Kasey Champion

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 SP 18 - Kasey Champion

Graph Design CSE 373 SP 18 - Kasey Champion

Graphs are about representing relationships… Physical distances Connections Bloodlines Probabilities Sequences States CSE 373 SP 18 - Kasey Champion

Scenario #1 Castle Flag Pole Dumbo It’s a small world Matter- horn Space Mtn Star Tours Jungle Cruise Indiana Jones Splash Thunder 6 7 You are going to Disneyland for spring break! You’ve never been, so you want to make sure you hit ALL the rides. Is there a graph algorithm that would help? BFS or DFS How would you draw the graph? What are the vertices? Rides What are the edges? Walkways 8 5 4 1 9 3 10 BFS = 0 1 2 3 5 6 7 8 9 4 10 DFS = 0 3 5 6 7 8 9 10 1 4 2 2 CSE 373 19 wi - Kasey Champion

Scenario #1 continued Castle Flag Pole Dumbo It’s a small world Matter- horn Space Mtn Star Tours Jungle Cruise Indiana Jones Splash Thunder 6 7 Now that you have your basic graph of Disneyland what might the following graph items represent in this context? Weighted edges Walkway distances Walking times Foot traffic Directed edges Entrances and exits Crowd control for fireworks Parade routes Self Loops Looping a ride Parallel Edges Foot traffic at different times of day Walkways and train routes 8 5 4 1 9 3 10 2 CSE 373 19 wi - Kasey Champion

Scenario #2 Castle Flag Pole Dumbo It’s a small world Matter- horn Space Mtn Star Tours Jungle Cruise Indiana Jones Splash Thunder 1 2 3 4 5 6 7 8 9 10 11 17 13 12 16 15 14 Castle Flag Pole Dumbo It’s a small world Matter- horn Space Mtn Star Tours Jungle Cruise Indiana Jones Splash Thunder 6 4 7 You are a Disneyland employee and you need to rope off as many miles of walkways as you can for the fireworks while leaving guests access to all the rides. Is there a graph algorithm that would help? MST How would you draw the graph? What are the vertices? Rides What are the edges? Walkways with distances 6 16 12 10 8 5 14 4 5 1 13 9 7 15 17 11 9 3 8 10 1 2 2 3 CSE 373 SP 18 - Kasey Champion

Scenario #3 Castle Flag Pole Dumbo It’s a small world Matter- horn Space Mtn Star Tours Jungle Cruise Indiana Jones Splash Thunder 1 2 3 4 5 6 7 8 9 10 11 17 13 12 16 15 14 21 23 24 28 29 20 Castle Flag Pole Dumbo It’s a small world Matter- horn Space Mtn Star Tours Jungle Cruise Indiana Jones Splash Thunder 6 4 7 You arrive at Disneyland and you want to visit all the rides, but do the least amount of walking possible. If you start at the Flag Pole, plan the shortest walk to each of the attractions. Is there a graph algorithm that would help? Dijkstra’s How would you draw the graph? What are the vertices? Rides What are the edges? Walkways with distances 6 16 12 10 8 5 14 4 5 1 13 9 7 15 17 11 9 3 8 10 1 2 2 3 CSE 373 SP 18 - Kasey Champion

Scenario #2b Castle Flag Pole Dumbo It’s a small world Matter- horn Space Mtn Star Tours Jungle Cruise Indiana Jones Splash Thunder 1 2 3 4 5 6 7 8 9 10 11 17 13 12 16 15 14 21 23 24 28 29 20 6 4 7 Now that you know the shortest distance to each attraction, can you make a plan to visit all the attractions with the least amount of total walking? 6 16 12 10 8 5 14 Nope! This is the travelling salesman problem which is much more complicated than Dijkstra’s. (NP Hard, more on this later) 4 5 1 13 9 7 15 17 11 9 3 8 10 1 2 2 3 CSE 373 SP 18 - Kasey Champion

Scenario #3 Castle Flag Pole Dumbo It’s a small world Matter- horn Space Mtn Star Tours Jungle Cruise Indiana Jones Splash Thunder 6 4 7 You have great taste so you are riding Space Mountain. Your friend makes poor choices so they are riding Splash Mountain. You decide to meet at the castle, how long before you can meet up? 6 16 12 10 8 5 14 Is there a graph algorithm that would help? Dijkstra’s What information do our edges need to store? Walking times How do we apply the algorithm? Run Dijkstra’s from Splash Mountain. Run Dijkstra’s from Space Mountain. Take the larger of the two times. 4 5 1 13 9 7 15 17 11 9 3 8 10 1 2 2 3 CSE 373 SP 18 - Kasey Champion