1.33k likes | 1.34k Views
Chapter 07. Greedy Algorithms. Chapter 06 Greedy Technique [Levitin: 315-344; Cormen : 414-450, 631-638, 658-664] Let revisit the change-making problem by giving change for a specific amount n with the least number of coins of the denominations d 1 > d 2 > … > d m .
E N D
Chapter 07 Greedy Algorithms
Chapter 06 Greedy Technique [Levitin: 315-344; Cormen: 414-450, 631-638, 658-664] • Let revisit the change-making problem by giving change for a specific amount n with the least number of coins of the denominations d1 > d2 > … > dm . • For example, the widely used coin denominations in United States are d1 = 25 (quarter), d2 = = 10 (dime), d3 = 5 (nickel), and d4 = 1 (penny). • How would you give change with coins of these denominations of, say, 48 cents? • “Greedy” thinking leads to give one quarter because it reduces the remaining amount the most, namely, to 23 cents. • In the second step, you had the same coins at your disposal, but you could not give a quarter, because it would have violated the problem’s constraints. • So your best selection in this step was one dime, reducing the remaining amount to 13 cents. • Giving one more dime left you with 3 cents to be given with three pennies. • You came up with the answer 1 quarter, 2 dimes and 3 pennies.
Chapter 06 Greedy Technique • Let revisit the change-making problem by giving change for a specific amount n with the least number of coins of the denominations d1 > d2 > … > dm . • For example, the widely used coin denominations in United States are d1 = 25 (quarter), d2 = = 10 (dime), d3 = 5 (nickel), and d4 = 1 (penny). • Using Greedy Technique, how would you give change with coins of these denominations of, say, 48 cents? • Using 1Q first, 48 – 25 = 23 • Using 1D, 23 – 10 = 13 • Using 1D, 13 – 10 = 3 • Using 1P, 3 – 1 = 2 • Using 1P, 2 – 1 = 1 • Using 1P, 1 – 1 = 0 • Conclusion: Using 1Q, 2D and 3P (6 items.)
Chapter 06 Greedy Technique • Let revisit the change-making problem by giving change for a specific amount n with the least number of coins of the denominations d1 > d2 > … > dm . • For example, the widely used coin denominations in United States are d1 = 25 (quarter), d2 = = 10 (dime), d3 = 5 (nickel), and d4 = 1 (penny). • Using dynamic programming, how would you give change with coins of these • denominations of, say, 48 cents? • Using dynamic programming, apply the following formula: • F(n) = minj: n ≥dj { F(n – dj ) } + 1 for n > 0 …………… 8.4 • F(0) = 0. • The solution is F(48) = min{F(48-1), F(48-5), F(48-10), F(48-25)} + 1 • = F(47) + 1 or F(38) + 1 or F(23) + 1 • = {{1Q, 2 D, 2 P} + {1P}} or {{1Q, 1D, 3P} + {1D}} or • {{2D, 3P} + {1Q}} = {1Q, 2D, 3P}
In previous, is this solution to the instance of the change-making problem optimal? • Yes, it is. In fact, once can prove that the greedy algorithm yields an optimal solution for every positive integer amount with these coins denominations. • However, it is easy to give an example of coin denominations that the greedy algorithm does not yield an optimal solution for some amounts: e.g., d1 = 25 (quarter), d2 = = 10 (dime), and d3 = 1 (penny) • How would you give change with coins of these denominations of, say, n = 30? • Using Greedy thinking, it leads to 1 quarter and 5 pennies. But the optimal solution is 3 dimes. • Using the Greedy Technique, it yields • Using 1Q, 30 – 25 = 5 • Using 1P, 5 – 1 = 4 • Using 1P, 4 – 1 = 3 • Using 1P, 3 – 1 = 2 • Using 1P, 2 – 1 = 1 • Using 1P, 1 – 1 = 0
Chapter 06 Greedy Technique • Let revisit the change-making problem by giving change for a specific amount n with the least number of coins of the denominations d1 > d2 > … > dm . • This is an example of coin denominations that the greedy algorithm does not yield an optimal solution for some amounts: e.g., d1 = 25 (quarter), d2 = = 10 (dime), and d3 = 1 (penny) • Then, using dynamic programming, how would you give change with coins of these denominations of, say, n = 30 cents? • Using dynamic programming, apply the following formula: • F(n) = minj: n ≥dj { F(n – dj ) } + 1 for n > 0 …………… 8.4 • F(0) = 0. • The solution is F(30) = min{F(30-1), F(30-10), F(30-25)} + 1 • = min{F(29) , F(20) , F(5) }+ 1 • = min{{1Q, 4P}, {2D}, {5P}} + 1 ={2D} + {1D}
Is this solution to the instance of the change-making problem optimal? • Yes, it is. In fact, once can prove that the greedy algorithm yields an optimal solution for every positive integer amount with these coins denominations. • If the coins denominations are again d1 = 25 (quarter), d2 = = 10 (dime), d3 = 5 (nickel), and d4 = 1 (penny). • How would you give change with coins of these denominations of, say, n = 30? • Using Greedy thinking, it leads to 1 quarter and 1 nickel, which is an optimal solution. • Using Greedy Technique, it yields • Using 1Q 30 – 25 = 5 • Using 1N 25 – 5 = 0
Chapter 06 Greedy Technique • Let revisit the change-making problem by giving change for a specific amount n with the least number of coins of the denominations d1 > d2 > … > dm . • Given these donominations, d1 = 25 (quarter), d2 = = 10 (dime), d3 = 5 (nickel), and d4 = 1 (penny). • Using dynamic programming, how would you give change with coins of these denominations of, say, n = 30 cents? • Using dynamic programming, apply the following formula: • F(n) = minj: n ≥dj { F(n – dj ) } + 1 for n > 0 …………… 8.4 • F(0) = 0. • The solution is F(30) = min{F(30-1), F(30-5), F(30-10), F(30-25)} + 1 • = min{F(29) , F(25), F(20) , F(5) }+ 1 • = min{{1Q, 4P}, {1Q}, {2D}, {1N}} + 1 ={1Q} + {1N}
Greedy Technique • The central point of the greedy approach • The greedy approach suggests constructing a solution through a sequence of steps, each expanding a partially constructed solution obtained so far, until a complete solution to the problem is reached. On each step --- and this is the central point of this technique --- the choice made must be: • Feasible, i.e., it has to satisfy the problem’s constraints • Locally optimal, i.e., it has to be the best local choice among all feasible choices available on that step • Irrevocable, i.e., once made, it cannot be changed on subsequent steps of the algorithm.
Greedy Technique • An example to show that the final result obtained by a greedy algorithm is optimal based on the algorithm’s output rather than the way it operates. • Consider the problem of placing the maximum number of chips on an 8 x 8 board so that no two chips are placed on the same or adjacent vertically, horizontally, or diagonally – squares. • To follow the prescription of the greedy strategy, we should place each new chip so as to leave as many available squares as possible for next chips. • For example, starting with the upper left corner of the board, we will be able to place 16 chips as shown in Figure 9.1a. Why is this solution optimal?
Greedy Technique: Placement of 16 chips on non-adjacent squares. Figure 9.1 Placement of 16 chips on non-adjacent squares.
Greedy Technique • Why is this solution optimal? Too see why, • partition the board into sixteen 4x4 squares as shown in Figure 9.1b. • Obviously, it is impossible to place more than one chip in each of these squares, which implies that the total number of nonadjacent chips on the board cannot exceed 16. Figure 9.1 (b) Partition of the board proving impossibility of placing more than 16 chips.
Greedy Technique An example for proving optimality of a greedy algorithm is to show that on each step it does at least as well as any other algorithm could in advancing toward the problem’s goal. Find the minimum number of moves needed for a chess knight to go from one corner of a 100x100 board to the diagonally opposite corner. The knight’s moves are L-shaped jumps: to squares horizontally or vertically following by one square in perpendicular direct. A greedy solution is: jump as close to the goal as possible on each move. Thus, if its starts and finish squares are (1, 1) and (100, 100), respectively, a sequence of 66 moves such as (1, 1), (3, 2), (4, 4), …, (97, 97), (99, 98), (100, 100)
Greedy Technique Find the minimum number of moves needed for a chess knight to go from one corner of a 100x100 board to the diagonally opposite corner. The knight’s moves are L-shaped jumps: to squares horizontally or vertically following by one square in perpendicular direct. A greedy solution is: jump as close to the goal as possible on each move. Thus, if its starts and finish squares are (1, 1) and (100, 100), respectively, a sequence of 66 moves such as (1, 1), (3, 2), (4, 4), …, (97, 97), (99, 98), (100, 100) Number of moves = ((100-1) + (100-1)) *1/3 = 66 moves?
Solution provided by Kyle Bostelman knight moves
Greedy Technique • One of the common ways to do this is illustrated by the proof of given as follows: using mathematical induction, we show that a partially constructed solution obtained by the greedy algorithm on each iteration can be extended to an optimal solution to the problem.
Prim’s Algorithm • Given n points (vertices), connect them in the cheapest possible way so that there will be a path between every pair of points (vertices). (This is a problem --- the cheapest way to achieve connectivity). • We can represent the points given by vertices of a graph, possible connections by the graph’s edges, and the connection costs by the edge weights. • Then the question can be posed as the minimum spanning tree problem, defined formally as follows.
Definition A spanning tree of an undirected connected graph is its connected acyclic subgraph (i.e., a tree) that contains all the vertices of the graph. • If such a graph has weights assigned to its edges, a minimum spanning tree is its spanning tree of the smallest weight, where the weight of a tree is defined as the sum of the weights on all its edges. • The minimum spanning tree problemis the problem of finding a minimum spanning tree for a given weighted connected graph. • Figure 9.2 presents a simple example illustrating these notions.
Figure 9.2 Graph and its spanning trees, with T1 being the minimum spanning tree. 1 1 a b a b 2 2 5 3 3 d c d c graph w(T1 ) = 6
1 1 a b a b 2 5 5 3 d c d c w(T2 ) = 9 w(T3 ) = 8
Give a graph and its spanning trees. The number of spanning trees grows exponentially with the graph size (in terms of number of vertices). 1 1 a b a b 4 2 4 4 5 3 3 d c d c The graph has 4 vertices, and it has 24 spanning trees. e.g., a spanning tree with w(T4 ) = 8.
Give a graph and its spanning trees. The number of spanning trees grows exponentially with the graph size (in terms of number of vertices). 1 a b a b 4 2 4 4 5 4 5 3 d c d c The graph has 4 vertices, and it has 24 spanning trees. e.g., a spanning tree with w(T5 ) = 13.
Give a graph and its spanning trees. The number of spanning trees grows exponentially with the graph size (in terms of number of vertices). 1 a b a b 2 2 4 4 5 4 5 3 d c d c The graph has 4 vertices, and it has 24 spanning trees. e.g., a spanning tree with w(T6 ) = 11.
Give a graph and its spanning trees. The number of spanning trees grows exponentially with the graph size (in terms of number of vertices). 1 1 a b a b 2 4 4 4 4 5 3 d c d c The graph has 4 vertices, and it has 24 spanning trees. e.g., a spanning tree with w(T7 ) = 9.
Give a graph and its spanning trees. The number of spanning trees grows exponentially with the graph size (in terms of number of vertices). 1 a b a b 2 4 4 4 5 5 3 3 d c d c The graph has 4 vertices, and it has 24 spanning trees. e.g., a spanning tree with w(T8 ) = 12.
Give a graph and its spanning trees. The number of spanning trees grows exponentially with the graph size (in terms of number of vertices). 1 a b a b 2 2 4 4 4 5 3 3 d c d c The graph has 4 vertices, and it has 24 spanning trees. e.g., a spanning tree with w(T9 ) = 9.
Give a graph and its spanning trees. The number of spanning trees grows exponentially with the graph size (in terms of number of vertices). 1 1 a b a b 2 4 4 4 5 3 3 d c d c The graph has 4 vertices, and it has 24 spanning trees. e.g., a spanning tree with w(T10 ) = 8. if W(b, d) = 1, w(T10 ) = 5.
Give a graph and its spanning trees. The number of spanning trees grows exponentially with the graph size (in terms of number of vertices). 1 1 a b a b 2 4 4 5 4 5 3 d c d c The graph has 4 vertices, and it has 24 spanning trees. e.g., a spanning tree with w(T11 ) = 10.
Give a graph and its spanning trees. The number of spanning trees grows exponentially with the graph size (in terms of number of vertices). 1 a b a b 2 4 4 5 4 5 3 3 d c d c The graph has 4 vertices, and it has 24 spanning trees. e.g., a spanning tree with w(T12 ) = 12.
Give a graph and its spanning trees. The number of spanning trees grows exponentially with the graph size (in terms of number of vertices). 1 1 a b a b 2 2 4 4 4 5 3 d c d c The graph has 4 vertices, and it has 24 spanning trees. e.g., a spanning tree with w(T13 ) = 10. (c, b, a, d)
Give a graph and its spanning trees. The number of spanning trees grows exponentially with the graph size (in terms of number of vertices). 1 a b a b 2 2 4 4 4 5 3 3 d c d c The graph has 4 vertices, and it has 24 spanning trees. e.g., a spanning tree with w(T14 ) = 9. (a, d, c, b)
Give a graph and its spanning trees. The number of spanning trees grows exponentially with the graph size (in terms of number of vertices). 1 a b a b 2 2 4 4 5 4 5 3 d c d c The graph has 4 vertices, and it has 24 spanning trees. e.g., a spanning tree with w(T15 ) = 11. (b, c, a, d)
Give a graph and its spanning trees. The number of spanning trees grows exponentially with the graph size (in terms of number of vertices). 1 a b a b 2 2 4 4 4 4 5 3 d c d c e.g., a spanning tree with w(T16 ) = 10. (a, d, b, c) The graph has 4 vertices, and it has 24 spanning trees. Total number of spanning trees is 2|V| = 24 = 16.
Give a graph and its spanning trees. The number of spanning trees grows exponentially with the graph size (in terms of number of vertices). 1 a b a b 2 7 2 2 4 4 e e 4 4 5 7 1 d c d c 1 3 e.g., a spanning tree with w(T1 ) = 9. (a, (d, e), b, c) The graph has 5 vertices, and it has 25 spanning trees. Total number of spanning trees is 2|V| = 24 = 16.
Problems for constructing a minimum spanning tree • If we were to try constructing a minimum spanning tree by exhaustive search, we would face two serious obstacles. • First the number of spanning trees grows exponentially with the graph size (at least for dense graphs). • Second, generating all spanning trees for a given graph is not easy; • in fact, it is more difficult than finding a minimum spanning tree for a weighted graph by using one of several efficient algorithms available for this problem.
Outline constructing a minimum spanning tree • Prim’s algorithm constructs a minimum spanning tree through a sequence of expanding subtrees. • The initial subtree in such a sequence consists of a single vertex selected arbitrarily from the set V of the graph’s vertices. • On each iteration, the algorithm expands the current (spanning) tree in the greedy manner by simply attaching to it the nearest vertexnot in that tree. • (By the nearest vertex, we mean a vertex not in the (spanning) tree connected to a vertex in the tree by an edge of the smallest weight. Ties can be broken arbitrarily.) • The algorithm stops after all the graph’s vertices have been included in the tree being constructed. • Since the algorithm expands a tree by exactly one vertex on each of its iterations, the total number of such iterations is n-1 where n is the number of vertices in the graph. • The tree generated by the algorithm is obtained as the set of edges used for the tree expansions.
Here is pseudocode of this algorithm. Algorithm Prim(G) //Prim’s algorithm for constructing a minimum spanning tree Input: A weighted connected graph G = (V, E) Output: ET , the set of edges composing a minimum spanning tree of G VT← { v0 } //the set of tree vertices can be initialized with any vertex ET ,← Ø fori← 1 to |V| - 1 do { find a minimum-weight edge d* = (v*, u*) among all the edge (v, u) such that v is in VT and u is in V - VT VT← VT U {u*} ET← ET U {e*} } //end for return ET VT V - VT = This rules out the cyclic- connected.
The nature of Prim’s algorithm makes it necessary to provide each vertex (in a given graph) not in the current (spanning) tree with the information about the shortest edge connecting the vertex to a tree vertex. • Provide each information by attaching two labels to a vertex: • the name of the nearest tree vertex and • the length (the weight) of the corresponding edge. • Vertices that are not adjacent to any of the tree vertices (in a given graph) can be given • the ∞ label indicating their “infinite” distance to the tree vertices and • a null label for the name of the nearest tree vertex.
(Alternatively, we can split the vertices that are not in the tree into two sets, the “fringe” and the “unseen.” • The fringe contains only the vertices that are not in the (spanning) tree but are adjacent to at least one tree vertex. These are the candidates from which the next tree vertex is selected. • The unseen vertices are all the other vertices of the graph, called “unseen” because they are yet to be affected by the algorithm.) • With such labels finding the next vertex to be added to the current (spanning) tree T = <VT, ET > becomes a simple task of finding a vertex with the smallest distance label in the set V - VT. • Ties can be broken arbitrarily.
After we have identified a vertex u* to be added to the (spanning) tree, we need to perform two operations: • Move u* from the set V- VT to the set of tree vertices VT . • For each remaining vertex u in V - VT that is connected to u* by a shorter edge than the u’s current distance label, update its labels by u* and the weight of the edge between u* and u, respectively. • Figure 9.3 demonstrates the application of Prim’s algorithm to a specific graph.
Figure 9.3 Application of Prim’s algorithm. The parenthesized labels of a vertex in the middle column indicate the nearest tree vertex and edge weight; selected vertices and edges are shown in bold. 1 b c 6 3 4 4 5 5 a f d 2 8 6 e
d* = (v*, u*) 1 b c a(-, -) b(a, 3) c(-, ∞) d(-, ∞) e(a, 6) f(a, 5) 6 3 4 4 5 5 a f d VT = { a } and V - VT = { b, c d, f, e} 2 8 6 e
1 b c b(a, 3) c(b, 1) d(-, ∞) e(a, 6)) f(b, 4) f(a, 5) 6 3 4 4 5 5 a f d VT = { a, b } and V - VT = { c d, f, e} 2 8 6 e
1 b c c(b, 1) d(c, 6) e(a, 6) f(b, 4) f(a, 5) f(c, 4) 6 3 4 4 5 5 a f d VT = { a, b, c } and V - VT = { d, f, e} 2 8 6 e
1 b c f(b, 4) d(f, 5) e(f, 2) e(a, 6) 6 3 4 4 5 5 a f d VT = { a, b, c, f } and V - VT = { d, e} 2 8 6 e
1 b c e(f, 2) d(f, 5) d(c,6) d(e, 8) d(f, 5) 6 3 4 4 VT = { a, b, c, f , e} and V - VT = { d} 5 5 a f d 2 8 6 e VT = { a, b, c, f , e, d} and V - VT = { }
1 b c e(f, 2) d(f, 5) d(c,6) d(e, 8) d(f, 5) 6 3 4 4 VT = { a, b, c, f , e} and V - VT = { d} 5 5 a f d 2 8 6 e VT = { a, b, c, f , e, d} and V - VT = { }
1 b c c(b, 1) d(c, 6) e(a, 6) f(b, 4) f(a, 5) f(c, 4) 6 3 4 4 5 5 a f d VT = { a, b, c } and V - VT = { d, f, e} 2 8 6 e
1 b c f(c, 4) e(a, 6) e(f, 2) d(f, 5) d(c, 6) 6 3 4 4 5 5 a f d VT = { a, b, c, f } and V - VT = { d, e} 2 8 6 e