Unformatted text preview: an "item" to be included into the solution. c Xin He (University at Buffalo) CSE 431/531 Algorithm Analysis and Design 4 / 49 Greedy Algorithms General Description
Given an optimization problem P, we seek an optimal solution. The solution is obtained by a sequence of steps. In each step, we select an "item" to be included into the solution. At each step, the decision is made based on the selections we have already made so far, that looks the best choice for achieving the optimization goal. c Xin He (University at Buffalo) CSE 431/531 Algorithm Analysis and Design 4 / 49 Greedy Algorithms General Description
Given an optimization problem P, we seek an optimal solution. The solution is obtained by a sequence of steps. In each step, we select an "item" to be included into the solution. At each step, the decision is made based on the selections we have already made so far, that looks the best choice for achieving the optimization goal. Once a selection is made, it cannot be undone: The selected item cannot be removed from the solution. c Xin He (University at Buffalo) CSE 431/531 Algorithm Analysis and Design 4 / 49 Minimum Spanning Tree (MST) Problem
This is a classical graph problem. We will study graph algorithms in detail later. Here we use MST as an example of Greedy Algorithms. c Xin He (University at Buffalo) CSE 431/531 Algorithm Analysis and Design 5 / 49 Minimum Spanning Tree (MST) Problem
This is a classical graph problem. We will study graph algorithms in detail later. Here we use MST as an example of Greedy Algorithms. Definition
A tree is a connected graph with no cycles. c Xin He (University at Buffalo) CSE 431/531 Algorithm Analysis and Design 5 / 49 Minimum Spanning Tree (MST) Problem
This is a classical graph problem. We will study graph algorithms in detail later. Here we use MST as an example of Greedy Algorithms. Definition
A tree is a connected graph with no cycles. Definition
Let G = (V, E) be a graph. A spanning tree of G is a subgraph of G that contains all vertices of G and is a tree. c Xin He (University at Buffalo) CSE 431/531 Algorithm Analysis and Design 5 / 49 Minimum Spanning Tree (MST) Problem
This is a classical graph problem. We will study graph algorithms in detail later. Here we use MST as an example of Greedy Algorithms. Definition
A tree is a connected graph with no cycles. Definition
Let G = (V, E) be a graph. A spanning tree of G is a subgraph of G that contains all vertices of G and is a tree. Minimum Spanning Tree (MST) Problem
Input: An connected undirected graph G = (V, E). Each edge e E has a weight w(e) 0. Find: a spanning tree T of G such that w(T) = eT w(e) is minimum.
c Xin He (University at Buffalo) CSE 431/531 Algorithm Analysis and Design 5 / 49 Kruskal's Algorithm Kruskal's Algorithm 1: Sort the edges by nondecreasing weight. Let e1 , e2 , . . . , em be the sorted edge list 2: T 3: for i = 1 to m do 4: if T {ei } does not contain a cycle then 5: T T {ei } 6: else 7: do nothing 8: end if 9: end for 10: output T c Xin He (University at Buffalo) CSE 431/531 Algorithm Analysis and Design 6 / 49 Kruskal's Algorithm The algorithm goes through a sequence of steps. c Xin He (University at Buffalo) CSE 431/531 Algorithm Analysis and Design 7 / 49 Kruskal's Algorithm The algorithm goes through a sequence of steps. At each step, we consider the edge ei , and decide whether add ei into T. c Xin He (University at Buffalo) CSE 431/531 Algorithm Analysis and Design 7 / 49 Kruskal's Algorithm The algorithm goes through a sequence of steps. At each step, we consider the edge ei , and decide whether add ei into T. Since we are building a spanning tree T, T can not contain any cycle. So if adding ei into T introduces a cycle in T, we do not add it into T. c Xin He (University at Buffalo) CSE 431/531 Algorithm Analysis and Design 7 / 49 Kruskal's Algorithm The algorithm goes through a sequence of steps. At each step, we consider the edge ei , and decide whether add ei into T. Since we are building a spanning tree T, T can not contain any cycle. So if adding ei into T introduces a cycle in T, we do not add it into T. Otherwise, we add ei into T. We are processing the edges in the order of increasing edge weight. So when ei is added into T, it looks the best to achieve the goal (minimum total weight). c Xin He (University at Buffalo) CSE 431/531 Algorithm Analysis and Design 7 / 49 Kruskal's Algorithm The algorithm goes through a sequence of steps. At each step, we consider the edge ei , and decide whether add ei into T. Since we are building a spanning tree T, T can not contain any cycle. So if adding ei into T introduces a cycle in T, we do not add it into T. Otherwise, we add ei into T. We are processing the edges in the order of increasing edge weight. So when ei is added into T, it looks the best to achieve the goal (minimum total weight). Once ei is added, it is never removed and is included into the final tree T. c Xin He (University at Buffa...
View
Full
Document
This note was uploaded on 02/27/2012 for the course CSE 431/531 taught by Professor Xinhe during the Fall '11 term at SUNY Buffalo.
 Fall '11
 XINHE
 Algorithms

Click to edit the document details