Download presentation
1
Lecture 9 Greedy Approach
Minimum spanning tree How to design greedy algorithms
2
Roadmap Minimum spanning tree How to design greedy algorithms
Change-making problem Activity selection problem Huffman code Knapsack problem
3
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
4
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
5
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.
6
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
7
Kruskal algorithm Θ(mlogm)
8
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
9
Θ(n2) Heap: Θ(mlogn)
10
Dense graph dense: m = n1+ε, ε is not too small. d-heap, d = m/n
complexity: O(ndlogdn + mlogdn) = O(m)
11
Comparison General Dense Prim O(mlogn) O(m) Kruskal O(mlogm) Dijkstra
12
Comparison Kruskal demo Prim demo
13
Correctness Both greedy algorithms Theorem
Kruskal algorithm and Prim algorithm correctly find a minimum cost spanning tree. Both greedy algorithms
14
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. 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. To be released in 2010.
15
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
16
Where are we? Minimum spanning tree How to design greedy algorithms
Change-making problem Activity selection problem Huffman code Knapsack problem
17
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 16?
18
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
19
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 .
20
Huffman code Robert Fano Claude Shannon David Huffman
21
Ambiguity Morse code: SOS ? V7 ? IAMIE ? EEWNI ?
22
Prefix-free code Prefix code: no codeword is a prefix of some other codeword. Use a binary tree to represent a prefix-free code.
23
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
24
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
25
Huffman code
26
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.
27
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
28
What if the thief can take fractions of items?
Knapsack problem What if the thief can take fractions of items? 0-1 knapsack
29
Horn formula Boolean variables:
x = the murder took place in the kitchen y = the butler is innocent z = the colonel was asleep at 8 pm Horn formula: Implication Negative clause
30
Horn formula Problem: HornFormula Input: Horn formula
Output: a satisfying assignment, if one exists 1. set all variables to false2. while there is an implication that is not satisfied: set the right-hand variable of the implication to true4. if all pure negative clauses are satisfied: return the assignment 6. else: return ‘‘formula is not satisfiable’’
31
Correctness Theorem If a certain set of variables is set to true, then they must be true in any satisfying assignment.
32
Conclusion Minimum spanning tree How to design greedy algorithms
Change-making problem Activity selection problem Huffman code Knapsack problem
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.