29
1 UIT – RGPV BHOPAL TOPIC:- Minimum Spanning Tree PRESENTED BY- Narendra Singh Patel

Minimum spanning Tree

Embed Size (px)

DESCRIPTION

Algorithm used in data structure of minimum Spanning tree

Citation preview

Page 1: Minimum spanning Tree

1

UIT – RGPV BHOPAL

TOPIC:- Minimum Spanning Tree

PRESENTED BY- Narendra Singh Patel

Page 2: Minimum spanning Tree

2

PROBLEM: LAYING TELEPHONE WIRE

Central office

Page 3: Minimum spanning Tree

3

WIRING: NAÏVE APPROACH

Central office

Expensive!

Page 4: Minimum spanning Tree

4

WIRING: BETTER APPROACH

Central office

Minimize the total length of wire connecting the customers

Page 5: Minimum spanning Tree

5

Minimum Spanning Trees

Page 6: Minimum spanning Tree

6

We are interested in:

Finding a tree T that contains all the vertices of a graph G spanning treeand has the least total weight over allsuch trees minimum-spanning tree (MST)

Tuv

uvwTw),(

)),(()(

Page 7: Minimum spanning Tree

7

Before discuss about MST (minimum spanning tree) lets get familiar with Graphs.

Page 8: Minimum spanning Tree

8

Graph-• A graph is a finite set of nodes with

edges between nodes.• Formally, a graph G is a structure

(V,E) consisting of – a finite set V called the set of nodes, and– a set E that is a subset of VxV. That is, E

is a set of pairs of the form (x,y) where x and y are nodes in V

1 2

3 4

Page 9: Minimum spanning Tree

9

Directed vs. Undirected Graphs

• If the directions of the edges matter, then we show the edge directions, and the graph is called a directed graph (or a digraph)

• If the relationships represented by the edges are symmetric (such as (x,y) is edge if and only if x is a sibling of y), then we don’t show the directions of the edges, and the graph is called an undirected graph.

Page 10: Minimum spanning Tree

10

SPANNING TREES

Suppose you have a connected undirected graph

Connected: every node is reachable from every other node

Undirected: edges do not have an associated direction

...then a spanning tree of the graph is a connected subgraph in which there are no cyclesA connected,

undirected graph

Four of the spanning trees of the graph

Page 11: Minimum spanning Tree

11

• it is a tree (i.e., it is acyclic)• it covers all the vertices V

– contains |V| - 1 edges

• the total cost associated with tree edges is the minimum among all possible spanning trees

• not necessarily unique

A minimum spanning tree is a subgraph of an undirected weighted graph G, such that

Minimum Spanning Tree (MST)

Page 12: Minimum spanning Tree

12

APPLICATIONS OF MST

Cancer imaging. The BC Cancer Research Ctr. uses minimum spanning trees to describe the arrangements of nuclei in skin cells. •Cosmology at the University of Kentucky. This group works on large-scale structure formation, using methods including N-body simulations and minimum spanning trees. •Detecting actin fibers in cell images. A. E. Johnson and R. E. Valdes-Perez use minimum spanning trees for biomedical image analysis. •The Euclidean minimum spanning tree mixing model. S. Subramaniam and S. B. Pope use geometric minimum spanning trees to model locality of particle interactions in turbulent fluid flows. The tree structure of the MST permits a linear-time solution of the resulting particle-interaction matrix.

Page 13: Minimum spanning Tree

13

• Extracting features from remotely sensed images. Mark Dobie and co-workers use minimum spanning trees to find road networks in satellite and aerial imagery.

• Finding quasar superstructures. M. Graham and co-authors use 2d and 3d minimum spanning trees for finding clusters of quasars and Seyfert galaxies.

•Learning salient features for real-time face verification, K. Jonsson, J. Matas, and J. Kittler. Includes a minimum-spanning-tree based algorithm for registering the images in a database of faces. •Minimal spanning tree analysis of fungal spore spatial patterns, C. L. Jones, G. T. Lonergan, and D. E. Mainwaring.

Page 14: Minimum spanning Tree

14

•A minimal spanning tree analysis of the CfA redshift survey. Dan Lauer uses minimum spanning trees to understand the large-scale structure of the universe.

•A mixing model for turbulent reactive flows based on Euclidean minimum spanning trees

, S. Subramaniam and S. B. Pope.

•Sausages, proteins, and rho. In the talk announced here, J. MacGregor Smith discusses Euclidean Steiner tree theory and describes potential applications of Steiner trees to protein conformation and molecular modeling.

•Weather data interpretation. The Insight group at Ohio State is using geometric techniques such as minimum spanning trees to extract features from large meteorological data sets

Page 15: Minimum spanning Tree

What is a Minimum-Cost Spanning Tree

For an edge-weighted , connected, undirected graph, G, the total cost of G is the sum of the weights on all its edges.

A minimum-cost spanning tree for G is a minimum spanning tree of G that has the least total cost.

Example: The graph

Has 16 spanning trees. Some are:

The graph has two minimum-cost spanning trees, each with a cost of 6:

Page 16: Minimum spanning Tree

16

HOW CAN WE GENERATE A MST?

Page 17: Minimum spanning Tree

17

We have two Ways to generate a MST

1. Prims algorithm2. Kruskals

algorithm

Page 18: Minimum spanning Tree

Prim’s Algorithm

Prim’s algorithm finds a minimum cost spanning tree by selecting edges from the graph one-by-one as follows:It starts with a tree, T, consisting of the starting

vertex, x.Then, it adds the shortest edge emanating from x that

connects T to the rest of the graph.It then moves to the added vertex and repeats the

process.

Page 19: Minimum spanning Tree

Prim’s Algorithm

The edges in set A always form a single tree

Starts from an arbitrary “root”: VA = {a}

At each step:

Find a light edge crossing (VA, V - VA)

Add this edge to A

Repeat until the tree spans all vertices

19

a

b c d

e

h g f

i

4

8 7

8

11

1 2

7

2

4 14

9

106

Page 20: Minimum spanning Tree

Example

20

0 Q = {a, b, c, d, e, f, g, h,

i} VA =

Extract-MIN(Q) a

a

b c d

e

h g f

i

4

8 7

8

11

1 2

7

2

4 14

9

106

a

b c d

e

h g f

i

4

8 7

8

11

1 2

7

2

4 14

9

106

key [b] = 4 [b] = a

key [h] = 8 [h] = a

4 8 Q = {b, c, d, e, f, g, h, i} VA = {a}

Extract-MIN(Q) b

4

8

Page 21: Minimum spanning Tree

Example

21

4

8

8

a

b c d

e

h g f

i

4

8 7

8

11

1 2

7

2

4 14

9

106

key [c] = 8 [c] = b

key [h] = 8 [h] = a - unchanged

8 8 Q = {c, d, e, f, g, h, i} VA = {a, b}

Extract-MIN(Q) c

a

b c d

e

h g f

i

4

8 7

8

11

1 2

7

2

4 14

9

106

key [d] = 7 [d] = c

key [f] = 4 [f] = c

key [i] = 2 [i] = c

7 4 8 2

Q = {d, e, f, g, h, i} VA = {a, b, c}

Extract-MIN(Q) i

4

8

8

7

4

2

Page 22: Minimum spanning Tree

Example

22

a

b c d

e

h g f

i

4

8 7

8

11

1 2

7

2

4 14

9

106

key [h] = 7 [h] = i

key [g] = 6 [g] = i

7 4 6 8

Q = {d, e, f, g, h} VA = {a, b, c, i}

Extract-MIN(Q) f

a

b c d

e

h g f

i

4

8 7

8

11

1 2

7

2

4 14

9

106

key [g] = 2 [g] = f

key [d] = 7 [d] = c unchanged

key [e] = 10 [e] = f

7 10 2 8

Q = {d, e, g, h} VA = {a, b, c, i, f}

Extract-MIN(Q) g

4 7

8 4

8

2

7 6

4 7

7 6 4

8

2

2

10

Page 23: Minimum spanning Tree

Example

23

a

b c d

e

h g f

i

4

8 7

8

11

1 2

7

2

4 14

9

106

key [h] = 1 [h] = g

7 10 1

Q = {d, e, h} VA = {a, b, c, i, f, g}

Extract-MIN(Q) h

a

b c d

e

h g f

i

4

8 7

8

11

1 2

7

2

4 14

9

106

7 10

Q = {d, e} VA = {a, b, c, i, f, g, h}

Extract-MIN(Q) d

4 7

10

7 2 4

8

2

14 7

10

1 2 4

8

2

Page 24: Minimum spanning Tree

Example

24

a

b c d

e

h g f

i

4

8 7

8

11

1 2

7

2

4 14

9

106

key [e] = 9 [e] = f

9

Q = {e} VA = {a, b, c, i, f, g, h, d}

Extract-MIN(Q) eQ = VA = {a, b, c, i, f, g, h, d, e}

4 7

10

1 2 4

8

2 9

Page 25: Minimum spanning Tree

Prim’s (V, E, w, r)

25

1. Q ←

2. for each u V

3. do key[u] ← ∞

4. π[u] ← NIL

5. INSERT(Q, u)

6. DECREASE-KEY(Q, r, 0) ► key[r] ← 0

7. while Q

8. do u ← EXTRACT-MIN(Q)

9. for each v Adj[u]

10. do if v Q and w(u, v) < key[v]

11. then π[v] ← u

12. DECREASE-KEY(Q, v, w(u, v))

O(V) if Q is implemented as a min-heap

Executed |V| times

Takes O(lgV)

Min-heap operations:O(VlgV)

Executed O(E) times total

Constant

Takes O(lgV)

O(ElgV)

Total time: O(VlgV + ElgV) = O(ElgV)

O(lgV)

Page 26: Minimum spanning Tree

Algorithm

26

How is it different from Prim’s algorithm?Prim’s algorithm grows one tree all the timeKruskal’s algorithm grows multiple trees (i.e., a forest) at the same time.Trees are merged together using safe edgesSince an MST has exactly |V| - 1 edges, after |V| - 1 merges, we would have only one component

u

v

tree1

tree2

Kruskal’s Algorithm

Page 27: Minimum spanning Tree

Kruskal’s Algorithm

27

Start with each vertex being its own component

Repeatedly merge two components into one by choosing the light edge that connects them

Which components to consider at each iteration?Scan the set of edges in

monotonically increasing order by weight

We would addedge (c, f)

a

b c d

e

h g f

i

4

8 7

8

11

1 2

7

2

4 14

9

106

Page 28: Minimum spanning Tree

Example

1. Add (h, g)

2. Add (c, i)

3. Add (g, f)

4. Add (a, b)

5. Add (c, f)

6. Ignore (i, g)

7. Add (c, d)

8. Ignore (i, h)

9. Add (a, h)

10. Ignore (b, c)

11. Add (d, e)

12. Ignore (e, f)

13. Ignore (b, h)

14. Ignore (d, f)

28

a

b c d

e

h g f

i

4

8 7

8

11

1 2

7

2

4 14

9

106

1: (h, g)

2: (c, i), (g, f)

4: (a, b), (c, f)

6: (i, g)

7: (c, d), (i, h)

8: (a, h), (b, c)

9: (d, e)

10: (e, f)

11: (b, h)

14: (d, f)

{g, h}, {a}, {b}, {c}, {d}, {e}, {f}, {i}

{g, h}, {c, i}, {a}, {b}, {d}, {e}, {f}

{g, h, f}, {c, i}, {a}, {b}, {d}, {e}

{g, h, f}, {c, i}, {a, b}, {d}, {e}

{g, h, f, c, i}, {a, b}, {d}, {e}

{g, h, f, c, i}, {a, b}, {d}, {e}

{g, h, f, c, i, d}, {a, b}, {e}

{g, h, f, c, i, d}, {a, b}, {e}

{g, h, f, c, i, d, a, b}, {e}

{g, h, f, c, i, d, a, b}, {e}

{g, h, f, c, i, d, a, b, e}

{g, h, f, c, i, d, a, b, e}

{g, h, f, c, i, d, a, b, e}

{g, h, f, c, i, d, a, b, e}{a}, {b}, {c}, {d}, {e}, {f}, {g}, {h}, {i}

Page 29: Minimum spanning Tree

Thank You

Bibliography- http://www.cs.brown.edu/en.wikipedia.org/wiki/

29