









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Notes; Professor: Bosworth; Class: Computer Architecture; Subject: Computer Science; University: Columbus State University; Term: Unknown 1989;
Typology: Study notes
1 / 17
This page cannot be seen from the preview
Don't miss anything!










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