lecture 21 disjoint sets
play

Lecture 21: Disjoint Sets CSE 373: Data Structures and with Arrays - PowerPoint PPT Presentation

Lecture 21: Disjoint Sets CSE 373: Data Structures and with Arrays Algorithms CSE 373 19 WI - KASEY CHAMPION 1 Implementation Use Nodes? In modern Java (assuming 64-bit JDK) each object takes about 32 bytes - int field takes 4 bytes -


  1. Lecture 21: Disjoint Sets CSE 373: Data Structures and with Arrays Algorithms CSE 373 19 WI - KASEY CHAMPION 1

  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 5

  3. Array Implementation rank = 0 rank = 3 rank = 3 11 1 0 15 12 2 6 13 14 16 17 3 4 5 7 10 18 8 9 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 11 11 12 12 13 13 14 14 15 15 16 16 17 17 18 18 -1 -1 -4 -1 1 1 2 2 2 2 2 2 1 1 6 6 7 7 7 7 6 6 -4 -1 11 11 12 12 12 12 11 11 15 15 15 15 17 17 Store (rank * -1) - 1 Each “node” now only takes 4 bytes of memory instead of 32 CSE 373 SP 18 - KASEY CHAMPION 6

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

  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 8

  6. Graph Design CSE 373 SP 18 - KASEY CHAMPION 10

  7. Graphs are about representing relationships… Physical distances Connections Bloodlines Probabilities Sequences States CSE 373 SP 18 - KASEY CHAMPION 11

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

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

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

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

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

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

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend