Docsity

Spanning Trees - Data Structures - Lecture Slides, Slides of Data Structures and Algorithms

Partial preview of the text

Download Spanning Trees - Data Structures - Lecture Slides and more Slides Data Structures and Algorithms in PDF only on Docsity!

Minimum Spanning

Trees

Spanning Tree properties:

On a connected graph G=(V, E), a spanning tree:

  • is a connected subgraph
  • acyclic
  • is a tree (|E| = |V| - 1)
  • contains all vertices of G

(spanning)

Finding a spanning tree

• BFS

• DFS

• Greedy algorithm

An undirected graph (^) Result of a BFS

starting from top

Result of a DFS starting from top

Minimum weight spanning tree (MST)

On a weighted graph, A MST:

  • connects all vertices through edges with least

weights.

  • has w(T) ≤ w(T’) for every other spanning tree T’

in G

  • Adding one edge not in MST will create a cycle

Kruskal’s algorithm

 Edge first

1. Arrange all edges in a list (L)

in non-decreasing order

2. Select edges from L, and include

that in set T, avoid cycle.

3. Repeat 3 until T becomes a tree

that covers all vertices

Kruskal’s Algorithm

14

15

15

14

13 13 16

16

12 12

14 14

Kruskal’s Algorithm

14

15

15

14

13 13 16

16

12 12

14 14

Kruskal’s Algorithm

14

15

15

14

13 13 16

16

12 12

14 14

Kruskal’s Algorithm

14

15

15

14

13 13 16

16

12 12

14 14

Kruskal’s Algorithm

14

15

15

14

13 13 16

16

12 12

14 14

Kruskal’s Algorithm

14

15

15

14

13 13 16

16

12 12

14 14

Skip

Skip {5,6} to avoid cycle

Skip

Kruskal’s Algorithm

14

15

15

14

13 13 16

16

12 12

14 14

MST is formed

14

15

15

14

13 13 16

16

12 12

14 14

Start from any arbitrary vertex

Prim’s algorithm

14

15

15

14

13 13 16

16

12

14 14

Weight:

The best choice is {1,2}

Prim’s algorithm

12 What’s next?