chapter 8 dynamic programming copyright © 2007 pearson addison-wesley. all rights reserved

92
Chapter 8 Chapter 8 Dynamic Dynamic Programming Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved.

Upload: phyllis-craig

Post on 13-Dec-2015

229 views

Category:

Documents


6 download

TRANSCRIPT

Page 1: Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved

Chapter 8Chapter 8

Dynamic ProgrammingDynamic Programming

Copyright © 2007 Pearson Addison-Wesley. All rights reserved.

Page 2: Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved

Dynamic Programming

Dynamic Programming is a general algorithm design technique for solving problems defined by or formulated as recurrences with overlapping subinstances

• Invented by American mathematician Richard Bellman in the 1950s to solve optimization problems and later assimilated by CS

• “Programming” here means “planning”

• Main idea:- set up a recurrence relating a solution to a larger instance to

solutions of some smaller instances- solve smaller instances once- record solutions in a table - extract solution to the initial instance from that table

Page 3: Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved

Examples of DP algorithms• Fibonacci numbers

• Longest common subsequence

• Spanning tree

• Warshall’s algorithm for transitive closure

• Floyd’s algorithm for all-pairs shortest paths

• Constructing an optimal binary search tree

• Some instances of difficult discrete optimization problems: - traveling salesman - - knapsack

Page 4: Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved

A spanning tree of a graph is just a subgraph that contains all the vertices and is a tree.

A graph may have many spanning trees.

or or or

Some Spanning Trees from Graph AGraph A

Spanning Trees

Page 5: Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved

All 16 of its Spanning TreesComplete Graph

Page 6: Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved

Minimum Spanning Trees

The Minimum Spanning Tree for a given graph is the Spanning Tree of minimum cost for that graph.

5

7

2

1

3

4

2

1

3

Complete Graph Minimum Spanning Tree

Page 7: Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved

Algorithms for Obtaining the Minimum Spanning Tree

• Kruskal's Algorithm

• Prim's Algorithm

• Boruvka's Algorithm

Page 8: Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved

Kruskal's Algorithm

This algorithm creates a forest of trees. Initially the forest consists of n

single node trees (and no edges). At each step, we add one edge (the

cheapest one) so that it joins two trees together. If it were to form a

cycle, it would simply link two nodes that were already part of a single

connected tree, so that this edge would not be needed.

Page 9: Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved

The steps are:

1. The forest is constructed - with each node in a separate tree. 2. The edges are placed in a priority queue. 3. Until we've added n-1 edges, 1. Extract the cheapest edge from the queue, 2. If it forms a cycle, reject it, 3. Else add it to the forest. Adding it to the forest will join two trees together.

Every step will have joined two trees in the forest together, so that at the end, there will only be one tree in T.

Page 10: Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved

4

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

Complete Graph

Page 11: Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved

1

4

2

5

2

5

4

3

4

4

4

10

1

6

3

3

2

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

A AB D

B B

B

C D

J C

C

E

F

D

D H

J E G

F FG I

G GI J

H J JI

Page 12: Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved

2

5

2

5

4

3

4

4

10

1

6

3

3

2

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

B

B

D

J

C

C

E

F

D

D H

J

E G

F

F

G

I

G

G

I

J

H J

JI

1A D

4B C

4A B

Sort Edges

(in reality they are placed in a priority queue - not sorted - but sorting them

makes the algorithm easier to visualize)

Page 13: Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved

2

5

2

5

4

3

4

4

10

1

6

3

3

2

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

B

B

D

J

C

C

E

F

D

D H

J

E G

F

F

G

I

G

G

I

J

H J

JI

1A D

4B C

4A B

Add Edge

Page 14: Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved

2

5

2

5

4

3

4

4

10

1

6

3

3

2

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

B

B

D

J

C

C

E

F

D

D H

J

E G

F

F

G

I

G

G

I

J

H J

JI

1A D

4B C

4A B

Add Edge

Page 15: Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved

2

5

2

5

4

3

4

4

10

1

6

3

3

2

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

B

B

D

J

C

C

E

F

D

D H

J

E G

F

F

G

I

G

G

I

J

H J

JI

1A D

4B C

4A B

Add Edge

Page 16: Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved

2

5

2

5

4

3

4

4

10

1

6

3

3

2

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

B

B

D

J

C

C

E

F

D

D H

J

E G

F

F

G

I

G

G

I

J

H J

JI

1A D

4B C

4A B

Add Edge

Page 17: Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved

2

5

2

5

4

3

4

4

10

1

6

3

3

2

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

B

B

D

J

C

C

E

F

D

D H

J

E G

F

F

G

I

G

G

I

J

H J

JI

1A D

4B C

4A B

Add Edge

Page 18: Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved

2

5

2

5

4

3

4

4

10

1

6

3

3

2

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

B

B

D

J

C

C

E

F

D

D H

J

E G

F

F

G

I

G

G

I

J

H J

JI

1A D

4B C

4A B

Cycle

Don’t Add Edge

Page 19: Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved

2

5

2

5

4

3

4

4

10

1

6

3

3

2

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

B

B

D

J

C

C

E

F

D

D H

J

E G

F

F

G

I

G

G

I

J

H J

JI

1A D

4B C

4A B

Add Edge

Page 20: Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved

2

5

2

5

4

3

4

4

10

1

6

3

3

2

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

B

B

D

J

C

C

E

F

D

D H

J

E G

F

F

G

I

G

G

I

J

H J

JI

1A D

4B C

4A B

Add Edge

Page 21: Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved

2

5

2

5

4

3

4

4

10

1

6

3

3

2

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

B

B

D

J

C

C

E

F

D

D H

J

E G

F

F

G

I

G

G

I

J

H J

JI

1A D

4B C

4A B

Add Edge

Page 22: Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved

2

5

2

5

4

3

4

4

10

1

6

3

3

2

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

B

B

D

J

C

C

E

F

D

D H

J

E G

F

F

G

I

G

G

I

J

H J

JI

1A D

4B C

4A B

Cycle

Don’t Add Edge

Page 23: Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved

2

5

2

5

4

3

4

4

10

1

6

3

3

2

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

B

B

D

J

C

C

E

F

D

D H

J

E G

F

F

G

I

G

G

I

J

H J

JI

1A D

4B C

4A B

Add Edge

Page 24: Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved

4

1

2

2 1

3

32

4

A

B C

D

E F

G

HI

J

4

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

Minimum Spanning Tree Complete Graph

Page 25: Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved

Analysis of Kruskal's Algorithm

Your First Part of Assignment:

Can the Running Time of Kruskal's Algorithm be O(m log n) where (m = edges, n = nodes)? Why and why not?

Testing if an edge creates a cycle can be slow unless a complicated data structure called a “union-find” data structure is used? Show how such data structure can be used to efficiently implement Kruskal's Algorithm ?

Page 26: Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved

Prim's Algorithm

This algorithm starts with one node. It then, one by one, adds a node that

is unconnected to the new graph, each time selecting the node whose

connecting edge has the smallest weight out of the available nodes’

connecting edges.

Page 27: Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved

The steps are:

1. The new graph is constructed - with one node from the old graph. 2. While new graph has fewer than n nodes, 1. Find the node from the old graph with the smallest connecting edge to the new graph, 2. Add it to the new graph

Every step will have joined one node, so that at the end we will have one graph with all the nodes and it will be a minimum spanning tree of the original graph.

Page 28: Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved

4

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

Complete Graph

Page 29: Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved

4

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

Old Graph New Graph

4

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

Page 30: Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved

4

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

Old Graph New Graph

4

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

Page 31: Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved

4

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

Old Graph New Graph

4

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

Page 32: Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved

4

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

Old Graph New Graph

4

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

Page 33: Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved

4

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

Old Graph New Graph

4

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

Page 34: Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved

4

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

Old Graph New Graph

4

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

Page 35: Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved

4

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

Old Graph New Graph

4

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

Page 36: Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved

4

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

Old Graph New Graph

4

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

Page 37: Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved

4

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

Old Graph New Graph

4

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

Page 38: Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved

4

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

Old Graph New Graph

4

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

Page 39: Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved

4

1

2

2 1

3

32

4

A

B C

D

E F

G

HI

J

Complete Graph Minimum Spanning Tree

4

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

Page 40: Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved

Analysis of Prim's Algorithm

•Unlike Kruskal’s, it doesn’t need to see all of the graph at once. It can deal with it one piece at a time.

• It also doesn’t need to worry if adding an edge will create a cycle since this algorithm deals primarily with the nodes, and not the edges.

•For this algorithm the number of nodes needs to be kept to a minimum in addition to the number of edges.

Page 41: Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved

Analysis of Prim's Algorithm

Your Second Part of Assignment:

What is the Running Time of Prim's Algorithm? When it can be O(m + n log n) where m = edges, n = nodes and when it could be O(n^2)?

Show in details in both cases , with example, the data structure can be used to reach the desired complexity?

Page 42: Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved

Boruvka's Algorithm

This algorithm is similar to Prim’s, but nodes are added to the new graph

in parallel all around the graph. It creates a list of trees, each containing

one node from the original graph and proceeds to merge them along the

smallest-weight connecting edges until there’s only one tree, which is, of

course, the MST. It works rather like a merge sort.

Page 43: Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved

The steps are:

1. Make a list of n trees, each containing a single node 2. While list has more than one tree, 1. For each tree in the list, find the node not connected to the tree with the smallest connecting edge to that tree, 2. Add all the edges found to the new graph, thus creating a new set of trees

Every step will have joined groups of trees, until only one tree remains.

Page 44: Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved

4

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

Complete Graph

Page 45: Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved

• A• B• C• D• E• F• G• H

• I• J

4

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

Trees of the Graph at Beginning of Round 1

List of Trees

Page 46: Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved

4

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

Round 1

1

4

A

B

D

Tree A

Page 47: Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved

4

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

Round 1

1

4

A

B

D

Edge A-D

Page 48: Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved

4

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

4

4

4

10

A

B C

D

J

Round 1 Tree B

Page 49: Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved

4

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

4

4

4

10

A

B C

D

J

Round 1 Edge B-A

Page 50: Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved

4

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

Round 1

4

2 1

B C

E F

Tree C

Page 51: Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved

4

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

Round 1

4

2 1

B C

E F

Edge C-F

Page 52: Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved

4

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

Round 1

1

5 6

4A

B

D

H

J

Tree D

Page 53: Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved

4

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

Round 1

1

5 6

4A

B

D

H

J

Edge D-A

Page 54: Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved

4

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

Round 1

2

2

C

E

G

Tree E

Page 55: Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved

4

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

Round 1

2

2

C

E

G

Edge E-C

Page 56: Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved

4

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

Round 1

3

1

5

C

F

G

I

Tree F

Page 57: Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved

4

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

Round 1

3

1

5

C

F

G

I

Edge F-C

Page 58: Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved

4

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

Round 1

2 3

3

4

E F

G

I

J

Tree G

Page 59: Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved

4

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

Round 1

2 3

3

4

E F

G

I

J

Edge G-E

Page 60: Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved

4

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

Round 1

2

5

D

H

J

Tree H

Page 61: Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved

4

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

Round 1

2

5

D

H

J

Edge H-J

Page 62: Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved

4

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

Round 1

3

5

3

F

G

I

J

Tree I

Page 63: Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved

4

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

Round 1

3

5

3

F

G

I

J

Edge I-G

Page 64: Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved

4

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

Round 1

3

4

2

6

10

B

D

G

HI

J

Tree J

Page 65: Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved

4

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

Round 1

3

4

2

6

10

B

D

G

HI

J

Edge J-H

Page 66: Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved

• A-D• B-A• C-F• D-A• E-C• F-C• G-E• H-J

• I-G• J-H

4

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

Round 1 Ends - Add Edges

List of Edges to Add

Page 67: Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved

• D-A-B

• F-C-E-G-I

• H-J

List of Trees

4

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

Trees of the Graph at Beginning of Round 2

Page 68: Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved

4

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

Round 2

4

1

2

2 1

3

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

Tree D-A-B

Page 69: Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved

4

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

Round 2

4

1

2

2 1

3

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

Edge B-C

Page 70: Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved

4

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

Round 2 Tree F-C-E-G-I

4

1

2 3

2 1

3

5

3

4

2

4

A

B C

D

E F

G

HI

J

Page 71: Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved

4

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

Round 2 Edge I-J

4

1

2 3

2 1

3

5

3

4

2

4

A

B C

D

E F

G

HI

J

Page 72: Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved

1

2

2 1

3

3

4

2

5 6

4

10

A

B C

D

E F

G

HI

J

4

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

Round 2 Tree H-J

Page 73: Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved

1

2

2 1

3

3

4

2

5 6

4

10

A

B C

D

E F

G

HI

J

4

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

Round 2 Edge J-I

Page 74: Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved

• B-C• I-J• J-I

4

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

List of Edges to Add

Round 2 Ends - Add Edges

Page 75: Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved

4

1

2

2 1

3

32

4

A

B C

D

E F

G

HI

J

4

1

2 3

2 1

3

5

3

4

2

5 6

4

4

10

A

B C

D

E F

G

HI

J

Minimum Spanning Tree Complete Graph

Page 76: Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved

Analysis of Boruvka's Algorithm

Although this algorithm is difficult to explain, unlike the two preceding algorithms, it does not require a complicated data structure.

Like Prim’s, it does not need to worry about detecting cycles. It does, however, need to see the whole graph, but it only examines pieces of it at a time, not all of it at once.

Like Kruskal’s it is best if edges are kept to a minimum (though it doesn’t hurt to keep the nodes to a minimum as well).

Page 77: Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved

Analysis of Boruvka's Algorithm

Your Third Part of Assignment:

What is the Running Time of Boruvka's Algorithm? Why?

Page 78: Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved

The Problem: Transitive Closure

B

A

D

C

E

G

• Given a graph G, Compute the Reachability Degree of each node ?

•Reachability degree means :the number of reachable node from each and single node.

•Given a transportation network G, Is there any path from station A to station E?

Page 79: Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved

Directed Graphs

The Problem: Transitive Closure

Given a digraph G, the transitive closure of G is the digraph G* such that:– G* has the same vertices as

G– if G has a directed path

from u to v (u v), G* has a directed edge from u to v

B

A

D

C

E

B

A

D

C

E

G

G*

The transitive closure provides reachability information about a digraph

Page 80: Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved

Think of a method to do so?

Page 81: Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved

Directed Graphs 81

Computing the Transitive Closure

We can perform DFS starting at each vertex– O(n(n+m))

If there's a way to get from A to B and from B to C, then there's a way to get from A to C.

Alternatively ... Use dynamic programming: the Floyd-Warshall Algorithm

Page 82: Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved

Directed Graphs 82

Floyd-Warshall Transitive Closure

Idea #1: Number the vertices 1, 2, …, n. Idea #2: Consider paths that use only vertices

numbered 1, 2, …, k, as intermediate vertices:

k

j

i

Uses only verticesnumbered 1,…,k-1 Uses only vertices

numbered 1,…,k-1

Uses only vertices numbered 1,…,k(add this edge if it’s not already in)

Page 83: Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved

Directed Graphs 83

Floyd-Warshall Example

JFK

BOS

MIA

ORD

LAXDFW

SFO

v2

v1v3

v4

v5

v6

v7

Page 84: Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved

Directed Graphs 84

Floyd-Warshall, Iteration 1

JFK

BOS

MIA

ORD

LAXDFW

SFO

v2

v1v3

v4

v5

v6

v7e.g v5 is connected to v1 and v1 is connected to v4 ,then connect v5 to v4

Page 85: Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved

Directed Graphs 85

Floyd-Warshall, Iteration 1

JFK

BOS

MIA

ORD

LAXDFW

SFO

v2

v1v3

v4

v5

v6

v7In a paper complete such transitive closure graph and show it to me ?

Page 86: Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved

Directed Graphs 86

Floyd-Warshall, Iteration 2

JFK

BOS

MIA

ORD

LAXDFW

SFO

v2

v1v3

v4

v5

v6

v7

Page 87: Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved

Directed Graphs 87

Floyd-Warshall, Iteration 3

JFK

BOS

MIA

ORD

LAXDFW

SFO

v2

v1v3

v4

v5

v6

v7

Page 88: Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved

Directed Graphs 88

Floyd-Warshall, Iteration 4

JFK

BOS

MIA

ORD

LAXDFW

SFO

v2

v1v3

v4

v5

v6

v7

Page 89: Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved

Directed Graphs 89

Floyd-Warshall, Iteration 5

JFK

MIA

ORD

LAXDFW

SFO

v2

v1v3

v4

v5

v6

v7

BOS

Page 90: Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved

Directed Graphs 90

Floyd-Warshall, Iteration 6

JFK

MIA

ORD

LAXDFW

SFO

v2

v1v3

v4

v5

v6

v7

BOS

Page 91: Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved

Directed Graphs 91

Floyd-Warshall, Conclusion

JFK

MIA

ORD

LAXDFW

SFO

v2

v1v3

v4

v5

v6

v7

BOS

Page 92: Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved

• Part four of your assignment :

• This is the pseudo code for Floyd-Warshall algorithm . In a step by step apply it on the previous graph and report its complexity?

Algorithm FloydWarshall(G)Input digraph GOutput transitive closure G* of Gi 1for all v G.vertices()

denote v as vi

i i + 1G0 Gfor k 1 to n do

Gk Gk 1

for i 1 to n (i k) dofor j 1 to n (j i, k) do

if Gk 1.areAdjacent(vi, vk) Gk 1.areAdjacent(vk,

vj)

if Gk.areAdjacent(vi, vj)

Gk.insertDirectedEdge(vi, vj , k)return Gn