Lecture 8 Greedy Approach

Slides:



Advertisements
Similar presentations
Introduction to Algorithms
Advertisements

Greedy Technique Constructs a solution to an optimization problem piece by piece through a sequence of choices that are: feasible, i.e. satisfying the.
Greedy Technique The first key ingredient is the greedy-choice property: a globally optimal solution can be arrived at by making a locally optimal (greedy)
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.
Comp 122, Spring 2004 Greedy Algorithms. greedy - 2 Lin / Devi Comp 122, Fall 2003 Overview  Like dynamic programming, used to solve optimization problems.
Greedy Algorithms Greed is good. (Some of the time)
Lecture 9 Greedy Approach
Greed is good. (Some of the time)
Chapter 4 The Greedy Approach. Minimum Spanning Tree A tree is an acyclic, connected, undirected graph. A spanning tree for a given graph G=, where E.
Introduction to Algorithms Jiafen Liu Sept
CPSC 411, Fall 2008: Set 4 1 CPSC 411 Design and Analysis of Algorithms Set 4: Greedy Algorithms Prof. Jennifer Welch Fall 2008.
Chapter 9: Greedy Algorithms The Design and Analysis of Algorithms.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
Chapter 9 Greedy Technique Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
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.
16.Greedy algorithms Hsu, Lih-Hsing. Computer Theory Lab. Chapter 16P An activity-selection problem Suppose we have a set S = {a 1, a 2,..., a.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Greedy methods Prudence Wong
Lecture 1: The Greedy Method 主講人 : 虞台文. Content What is it? Activity Selection Problem Fractional Knapsack Problem Minimum Spanning Tree – Kruskal’s Algorithm.
Advanced Algorithm Design and Analysis (Lecture 5) SW5 fall 2004 Simonas Šaltenis E1-215b
Greedy Algorithms Dr. Yingwu Zhu. Greedy Technique Constructs a solution to an optimization problem piece by piece through a sequence of choices that.
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.
Dynamic programming vs Greedy algo – con’t Input: Output: Objective: a number W and a set of n items, the i-th item has a weight w i and a cost c i a subset.
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.
 2004 SDU Lecture 6- Minimum Spanning Tree 1.The Minimum Spanning Tree Problem 2.Greedy algorithms 3.A Generic Algorithm 4.Kruskal’s Algorithm.
Greedy Algorithms Z. GuoUNC Chapel Hill CLRS CH. 16, 23, & 24.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Spring, 2010 Lecture 2 Tuesday, 2/2/10 Design Patterns for Optimization.
Greedy Algorithms Analysis of Algorithms.
Minimum Spanning Trees Text Read Weiss, §9.5 Prim’s Algorithm Weiss §9.5.1 Similar to Dijkstra’s Algorithm Kruskal’s Algorithm Weiss §9.5.2 Focuses on.
Greedy Algorithms Lecture 10 Asst. Prof. Dr. İlker Kocabaş 1.
Greedy Algorithms. p2. Activity-selection problem: Problem : Want to schedule as many compatible activities as possible., n activities. Activity i, start.
CSCI 58000, Algorithm Design, Analysis & Implementation Lecture 12 Greedy Algorithms (Chapter 16)
Greedy Algorithms General principle of greedy algorithm
Algorithm Analysis Fall 2017 CS 4306/03
CSCE 411 Design and Analysis of Algorithms
COMP108 Algorithmic Foundations Greedy methods
Introduction to Algorithms
Greedy Technique Constructs a solution to an optimization problem piece by piece through a sequence of choices that are: feasible locally optimal irrevocable.
Lecture on Design and Analysis of Computer Algorithm
Chapter 5 : Trees.
Greedy Technique.
Introduction to Algorithms
Chapter 8 The Greedy Approach.
Minimum Spanning Tree Chapter 13.6.
The Greedy Method and Text Compression
Greedy Algorithms (Chap. 16)
Design & Analysis of Algorithm Greedy Algorithm
The Greedy Method and Text Compression
Introduction to Algorithms`
Greedy Algorithm.
Chapter 16: Greedy Algorithms
Greedy Algorithms TOPICS Greedy Strategy Activity Selection
CSCE350 Algorithms and Data Structure
ICS 353: Design and Analysis of Algorithms
Merge Sort 11/28/2018 2:21 AM The Greedy Method The Greedy Method.
CS6045: Advanced Algorithms
Minimum Spanning Trees
Greedy Algorithms Many optimization problems can be solved more quickly using a greedy approach The basic principle is that local optimal decisions may.
Greedy Algorithm (17.4/16.4) Greedy Algorithm (GA)
Chapter 23 Minimum Spanning Tree
Chapter 16: Greedy algorithms Ming-Te Chi
Greedy Algorithms TOPICS Greedy Strategy Activity Selection
Minimum Spanning Trees
Chapter 16: Greedy algorithms Ming-Te Chi
ICS 353: Design and Analysis of Algorithms
Greedy Algorithms Comp 122, Spring 2004.
Algorithm Design Techniques Greedy Approach vs Dynamic Programming
Asst. Prof. Dr. İlker Kocabaş
Presentation transcript:

Lecture 8 Greedy Approach Minimum spanning tree How to design greedy algorithms

Roadmap Minimum spanning tree How to design greedy algorithms Change-making problem Activity selection problem Huffman code Knapsack problem

MST Given. Undirected graph G with positive edge weights (connected). Def. A spanning tree of G is a subgraph T that is connected and acyclic. not connected 23 10 21 14 24 16 4 18 9 7 11 8 5 6 23 10 21 14 24 16 4 18 9 7 11 8 5 6 not acyclic

Minimum spanning tree a b h c i d e f g b c d a i e h g f Problem: MinSpanning Input: A connected undirected graph G = (V , E ) in which each edge has a weighted length Output: A spanning tree of G that has minimum cost a b h c i d e f g 4 2 7 10 9 8 11 1 6 14 8 7 b c d 9 4 2 a i e 11 14 4 7 6 10 8 h g f 1 2

Cut property b c d a i e h g f 8 7 9 4 2 11 14 4 7 6 10 8 1 2 Definition: Cut A cut {S,T} is a partition of the vertex set V into two subsets S and T. Theorem (Cut property)Given any cut, the crossing edge of min weight is in some MST.

Correctness Theorem Kruskal algorithm and Prim algorithm correctly find a minimum cost spanning tree.

Kruskal algorithm b c d a i e h g f (h,g), (c,i), (g,f), (a,b), (c,f), (c,d), (i,g), (i,h), (b,c), (a,h), (d,e), (e,f), (b,h) 8 7 b c d 9 4 2 a i e 11 14 4 7 6 10 8 h g f 1 2

Kruskal algorithm Θ(mlogm)

Prim algorithm 8 7 b c d 4 9 2 a i e 11 14 4 7 6 10 8 h g f 1 2

Θ(n2) Heap: Θ(mlogn)

Comparison General Dense Prim O(mlogn) O(m) Kruskal O(mlogm) Dijkstra

Comparison Kruskal demo Prim demo

Greed is good. Greed, for lack of a better word, is good. Greed is right. Greed works. Greed clarifies, cuts through, and captures, the essence of the evolutionary spirit. Greed, in all of its forms; greed for life, for money, for love, knowledge, has marked the upward surge of mankind and greed, you mark my words, will not only save Teldar Paper, but that other malfunctioning corporation called the U.S.A. ---- Michael Douglas, Wall Street click Wall Street movie image to play clip from Wall Street (iconic film about 1980s excess - directed by Oliver Stone and starring Michael Douglas) Wall Street 2 (directed by Oliver Stone and starring Michael Douglas) is the sequel revolving around the 2008 stock market crash.

Dijkstra algorithm, Prim algorithm, Kruskal algorithm Greedy approach A greedy algorithm always makes the choice that looks best at the moment. locally optimal choices --> a globally optimal solution. Greedy algorithms DO NOT always yield optimal solutions, but for many problems they do. Dijkstra algorithm, Prim algorithm, Kruskal algorithm

Where are we? Minimum spanning tree How to design greedy algorithms Change-making problem Activity selection problem Huffman code Knapsack problem

Change making 1 2 5 1 2 7 10 16? Problem: ChangeMaking Input: an integer m and a coin system a1, a2, ..., an Output: the minimum number of coins needed 1 2 5 1 2 7 10 16?

Activity selection problem Problem: ActivitySelection Input: a set S = {a1, a2, ... , an} of n proposed activities, each ai has a starting time si and a finishing time fi with 0 ≤ si < fi <∞ Output: A maximum-size subset of compatible activities

Activity-selection Theorem Consider any nonempty subproblem Sk, and let am be an activity in Sk with the earliest finish time. Then am is included in some maximum-size subset of mutually compatible activities of Sk .

Huffman code Robert Fano Claude Shannon David Huffman

Ambiguity Morse code: SOS ? V7 ? IAMIE ? EEWNI ?

Prefix-free code Prefix code: no codeword is a prefix of some other codeword. Use a binary tree to represent a prefix-free code.

Huffman code a b c d e f Frequency Fixed-length Huffman code 45 13 12 16 9 5 Fixed-length 000 001 010 011 100 101 Huffman code 111 1101 1100 Robert M. Fano, 100,000 characters, Fixed length code: 300,000 bits Huffman code: 224,000 bits

Huffman code O(nlogn) a b c d e f 45 13 12 16 9 5 100 1 a:45 55 1 25 1 a:45 55 1 25 30 1 1 c:12 b:13 14 d:16 1 f:5 e:9

Huffman code

Correctness Lemma Let x and y be two characters in C having the lowest frequencies. Then there exists an optimal prefix code for C in which x and y have the same length and differ only in the last bit. Lemma Let x and y be two characters in C having the lowest frequencies. C’ = C-{x,y} +{z}, fz= fx + fy. If T’ is an optimal prefix code for C’, then T=T’+{(z,x),(z,y)} is an optimal prefix code for C. Theorem Procedure HUFFMAN produces an optimal prefix code.

Knapsack problem Problem: Knapsack Input: A set of items U = {u1,...,un} with sizes s1,s2,...,sn and values v1,v2,...,vn and a knapsack capacity C Output: The maximum value that can be put into the knapsack

What if the thief can take fractions of items? Knapsack problem What if the thief can take fractions of items? 0-1 knapsack

Conclusion Minimum spanning tree How to design greedy algorithms Change-making problem Activity selection problem Huffman code Knapsack problem