Union Find - Dynamic Connectivity

Ace your homework & exams now with Quizwiz!

What applications involve manipulating objects of all types?

1. Pixels in a digital photo 2. Computers in a network 3. Friends in a social network 4. Transistors in a computer chip 5. Elements in a mathematical set 6. Variable names in a Fortran program 7. Metallic sites in a composite system

(IMP)What are the three equivalence relations, and what do they mean?

1. Reflexive: p is connected to p 2. Symmetric: if p is connected to q, then q is connected to p 3. Transitive: if p is connected to q and q is connected to r, then p is connected to r.

What are some convenient things to keep in mind when naming objects 0 to N-1 when programming?

1. Use integers as array index. 2. Suppress details not relevant to union find

How would you design an effecient data structure for a union-find ? (Note: this is for Java) Conditions: * The # of objects N can be huge. * The # of operations M can be huge. * Find queries and union command may be intermixed

1.Create a class named UF that contains two methods - one to implement union ( adds the connected between p and q; void union(int p, int q) ) ( initializes the union-find data structure with N objects (0 to N-1) ; UF(int N) ) - one to implement connected (returns a boolean) ( are p and q in the same component?; boolean connected(int p, int q) )

Question: How many connected components result after performing the following sequence of union operations on a set of 10 items? Explain. 1-2 3-4 5-6 7-8 7-9 2-8 0-5 1-9

3; The connected components are {0, 5, 6}, {3,4}, and {1,2,7,8,9} On a sheet of paper, following the sequences listed in order, and keep connecting each object until finishing the last sequence. Count how many you have.

(IMP)What are connected components?

A maximal set of objects that are mutually connected

(IMP)Explain Dynamic Connectivity

Given a set of N objects 1. Union Command 2. Find/connected query

(IMP)How do you connect two objects that are not in the same component?

You make a union command between the two objects, which essentially merges the two connected components into one new component (Example in the image)

(IMP)What does the Union command do

connects two objects

Given this output below, code a client that reads in a pair of integers from standard output, and if they aren't yet connected, connect them and print out the pair

int N = StdIn.readInt(); UF uf = new UF(N); while (!StdIn.isEmpty()) { int p = StdIn.readInt(); int q = StdIn.readInt(); if (!uf.connected(p, q)) { uf.union(p, q); StdOut.println(p + " " + q); } }

(IMP)What does the Find/Connect query imply?

is there a path connecting the two objects?


Related study sets

Catcher and the Rye Chapters 15-26

View Set

Racial and Ethnic Relations- Final Exam (Old Stuff)

View Set

General Psychology Module 5 Quiz

View Set

Art 4561 Midterm Key Terms 281-420

View Set