Download presentation
Presentation is loading. Please wait.
1
Lecture 21: Disjoint Sets with Arrays
CSE 373: Data Structures and Algorithms CSE wi - Kasey Champion
2
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
3
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
4
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
5
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
6
Graph Design CSE 373 SP 18 - Kasey Champion
7
Graphs are about representing relationships…
Physical distances Connections Bloodlines Probabilities Sequences States CSE 373 SP 18 - Kasey Champion
8
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 = DFS = 2 CSE wi - Kasey Champion
9
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 wi - Kasey Champion
10
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
11
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
12
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
13
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.