Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Tree tree = connected graph with no cycle tree = connected graph with |V|-1 edges tree = graph with |V|-1 edges and no cycles."— Presentation transcript:

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

2 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  1 connected  no vertex of degree 0 (unless |V|=1)

3 no cycle  vertex of degree  1 Supose not, i.e., all vertices of degree  2, yet no cycle. Let v 1,...,v t be the longest path v t has 2-neighbors, one different from v t-1. Why cannot take v 1,...,v t,u ? Cycle. Contradiction.

4 |V|-1 edges  vertex of degree  1 Suppose all degrees  2. Then |E|=(1/2)  deg(v)  |V| Contradiction. Done. vVvV

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

6 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  1 connected  no vertex of degree 0 (unless |V|=1) 

7 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 connected and has no cycle. By IH G’ has |V|-2 edges and hence G has |V|-1 edges. 

8 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 connected and has |V’|-1 edges. By IH G’ has no cycle. Hence G has no cycle. 

9 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 |G 1 |,...,|G k | be the connected components. Then |E_i| = |V_i| - 1, hence |E| = |V| - k. Thus k = 1.

10 Spanning trees

11

12

13 How many spanning trees ?

14 Spanning trees How many spanning trees ? 4

15 Spanning trees How many spanning trees ?

16 Spanning trees How many spanning trees ? 8

17 Minimum weight spanning trees 1 5 3 6 4 2 4 2 7 3 6

18 1 5 3 6 4 2 4 2 7 3 6

19 1 5 3 6 4 2 4 2 7 3 6

20 1 5 3 6 4 2 4 2 7 3 6

21 1 5 3 6 4 2 4 2 7 3 6

22 1 5 3 6 4 2 4 2 7 3 6

23 Kruskal’s algorithm 1) sort the edges by weight 2) add edges in order if they do not create cycle

24 Kruskal’s algorithm 1) sort the edges by weight 2) add edges in order if they do not create cycle K = MST output by Kruskal T = optimal MST Is K = T ?

25 Kruskal’s algorithm 1) sort the edges by weight 2) add edges in order if they do not create cycle K = MST output by Kruskal T = optimal MST Is K = T ? no – e.g. if all edge-weights are the same  many optima

26 Kruskal’s algorithm 1) sort the edges by weight 2) add edges in order if they do not create cycle K = MST output by Kruskal T = optimal MST Assume all edge weights are different. Then K=T. (In particular, unique optimum)

27 Kruskal’s algorithm 1) sort the edges by weight 2) add edges in order if they do not create cycle G connected, e in a cycle  G-e connected K = MST output by Kruskal T = optimal MST

28 Kruskal’s algorithm e the edge of the smallest weight in K-T. Consider T+e. G connected, e in a cycle  G-e connected K = MST output by Kruskal T = optimal MST

29 Kruskal’s algorithm e the edge of the smallest weight in K-T. Consider T+e. Case 1: all edgeweights in C smaller that w e Case 2: one edgeweight in C larger that w e

30 Kruskal’s algorithm 1) sort the edges by weight 2) add edges in order if they do not create cycle Need to maintain components. Find-Set(u) Union(u,v)

31 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]

32 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)

33 Kruskal’s algorithm Find-Set(u) Union(u,v) O(1) O(n) Kruskal’s algorithm 1) sort the edges by weight 2) add edges in order if they do not create cycle

34 Kruskal’s algorithm Find-Set(u) Union(u,v) O(1) O(n) Kruskal’s algorithm 1) sort the edges by weight 2) add edges in order if they do not create cycle O(E log E + V^2)

35 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| u v

36 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| u v

37 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)

38 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)

39 Kruskal’s algorithm Find-Set(u) Union(u,v) O(log n) Kruskal’s algorithm 1) sort the edges by weight 2) add edges in order if they do not create cycle

40 Kruskal’s algorithm Find-Set(u) Union(u,v) O(log n) Kruskal’s algorithm 1) sort the edges by weight 2) add edges in order if they do not create cycle O(E log E + (E+V)log V)

41 Kruskal’s algorithm log E  log V 2 = 2 log V = O(log V) Kruskal’s algorithm 1) sort the edges by weight 2) add edges in order if they do not create cycle O(E log E + (E+V)log V) O((E+V) log V)

42 Minimum weight spanning trees 1 5 3 6 4 2 4 2 7 3 6

43 1 5 3 6 4 2 4 2 7 3 6

44 1 5 3 6 4 2 4 2 7 3 6

45 1 5 3 6 4 2 4 2 7 3 6

46 1 5 3 6 4 2 4 2 7 3 6

47 1 5 3 6 4 2 4 2 7 3 6

48 Prim’s algorithm 1) S  {1} 2) add the cheapest edge {u,v} such that u  S and v  S C S  S  {v} (until S=V)

49 Minimum weight spanning trees 1) S  {1} 2) add the cheapest edge {u,v} such that u  S and v  S C S  S  {v} (until S=V) P = MST output by Prim T = optimal MST Is P = T ? assume all the edgeweights different

50 Minimum weight spanning trees P = MST output by Prim T = optimal MST P = T assuming all the edgeweights different v 1,v 2,...,v n order added to S by Prim smallest i such that an edge e  E connecting S={v 1,...,v i } to S C different in T than in Prim (f)

51 Minimum weight spanning trees smallest i such that an edge e  E connecting S={v 1,...,v i } to S C different in T than in Prim (f) e SCSC S T

52 Minimum weight spanning trees smallest i such that an edge e  E connecting S={v 1,...,v i } to S C different in T than in Prim (f) e f SCSC S T+f

53 Minimum weight spanning trees for i  1 to n do C[i]   C[0]=0 S={} while S  V do j  smallest such that j  S C 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

54 Minimum weight spanning trees for i  1 to n do C[i]   C[0]=0 S={} while S  V do j  smallest such that j  S C 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)

55 Minimum weight spanning trees for i  1 to n do C[i]   C[0]=0 S={} while S  V do j  smallest such that j  S C 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(V 2 + E) = O(V 2 )

56 Minimum weight spanning trees for i  1 to n do C[i]   C[0]=0 S={} while S  V do j  smallest such that j  S C 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

57 Minimum weight spanning trees Kruskal O( (E+V) log V) Prim O( (E+V) log V)

58 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)


Download ppt "Tree tree = connected graph with no cycle tree = connected graph with |V|-1 edges tree = graph with |V|-1 edges and no cycles."

Similar presentations


Ads by Google