Download presentation

Presentation is loading. Please wait.

Published byNathaniel Berkshire Modified about 1 year ago

1
Minimum spanning tree How to design greedy algorithms Lecture 9 Greedy Approach

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 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 fg 4 2 7 4 10 9 8 11 87 1 2 6 14 a b h c i d e fg 4 2 7 4 10 9 8 11 87 1 2 6 14

5
Cut property a b h c i d e fg 4 2 7 4 10 9 8 11 87 1 2 6 14 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 a b h c i d e fg 4 2 7 4 10 9 8 11 87 1 2 6 14 (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)

7
Kruskal algorithm Θ(mlogm)

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

9
Θ(n 2 ) Heap: Θ(mlogn)

10
Dense graph dense: m = n 1+ε, ε is not too small. d-heap, d = m/n complexity: O(ndlog d n + mlog d n) = O(m)

11
Comparison GeneralDense PrimO(mlogn)O(m) KruskalO(mlogm) DijkstraO(mlogn)O(m)

12
Comparison Kruskal demo Prim demo

13
Correctness 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.

15
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 1016? Problem: ChangeMaking Input: an integer m and a coin system a 1, a 2,..., a n Output: the minimum number of coins needed

18
Activity selection problem Problem: ActivitySelection Input: a set S = {a 1, a 2,..., a n } of n proposed activities, each a i has a starting time s i and a finishing time f i with 0 ≤ si < fi <∞ Output: A maximum-size subset of compatible activities

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

20
Huffman code Robert FanoClaude Shannon David Huffman

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

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

23
Huffman code abcdef Frequency 4513121695 Fixed-length 000001010011100101 Huffman code 010110011111011100 100,000 characters, Fixed length code: 300,000 bits Huffman code: 224,000 bits

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

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}, f z = f x + f y. 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 = {u 1,...,u n } with sizes s 1,s 2,...,s n and values v 1,v 2,...,v n and a knapsack capacity C Output: The maximum value that can be put into the knapsack

28
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: 3. set the right-hand variable of the implication to true4. if all pure negative clauses are satisfied: 5. return the assignment 6. else: 7. 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

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google