20
CS2420: Lecture 40 Vladimir Kulyukin Computer Science Department Utah State University

CS2420: Lecture 40

  • Upload
    bowie

  • View
    29

  • Download
    2

Embed Size (px)

DESCRIPTION

CS2420: Lecture 40. Vladimir Kulyukin Computer Science Department Utah State University. Outline. Graph Algorithms (Chapter 9). Spanning Trees. An edge e in G is redundant if, after the removal of e , G remains connected. - PowerPoint PPT Presentation

Citation preview

Page 1: CS2420: Lecture 40

CS2420: Lecture 40

Vladimir Kulyukin

Computer Science Department

Utah State University

Page 2: CS2420: Lecture 40

Outline

• Graph Algorithms (Chapter 9)

Page 3: CS2420: Lecture 40

Spanning Trees

• An edge e in G is redundant if, after the removal of e, G remains connected.

• A spanning tree of G is a tree (acyclic graph) that contains all vertices of G and preserves G’s connectivity without any redundant edges.

Page 4: CS2420: Lecture 40

Spanning Tree

• A spanning tree of a connected graph is its connected acyclic sub-graph (a tree) that contains all vertices of the graph and preserves the graph’s connectivity.

• In a weighted connected graph, the weight of a spanning tree is defined as the sum of the tree’s edges.

• A minimal (minimum) spanning tree of a weighted connected graph is a spanning tree of the smallest weight.

Page 5: CS2420: Lecture 40

Spanning Tree: Example

A

B C

D

A

B C

D

Page 6: CS2420: Lecture 40

Spanning Trees: Examples

A B

C D

5

1

2

3

Original Graph

A B

DC

1

2

3

Tree T1

Weight(T1) = 6

A B

C D

1

53

Tree T2

Weight(T2) = 9

A B

C D

1

25

Tree T3

Weight(T3) = 8

Page 7: CS2420: Lecture 40

Min Spanning Tree Construction

• Construct a minimum spanning tree by expanding it with one vertex at each iteration.

• The initial tree consists of one vertex, i.e. the root.

• On each iteration, the tree so far is expanded by attaching to it the nearest vertex.

• Stop when all the vertices are included.

Page 8: CS2420: Lecture 40

Min Spanning Tree Construction: Example

A B

C D

5

1

2

3

Original Graph

A B

D

C

1

2Step 1

A B1

Step 2

A B

D

1

2

3

Step 3

Page 9: CS2420: Lecture 40

Min Spanning Tree Construction: Prim’s Algorithm

• G = (V, E) is a weighted connected graph

• Input: G = (V, E).

• Output: ET, the set of edges composing a minimum spanning tree.

Page 10: CS2420: Lecture 40

Prim’s Algorithm: Insight

v2

v3

v4

u*

u is the vertex we have reached; next we choose the vertex closest to u*by comparing the weights:w1, w2, w3, and w4.

v1

w1

w2

w3

w4

Page 11: CS2420: Lecture 40

Prim’s Algorithm: Update

• After v is reached, two operations must be performed:– Mark v as visited, i.e. move it into VT;

– For each remaining vertex v not in VT and connected to u by a shorter edge than v’s current weight label, update v’s weight label (V.Wght) by the weight of (u, v) and set v’s previous label (V.Prev) to u.

Page 12: CS2420: Lecture 40

Prim’s Algorithm: ExampleB C

A DF

E

3

1

6

86 2

445 5

Tree Remaining VerticesNULL <A, _, INF>, <B, _, INF>,

<C, _, INF>, <D, _, INF>,<E, _, INF>, <F, _, INF>

<A, _, 0> <B, A, 3> , <C,_,INF>,<D, _, INF>, <E, A, 6> ,<F, A, 5>

<B, A, 3> <C, B, 1>, <D, _, INF>,<E, A, 6>, <F, B, 4>

<C, B, 1> <D, C, 6>, <E, A, 6>,<F, B, 4>

<F, B, 4> <D, F, 5> , <E, F, 2>

<E, F, 2> <D, F, 5>

<D, F, 5>

Page 13: CS2420: Lecture 40

Prim’s Algorithm: Example

B C

A DF

E

3

1

6

86 2

44

5 5

Page 14: CS2420: Lecture 40

Prim’s Algorithm: Example

B C

A DF

E

3

1

6

86 2

44

5 5

Page 15: CS2420: Lecture 40

Prim’s Algorithm: Example

B C

A DF

E

3

1

6

86 2

44

5 5

Page 16: CS2420: Lecture 40

Prim’s Algorithm: Example

B C

A DF

E

3

1

6

86 2

44

5 5

Page 17: CS2420: Lecture 40

Prim’s Algorithm: Example

B C

A DF

E

3

1

6

86 2

44

5 5

Page 18: CS2420: Lecture 40

Prim’s Algorithm: Result Spanning Tree

B C

A DF

E

3

1

2

4

5

Page 19: CS2420: Lecture 40

Prim’s Algorithm: Implementation

• A vertex v has four labels:– 1) The vertex’s name, i.e., V;– 2) The name of the vertex U from which V was

reached (V.Prev);– 3) The weight of (u, v) (V.Wght). Remember V.Wght

= weight(u,v);– 4) Visitation status of V (V.Visisted); this is a boolean

variable, initially false, and set to true when V is visited.

• Unreached vertices have NULL as the value of V.Prev and INF as the value of V.Wght.

Page 20: CS2420: Lecture 40

Prim’s Algorithm: Implementation

• We assume that we have implemented a dynamic priority queue data structure (DynamicPriorityQueue).

• If Q is a dynamic priority queue, then we can perform the following operations on it:– Q.Insert(v); // inserts an element v into Q;– Q.UpdatePriority(v); // moves v up or down the

minimal heap;– Q.Pop(); // removes the minimal element; – Q.Push(v); // inserts v into Q.