50
Spanning Tree

Lecture 16 data structures and algorithms

Embed Size (px)

Citation preview

Page 1: Lecture 16 data structures and algorithms

Spanning Tree

Page 2: Lecture 16 data structures and algorithms

What is A Spanning Tree?

u

v

b

a

c

d

e

f

• A spanning tree for an undirected graph G=(V,E) is a subgraph of G that is a tree and contains all the vertices of G

• Can a graph have more than one spanning tree?

• Can an unconnected graph have a spanning tree?

Page 3: Lecture 16 data structures and algorithms

Minimal Spanning Tree.

4 4

3

2

9

15

8

1014

3

u

v

b

a

c

d

e

f

Mst T: w( T )= (u,v) T w(u,v ) is minimized

• The weight of a subgraph is the sum of the weights of it edges.

• A minimum spanning tree for a weighted graph is a spanning tree with minimum weight.

• Can a graph have more then one minimum spanning tree?

Page 4: Lecture 16 data structures and algorithms

Example of a Problem that Translates into a MST

The Problem• Several pins of an electronic circuit must be

connected using the least amount of wire.

Modeling the Problem • The graph is a complete, undirected graph

G = ( V, E ,W ), where V is the set of pins, E is the set of all possible interconnections between the pairs of pins and w(e) is the length of the wire needed to connect the pair of vertices.

• Find a minimum spanning tree.

Page 5: Lecture 16 data structures and algorithms

Greedy ChoiceWe will show two ways to build a minimum

spanning tree.• A MST can be grown from the current

spanning tree by adding the nearest vertex and the edge connecting the nearest vertex to the MST. (Prim's algorithm)

• A MST can be grown from a forest of spanning trees by adding the smallest edge connecting two spanning trees. (Kruskal's algorithm)

Page 6: Lecture 16 data structures and algorithms

Notation• Tree-vertices: in the tree constructed so far• Non-tree vertices: rest of vertices

Prim’s Selection rule

• Select the minimum weight edge between a tree-node and a non-tree node and add to the tree

Page 7: Lecture 16 data structures and algorithms

The Prim algorithm Main Idea

Select a vertex to be a tree-node

while (there are non-tree vertices) { if there is no edge connecting a tree node with a non-tree node

return “no spanning tree”

select an edge of minimum weight between a tree node and a non-tree node

add the selected edge and its new vertex to the tree}

return tree

Page 8: Lecture 16 data structures and algorithms

Prim's Algorithm

C

FE

A B

D

5

64

3

4

2

1 2

3

2

Page 9: Lecture 16 data structures and algorithms

Prim's Algorithm

C

FE

A B

D

A

Page 10: Lecture 16 data structures and algorithms

Prim's Algorithm

C

FE

A B

D2

Page 11: Lecture 16 data structures and algorithms

Prim's Algorithm

C

FE

A B

D2

13

2

2

4

5

6

4

Page 12: Lecture 16 data structures and algorithms

Prim's Algorithm

C

FE

A B

D

3

2

1 2

3

2

Page 13: Lecture 16 data structures and algorithms

Prim's Algorithm

C

FE

A B

D

3

2

1 2

2

3

Page 14: Lecture 16 data structures and algorithms

Prim's Algorithm

C

FE

A B

D

3

2

1 2

2

Page 15: Lecture 16 data structures and algorithms

Prim's Algorithm

C

FE

A B

D

3

2

1 2

2

Page 16: Lecture 16 data structures and algorithms

Prim's Algorithm

C

FE

A B

D

3

2

1 2

2

minimum- spanning tree

Page 17: Lecture 16 data structures and algorithms

Kruskal's Algorithm

Kruskal‘s Algorithm

1. Each vertex is in its own cluster

2. Take the edge e with the smallest weight - if e connects two vertices in different clusters, then e is added to the MST and the two clusters, which are connected by e, are merged into a

single cluster - if e connects two vertices, which are already

in the same cluster, ignore it

3. Continue until n-1 edges were selected

Page 18: Lecture 16 data structures and algorithms

Kruskal's Algorithm

C

FE

A B

D

5

64

3

4

2

1 2

3

2

Page 19: Lecture 16 data structures and algorithms

Kruskal's Algorithm

C

FE

A B

D

5

64

3

4

2

1 2

3

2

Page 20: Lecture 16 data structures and algorithms

Kruskal's Algorithm

C

FE

A B

D

5

64

3

4

2

1 2

3

2

Page 21: Lecture 16 data structures and algorithms

Kruskal's Algorithm

C

FE

A B

D

5

64

3

4

2

1 2

3

2

Page 22: Lecture 16 data structures and algorithms

Kruskal's Algorithm

C

FE

A B

D

5

64

3

4

2

1 2

3

2

Page 23: Lecture 16 data structures and algorithms

Kruskal's Algorithm

C

FE

A B

D

5

64

3

4

2

1 2

3

2

cycle!!

Page 24: Lecture 16 data structures and algorithms

Kruskal's Algorithm

C

FE

A B

D

5

64

3

4

2

1 2

3

2

Page 25: Lecture 16 data structures and algorithms

Kruskal's Algorithm

C

FE

A B

D

5

64

3

4

2

1 2

3

2

Page 26: Lecture 16 data structures and algorithms

Kruskal's Algorithm

C

FE

A B

D

3

2

1 2

2

minimum- spanning tree

Page 27: Lecture 16 data structures and algorithms

Graph Traversal

Traversing a graph means visiting all the vertices in the graph exactly once.

Breadth First Search (BFS)Depth First Search (DFS)

Page 28: Lecture 16 data structures and algorithms

DFSSimilar to in-order traversal of a binary

search treeStarting from a given node, this

traversal visits all the nodes up to the deepest level and so on.

Page 29: Lecture 16 data structures and algorithms

v1

v8

v2 v3

v4

v7

v6v5

v1

v8

v2 v3

v4

v7

v6v5

DFS

DFS : V1 - V2 - V5 - V7 – V4 - V8 – V6 – V3

Page 30: Lecture 16 data structures and algorithms

v1

v8

v2 v3

v4

v7

v6v5

v1

v8

v2 v3

v4

v7

v6v5

DFS

DFS : V1 - V2 - V5 - V7 – V4 - V8 – V3 – V6

Page 31: Lecture 16 data structures and algorithms

DFS TraversalVisit the vertex vVisit all the vertices along the path which begins

at v

Visit the vertex v, then the vertex immediate adjacent to v, let it be vx . If vx has an immediate adjacent vy then visit it and so on till there is a dead end.

Dead end: A vertex which does not have an immediate adjacent or its immediate adjacent has been visited.

Page 32: Lecture 16 data structures and algorithms

After coming to an dead end we backtrack to v to see if it has an another adjacent vertex other than vx and then continue the same from it else from the adjacent of the adjacent (which is not visited earlier) and so on.

Page 33: Lecture 16 data structures and algorithms

Push the starting vertex into the STACKWhile STACK not empty do

POP a vertex V If V is not visited

Visit the vertex VStore V in VISIT PUSH all adjacent vertex of V

onto STACK End of IF

End of While STOP

Page 34: Lecture 16 data structures and algorithms

C

K

A

B

DE

F

G

J

Adjacency List

A: F,C,BB: G,CC: FD: CE: D,C,JF: DG: C,EJ: D,KK: E,G

Page 35: Lecture 16 data structures and algorithms

DFS of G starting at J[1] Initially push J onto STACK

STACK : JVISIT: Ø

[2] POP J from the STACK, add it in VISIT and PUSH onto the STACK all neighbor of J

STACK: D, KVISIT: J

Page 36: Lecture 16 data structures and algorithms

[3] POP the top element K, add it in VISIT and PUSH all neighbor of K onto STACK

STACK: D,E,GVISIT: J, K

[4] POP the top element G, add it in VISIT and PUSH all neighbor of G onto STACK

STACK: D,E, E, C,VISIT: J, K, G

Page 37: Lecture 16 data structures and algorithms

[5] POP the top element C, add it in VISIT and PUSH all neighbor of C onto STACK

STACK: D,E,E, FVISIT: J, K, G, C

[6] POP the top element F, add it in VISIT and PUSH all neighbor of F onto STACK

STACK: D,E, E, DVISIT: J, K, G, C, F

Page 38: Lecture 16 data structures and algorithms

[5] POP the top element D, add it in VISIT and PUSH all neighbor of D onto STACK

STACK: D,E,E, CVISIT: J, K, G, C, F,D

[6] POP the top element C, which is already in VISIT

STACK: D,E, EVISIT: J, K, G, C, F,D

Page 39: Lecture 16 data structures and algorithms

[5] POP the top element E, add it in VISIT which is already in VISIT and its neighbor onto STACK

STACK: D,E, D, C, J VISIT: J, K, G, C, F,D,E

[6] POP the top element J, C, D,E, D which is already in VISIT

STACK: VISIT: J, K, G, C, F, D, E

Page 40: Lecture 16 data structures and algorithms

C

K

A

B

DE

F

G

J

Adjacency List

A: F,C,BB: G,CC: FD: CE: D,C,JF: DG: C,EJ: D,KK: E,G

J, K, G, C, F, D, E

Page 41: Lecture 16 data structures and algorithms

BFS Traversal

Any vertex in label i will be visited only after the visiting of all the vertices in its preceding level that is at level i – 1

Page 42: Lecture 16 data structures and algorithms

BFS Traversal [1] Enter the starting vertex v in a

queue Q[2] While Q is not empty do

Delete an item from Q, say u

If u is not in VISIT store u in VISIT

Enter all adjacent vertices of u into Q

[3] Stop

Page 43: Lecture 16 data structures and algorithms

v1

v8

v2 v3

v4

v7

v6v5

Page 44: Lecture 16 data structures and algorithms

[1] Insert the starting vertex V1 in Q

Q = V1

VISIT = Ø

[2] Delete an item from Q, let it be u = V1

u is not in VISIT. Store u in VISIT and its adjacent element in Q

Q = V2 , V3

VISIT = V1

Page 45: Lecture 16 data structures and algorithms

[3] Delete an item from Q, let it be u = V2

u is not in VISIT. Store u in VISIT and its adjacent element in Q

Q = V2 , V3 , V4 , V5

VISIT = V1 , V2

[4] Delete an item from Q, let it be u = V3

u is not in VISIT. Store u in VISIT and its adjacent element in Q

Q = V3 , V4 , V5 , V4 , V6

VISIT = V1 , V2 , V3

Page 46: Lecture 16 data structures and algorithms

[5] Delete an item from Q, let it be u = V4

u is not in VISIT. Store u in VISIT and its adjacent element in Q

Q = V4 , V5 , V4 , V6 , V8

VISIT = V1 , V2 , V3 , V4

[6] Delete an item from Q, let it be u =V5

u is not in VISIT. Store u in VISIT and its adjacent element in Q

Q = V5 , V4 , V6 , V8 , V7

VISIT = V1 , V2 , V3 , V4 , V5

Page 47: Lecture 16 data structures and algorithms

[7] Delete an item from Q, let it be u =V4

u is in VISIT.

Q = V4 , V6 , V8 , V7

VISIT = V1 , V2 , V3 , V4 , V5

[8] Delete an item from Q, let it be u =V6

u is not in VISIT. Store u in VISIT and its adjacent element in Q

Q = V6 , V8 , V7

VISIT = V1 , V2 , V3 , V4 , V5 , V6

Page 48: Lecture 16 data structures and algorithms

[9] Delete an item from Q, let it be u =V8

u is not in VISIT. Store u in VISIT and its adjacent element in Q

Q = V8 , V7 , V1

VISIT = V1 , V2 , V3 , V4 , V5 , V6 , V8

[10] Delete an item from Q, let it be u =V7

u is not in VISIT. Store u in VISIT and its adjacent element in Q

Q = V7 , V1

VISIT = V1 , V2 , V3 , V4 , V5 , V6 , V8 , V7

Page 49: Lecture 16 data structures and algorithms

[11] Delete an item from Q, let it be u =V1

u is in VISIT.

Q = V1

VISIT = V1 , V2 , V3 , V4 , V5 , V6 , V8 , V7

[12] Q is empty, Stop Q =

VISIT = V1 , V2 , V3 , V4 , V5 , V6 , V8 , V7

Page 50: Lecture 16 data structures and algorithms

v1

v8

v2 v3

v4

v7

v6v5

v1

v8

v2 v3

v4

v7

v6v5

BFS