Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved."— Presentation transcript:

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

2 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

3 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

4 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 Some Spanning Trees from Graph AGraph A Spanning Trees

5 All 16 of its Spanning TreesComplete Graph

6 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 GraphMinimum Spanning Tree

7 Algorithms for Obtaining the Minimum Spanning Tree Kruskal's Algorithm Prim's Algorithm Boruvka's Algorithm

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

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

10 4 1 2 3 2 1 3 5 3 4 2 56 4 4 10 A BC D E F G H I J Complete Graph

11 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 56 4 4 A BC D E F G H I J AABD BB B CD JC C E F D DH JEG FFGI GGIJ HJJI

12 2 5 2 5 4 3 4 4 1 6 3 3 2 1 2 3 2 1 3 5 3 4 2 56 4 4 A BC D E F G H I J B B D J C C E F D DH J EG F F G I G G I J HJ JI 1 AD 4 BC 4 AB Sort Edges (in reality they are placed in a priority queue - not sorted - but sorting them makes the algorithm easier to visualize)

13 2 5 2 5 4 3 4 4 10 1 6 3 3 2 1 2 3 2 1 3 5 3 4 2 56 4 4 A BC D E F G H I J B B D J C C E F D DH J EG F F G I G G I J HJ JI 1 AD 4 BC 4 AB Add Edge

14 2 5 2 5 4 3 4 4 10 1 6 3 3 2 1 2 3 2 1 3 5 3 4 2 56 4 4 A BC D E F G H I J B B D J C C E F D DH J EG F F G I G G I J HJ JI 1 AD 4 BC 4 AB Add Edge

15 2 5 2 5 4 3 4 4 10 1 6 3 3 2 1 2 3 2 1 3 5 3 4 2 56 4 4 A BC D E F G H I J B B D J C C E F D DH J EG F F G I G G I J HJ JI 1 AD 4 BC 4 AB Add Edge

16 2 5 2 5 4 3 4 4 10 1 6 3 3 2 1 2 3 2 1 3 5 3 4 2 56 4 4 A BC D E F G H I J B B D J C C E F D DH J EG F F G I G G I J HJ JI 1 AD 4 BC 4 AB Add Edge

17 2 5 2 5 4 3 4 4 10 1 6 3 3 2 1 2 3 2 1 3 5 3 4 2 56 4 4 A BC D E F G H I J B B D J C C E F D DH J EG F F G I G G I J HJ JI 1 AD 4 BC 4 AB Add Edge

18 2 5 2 5 4 3 4 4 10 1 6 3 3 2 1 2 3 2 1 3 5 3 4 2 56 4 4 A BC D E F G H I J B B D J C C E F D DH J EG F F G I G G I J HJ JI 1 AD 4 BC 4 AB Cycle Don’t Add Edge

19 2 5 2 5 4 3 4 4 10 1 6 3 3 2 1 2 3 2 1 3 5 3 4 2 56 4 4 A BC D E F G H I J B B D J C C E F D DH J EG F F G I G G I J HJ JI 1 AD 4 BC 4 AB Add Edge

20 2 5 2 5 4 3 4 4 10 1 6 3 3 2 1 2 3 2 1 3 5 3 4 2 56 4 4 A BC D E F G H I J B B D J C C E F D DH J EG F F G I G G I J HJ JI 1 AD 4 BC 4 AB Add Edge

21 2 5 2 5 4 3 4 4 10 1 6 3 3 2 1 2 3 2 1 3 5 3 4 2 56 4 4 A BC D E F G H I J B B D J C C E F D DH J EG F F G I G G I J HJ JI 1 AD 4 BC 4 AB Add Edge

22 2 5 2 5 4 3 4 4 10 1 6 3 3 2 1 2 3 2 1 3 5 3 4 2 56 4 4 A BC D E F G H I J B B D J C C E F D DH J EG F F G I G G I J HJ JI 1 AD 4 BC 4 AB Cycle Don’t Add Edge

23 2 5 2 5 4 3 4 4 10 1 6 3 3 2 1 2 3 2 1 3 5 3 4 2 56 4 4 A BC D E F G H I J B B D J C C E F D DH J EG F F G I G G I J HJ JI 1 AD 4 BC 4 AB Add Edge

24 4 1 2 2 1 3 3 2 4 A BC D E F G H I J 4 1 2 3 2 1 3 5 3 4 2 56 4 4 10 A BC D E F G H I J Minimum Spanning TreeComplete Graph

25 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 ?

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

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

28 4 1 2 3 2 1 3 5 3 4 2 56 4 4 10 A BC D E F G H I J Complete Graph

29 4 1 2 3 2 1 3 5 3 4 2 56 4 4 10 A BC D E F G H I J Old GraphNew Graph 4 1 2 3 2 1 3 5 3 4 2 56 4 4 10 A BC D E F G H I J

30 4 1 2 3 2 1 3 5 3 4 2 56 4 4 A BC D E F G H I J Old GraphNew Graph 4 1 2 3 2 1 3 5 3 4 2 56 4 4 10 A BC D E F G H I J

31 4 1 2 3 2 1 3 5 3 4 2 56 4 4 A BC D E F G H I J Old GraphNew Graph 4 1 2 3 2 1 3 5 3 4 2 56 4 4 10 A BC D E F G H I J

32 4 1 2 3 2 1 3 5 3 4 2 56 4 4 A BC D E F G H I J Old GraphNew Graph 4 1 2 3 2 1 3 5 3 4 2 56 4 4 10 A BC D E F G H I J

33 4 1 2 3 2 1 3 5 3 4 2 56 4 4 A BC D E F G H I J Old GraphNew Graph 4 1 2 3 2 1 3 5 3 4 2 56 4 4 10 A BC D E F G H I J

34 4 1 2 3 2 1 3 5 3 4 2 56 4 4 A BC D E F G H I J Old GraphNew Graph 4 1 2 3 2 1 3 5 3 4 2 56 4 4 10 A BC D E F G H I J

35 4 1 2 3 2 1 3 5 3 4 2 56 4 4 A BC D E F G H I J Old GraphNew Graph 4 1 2 3 2 1 3 5 3 4 2 56 4 4 10 A BC D E F G H I J

36 4 1 2 3 2 1 3 5 3 4 2 56 4 4 A BC D E F G H I J Old GraphNew Graph 4 1 2 3 2 1 3 5 3 4 2 56 4 4 10 A BC D E F G H I J

37 4 1 2 3 2 1 3 5 3 4 2 56 4 4 A BC D E F G H I J Old GraphNew Graph 4 1 2 3 2 1 3 5 3 4 2 56 4 4 10 A BC D E F G H I J

38 4 1 2 3 2 1 3 5 3 4 2 56 4 4 A BC D E F G H I J Old GraphNew Graph 4 1 2 3 2 1 3 5 3 4 2 56 4 4 10 A BC D E F G H I J

39 4 1 2 2 1 3 3 2 4 A BC D E F G H I J Complete GraphMinimum Spanning Tree 4 1 2 3 2 1 3 5 3 4 2 56 4 4 10 A BC D E F G H I J

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

41 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?

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

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

44 4 1 2 3 2 1 3 5 3 4 2 56 4 4 10 A BC D E F G H I J Complete Graph

45 A B C D E F G H I J 4 1 2 3 2 1 3 5 3 4 2 56 4 4 10 A BC D E F G H I J Trees of the Graph at Beginning of Round 1 List of Trees

46 4 1 2 3 2 1 3 5 3 4 2 56 4 4 10 A BC D E F G H I J Round 1 1 4 A B D Tree A

47 4 1 2 3 2 1 3 5 3 4 2 56 4 4 10 A BC D E F G H I J Round 1 1 4 A B D Edge A-D

48 4 1 2 3 2 1 3 5 3 4 2 56 4 4 10 A BC D E F G H I J 4 4 4 A BC D J Round 1Tree B

49 4 1 2 3 2 1 3 5 3 4 2 56 4 4 10 A BC D E F G H I J 4 4 4 A BC D J Round 1Edge B-A

50 4 1 2 3 2 1 3 5 3 4 2 56 4 4 10 A BC D E F G H I J Round 1 4 2 1 BC E F Tree C

51 4 1 2 3 2 1 3 5 3 4 2 56 4 4 10 A BC D E F G H I J Round 1 4 2 1 BC E F Edge C-F

52 4 1 2 3 2 1 3 5 3 4 2 56 4 4 10 A BC D E F G H I J Round 1 1 56 4A B D H J Tree D

53 4 1 2 3 2 1 3 5 3 4 2 56 4 4 10 A BC D E F G H I J Round 1 1 56 4A B D H J Edge D-A

54 4 1 2 3 2 1 3 5 3 4 2 56 4 4 10 A BC D E F G H I J Round 1 2 2 C E G Tree E

55 4 1 2 3 2 1 3 5 3 4 2 56 4 4 10 A BC D E F G H I J Round 1 2 2 C E G Edge E-C

56 4 1 2 3 2 1 3 5 3 4 2 56 4 4 10 A BC D E F G H I J Round 1 3 1 5 C F G I Tree F

57 4 1 2 3 2 1 3 5 3 4 2 56 4 4 10 A BC D E F G H I J Round 1 3 1 5 C F G I Edge F-C

58 4 1 2 3 2 1 3 5 3 4 2 56 4 4 10 A BC D E F G H I J Round 1 2 3 3 4 E F G I J Tree G

59 4 1 2 3 2 1 3 5 3 4 2 56 4 4 10 A BC D E F G H I J Round 1 2 3 3 4 E F G I J Edge G-E

60 4 1 2 3 2 1 3 5 3 4 2 56 4 4 10 A BC D E F G H I J Round 1 2 5 D H J Tree H

61 4 1 2 3 2 1 3 5 3 4 2 56 4 4 10 A BC D E F G H I J Round 1 2 5 D H J Edge H-J

62 4 1 2 3 2 1 3 5 3 4 2 56 4 4 10 A BC D E F G H I J Round 1 3 5 3 F G I J Tree I

63 4 1 2 3 2 1 3 5 3 4 2 56 4 4 10 A BC D E F G H I J Round 1 3 5 3 F G I J Edge I-G

64 4 1 2 3 2 1 3 5 3 4 2 56 4 4 10 A BC D E F G H I J Round 1 3 4 2 6 10 B D G H I J Tree J

65 4 1 2 3 2 1 3 5 3 4 2 56 4 4 10 A BC D E F G H I J Round 1 3 4 2 6 10 B D G H I J Edge J-H

66 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 56 4 4 10 A BC D E F G H I J Round 1 Ends - Add Edges List of Edges to Add

67 D-A-B F-C-E-G-I H-J List of Trees 4 1 2 3 2 1 3 5 3 4 2 56 4 4 10 A BC D E F G H I J Trees of the Graph at Beginning of Round 2

68 4 1 2 3 2 1 3 5 3 4 2 56 4 4 10 A BC D E F G H I J Round 2 4 1 2 2 1 3 2 56 4 4 10 A BC D E F G H I J Tree D-A-B

69 4 1 2 3 2 1 3 5 3 4 2 56 4 4 10 A BC D E F G H I J Round 2 4 1 2 2 1 3 2 56 4 4 10 A BC D E F G H I J Edge B-C

70 4 1 2 3 2 1 3 5 3 4 2 56 4 4 10 A BC D E F G H I J Round 2Tree F-C-E-G-I 4 1 2 3 2 1 3 5 3 4 2 4 A BC D E F G H I J

71 4 1 2 3 2 1 3 5 3 4 2 56 4 4 10 A BC D E F G H I J Round 2Edge I-J 4 1 2 3 2 1 3 5 3 4 2 4 A BC D E F G H I J

72 1 2 2 1 3 3 4 2 56 4 10 A BC D E F G H I J 4 1 2 3 2 1 3 5 3 4 2 56 4 4 A BC D E F G H I J Round 2Tree H-J

73 1 2 2 1 3 3 4 2 56 4 10 A BC D E F G H I J 4 1 2 3 2 1 3 5 3 4 2 56 4 4 A BC D E F G H I J Round 2Edge J-I

74 B-C I-J J-I 4 1 2 3 2 1 3 5 3 4 2 56 4 4 10 A BC D E F G H I J List of Edges to Add Round 2 Ends - Add Edges

75 4 1 2 2 1 3 3 2 4 A BC D E F G H I J 4 1 2 3 2 1 3 5 3 4 2 56 4 4 10 A BC D E F G H I J Minimum Spanning TreeComplete Graph

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

77 Analysis of Boruvka's Algorithm Your Third Part of Assignment: What is the Running Time of Boruvka's Algorithm? Why?

78 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?

79 Directed Graphs The Problem: Transitive Closure bGiven 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* bThe transitive closure provides reachability information about a digraph

80 Think of a method to do so?

81 Directed Graphs81 Computing the Transitive Closure bWe 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

82 Directed Graphs82 Floyd-Warshall Transitive Closure bIdea #1: Number the vertices 1, 2, …, n. bIdea #2: Consider paths that use only vertices numbered 1, 2, …, k, as intermediate vertices: k j i Uses only vertices numbered 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)

83 Directed Graphs83 Floyd-Warshall Example JFK BOS MIA ORD LAX DFW SFO v 2 v 1 v 3 v 4 v 5 v 6

84 Directed Graphs84 Floyd-Warshall, Iteration 1 JFK BOS MIA ORD LAX DFW SFO v 2 v 1 v 3 v 4 v 5 v 6 e.g v5 is connected to v1 and v1 is connected to v4,then connect v5 to v4

85 Directed Graphs85 Floyd-Warshall, Iteration 1 JFK BOS MIA ORD LAX DFW SFO v 2 v 1 v 3 v 4 v 5 v 6 In a paper complete such transitive closure graph and show it to me ?

86 Directed Graphs86 Floyd-Warshall, Iteration 2 JFK BOS MIA ORD LAX DFW SFO v 2 v 1 v 3 v 4 v 5 v 6

87 Directed Graphs87 Floyd-Warshall, Iteration 3 JFK BOS MIA ORD LAX DFW SFO v 2 v 1 v 3 v 4 v 5 v 6

88 Directed Graphs88 Floyd-Warshall, Iteration 4 JFK BOS MIA ORD LAX DFW SFO v 2 v 1 v 3 v 4 v 5 v 6

89 Directed Graphs89 Floyd-Warshall, Iteration 5 JFK MIA ORD LAX DFW SFO v 2 v 1 v 3 v 4 v 5 v 6 BOS

90 Directed Graphs90 Floyd-Warshall, Iteration 6 JFK MIA ORD LAX DFW SFO v 2 v 1 v 3 v 4 v 5 v 6 BOS

91 Directed Graphs91 Floyd-Warshall, Conclusion JFK MIA ORD LAX DFW SFO v 2 v 1 v 3 v 4 v 5 v 6 BOS

92 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 G Output transitive closure G* of G i  1 for all v  G.vertices() denote v as v i i  i + 1 G 0  G for k  1 to n do G k  G k  1 for i  1 to n (i  k) do for j  1 to n (j  i, k) do if G k  1.areAdjacent(v i, v k )  G k  1.areAdjacent(v k, v j ) if  G k.areAdjacent(v i, v j ) G k.insertDirectedEdge(v i, v j, k) return G n


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

Similar presentations


Ads by Google