Discussion 36 spanning trees
This presentation is the property of its rightful owner.
Sponsored Links
1 / 17

Discussion #36 Spanning Trees PowerPoint PPT Presentation


  • 85 Views
  • Uploaded on
  • Presentation posted in: General

Discussion #36 Spanning Trees. Topics. Spanning trees Minimal spanning trees Kruskal’s algorithm Prim’s algorithm. E. A. D. B. C. F. E. A. D. B. C. F. One of many Spanning Trees. Spanning Trees.

Download Presentation

Discussion #36 Spanning Trees

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Discussion 36 spanning trees

Discussion #36Spanning Trees


Topics

Topics

  • Spanning trees

  • Minimal spanning trees

  • Kruskal’s algorithm

  • Prim’s algorithm


Spanning trees

E

A

D

B

C

F

E

A

D

B

C

F

One of many Spanning Trees

Spanning Trees

  • A spanning tree for a connected, undirected graph G is a graph S consisting of the nodes of G together with enough edges of G such that:

    1. S is connected, and

    2. S is acyclic.

  • Example:

Why “tree”? Pick any node as the root  is a tree.

Why “spanning”? Every node is (still) connected to every other node.


Algorithm for generating a spanning tree

E

A

D

B

C

F

C

A

D

B

E

F

Algorithm for Generating a Spanning Tree

  • A DFS algorithm works.

  • Just keep the tree edges.

O(m) = O(n2) (worse case)


Spanning trees for weighted graphs

6

6

6

7

5

5

2

2

2

8

1

1

1

B

C

F

F

A

D

E

A

D

C

E

B

E

A

D

C

F

B

5

5

4

4

4

Weighted Graph

Spanning Trees for Weighted Graphs

  • A minimal spanning tree for a weighted, connected, undirected graph G is a graph S consisting of the nodes of G together with enough edges of G such that:

    1. S is connected, and

    2. S is acyclic, and

    3. S has minimal weight.

  • Examples:

Note: all weights positive.

(Only) two minimal spanning trees


Kruskal s algorithm

6

7

6

5

F

D

D

B

A

E

B

C

E

F

C

A

5

2

8

2

1

1

5

4

4

Kruskal’s Algorithm

  • Sort edges by weight (smallest first)

  • For each edge e = {x, y} (in order) if nodes x and y are not in the same connected component, add e

{A,B}1

{D,F}2

{B,C}4

{A,D}5

{C,D}5

{E,D}6

{A,E}7

{B,D}8

Could have stopped here… we had n-1 edges.


Prim s algorithm

6

7

6

(5)

5

5

(3)

2

8

2

1

5

1

(4)

(1)

4

4

B

F

A

C

B

D

E

C

D

E

F

A

(2)

Prim’s Algorithm

  • Initialize a spanning tree S containing a single vertex, chosen arbitrarily from the graph

  • Until n-1 edges have been added

    • find the edge e = {x, y} such that

      • x is in S and y is not in S

      • e is the smallest weighted edge left

    • Add e and y to S


Correctness proofs complexity analysis

Correctness Proofs& Complexity Analysis

  • Correctness

    • Detailed proofs  lengthy

    • Essential idea:

      • Prove connected: add edges until all nodes connected

      • Prove acyclic: don’t add an edge that would create a cycle

      • Prove minimal weight: only add overall or local minimal-weight edges

  • Complexity

    • Straightforward implementations are easy to analyze.

    • Both algorithms have clever implementations that make them run faster.


Correctness of prim s algorithm

Correctness of Prim’s Algorithm

  • Prim’s algorithm produces a minimal spanning tree S from a connected, weighted, undirected graph G.

  • Proof (sketch)

    • Connected: Assume not, then there are at least two disconnected components, C1 and C2 in S. But since G is connected, there is a smallest weight edge {x, y} with x in C1 and y in C2 that would have been found. Thus, after running Prim’s algorithm C1 and C2 must be connected.

    • Acyclic: Assume not, then there was a first edge {x, y} added to make a cycle. But to have been added, x must have been in S and y must not have been in S. Since y must not have been in S, there was no path from x to y at the time {x, y} was added, and thus no cycle was created when {x, y} was added.

    • Minimal weight: By induction with loop invariant: After k iterations the growing spanning tree S has minimal weight.

      • Basis: 0 iterations: The initialization step selects a single node and the weight of S is 0, the minimum possible since all weights are assumed to be positive.

      • Induction: By the induction hypothesis, after k iterations S has minimal weight. Then, after k+1 iterations S has minimal weight. For suppose not, then the chosen edge e must not have been the smallest-weight edge left.


Complexity of prim s algorithm

Complexity of Prim’s Algorithm

  • Initialize a spanning tree S containing a single vertex, chosen arbitrarily from the graph

  • Until n-1 edges have been added

    • find the edge e = {x, y} such that

      • x is in S and y is not in S

      • e is the smallest weighted edge left

    • Add e and y to S

  • O(1) + O(n(m+1)) = O(nm) = O(n3) in the worst case

O(1)

O(n)

O(m)

O(1)


A faster prim s algorithm

A Faster Prim’s Algorithm

  • To make Prim’s Algorithm faster, we need a way to find the edge e faster.

  • Can we avoid looking through all edges in each iteration?

    • We can if we sort them first and then make a sorted list of incident edges for each node.

    • In the initialization step this takes O(mlogm) to sort and O(m) to make lists for each node  O(mlogm) = O(n2logn2) in the worst case.

    • Now for each of the n1 iterations, we find the edge {x, y} by looking only at the first edge of at most n lists  O(n2) over all iterations. We must, of course, discard edges on these lists as we build S, so that the edge we want will be first, but with appropriate links, this is O(1).

    • Thus, the sort in the initialization dominates, which makes the algorithm O(mlogm) = O(n2logn2) in the worst case.

  • To make the algorithm even faster, we must somehow avoid the sort. Can we?


An even faster prim s algorithm

An Even Faster Prim’s Algorithm

  • Start with the smallest-weight edge e = {x, y} in S

    • discard x and y from the list of nodes to consider

    • for both x and y, find the closest node, if any (among the non-discarded nodes), and keep them and their edge weights

  • Until n-1 edges have been added

    • find the edge e = {x, y} such that

      • x is in S and y is not in S

      • e is the smallest weighted edge left

    • add e and y to S

    • discard y from the list of nodes to consider, and for both x and y, find the closest node, if any (among the non-discarded nodes), and keep them and their edge weights

  • O(m) + O(n(n+1+n)) = O(n2) in the worst case

O(m)

O(n)

O(n)

O(1)

O(n)


Correctness of kruskal s algorithm

Correctness of Kruskal’s Algorithm

  • Kruskal’s algorithm produces a minimal spanning tree S from a connected, weighted, undirected graph G.

  • Proof (sketch)

    • Connected: Assume not, then there are at least two disconnected components, C1 and C2 in S. But since G is connected, there is a smallest weight edge {x, y} with x in C1 and y in C2 that would have been added. Thus, after running Kruskal’s algorithm C1 and C2 must be connected.

    • Acyclic: Assume not, then there was a first edge {x, y} added to make a cycle. But to have been added, x and y must have not been connected in S. Since x and y must not have been connected in S, there was no path from x to y at the time {x, y} was added, and thus no cycle was created when {x, y} was added.

    • Minimal weight: …


Minimal weight proof

Minimal Weight Proof

  • Assume the weights in G are unique, so that there is a unique minimal spanning tree T. (We can make the weights unique without changing S or T by adding different infinitesimal amounts to edges with equal weights.)

  • We can show that S and T are equivalent by assuming otherwise. If S and T differ, there must be at least one edge that is in one but not the other. Let e = {x, y} be the first such edge considered by Kruskal’s algorithm.

    • Case 1: e is in T but not S. Since Kruskal’s algorithm rejects e, x and y must be connected, but then since these edges must also be in T, T has a cycle  a contradiction.

    • Case 2: e is in S but not T. Since T is connected and acyclic, there is an acyclic path P = <y, …, v, w, …, x> in T connecting y and x (y = v and/or w = x is possible). If all the edges in P have lower weight than e, S has a cycle <y, …, v, w, …, x, y>  a contradiction. If not, there is an edge, f = {v, w}, in P that has a higher weight than e. But then removing f and adding e in T results in a spanning tree with lower aggregate weight  a contradiction.


Complexity of kruskal s algorithm

Complexity of Kruskal’s Algorithm

  • Sort edges by weight (smallest first)

  • For each edge e={x, y}, until n1 edges added if nodes x and y are not in the same connected component, add e

  • O(mlogm) + O(nm) = O(n2logn2) + O(n3) = O(n3)

  • How can Kruskal’s algorithm be improved?

    • Often already sorted (omit first step)

    • Find a faster way to check “in same connected component”

O(mlogm)

O(m)

O(n), i.e. using DFS*

O(1)

*O(n)  not O(m)  because the edges for the DFS check are the edges added to the spanning tree so far, and there can never be more than n1 edges in the spanning tree.


Complexity of kruskal s algorithm1

Complexity of Kruskal’s Algorithm

  • Assume edges sorted by weight in descending order.

  • Initialize pointers to trees of height 0

  • For each edge e={x, y}, until n1 edges added if x and y are not in the same tree (i.e. don’t have the same root), add e merge smaller tree (of x or y) into larger tree

  • O(n) + O(mlogn) = O(n2logn)

O(1)

O(1)

O(n)

O(m)

O(logn)  search tree


Kruskal s algorithm tree construction

A

B

C

D

E

F

6

7

A

6

2

5

2

F

C

B

D

A

E

D

E

A

B

F

C

5

1

1

2

8

2

1

0

0

0

0

0

1

0

5

E

D

C

B

0

1

0

0

4

4

F

0

Kruskal’s Algorithm: Tree Construction

{A,B}1

{D,F}2

{B,C}4

{A,D}5

{C,D}5

{E,D}6

{A,E}7

{B,D}8


  • Login