Greedy Optimization Problems - Lecture Notes | CPSC 5155U, Study notes of Computer Architecture and Organization

Material Type: Notes; Professor: Bosworth; Class: Computer Architecture; Subject: Computer Science; University: Columbus State University; Term: Unknown 1989;

Typology: Study notes

Pre 2010

Uploaded on 08/04/2009

koofers-user-kb7
koofers-user-kb7 🇺🇸

10 documents

1 / 17

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Greedy
This lecture focuses on the algorithm design strategy called “greedy”.
The greedy design strategy is usually applied to optimization techniques.
Topics for the lecture include:
1. Definition of optimization problems,
2. Definition of the greedy approach,
3. Example of greedy as applied to the problem of making change,
4. Spanning subgraphs, spanning trees, and minimum spanning trees; and
5. Prim’s Algorithm for generating a Minimum Spanning Tree (MST).
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Greedy Optimization Problems - Lecture Notes | CPSC 5155U and more Study notes Computer Architecture and Organization in PDF only on Docsity!

Greedy

This lecture focuses on the algorithm design strategy called “greedy”. The greedy design strategy is usually applied to optimization techniques. Topics for the lecture include:

  1. Definition of optimization problems,
  2. Definition of the greedy approach,
  3. Example of greedy as applied to the problem of making change,
  4. Spanning subgraphs, spanning trees, and minimum spanning trees; and
  5. Prim’s Algorithm for generating a Minimum Spanning Tree (MST).

Optimization Problems

Optimization problems are those in which some function, called the “value function” is either to be maximized or minimized. In general, we maximize profits or capacities, or we minimize costs or distances. Very often, the problem solution is subject to constraints. Typical constraints are: 1) Weight limitations in a maximization problem.

  1. Count limitations in either a maximization or minimization problem.
  2. Structural constraints, such as visiting each node exactly once, or assigning exactly one job to each worker. A solution is called feasible if and only if it satisfies all constraints on the problem. A solution is called optimal if and only if both
  3. It is a feasible solution, and
  4. It maximizes or minimizes the value function. There is no requirement for an optimal solution to be unique.

Example: Making Change

Consider the problem of giving change of seventeen cents, using only U.S. coins currently in circulation. In this problem we consider only dimes, nickels, and pennies. The value function to be minimized is the number of coins given as change. Here is the backtracking solution. One can give either 0 dimes or 1 dime.

  1. If 0 dimes are given, one can give 0, 1, 2, or 3 nickels.
  2. If 1 dime is given, one can give either 0 nickels or 1 nickel. All solutions must be considered; the optimal solution is known only at the end.

Making Change Using the Greedy Algorithm

At each step Greedy makes the right choice. Since there is only one choice at each level of the decision tree, the tree is quite simple. Given the U. S. coin set (half dollar, quarter, dime, nickel, and penny) the algorithm for giving change amounting to less than $1.00 is simple.

  1. Give as many half dollars as possible.
  2. Give as many quarters as possible.
  3. Give as many dimes as possible.
  4. Give a nickel if possible.
  5. Give the rest in pennies. Given the older U.S. coin set (50¢, 25¢, 10¢, 5¢, 3¢, 2¢, and 1¢), it is possible to give examples for which Greedy fails: 9¢ as a 1) nickel and four pennies or 2) three 3¢.

Subgraphs, Spanning Subgraphs, and Spanning Trees

Let G be an (N, M)–graph with vertex set V(G) and edge set E(G). N  1 and M  0. Let H be a graph with vertex set V(H) and edge set E(H). H is said to be a subgraph of G if and only if V(H)  V(G) and E(H)  E(G). H is said to be a spanning subgraph of G if and only if V(H) = V(G) and E(H)  E(G). Note the difference here. The two vertex sets V(G) and V(H) are identical. H is said to be a spanning tree of G if and only if H is a spanning subgraph of G and H is a tree. Specifically, H is a connected acyclic graph. If H is a spanning tree of the (N, M)–graph G, then H is an (N, N – 1) graph; it has N vertices and (N – 1) edges. The weight of a tree is the sum of the weights of its edges. A MST (Minimum Spanning Tree) of a graph with edge weights is the spanning tree of minimum total edge weight. The MST need not be unique. For an unweighted graph, any spanning tree is a MST. All have weight (N – 1).

Sample Graph and Three Spanning Trees

This example is adapted from the textbook. Here is the weighted tree. Here are its three spanning trees Weight = 6 Weight = 8 Weight = 9 The subtree on the left is the minimum spanning tree of G.

Prim’s Algorithm

Algorithm Prim (G) // Prim’s algorithm for constructing a minimum spanning tree // Input: A weighted connected graph G = (V, E), with vertex // set V and edge set E. // Output: ET - the set of edges for the spanning tree, which by // definition must have the same vertex set as G. // WT – the total weight of that tree. // VT = { v 0 } // Initialize the vertex set of T to any vertex ET =  // Initialize its edge set to the empty set WT = 0 // Weight of this tree is 0. // For I = 1 to |V| - 1 Do // |V| is the size of the vertex set Find a minimum-weight edge e * = ( u , w) with w  VT and u  V, but u  VT VT = VT  { u }; ET = ET  { e *} // w * = weight( e *); WT = WT + w * End Do // Return ET and WT

Start Prim’s Algorithm

We now illustrate the execution of Prim’s algorithm on this example. The graph G has four edges, each with weight as follows. Weight ([A, B]) = 1 Weight ([A, C]) = 5 Weight ([A, D]) = 2 Weight ([C, D]) = 3 Start the algorithm with VT = {A}. V – VT = {B, C, D} Consider (A, B) with 1 (A, C) with 5 (A, D) with 2

Place the Second and Third Tree Edges

Now, the shortest edge connecting a vertex in set {A, B} to a vertex in V – {A, B} = { C, D} is the edge (A, D). We add this to the subtree with the following results. VT = {A, B, D} // The tree has three vertices ET = { (A, B), (A, D) } // The tree has two edges. WT = 3 Now V = {A, B, D} and V – VT = {C} Finally, the shortest edge connecting vertex C to a vertex in the set {A, B, D} is the edge (C, D) with weight 3. We add this edge to the subtree to get the following spanning tree. VT = {A, B, C, D} ET = { (A, B), (A, D), (C, D) } WT = 6 This is our spanning tree.

Comparison to Single–Source Minimum Distance Trees

In a later lecture, we shall consider Dijkstra’s Algorithm for determining the shortest path from one special vertex, the source vertex , to all other vertices in the graph. It is easily proven that the paths generated form a spanning tree of the graph, rooted at the source vertex. Occasionally, this tree might be a minimum spanning tree. Here are two shortest path trees for our sample graph, with vertex A as the source vertex. Each tree shows the following distances: A to B distance 1 A to C distance 5 A to D distance 2 The tree at left has total weight 6, and the tree on the right has total weight 8. Only the tree on the left is a MST.

Correctness Proof (Part 1)

We are constructing the spanning tree and, at the moment, have a tree TI with I vertices. We are considering addition of an edge to generate a tree TI+1 with (I + 1) vertices. We are looking for edges of the form e = ( x , y ), with x  VT and y  (V – VT). The first thing that we note is that the graph TI+1 with (I + 1) vertices is a tree, provided only that the graph TI with I vertices is also a tree.

  1. TI is a connected acyclic graph with I vertices and (I – 1) edges. We add one edge and one vertex to get a connected graph TI+1 with (I + 1) vertices and I edges.
  2. Because x  VT and y  (V – VT), addition of edge e = ( x , y ) does not introduce a cycle. A cycle would imply another xy path, thus that y  VT already.

Correctness Proof (Part 2)

We are constructing the spanning tree and, at the moment, have a tree TI with I vertices. We are considering addition of an edge to generate a tree TI+1 with (I + 1) vertices. Consider two candidate edges ( u , v ) and ( x , y ), with W( x , y ) > W( u , v ). Assume that we add edge ( x , y ) to the spanning tree and omit ( u , v ). We continue to expand the tree until it has N vertices and (N – 1) edges, a spanning tree of the graph. Assume the tree T has total weight W*.

  1. Adding the edge ( u , v ) to this tree forms a spanning subgraph H that has one cycle and total weight W* + W( u , v ).
  2. Deleting edge ( x , y ) from H maintains H as a connected spanning subgraph, but removes its one cycle. We now have a spanning tree with weight W* + W( u , v ) – W( x , y ), less that that of T. Thus T was not a MST.