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

Preview:

Citation preview

Chapter 8Chapter 8

Dynamic ProgrammingDynamic 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

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

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

All 16 of its Spanning TreesComplete Graph

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

Algorithms for Obtaining the Minimum Spanning Tree

• Kruskal's Algorithm

• Prim's Algorithm

• Boruvka's Algorithm

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.

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.

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

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

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)

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

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

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

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

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

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

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

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

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

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

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

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

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 ?

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.

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.

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

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

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

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

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

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

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

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

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

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

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

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

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.

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?

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.

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.

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

• 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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

• 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

• 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

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

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

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

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

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

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

• 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

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

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).

Analysis of Boruvka's Algorithm

Your Third Part of Assignment:

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

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?

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

Think of a method to do so?

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

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)

Directed Graphs 83

Floyd-Warshall Example

JFK

BOS

MIA

ORD

LAXDFW

SFO

v2

v1v3

v4

v5

v6

v7

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

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 ?

Directed Graphs 86

Floyd-Warshall, Iteration 2

JFK

BOS

MIA

ORD

LAXDFW

SFO

v2

v1v3

v4

v5

v6

v7

Directed Graphs 87

Floyd-Warshall, Iteration 3

JFK

BOS

MIA

ORD

LAXDFW

SFO

v2

v1v3

v4

v5

v6

v7

Directed Graphs 88

Floyd-Warshall, Iteration 4

JFK

BOS

MIA

ORD

LAXDFW

SFO

v2

v1v3

v4

v5

v6

v7

Directed Graphs 89

Floyd-Warshall, Iteration 5

JFK

MIA

ORD

LAXDFW

SFO

v2

v1v3

v4

v5

v6

v7

BOS

Directed Graphs 90

Floyd-Warshall, Iteration 6

JFK

MIA

ORD

LAXDFW

SFO

v2

v1v3

v4

v5

v6

v7

BOS

Directed Graphs 91

Floyd-Warshall, Conclusion

JFK

MIA

ORD

LAXDFW

SFO

v2

v1v3

v4

v5

v6

v7

BOS

• 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

Recommended