HuiHan Chin, hchin@cmu · T HE P ARTY P ROBLEM You arrive at a party. As usual, there are separate...

22
15211: RECITATION 8, SECTION M Hui Han Chin, [email protected]

Transcript of HuiHan Chin, hchin@cmu · T HE P ARTY P ROBLEM You arrive at a party. As usual, there are separate...

Page 1: HuiHan Chin, hchin@cmu · T HE P ARTY P ROBLEM You arrive at a party. As usual, there are separate groups of people standing around. In each group people talk to each other, but they

15‐211: RECITATION 8, SECTION MHui Han Chin, [email protected]

Page 2: HuiHan Chin, hchin@cmu · T HE P ARTY P ROBLEM You arrive at a party. As usual, there are separate groups of people standing around. In each group people talk to each other, but they

NOTICE!

Lab 4 reviewThere will be 1 more set of “secret” test

Please start early for lab 5

Page 3: HuiHan Chin, hchin@cmu · T HE P ARTY P ROBLEM You arrive at a party. As usual, there are separate groups of people standing around. In each group people talk to each other, but they

TODAY

Graph and lab5

Some use of Kruskal and union find

Page 4: HuiHan Chin, hchin@cmu · T HE P ARTY P ROBLEM You arrive at a party. As usual, there are separate groups of people standing around. In each group people talk to each other, but they

MAZES

Think about a grid of rooms separated by walls.

Each room can be given a name.

a b c d

hgfe

i j k l

ponm

Randomly knock out walls until we get a good maze.

Page 5: HuiHan Chin, hchin@cmu · T HE P ARTY P ROBLEM You arrive at a party. As usual, there are separate groups of people standing around. In each group people talk to each other, but they

THE PARTY PROBLEM

You arrive at a party. As usual, there are separate groups of people standing around. In each group people talk to each other, but they don't talk to anyone outside of the group.

You scan the groups, find someone that you know and join the corresponding group. If someone in another group knows you too, the two groups merge.

How do we figure out the groups given a list of “is-friend-of” relations. The list is revealed step by step, we don't have access to the whole list from the start.

Page 6: HuiHan Chin, hchin@cmu · T HE P ARTY P ROBLEM You arrive at a party. As usual, there are separate groups of people standing around. In each group people talk to each other, but they

DISJOINT SET

Used for representing partition of dataWhat is a partition?

Any data structure that supports Union

Find

Page 7: HuiHan Chin, hchin@cmu · T HE P ARTY P ROBLEM You arrive at a party. As usual, there are separate groups of people standing around. In each group people talk to each other, but they

UNION‐FIND

In the world of programming the key operations are called

- find(x) return the fixed point- union(x,y) union the classes of x and y

So far, this is clever but not too exciting: both operations may be linear in n.

We need to be more careful about how to perform the union operation. Note that our definition of representation gives us a lot of leeway.

Page 8: HuiHan Chin, hchin@cmu · T HE P ARTY P ROBLEM You arrive at a party. As usual, there are separate groups of people standing around. In each group people talk to each other, but they

EXAMPLE

{1} {2} {3} {4} {5} {6} {7}

{1} {2,3} {4} {5} {6} {7}

{1} {2,3,4} {5} {6} {7}

{1} {2,3,4} {5,6} {7}

{1} {2,3,4,5,6} {7}

union(2,3)

union(3,4)

union(5,6)

union(6,3)

{1} {2,3,4,5,6} {7}

union(2,6)

Page 9: HuiHan Chin, hchin@cmu · T HE P ARTY P ROBLEM You arrive at a party. As usual, there are separate groups of people standing around. In each group people talk to each other, but they

THINK TREEIt is helpful to think of the represention as a rooted tree.

1 3

0

4

2

5

1

3

0

4

2

5

Page 10: HuiHan Chin, hchin@cmu · T HE P ARTY P ROBLEM You arrive at a party. As usual, there are separate groups of people standing around. In each group people talk to each other, but they

A TRICK: PATH COMPRESSION

Since we have to traverse a path from a node to the root we might as well smash all the nodes on that path up to the root.

E.g., find(0) would produce:

13

0

42

5 10

42

53

Page 11: HuiHan Chin, hchin@cmu · T HE P ARTY P ROBLEM You arrive at a party. As usual, there are separate groups of people standing around. In each group people talk to each other, but they

HOW HARD TO IMPLEMENT?

One might wonder how hard it is to code all these tricks (without union by size/depth and path compressions the code is nearly trivial).

Also, what is the actual payoff in the end?

As it turns out, the code is really simple, and the payoff is tremendous.

Page 12: HuiHan Chin, hchin@cmu · T HE P ARTY P ROBLEM You arrive at a party. As usual, there are separate groups of people standing around. In each group people talk to each other, but they

The Code

Page 13: HuiHan Chin, hchin@cmu · T HE P ARTY P ROBLEM You arrive at a party. As usual, there are separate groups of people standing around. In each group people talk to each other, but they

ALL THE CODE

class UnionFind {int[] u;

UnionFind(int n) {u = new int[n];for (int i = 0; i < n; i++)u[i] = -1;

}

int find(int i) {int j,root;for (j = i; u[j] >= 0; j = u[j]) ;root = j;while (u[i] >= 0) { j = u[i]; u[i] = root; i = j; }return root;

}

void union(int i,int j) {i = find(i);j = find(j);if (i !=j) {if (u[i] < u[j]){ u[i] += u[j]; u[j] = i; }

else { u[j] += u[i]; u[i] = j; }

}}

}

Page 14: HuiHan Chin, hchin@cmu · T HE P ARTY P ROBLEM You arrive at a party. As usual, there are separate groups of people standing around. In each group people talk to each other, but they

THE UNIONFIND CLASS

class UnionFind {int[] u;int[] S;UnionFind(int n) {u = new int[n];for (int i = 0; i < n; i++){u[i] = i;S[i] = 1;}

}int find(int i) { ... }void union(int i,int j) { ... }

}

Page 15: HuiHan Chin, hchin@cmu · T HE P ARTY P ROBLEM You arrive at a party. As usual, there are separate groups of people standing around. In each group people talk to each other, but they

ITERATIVE FIND

int find(int i) {int j, root;

for (j = i; u[j] != j; j = u[j]);root = j;

while (u[i] != i){ j = u[i]; u[i] = root; i = j; }

return root;}

Page 16: HuiHan Chin, hchin@cmu · T HE P ARTY P ROBLEM You arrive at a party. As usual, there are separate groups of people standing around. In each group people talk to each other, but they

UNION BY SIZE

void union(int i,int j) {i = find(i);j = find(j);

if (i != j) {if (S[i] < S[j])

{ S[i] += S[j]; S[j] = i; }else

{ S[j] += u[i]; S[i] = j; }}

}

Page 17: HuiHan Chin, hchin@cmu · T HE P ARTY P ROBLEM You arrive at a party. As usual, there are separate groups of people standing around. In each group people talk to each other, but they

TIME BOUNDSVariables

M operations. N elements.

Algorithms

Simple forest representationWorst: find O(N).  mixed operations O(MN).

Average: tricky

Union by height; Union by sizeWorst: find O(log N).  mixed operations O(M log N).

Average: mixed operations O(M)    [see text]

Path compression in findWorst: mixed operations: “nearly linear”

[analysis in 15‐451]

Page 18: HuiHan Chin, hchin@cmu · T HE P ARTY P ROBLEM You arrive at a party. As usual, there are separate groups of people standing around. In each group people talk to each other, but they

Putting it Together

Page 19: HuiHan Chin, hchin@cmu · T HE P ARTY P ROBLEM You arrive at a party. As usual, there are separate groups of people standing around. In each group people talk to each other, but they

ABSTRACTGRAPH

Implement Graph as an adjacency listImplement addEdge and RemoveEdge in O(max node degree) = constant time for our example graph

Page 20: HuiHan Chin, hchin@cmu · T HE P ARTY P ROBLEM You arrive at a party. As usual, there are separate groups of people standing around. In each group people talk to each other, but they

MAZE GENERATION

Create Minimum Spanning Tree over the nodes using Kruskals Alg. (all edge weights equal ‐> pick random edges) and Union Find equivalence relation.

a b c d

hgfe

i j k l

ponm

Randomly knock out walls until we get a good maze.

Page 21: HuiHan Chin, hchin@cmu · T HE P ARTY P ROBLEM You arrive at a party. As usual, there are separate groups of people standing around. In each group people talk to each other, but they

BFS AND DFS

Implement BFS and DFS to find the solutions to the mazes you generate

a b c d

hgfe

i j k l

ponm

Page 22: HuiHan Chin, hchin@cmu · T HE P ARTY P ROBLEM You arrive at a party. As usual, there are separate groups of people standing around. In each group people talk to each other, but they

RECAP

MST, Union‐Find, Mazes