Tree

58
Tree ee = connected graph with no cycle ee = connected graph with |V|-1 edges ee = graph with |V|-1 edges and no cycles

description

Tree. tree = connected graph with no cycle tree = connected graph with |V|-1 edges tree = graph with |V|-1 edges and no cycles. Tree. tree = connected graph with no cycle tree = connected graph with |V|-1 edges tree = graph with |V|-1 edges and no cycles. - PowerPoint PPT Presentation

Transcript of Tree

Page 1: Tree

Tree

tree = connected graph with no cycle

tree = connected graph with |V|-1 edges

tree = graph with |V|-1 edges and no cycles

Page 2: Tree

Tree

tree = connected graph with no cycle

tree = connected graph with |V|-1 edges

tree = graph with |V|-1 edges and no cycles

no cycle vertex of degree 1|V|-1 edges vertex of degree 1connected no vertex of degree 0 (unless |V|=1)

Page 3: Tree

no cycle vertex of degree 1

Supose not, i.e., all vertices of degree 2, yet no cycle.

Let v1,...,vt be the longest pathvt has 2-neighbors, one differentfrom vt-1. Why cannot takev1,...,vt,u ? Cycle. Contradiction.

Page 4: Tree

|V|-1 edges vertex of degree 1

Suppose all degrees 2. Then

|E|=(1/2) deg(v) |V|

Contradiction. Done.vV

Page 5: Tree

connected no vertex of degree 0(unless |V|=1)

Page 6: Tree

Tree

connected graph with no cycle

connected graph with |V|-1 edges

graph with |V|-1 edges and no cycles

no cycle vertex of degree 1|V|-1 edges vertex of degree 1connected no vertex of degree 0 (unless |V|=1)

Page 7: Tree

connected graph with no cycle

connected graph with |V|-1 edges

Induction on |V|. Base case |V|=1.Let G be connected with no cycles.Then G has vertex v of degree 1.Let G’ = G – v. Then G’ is connectedand has no cycle. By IH G’ has |V|-2 edges and hence G has |V|-1 edges.

Page 8: Tree

connected graph with no cycle

connected graph with |V|-1 edges

Induction on |V|. Base case |V|=1.Let G be connected |V|-1 edges. Then G has vertex v of degree 1.Let G’ = G – v. Then G’ is connectedand has |V’|-1 edges. By IH G’ has no cycle. Hence G has no cycle.

Page 9: Tree

connected graph with no cycle

connected graph with |V|-1 edges

graph with |V|-1 edges and no cycles

Assume |V|-1 edges and no cycles.Let |G1|,...,|Gk| be the connected components. Then |E_i| = |V_i| - 1,hence |E| = |V| - k. Thus k = 1.

Page 10: Tree

Spanning trees

Page 11: Tree

Spanning trees

Page 12: Tree

Spanning trees

Page 13: Tree

Spanning trees

How many spanning trees ?

Page 14: Tree

Spanning trees

How many spanning trees ?

4

Page 15: Tree

Spanning trees

How many spanning trees ?

Page 16: Tree

Spanning trees

How many spanning trees ?

8

Page 17: Tree

Minimum weight spanning trees

1

53

6

4

2

4

27

3

6

Page 18: Tree

Minimum weight spanning trees

1

53

6

4

2

4

27

3

6

Page 19: Tree

Minimum weight spanning trees

1

53

6

4

2

4

27

3

6

Page 20: Tree

Minimum weight spanning trees

1

53

6

4

2

4

27

3

6

Page 21: Tree

Minimum weight spanning trees

1

53

6

4

2

4

27

3

6

Page 22: Tree

Minimum weight spanning trees

1

53

6

4

2

4

27

3

6

Page 23: Tree

Minimum weight spanning trees

Kruskal’s algorithm

1) sort the edges by weight2) add edges in order if they do not create cycle

Page 24: Tree

Kruskal’s algorithm1) sort the edges by weight2) add edges in order if they do not create cycle

K = MST output by KruskalT = optimal MST

Is K = T ?

Page 25: Tree

Kruskal’s algorithm1) sort the edges by weight2) add edges in order if they do not create cycle

K = MST output by KruskalT = optimal MST

Is K = T ?

no – e.g. if all edge-weights are the same many optima

Page 26: Tree

Kruskal’s algorithm1) sort the edges by weight2) add edges in order if they do not create cycle

K = MST output by KruskalT = optimal MST

Assume all edge weightsare different. Then K=T. (In particular, unique optimum)

Page 27: Tree

Kruskal’s algorithm1) sort the edges by weight2) add edges in order if they do not create cycle

G connected, e in a cycle G-e connected

K = MST output by KruskalT = optimal MST

Page 28: Tree

Kruskal’s algorithm

e the edge of the smallest weightin K-T. Consider T+e.

G connected, e in a cycle G-e connected

K = MST output by KruskalT = optimal MST

Page 29: Tree

Kruskal’s algorithm

e the edge of the smallest weightin K-T. Consider T+e.

Case 1: all edgeweights in C smaller that we

Case 2: one edgeweight in C larger that we

Page 30: Tree

Kruskal’s algorithm

1) sort the edges by weight2) add edges in order if they do not create cycle

Need to maintain components. Find-Set(u) Union(u,v)

Page 31: Tree

Union-Find

S[1..n] for i 1 to n do S[i] i

Find-Set(u) return S[u] Union(u,v) a S[u] for i 1 to n do if S[i]=a then S[i] S[v]

Page 32: Tree

Union-Find

S[1..n] for i 1 to n do S[i] i

Find-Set(u) return S[u] Union(u,v) a S[u] for i 1 to n do if S[i]=a then S[i] S[v]

O(1)

O(n)

Page 33: Tree

Kruskal’s algorithm Find-Set(u) Union(u,v)

O(1)

O(n)

Kruskal’s algorithm

1) sort the edges by weight2) add edges in order if they do not create cycle

Page 34: Tree

Kruskal’s algorithm Find-Set(u) Union(u,v)

O(1)

O(n)

Kruskal’s algorithm

1) sort the edges by weight2) add edges in order if they do not create cycle

O(E log E + V^2)

Page 35: Tree

Union-Find 2

S[1..n] for i 1 to n do S[i] i

Find-Set(u) while (S[u] u) do u S[u] S[u] Union(u,v) S[Find-Set(u)] Find-Set(v)

n=|V|

uv

Page 36: Tree

Union-Find 2

S[1..n] for i 1 to n do S[i] i

Find-Set(u) while (S[u] u) do u S[u] S[u] Union(u,v) S[Find-Set(u)] Find-Set(v)

n=|V|

uv

Page 37: Tree

Union-Find 2

S[1..n] for i 1 to n do S[i] i

Find-Set(u) while (S[u] u) do u S[u] S[u] Union(u,v) S[Find-Set(u)] Find-Set(v)

n=|V|

O(n)

O(n)

Page 38: Tree

Union-Find 2

S[1..n] for i 1 to n do S[i] i

Find-Set(u) while (S[u] u) do u S[u] S[u] Union(u,v) u Find-Set(u); v Find-Set(v); if D[u]<D[v] then S[u] v if D[u]>D[v] then S[v] u if D[u]=D[v] then S[u] v; D[v]++

n=|V|

O(log n)

O(log n)

Page 39: Tree

Kruskal’s algorithm Find-Set(u) Union(u,v)

O(log n)

O(log n)

Kruskal’s algorithm

1) sort the edges by weight2) add edges in order if they do not create cycle

Page 40: Tree

Kruskal’s algorithm Find-Set(u) Union(u,v)

O(log n)

O(log n)

Kruskal’s algorithm

1) sort the edges by weight2) add edges in order if they do not create cycle

O(E log E + (E+V)log V)

Page 41: Tree

Kruskal’s algorithmlog E log V2 = 2 log V = O(log V)

Kruskal’s algorithm

1) sort the edges by weight2) add edges in order if they do not create cycle

O(E log E + (E+V)log V)

O((E+V) log V)

Page 42: Tree

Minimum weight spanning trees

1

53

6

4

2

4

27

3

6

Page 43: Tree

Minimum weight spanning trees

1

53

6

4

2

4

27

3

6

Page 44: Tree

Minimum weight spanning trees

1

53

6

4

2

4

27

3

6

Page 45: Tree

Minimum weight spanning trees

1

53

6

4

2

4

27

3

6

Page 46: Tree

Minimum weight spanning trees

1

53

6

4

2

4

27

3

6

Page 47: Tree

Minimum weight spanning trees

1

53

6

4

2

4

27

3

6

Page 48: Tree

Minimum weight spanning trees

Prim’s algorithm 1) S {1}2) add the cheapest edge {u,v} such that uS and vSC

S S{v} (until S=V)

Page 49: Tree

Minimum weight spanning trees

1) S {1}2) add the cheapest edge {u,v} such that uS and vSC

S S{v} (until S=V)

P = MST output by PrimT = optimal MST

Is P = T ?

assume all the edgeweights different

Page 50: Tree

Minimum weight spanning treesP = MST output by PrimT = optimal MST

P = T assuming all the edgeweights different

v1,v2,...,vn order added to S by Prim

smallest i such that an edge e E connecting S={v1,...,vi} to SC different in T than in Prim (f)

Page 51: Tree

Minimum weight spanning trees

smallest i such that an edge e E connecting S={v1,...,vi} to SC different in T than in Prim (f)

e

SC

ST

Page 52: Tree

Minimum weight spanning trees

smallest i such that an edge e E connecting S={v1,...,vi} to SC different in T than in Prim (f)

e

f

SC

ST+f

Page 53: Tree

Minimum weight spanning trees

for i 1 to n do C[i] C[0]=0

S={}while S V do j smallest such that jSC and C[j] is minimal S S { j } for u neighbors of j do if w[{j,u}] < C[u] then C[u]w[{j,u}]; P[u] j

Page 54: Tree

Minimum weight spanning trees

for i 1 to n do C[i] C[0]=0

S={}while S V do j smallest such that jSC and C[j] is minimal S S { j } for u neighbors of j do if w[{j,u}] < C[u] then C[u]w[{j,u}]; P[u] j

O(n)

Page 55: Tree

Minimum weight spanning trees

for i 1 to n do C[i] C[0]=0

S={}while S V do j smallest such that jSC and C[j] is minimal S S { j } for u neighbors of j do if w[{j,u}] < C[u] then C[u]w[{j,u}]; P[u] j

O(n)

O(V2 + E) = O(V2)

Page 56: Tree

Minimum weight spanning trees

for i 1 to n do C[i] C[0]=0

S={}while S V do j smallest such that jSC and C[j] is minimal S S { j } for u neighbors of j do if w[{j,u}] < C[u] then C[u]w[{j,u}]; P[u] j

O(log n)

O((E+V)log V) using heap

Page 57: Tree

Minimum weight spanning trees

Kruskal O( (E+V) log V)

Prim O( (E+V) log V)

Page 58: Tree

Minimum weight spanning trees

Kruskal O( (E+V) log V)

Prim O( (E+V) log V)

can be made O(E + V log V)using Fibonacci heaps

if edges already sorted then O(E log*V)