Loading...

PPT – MST Algorithm Review PowerPoint presentation | free to download - id: 270ecc-ZDc1Z

The Adobe Flash plugin is needed to view this content

MST Algorithm Review

Minimal Cost Spanning Trees

- Minimal Cost Spanning Tree (MST) Problem
- Input An undirected, connected graph G.
- Output The subgraph of G that
- 1) has minimum total cost as measured by summing

the weight of all the edges in the subset, and - 2) keeps the vertices connected.

Partition Property

- Partition Property
- Consider a partition of the vertices of G into

subsets U and V - Let e be an edge of minimum weight across the

partition - There is a minimum spanning tree of G containing

edge e

U

V

7

f

4

9

5

2

8

3

8

e

7

Prims MST Algorithm

- As with Dijkstras algorithm, uses a priority

queue. - Running time is identical to Dijkstras algorithm

implementations.

B

5

A

7

C

6

1

2

9

D

2

F

E

1

Prims MST Algorithm

Prims MST Algorithm

(?)

(?)

B

Step 1

5

A

7

C

(?)

6

1

2

9

D

2

(?)

F

E

(0)

1

(?)

Prims MST Algorithm

(6,F)

(?)

B

Step 2

5

A

7

C

(2,F)

6

1

2

9

D

2

(2,F)

F

E

(0)

1

(1,F)

Prims MST Algorithm

(6,F)

(?)

B

Step 3

5

A

7

C

(2,F)

6

1

2

9

D

2

(2,F)

F

E

(0)

1

(1,F)

Prims MST Algorithm

(6,F)

(9,E)

B

Step 3a

5

A

7

C

(2,F)

6

1

2

9

D

2

(2,F)

F

E

(0)

1

(1,F)

Prims MST Algorithm

(6,F)

(9,E)

B

Step 4

5

A

7

C

(2,F)

6

1

2

9

D

2

(2,F)

F

E

(0)

1

(1,F)

Prims MST Algorithm

(5,C)

(7,C)

B

Step 4a

5

A

7

C

(2,F)

6

1

2

9

D

2

(1,C)

F

E

(0)

1

(1,F)

Prims MST Algorithm

(5,C)

(7,C)

B

Step 5

5

A

7

C

(2,F)

6

1

2

9

D

2

(1,C)

F

E

(0)

1

(1,F)

Prims MST Algorithm

(5,C)

(7,C)

B

Step 5a

5

A

7

C

(2,F)

6

1

2

9

D

2

(1,C)

F

E

(0)

1

(1,F)

Prims MST Algorithm

(5,C)

(7,C)

B

Step 6

5

A

7

C

(2,F)

6

1

2

9

D

2

(1,C)

F

E

(0)

1

(1,F)

Prims MST Algorithm

(5,C)

(7,C)

B

Step 6a

5

A

7

C

(2,F)

6

1

2

9

D

2

(1,C)

F

E

(0)

1

(1,F)

Prims MST Algorithm

(5,C)

(7,C)

B

Step 7

5

A

7

C

(2,F)

6

1

2

9

D

2

(1,C)

F

E

(0)

1

(1,F)

Kruskals MST Algorithm

B

5

A

7

C

6

1

2

9

D

2

F

E

1

- Initially, each vertex is in its own MST.
- Merge two MSTs that have the shortest edge

between them. - Use a priority queue to order the unprocessed

edges. Grab the minimum one at each step. - How to tell if an edge connects two vertices

already in the same MST? - Use parent-pointer representation.

Kruskals MST Algorithm

Step 1

E

D

F

B

C

A

Kruskals MST Algorithm

Step 2

B

5

A

E

D

F

B

C

A

7

C

6

1

2

9

D

2

F

E

1

Kruskals MST Algorithm

Step 3

B

5

A

E

D

F

B

C

A

7

C

6

1

2

9

D

2

F

E

1

Kruskals MST Algorithm

Step 4

B

5

A

E

D

F

B

C

A

7

C

6

1

2

9

D

2

F

E

1

Kruskals MST Algorithm

Step 5

B

5

A

E

D

F

B

C

A

7

C

6

1

2

9

D

2

F

E

1

Kruskals MST Algorithm

Step 6

B

5

A

E

D

F

B

C

A

7

C

6

1

2

9

D

2

F

E

1

Kruskals MST Algorithm

Step 7

B

5

A

E

D

F

B

C

A

7

C

6

1

2

9

D

2

F

E

1

Kruskals MST Algorithm

- Cost is dominated by the time to remove edges

from the heap. - Can stop processing edges once all vertices are

in the same MST - Total cost Q((n m) log m).

Baruvkas MST Algorithm

B

5

A

7

C

6

1

2

9

D

2

F

E

1

- Create a forest of n trees
- Loop while (there is gt 1 tree in the forest)
- For each tree Ti in the forest
- Find the smallest edge e (u,v), in the edge

list with u in Ti and v in Tj ?Ti - connects 2 trees from the forest in to one.
- end loop

Baruvkas MST Algorithm

Step 1

E

D

F

B

C

A

Baruvkas MST Algorithm

Step 2

B

5

A

E

D

F

B

C

A

7

C

6

1

2

9

D

2

F

E

1

Baruvkas MST Algorithm

Step 3

B

5

A

E

D

F

B

C

A

7

C

6

1

2

9

D

2

F

E

1

Baruvkas MST Algorithm

Step 4

B

5

A

E

D

F

B

C

A

7

C

6

1

2

9

D

2

F

E

1

Baruvkas MST Algorithm

Step 5

B

5

A

E

D

F

B

C

A

7

C

6

1

2

9

D

2

F

E

1

Baruvkas MST Algorithm

Step 6

B

5

A

E

D

F

B

C

A

7

C

6

1

2

9

D

2

F

E

1

Baruvkas Algorithm

- Similar to Kruskals Algorithm, but no Priority

queueS. - Each iteration of the while-loop halves the

number of connected compontents in T. - The running time is O(m log n).

Algorithm BaruvkaMST(G) T ? V just the

vertices of G while T has fewer than n-1 edges

do for each connected component C in T

do Let edge e be the smallest-weight edge from

C to another component in T. if e is not

already in T then Add edge e to T return T

Network Flow Problem

Flow Network

- A network N consists of
- A weighted digraph G with nonnegative integer

edge weights, where the weight of an edge e is

called the capacity c(e) of e - Two distinguished vertices, s and t of G, called

the source and sink or target, respectively, such

that s has no incoming edges and t has no

outgoing edges. - Example

Flow

- A flow f for a network N is an assignment of an

value f(e) to each edge e that satisfies the

following properties - Capacity Rule For each edge e, 0 ? f (e) ? c(e)

Flow

- A flow f for a network N is an assignment of an

value f(e) to each edge e that satisfies the

following properties - Capacity Rule For each edge e, 0 ? f (e) ? c(e)
- Conservation Rule For each vertex v ? s,t

Flow

- The value of a flow f , denoted f, is
- the total flow from the source
- which is the same as,
- the total flow into the sink

Maximum Flow

- A flow for a network N is said to be maximum if

its value is the largest of all flows for N - The maximum flow problem consists of finding a

maximum flow for a given network N - Applications
- Hydraulic systems
- Electrical circuits
- Traffic movements
- Freight transportation

Maximum Flow

v

1/3

2/6

1/1

t

3/7

3/3

s

w

2/9

4/5

1/1

3/5

z

u

2/2

Flow 2 3 3 1 3 4 8

A Simple Flow Algorithm

- Initialize all edges of the flow graph with zero

flow - Loop while ? a path in Gr from s to t
- find a path in Gr from s to t (augmenting path)
- Add to the flow graph the minimum residual

capacity - from this path
- Reduce the residual capacity of the edges
- end Loop

Flow Graph (Gf)

A Simple Flow Algorithm

- Initialize all edges of the flow graph with zero

flow - Loop while ? a path in Gr from s to t
- find a path in Gr from s to t (augmenting path)
- Add to the flow graph the minimum residual

capacity - from this path
- Reduce the residual capacity of the edges
- end Loop

Residual Graph (Gr)

Flow Graph (Gf)

A Simple Flow Algorithm

- Initialize all edges of the flow graph with zero

flow - Loop while ? a path in Gr from s to t
- find a path in Gr from s to t (augmenting path)
- Add to the flow graph the minimum residual

capacity - from this path
- Reduce the residual capacity of the edges
- end Loop

Residual Graph (Gr)

Flow Graph (Gf)

v

3

6

1

t

7

3

s

w

9

5

1

5

z

u

2

A Simple Flow Algorithm

- Initialize all edges of the flow graph with zero

flow - Loop while ? a path in Gr from s to t
- find a path in Gr from s to t (augmenting path)
- Add to the flow graph the minimum residual

capacity - from this path
- Reduce the residual capacity of the edges
- end Loop

Residual Graph (Gr)

Flow Graph (Gf)

v

3

6

1

t

7

3

s

w

9

3

1

3

z

u

A Simple Flow Algorithm

- Initialize all edges of the flow graph with zero

flow - Loop while ? a path in Gr from s to t
- find a path in Gr from s to t (augmenting path)
- Add to the flow graph the minimum residual

capacity - from this path
- Reduce the residual capacity of the edges
- end Loop

Residual Graph (Gr)

Flow Graph (Gf)

v

3

6

1

t

7

3

s

w

9

3

1

3

z

u

A Simple Flow Algorithm

- Initialize all edges of the flow graph with zero

flow - Loop while ? a path in Gr from s to t
- find a path in Gr from s to t (augmenting path)
- Add to the flow graph the minimum residual

capacity - from this path
- Reduce the residual capacity of the edges
- end Loop

Residual Graph (Gr)

Flow Graph (Gf)

v

3

6

1

t

7

s

w

6

1

3

z

u

A Simple Flow Algorithm

- Initialize all edges of the flow graph with zero

flow - Loop while ? a path in Gr from s to t
- find a path in Gr from s to t (augmenting path)
- Add to the flow graph the minimum residual

capacity - from this path
- Reduce the residual capacity of the edges
- end Loop

Residual Graph (Gr)

Flow Graph (Gf)

v

3

6

1

t

7

s

w

6

1

3

z

u

A Simple Flow Algorithm

- Initialize all edges of the flow graph with zero

flow - Loop while ? a path in Gr from s to t
- find a path in Gr from s to t (augmenting path)
- Add to the flow graph the minimum residual

capacity - from this path
- Reduce the residual capacity of the edges
- end Loop

Residual Graph (Gr)

Flow Graph (Gf)

v

3

1

t

7

s

w

6

1

3

z

u

A Simple Flow Algorithm

- Initialize all edges of the flow graph with zero

flow - Loop while ? a path in Gr from s to t
- find a path in Gr from s to t (augmenting path)
- Add to the flow graph the minimum residual

capacity - from this path
- Reduce the residual capacity of the edges
- end Loop

Residual Graph (Gr)

Flow Graph (Gf)

v

3

1

t

7

s

w

6

1

3

z

u

A Simple Flow Algorithm

- Initialize all edges of the flow graph with zero

flow - Loop while ? a path in Gr from s to t
- find a path in Gr from s to t (augmenting path)
- Add to the flow graph the minimum residual

capacity - from this path
- Reduce the residual capacity of the edges
- end Loop

Residual Graph (Gr)

Flow Graph (Gf)

v

3

1

t

6

s

w

6

2

z

u

A Simple Flow Algorithm

- Initialize all edges of the flow graph with zero

flow - Loop while ? a path in Gr from s to t
- find a path in Gr from s to t (augmenting path)
- Add to the flow graph the minimum residual

capacity - from this path
- Reduce the residual capacity of the edges
- end Loop

Residual Graph (Gr)

Flow Graph (Gf)

v

3

1

t

6

s

w

6

2

z

u

A Simple Flow Algorithm

- Initialize all edges of the flow graph with zero

flow - Loop while ? a path in Gr from s to t
- find a path in Gr from s to t (augmenting path)
- Add to the flow graph the minimum residual

capacity - from this path
- Reduce the residual capacity of the edges
- end Loop

Residual Graph (Gr)

Flow Graph (Gf)

v

2

t

5

s

w

6

2

z

u

A Simple Flow Algorithm

- Initialize all edges of the flow graph with zero

flow - Loop while ? a path in Gr from s to t
- find a path in Gr from s to t (augmenting path)
- Add to the flow graph the minimum residual

capacity - from this path
- Reduce the residual capacity of the edges
- end Loop

Residual Graph (Gr)

Flow Graph (Gf)

/2

2

w

v

w

v

/2

2

/3

3

s

s

/4

4

t

/1

t

1

/2

2

/3

3

z

z

u

u

/3

3

A Simple Flow Algorithm

- Initialize all edges of the flow graph with zero

flow - Loop while ? a path in Gr from s to t
- find a path in Gr from s to t (augmenting path)
- Add to the flow graph the minimum residual

capacity - from this path
- Reduce the residual capacity of the edges
- end Loop

Residual Graph (Gr)

Flow Graph (Gf)

0/2

2

w

v

w

v

0/2

2

0/3

3

s

s

0/4

4

t

0/1

t

1

0/2

2

0/3

3

z

z

u

u

0/3

3

A Simple Flow Algorithm

- Initialize all edges of the flow graph with zero

flow - Loop while ? a path in Gr from s to t
- find a path in Gr from s to t (augmenting path)
- Add to the flow graph the minimum residual

capacity - from this path
- Reduce the residual capacity of the edges
- end Loop

Residual Graph (Gr)

Flow Graph (Gf)

0/2

2

w

v

w

v

0/2

2

3/3

s

s

1

3/4

t

0/1

t

1

0/2

2

3/3

z

z

u

u

0/3

3

Algorithm terminates with an sub-optimal flow

solution

Ford-Fulkersons Algorithm

- Initialize all edges of the flow graph with zero

flow - Loop while ? a path in Gr from s to t
- find a path in Gr from s to t (augmenting path)
- Add to the flow graph the minimum residual

capacity - from this path
- Reduce the residual capacity of the edges
- Add a reversed path in the residual graph
- end Loop

Residual Graph (Gr)

Flow Graph (Gf)

0/2

2

w

v

w

v

0/2

2

0/3

3

s

s

0/4

4

t

0/1

t

1

0/2

2

0/3

3

z

z

u

u

0/3

3

Ford-Fulkersons Algorithm

- Initialize all edges of the flow graph with zero

flow - Loop while ? a path in Gr from s to t
- find a path in Gr from s to t (augmenting path)
- Add to the flow graph the minimum residual

capacity - from this path
- Reduce the residual capacity of the edges
- Add a reversed path in the residual graph
- end Loop

Residual Graph (Gr)

Flow Graph (Gf)

0/2

2

w

v

w

v

0/2

2

3/3

-3

s

s

3/4

1

t

0/1

t

1

-3

0/2

2

3/3

-3

z

z

u

u

0/3

3

Ford-Fulkersons Algorithm

- Initialize all edges of the flow graph with zero

flow - Loop while ? a path in Gr from s to t
- find a path in Gr from s to t (augmenting path)
- Add to the flow graph the minimum residual

capacity - from this path
- Reduce the residual capacity of the edges
- Add a reversed path in the residual graph
- end Loop

Residual Graph (Gr)

Flow Graph (Gf)

0/2

2

w

v

w

v

0/2

2

3/3

-3

s

s

3/4

1

t

0/1

t

1

-3

0/2

2

3/3

-3

z

z

u

u

0/3

3

Ford-Fulkersons Algorithm

- Initialize all edges of the flow graph with zero

flow - Loop while ? a path in Gr from s to t
- find a path in Gr from s to t (augmenting path)
- Add to the flow graph the minimum residual

capacity - from this path
- Reduce the residual capacity of the edges
- Add a reversed path in the residual graph
- end Loop

Residual Graph (Gr)

Flow Graph (Gf)

2/2

-2

w

v

w

v

2/2

-2

3/3

-3

s

s

1/4

3

t

0/1

t

1

-1

2/2

-2

3/3

-3

-2

z

z

u

u

2/3

1

Analysis

- Each augmenting path increases the flow value by

at least 1. - Let f is a maximum flow, then in the worst

case, Ford-Fulkersons algorithm performs f

flow augmentations. - Finding an augmenting path and augmenting the

flow takes O(n m) time - The running time of Ford-Fulkersons algorithm is

O(f(n m))

Analysis

v

50

50

Classic worst case example

s

1

t

50

50

u

Flow Graph (Gf)

Residual Graph (Gr)

v

v

50

49

1/50

0/50

-1

s

s

1/1

t

t

-1

-1

49

50

1/50

0/50

u

u

Analysis

v

50

50

Classic worst case example

s

1

t

50

50

u

Flow Graph (Gf)

Residual Graph (Gr)

v

v

49

49

1/50

1/50

-1

-1

s

s

1

0/1

t

t

-1

-1

49

49

1/50

1/50

u

u