Lecture 23. Greedy Algorithms

Slides:



Advertisements
Similar presentations
Greedy Technique Constructs a solution to an optimization problem piece by piece through a sequence of choices that are: feasible, i.e. satisfying the.
Advertisements

Chapter 9 Greedy Technique. Constructs a solution to an optimization problem piece by piece through a sequence of choices that are: b feasible - b feasible.
Greedy Algorithms.
Introduction to Algorithms Greedy Algorithms
Algorithm Design Techniques: Greedy Algorithms. Introduction Algorithm Design Techniques –Design of algorithms –Algorithms commonly used to solve problems.
Types of Algorithms.
Greedy Algorithms Greed is good. (Some of the time)
Greedy Algorithms Spanning Trees Chapter 16, 23. What makes a greedy algorithm? Feasible –Has to satisfy the problem’s constraints Locally Optimal –The.
Greed is good. (Some of the time)
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
Lecture 3: Greedy Method Greedy Matching Coin Changing Minimum Spanning Tree Fractional Knapsack Dijkstra's Single-Source Shortest-Path.
1 Greedy Algorithms. 2 2 A short list of categories Algorithm types we will consider include: Simple recursive algorithms Backtracking algorithms Divide.
3 -1 Chapter 3 The Greedy Method 3 -2 The greedy method Suppose that a problem can be solved by a sequence of decisions. The greedy method has that each.
Chapter 9: Greedy Algorithms The Design and Analysis of Algorithms.
Chapter 9 Greedy Technique Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
ASC Program Example Part 3 of Associative Computing Examining the MST code in ASC Primer.
Greedy Algorithms CSE 331 Section 2 James Daly. Reminders Exam 2 next week Thursday, April 9th Covers heaps to spanning trees Greedy algorithms (today)
CPSC 411, Fall 2008: Set 4 1 CPSC 411 Design and Analysis of Algorithms Set 4: Greedy Algorithms Prof. Jennifer Welch Fall 2008.
CS420 lecture eight Greedy Algorithms. Going from A to G Starting with a full tank, we can drive 350 miles before we need to gas up, minimize the number.
Minimum Spanning Trees. Subgraph A graph G is a subgraph of graph H if –The vertices of G are a subset of the vertices of H, and –The edges of G are a.
Computability Reports. More examples. Homework: Optimization. Other follow- ups. Start to plan presentation.
Minimal Spanning Trees What is a minimal spanning tree (MST) and how to find one.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Graph Algorithms: Minimum.
1 Introduction to Approximation Algorithms. 2 NP-completeness Do your best then.
SPANNING TREES Lecture 21 CS2110 – Spring
Fundamentals of Algorithms MCS - 2 Lecture # 7
Dr. Naveed Ahmad Assistant Professor Department of Computer Science University of Peshawar.
COSC 2007 Data Structures II Chapter 14 Graphs III.
Dijkstra’s Algorithm. Announcements Assignment #2 Due Tonight Exams Graded Assignment #3 Posted.
Prims’ spanning tree algorithm Given: connected graph (V, E) (sets of vertices and edges) V1= {an arbitrary node of V}; E1= {}; //inv: (V1, E1) is a tree,
CPSC 320: Intermediate Algorithm Design & Analysis Greedy Algorithms and Graphs Steve Wolfman 1.
Lecture 19 Greedy Algorithms Minimum Spanning Tree Problem.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 9 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Minimal Spanning Tree Problems in What is a minimal spanning tree An MST is a tree (set of edges) that connects all nodes in a graph, using.
Minimum Spanning Trees CS 146 Prof. Sin-Min Lee Regina Wang.
1 Greedy Technique Constructs a solution to an optimization problem piece by piece through a sequence of choices that are: b feasible b locally optimal.
SPANNING TREES Lecture 20 CS2110 – Fall Spanning Trees  Definitions  Minimum spanning trees  3 greedy algorithms (incl. Kruskal’s & Prim’s)
Theory of Algorithms: Greedy Techniques James Gain and Edwin Blake {jgain | Department of Computer Science University of Cape Town.
SPANNING TREES Lecture 21 CS2110 – Fall Nate Foster is out of town. NO 3-4pm office hours today!
Lecture 19 Minimal Spanning Trees CSCI – 1900 Mathematics for Computer Science Fall 2014 Bill Pine.
I can describe the differences between Hamilton and Euler circuits and find efficient Hamilton circuits in graphs. Hamilton Circuits I can compare and.
Spanning Trees Dijkstra (Unit 10) SOL: DM.2 Classwork worksheet Homework (day 70) Worksheet Quiz next block.
A greedy algorithm is an algorithm that follows the problem solving heuristic of making the locally optimal choice at each stage with the hope of finding.
SPANNING TREES Lecture 21 CS2110 – Spring
Greedy Algorithms.
Minimum Spanning Trees
Chapter 9 : Graphs Part II (Minimum Spanning Trees)
Greedy Technique.
Greedy function greedy { S <- S0 //Initialization
Chapter 5. Greedy Algorithms
Greedy Algorithms.
Department of Computer Science
Spanning Trees Lecture 21 CS2110 – Fall 2016.
Graph Algorithm.
Greedy Method     Greedy Matching     Coin Changing     Minimum Spanning Tree     Fractional Knapsack     Dijkstra's Single-Source Shortest-Path.
CSCE350 Algorithms and Data Structure
Minimum Spanning Tree.
Minimum Spanning Trees
Types of Algorithms.
Chapter 2: Business Efficiency Business Efficiency
Advanced Analysis of Algorithms
Hannah Tang and Brian Tjaden Summer Quarter 2002
Graph Searching.
CSC 380: Design and Analysis of Algorithms
CSE 373: Data Structures and Algorithms
Types of Algorithms.
CSC 380: Design and Analysis of Algorithms
The Greedy Approach Young CS 530 Adv. Algo. Greedy.
Spanning Trees Lecture 20 CS2110 – Spring 2015.
Approximation Algorithms
Presentation transcript:

Lecture 23. Greedy Algorithms

Recap In previous lecture, we discuss about the two other techniques to construct a Minimal Spanning Tree from a given network., i,e. Kruskal and Dijkstra Algorithms In Kruskal algorithm, edges are listed in order (Ascending) and then one edge take to show the connection between two nodes or vertices. In Dijstra algorithm, all edges of current start node are expanded and used the edge with smallest value. This process remain continue till all nodes are visited. 2

Optimization problems An optimization problem is one in which you want to find, not just a solution, but the best solution A “greedy algorithm” sometimes works well for optimization problems A greedy algorithm works in phases. At each phase: You take the best you can get right now, without regard for future consequences You hope that by choosing a local optimum at each step, you will end up at a global optimum 2

Example: Counting money Suppose you want to count out a certain amount of money, using the fewest possible bills and coins A greedy algorithm would do this would be: At each step, take the largest possible bill or coin that does not overshoot Example: To make $6.39, you can choose: a $5 bill a $1 bill, to make $6 a 25¢ coin, to make $6.25 A 10¢ coin, to make $6.35 four 1¢ coins, to make $6.39 For US money, the greedy algorithm always gives the optimum solution 3

A failure of the greedy algorithm In some (fictional) monetary system, “krons” come in 1 kron, 7 kron, and 10 kron coins Using a greedy algorithm to count out 15 krons, you would get A 10 kron piece Five 1 kron pieces, for a total of 15 krons This requires six coins A better solution would be to use two 7 kron pieces and one 1 kron piece This only requires three coins The greedy algorithm results in a solution, but not in an optimal solution 4

A scheduling problem You have to run nine jobs, with running times of 3, 5, 6, 10, 11, 14, 15, 18, and 20 minutes You have three processors on which you can run these jobs You decide to do the longest-running jobs first, on whatever processor is available 20 10 3 P1 P2 P3 18 11 6 15 14 5 Time to completion: 18 + 11 + 6 = 35 minutes This solution isn’t bad, but we might be able to do better 5

Another approach What would be the result if you ran the shortest job first? Again, the running times are 3, 5, 6, 10, 11, 14, 15, 18, and 20 minutes P1 P2 P3 3 10 15 5 11 18 6 14 20 That wasn’t such a good idea; time to completion is now 6 + 14 + 20 = 40 minutes Note, however, that the greedy algorithm itself is fast All we had to do at each stage was pick the minimum or maximum 6

An optimum solution Better solutions do exist: 20 18 15 14 11 10 6 5 3 P1 P2 P3 This solution is clearly optimal (why?) Clearly, there are other optimal solutions (why?) How do we find such a solution? One way: Try all possible assignments of jobs to processors Unfortunately, this approach can take exponential time 7

An Introduction to Huffman Coding March 21, 2000 Huffman encoding 1. Scan text to be compressed and tally occurrence of all characters. 2. Sort or prioritize characters based on number of occurrences in text. 3. Build Huffman code tree based on prioritized list. 4. Perform a traversal of tree to determine all code words. 5. Scan text again and create new file using the Huffman codes. Mike Scott

Huffman encoding The Huffman encoding algorithm is a greedy algorithm You always pick the two smallest numbers to combine 100 Average bits/char: 0.22*2 + 0.12*3 + 0.24*2 + 0.06*4 + 0.27*2 + 0.09*4 = 2.42 The Huffman algorithm finds an optimal solution 54 A=00 B=100 C=01 D=1010 E=11 F=1011 27 46 15 22 12 24 6 27 9 A B C D E 3 F 8

Minimum spanning tree A minimum spanning tree is a least-cost subset of the edges of a graph that connects all the nodes Start by picking any node and adding it to the tree Repeatedly: Pick any least-cost edge from a node in the tree to a node not in the tree, and add the edge and new node to the tree Stop when all nodes have been added to the tree 6 The result is a least-cost (3+3+2+2+2=12) spanning tree If you think some other edge should be in the spanning tree: Try adding that edge Note that the edge is part of a cycle To break the cycle, you must remove the edge with the greatest cost This will be the edge you just adde 3 2 4 5 3 1 2 4 9

Traveling salesman E A B C D A salesman must visit every city (starting from city A), and wants to cover the least possible distance He can revisit a city (and reuse a road) if necessary He does this by using a greedy algorithm: He goes to the next nearest city from wherever he is E A B C D 2 3 4 From A he goes to B From B he goes to D This is not going to result in a shortest path! The best result he can get now will be ABDBCE, at a cost of 13 An actual least-cost path from A is DBCE, at a cost of 14 10

Connecting wires There are n white dots and n black dots, equally spaced, in a line You want to connect each white dot with some one black dot, with a minimum total length of “wire” Example: Total wire length above is 1 + 1 + 1 + 5 = 8

Collecting coins A checkerboard has a certain number of coins on it A robot starts in the upper-left corner, and walks to the bottom right-hand corner The robot can only move in two directions: right and down The robot collects coins as it goes You want to collect all the coins using the minimum number of robots Example:

Other greedy algorithms Dijkstra’s algorithm for finding the shortest path in a graph Always takes the shortest edge connecting a known node to an unknown node Kruskal’s algorithm for finding a minimum-cost spanning tree Always tries the lowest-cost remaining edge Prim’s algorithm for finding a minimum-cost spanning tree Always takes the lowest-cost edge between nodes in the spanning tree and nodes not yet in the spanning tree

Analysis Therefore, coin counting is (n) A greedy algorithm typically makes (approximately) n choices for a problem of size n (The first or last choice may be forced) Hence the expected running time is: O(n * O(choice(n))), where choice(n) is making a choice among n objects Counting: Must find largest useable coin from among k sizes of coin (k is a constant), an O(k)=O(1) operation; Therefore, coin counting is (n) Huffman: Must sort n values before making n choices Therefore, Huffman is O(n log n) + O(n) = O(n log n) Minimum spanning tree: At each new node, must include new edges and keep them sorted, which is O(n log n) overall Therefore, MST is O(n log n) + O(n) = O(n log n) 11

Summary Greedy approach algorithms are used tofind the optimal solution among all possible solution. Huffman coding and other real world problem uses greedy approach to find optimal solution. Some graphs algorithms like prims, Kruskal and Dijkstra also uses greedy approach to find MST. 11

In Next Lecture In next lecturer we will discuss about the dynamic programming approach. 11