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

Post on 10-Jul-2020

0 views 0 download

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

15‐211: RECITATION 8, SECTION MHui Han Chin, hchin@cmu.edu

NOTICE!

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

Please start early for lab 5

TODAY

Graph and lab5

Some use of Kruskal and union find

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.

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.

DISJOINT SET

Used for representing partition of dataWhat is a partition?

Any data structure that supports Union

Find

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.

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)

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

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

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.

The Code

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; }

}}

}

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) { ... }

}

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;}

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; }}

}

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]

Putting it Together

ABSTRACTGRAPH

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

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.

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

RECAP

MST, Union‐Find, Mazes