CS 161: Design and Analysis of Algorithms

Size: px
Start display at page:

Download "CS 161: Design and Analysis of Algorithms"

Transcription

1 CS 161: Design and Analysis o Algorithms

2 Announcements Homework 3, problem 3 removed

3 Greedy Algorithms 4: Human Encoding/Set Cover Human Encoding Set Cover

4 Alphabets and Strings Alphabet = inite set o symbols English alphabet = {a,b,c,,z} Hex values = {0,1,,9,A,B,C,D,E,F} String = sequence o symbols rom some alphabet This is a string

5 How to Encode Computers store things as 0s and 1s How do we encode strings as sequence o bits? Must be invertible (one-to-one) What to use as ew bits as possible One approach: choose encoding or characters, induce encoding o strings by concatenating codes or each character

6 How to Encode Obvious solution: I alphabet size is 2 k or some k, encode each character using k bits Each character takes k bits n characters kn bits total Letter Encoding A 00 B 01 C 10 D 11 ABACBDAAADBAC

7 How to Encode Issues: Wasteul: I not exactly 2 k characters, some sequences never used Letter A 00 B 01 C 10 Encoding Never use 11

8 How to Encode Issues: What i one character occurs very oten? AAAAAAABAAACAABAADAAAAAAACAAAB I almost all letters are A s, then an encoding that uses ewer bits to represent A and more to represent everything else would save on space

9 Variable Length Encoding Variable Length Encoding = encoding o characters as bits where dierent letters may use a dierent number o bits Still need encoding on strings to be one-to-one. What does this say about the encoding or characters?

10 Variable Length Encoding Letter Encoding A 0 B 01 C 10 D 11 AC 010 BA 010 Not one-to-one!

11 Preix-Free Encoding A preix o a bit sequence is the irst i bits, or some i

12 Preix-Free Encoding A preix-ree encoding is an encoding o an alphabet such that no encoding o any character is a preix o the encoding o any other character Letter Encoding A 0 B 01 C 10 D 11 The encoding o A is a preix o the encoding o C

13 Preix-Free Encoding A preix-ree encoding is an encoding o an alphabet such that no encoding o any character is a preix o the encoding o any other character Letter Encoding A 0 B 10 C 110 D 111

14 Preix-Free Encoding Theorem: Any preix-ree encoding o an alphabet induces a one-to-one encoding o strings over that alphabet

15 Preix-Free Encoding Proo: Suppose toward contradiction that S and T are two dierent strings that map to the same sequence o bits Assume w.l.o.g. that S and T dier on the irst character. Let c be the irst character o S, d the irst character o T. Let E(c) and E(d) be the encodings o c and d Assume w.l.o.g. E(c) E(d)

16 Preix-Free Encoding Since all bits in encodings o S and T are the same, the irst E(d) bits are Thereore, the irst E(d) bits o E(c) are equal to E(d) E(d) is a preix o E(c) Since c was assumed dierent rom d, our encoding is not preix-ree.

17 Tree View o Preix-Free Encoding Every node represents a partial codeword Every node has two children, one or appending 0 to the partial codeword, one or appending 1. Leaves correspond to actual codewords Root is empty

18 Tree View o Preix-Free Encoding 0 1 A: B:11 C:100 D:101

19 Tree View o Preix-Free Encoding To encode: Find path rom root to character, concatenate edge labels To decode b 1 b 2 : Starting rom the root, ollow edge labeled b 1, then edge labeled b 2, until we ind a lea. Output that character, and start over rom the root

20 Optimal Encoding What is the best possible preix-ree encoding we can ind? Let n be the length o the string Let C be the cost o the encoding, deined as (length o encoding)/n C = average length o encoding o characters, weighted by requency

21 Optimal Encoding Let l i be the length o the encoding o character i Let i be the requency i occurs in the string i (number o instances o i)/n C = i i l i

22 Optimal Encoding l i is also the depth o character i in the encoding tree. Optimal encoding is always a ull binary tree I there is a node with only 1 child, replace node with child. Depth o leas only decreases.

23 Optimal Encoding Entropy: H = i Theorem (Shannon Coding Theorem): C H i log i

24 Proo O Coding Theorem Let g(x) = x log x Lemma: g( (x+y)/2 ) ( g(x)+g(y) )/2

25 Proo O Coding Theorem True when only 2 characters Only possible encoding is or each character to get 1 bit. C = 1 H g( 1) g( 2) = log 1 2 log 2 = 2 2 g = 2g(1/ 2) = 1

26 Proo o Coding Theorem Inductively assume true or m-1 characters Let T be the tree corresponding to an optimal encoding over some alphabet o m characters At least two leas at bottom level. Assume w.l.o.g. these correspond to characters 1 and 2 Replace all instances o characters 1 and 2 with a new character Has requency 1 + 2

27 Proo o Coding Theorem Now we have an alphabet o size m-1 Encoding or alphabet: start with T delete the nodes corresponding to characters 1 and 2 Assign the new character to the parent o these nodes (which is now a lea) New character has code length 1 less than deleted characters

28 Proo o Coding Theorem How does C change? Removed character 1 with length l, requency 1 Removed character 2 with length l, requency 2 Added new character, length l-1, requency C = i i l i C' = C ( 1 + 2) l + ( 1 + 2)( l 1) = C ( 1 + 2)

29 Proo o Coding Theorem By inductive assumption, Recall ) )log( ( log ' 'log ' ' H C i i i i i + + = = ) )log( ( log log ) )log( ( log log log H i i i = = 2 1 ' C C + + =

30 Proo o Coding Theorem ( ) 1 ) log( ) ( log log H C H g g g H H = = 2 ) ( 2 1 ) ( )log ( log log

31 How to Find Optimal Encoding Claim 1: There is an optimal solution where the two least requent characters have the longest codewords (i.e. lowest level o tree), and are identical except or last bit I not, swap these two characters with two o the characters with the longest codewords Can swap with two that are siblings

32 How to Find Optimal Encoding Assume the two lowest-requency characters are 1 and 2. What i we merge the two characters into a new character with requency 1 + 2? New character gets codeword obtained by dropping last bit o the codewords or 1 or 2

33 Merging Two Characters 0 1 A: B: D:101 C:1000 E:1001

34 Merging Two Characters 0 1 A: B:11 CE:100 D:101

35 How to Find Optimal Encoding Claim 2: For any optimal encoding, the encoding obtained by merging characters 1 and 2 must be an optimal encoding or the reduced alphabet, where characters 1 and 2 are replaced with a new character o requency 1 + 2

36 How to Find Optimal Encoding Character Frequency Codeword A 1 0 B 2 11 C D E Character Frequency Codeword A 1 0 B 2 11 CE D 4 101

37 How to Find Optimal Encoding Idea: Take two characters with lowest requency Merge them Recursively solve reduced problem Split characters apart again

38 How to Find Optimal Encoding Character Frequency Codeword A 0.45 B 0.25 C 0.10 D 0.15 E 0.05

39 How to Find Optimal Encoding Character Frequency Codeword A 0.45 B 0.25 C 0.10 D 0.15 E 0.05

40 How to Find Optimal Encoding Character Frequency Codeword A 0.45 B 0.25 [CE] 0.15 D 0.15

41 How to Find Optimal Encoding Character Frequency Codeword A 0.45 B 0.25 [CE] 0.15 D 0.15

42 How to Find Optimal Encoding Character Frequency Codeword A 0.45 B 0.25 [[CE]D] 0.30

43 How to Find Optimal Encoding Character Frequency Codeword A 0.45 B 0.25 [[CE]D] 0.30

44 How to Find Optimal Encoding Character Frequency Codeword A 0.45 [[[CE]D]B] 0.55

45 How to Find Optimal Encoding Character Frequency Codeword A 0.45 [[[CE]D]B] 0.55

46 How to Find Optimal Encoding Character Frequency Codeword [A[[[CE]D]B]] 1.00

47 How to Find Optimal Encoding Character Frequency Codeword [A[[[CE]D]B]] 1.00 [A[[[CE]D]B]]

48 How to Find Optimal Encoding Character Frequency Codeword A [[[CE]D]B] A 0 1 [[[CE]D]B]

49 How to Find Optimal Encoding Character Frequency Codeword A B [[CE]D] A [[CE]D] B

50 How to Find Optimal Encoding Character Frequency Codeword A B [CE] D A B [CE] D

51 How to Find Optimal Encoding Character Frequency Codeword A B C D E A B 0 1 D C E

52 How to Find Optimal Encoding Let q be a heap o characters, ordered by requency For each character c, q.insert(c) While q has at least two characters: c 1 = q.deletemin(), c 2 = q.deletemin() Create a node labeled [c 1 c 2 ] with children c 1 and c 2 ([c 1 c 2 ] ) = (c 1 ) + (c 2 ) q.insert ([c 1 c 2 ] ) Return q.deletemin()

53 Running Time n inserts initially: O(n log n) Every run o loop decreases size o heap by 1 n-1 runs o loop Each run o loop involves 3 heap operations: O(log n) Total running time: O(n log n)

54

55 Set Cover Given a set o elements B, and a collection o subsets S i, output a selection o the S i whose union is B, such that the number o subsets used is minimal.

56 Example: Schools Suppose we have a collection o towns, and we want to igure out the best towns to put schools Need at least one school within 20 miles o each town Every school should be in a town

57 Example: Schools B = set o towns S i = subset o towns within 20 miles o town i

58 Greedy Solution Obvious solution: repeatedly pick the set S i with the largest number o uncovered elements.

59 Example B = {1, 2, 3, 4, 5, 6} S 1 = {1, 2, 3} S 2 = {1, 4} S 3 = {2, 5} S 4 = {3, 6}

60 Example B = {1, 2, 3, 4, 5, 6} S 1 = {1, 2, 3} S 2 = {1, 4} S 3 = {2, 5} S 4 = {3, 6} Sets used: {} Greedy Algorithm Elements let: {1, 2, 3, 4, 5, 6}

61 Example B = {1, 2, 3, 4, 5, 6} S 1 = {1, 2, 3} S 2 = {1, 4} S 3 = {2, 5} S 4 = {3, 6} Sets used: {S 1 } Greedy Algorithm Elements let: {4, 5, 6}

62 Example B = {1, 2, 3, 4, 5, 6} S 1 = {1, 2, 3} S 2 = {1, 4} S 3 = {2, 5} S 4 = {3, 6} Sets used: {S 1, S 2 } Greedy Algorithm Elements let: {5, 6}

63 Example B = {1, 2, 3, 4, 5, 6} S 1 = {1, 2, 3} S 2 = {1, 4} S 3 = {2, 5} S 4 = {3, 6} Sets used: {S 1, S 2, S 3 } Greedy Algorithm Elements let: {6}

64 Example B = {1, 2, 3, 4, 5, 6} S 1 = {1, 2, 3} S 2 = {1, 4} S 3 = {2, 5} S 4 = {3, 6} Sets used: {S 1, S 2, S 3, S 4 } Greedy Algorithm Elements let: {}

65 Example B = {1, 2, 3, 4, 5, 6} S 1 = {1, 2, 3} S 2 = {1, 4} S 3 = {2, 5} S 4 = {3, 6} Sets used: {S 1, S 2, S 3, S 4 } Greedy Algorithm Optimal: { S 2, S 3, S 4 } Elements let: {}

66 Set Cover Greedy algorithm isn t optimal! Obtaining optimal solution believed hard Settle or approximation: I optimal uses k sets, want to get solution using only slightly more than k sets

67 Approximation Claim: I B contains n elements, and the optimal solution uses k sets, then greedy uses at most k ln n sets

68 Proo Let n t be the number o uncovered elements ater t iterations o greedy algorithm (n 0 = n) Remaining elements covered by the optimal k sets Must be some set with at least n t /k o the uncovered elements Thereore, greedy picks a set that covers at least n t /k o the remaining elements

69 Proo Greedy picks a set that covers at least n t /k o the remaining elements n t+1 n t - n t /k = n t (1-1/k) Thereore, n t n 0 (1-1/k) t = n(1-1/k) t

70 Proo Fact: 1-x e -x, with equality i and only i x = 0

71 Proo n t n(1-1/k) t < n(e -1/k ) t < ne -t/k Ater t = k ln n iterations, n t < n e -ln n = 1 Thereore, ater t = k ln n iterations, n t = 0 Thereore, greedy algorithm uses at most k ln n sets, as desired

72 Can We Do Better Our algorithm achieves an approximation ratio o ln n This gives two questions: Can the analysis be tightened so that greedy achieves a better approximation ratio? Are there more sophisticated algorithms that achieve better approximation ratio? Answer to both: most likely not I domr eicient algorithm can do much better, than we can solve a whole host o very diicult problems eiciently

16.3 The Huffman code problem

16.3 The Huffman code problem 6.3 The Human code problem Human codes are widely used and very eective techniques or data compression. The principle behind this technique is to design a binary character code or each character, using

More information

16.3 The Huffman code problem

16.3 The Huffman code problem 6.3 The Human code problem Human codes are widely used and very eective techniques or data compression. The principle behind this technique is to design a binary character code or each character, using

More information

Huffman Coding. Version of October 13, Version of October 13, 2014 Huffman Coding 1 / 27

Huffman Coding. Version of October 13, Version of October 13, 2014 Huffman Coding 1 / 27 Huffman Coding Version of October 13, 2014 Version of October 13, 2014 Huffman Coding 1 / 27 Outline Outline Coding and Decoding The optimal source coding problem Huffman coding: A greedy algorithm Correctness

More information

4.8 Huffman Codes. These lecture slides are supplied by Mathijs de Weerd

4.8 Huffman Codes. These lecture slides are supplied by Mathijs de Weerd 4.8 Huffman Codes These lecture slides are supplied by Mathijs de Weerd Data Compression Q. Given a text that uses 32 symbols (26 different letters, space, and some punctuation characters), how can we

More information

16 Greedy Algorithms

16 Greedy Algorithms 16 Greedy Algorithms Optimization algorithms typically go through a sequence of steps, with a set of choices at each For many optimization problems, using dynamic programming to determine the best choices

More information

CSE 421 Greedy: Huffman Codes

CSE 421 Greedy: Huffman Codes CSE 421 Greedy: Huffman Codes Yin Tat Lee 1 Compression Example 100k file, 6 letter alphabet: File Size: ASCII, 8 bits/char: 800kbits 2 3 > 6; 3 bits/char: 300kbits a 45% b 13% c 12% d 16% e 9% f 5% Why?

More information

February 24, :52 World Scientific Book - 9in x 6in soltys alg. Chapter 3. Greedy Algorithms

February 24, :52 World Scientific Book - 9in x 6in soltys alg. Chapter 3. Greedy Algorithms Chapter 3 Greedy Algorithms Greedy algorithms are algorithms prone to instant gratification. Without looking too far ahead, at each step they make a locally optimum choice, with the hope that it will lead

More information

Binary recursion. Unate functions. If a cover C(f) is unate in xj, x, then f is unate in xj. x

Binary recursion. Unate functions. If a cover C(f) is unate in xj, x, then f is unate in xj. x Binary recursion Unate unctions! Theorem I a cover C() is unate in,, then is unate in.! Theorem I is unate in,, then every prime implicant o is unate in. Why are unate unctions so special?! Special Boolean

More information

Text Compression through Huffman Coding. Terminology

Text Compression through Huffman Coding. Terminology Text Compression through Huffman Coding Huffman codes represent a very effective technique for compressing data; they usually produce savings between 20% 90% Preliminary example We are given a 100,000-character

More information

Scribe: Virginia Williams, Sam Kim (2016), Mary Wootters (2017) Date: May 22, 2017

Scribe: Virginia Williams, Sam Kim (2016), Mary Wootters (2017) Date: May 22, 2017 CS6 Lecture 4 Greedy Algorithms Scribe: Virginia Williams, Sam Kim (26), Mary Wootters (27) Date: May 22, 27 Greedy Algorithms Suppose we want to solve a problem, and we re able to come up with some recursive

More information

CS473-Algorithms I. Lecture 11. Greedy Algorithms. Cevdet Aykanat - Bilkent University Computer Engineering Department

CS473-Algorithms I. Lecture 11. Greedy Algorithms. Cevdet Aykanat - Bilkent University Computer Engineering Department CS473-Algorithms I Lecture 11 Greedy Algorithms 1 Activity Selection Problem Input: a set S {1, 2,, n} of n activities s i =Start time of activity i, f i = Finish time of activity i Activity i takes place

More information

Efficient Sequential Algorithms, Comp309. Motivation. Longest Common Subsequence. Part 3. String Algorithms

Efficient Sequential Algorithms, Comp309. Motivation. Longest Common Subsequence. Part 3. String Algorithms Efficient Sequential Algorithms, Comp39 Part 3. String Algorithms University of Liverpool References: T. H. Cormen, C. E. Leiserson, R. L. Rivest Introduction to Algorithms, Second Edition. MIT Press (21).

More information

Greedy Algorithms CHAPTER 16

Greedy Algorithms CHAPTER 16 CHAPTER 16 Greedy Algorithms In dynamic programming, the optimal solution is described in a recursive manner, and then is computed ``bottom up''. Dynamic programming is a powerful technique, but it often

More information

MCS-375: Algorithms: Analysis and Design Handout #G2 San Skulrattanakulchai Gustavus Adolphus College Oct 21, Huffman Codes

MCS-375: Algorithms: Analysis and Design Handout #G2 San Skulrattanakulchai Gustavus Adolphus College Oct 21, Huffman Codes MCS-375: Algorithms: Analysis and Design Handout #G2 San Skulrattanakulchai Gustavus Adolphus College Oct 21, 2016 Huffman Codes CLRS: Ch 16.3 Ziv-Lempel is the most popular compression algorithm today.

More information

EE 368. Weeks 5 (Notes)

EE 368. Weeks 5 (Notes) EE 368 Weeks 5 (Notes) 1 Chapter 5: Trees Skip pages 273-281, Section 5.6 - If A is the root of a tree and B is the root of a subtree of that tree, then A is B s parent (or father or mother) and B is A

More information

Design and Analysis of Algorithms

Design and Analysis of Algorithms CSE 101, Winter 018 D/Q Greed SP s DP LP, Flow B&B, Backtrack Metaheuristics P, NP Design and Analysis of Algorithms Lecture 8: Greed Class URL: http://vlsicad.ucsd.edu/courses/cse101-w18/ Optimization

More information

Algorithms Dr. Haim Levkowitz

Algorithms Dr. Haim Levkowitz 91.503 Algorithms Dr. Haim Levkowitz Fall 2007 Lecture 4 Tuesday, 25 Sep 2007 Design Patterns for Optimization Problems Greedy Algorithms 1 Greedy Algorithms 2 What is Greedy Algorithm? Similar to dynamic

More information

1. [1 pt] What is the solution to the recurrence T(n) = 2T(n-1) + 1, T(1) = 1

1. [1 pt] What is the solution to the recurrence T(n) = 2T(n-1) + 1, T(1) = 1 Asymptotics, Recurrence and Basic Algorithms 1. [1 pt] What is the solution to the recurrence T(n) = 2T(n-1) + 1, T(1) = 1 1. O(logn) 2. O(n) 3. O(nlogn) 4. O(n 2 ) 5. O(2 n ) 2. [1 pt] What is the solution

More information

COMP 250 Fall graph traversal Nov. 15/16, 2017

COMP 250 Fall graph traversal Nov. 15/16, 2017 Graph traversal One problem we oten need to solve when working with graphs is to decide i there is a sequence o edges (a path) rom one vertex to another, or to ind such a sequence. There are various versions

More information

Greedy Algorithms. CLRS Chapters Introduction to greedy algorithms. Design of data-compression (Huffman) codes

Greedy Algorithms. CLRS Chapters Introduction to greedy algorithms. Design of data-compression (Huffman) codes Greedy Algorithms CLRS Chapters 16.1 16.3 Introduction to greedy algorithms Activity-selection problem Design of data-compression (Huffman) codes (Minimum spanning tree problem) (Shortest-path problem)

More information

Larger K-maps. So far we have only discussed 2 and 3-variable K-maps. We can now create a 4-variable map in the

Larger K-maps. So far we have only discussed 2 and 3-variable K-maps. We can now create a 4-variable map in the EET 3 Chapter 3 7/3/2 PAGE - 23 Larger K-maps The -variable K-map So ar we have only discussed 2 and 3-variable K-maps. We can now create a -variable map in the same way that we created the 3-variable

More information

Horn Formulae. CS124 Course Notes 8 Spring 2018

Horn Formulae. CS124 Course Notes 8 Spring 2018 CS124 Course Notes 8 Spring 2018 In today s lecture we will be looking a bit more closely at the Greedy approach to designing algorithms. As we will see, sometimes it works, and sometimes even when it

More information

Global Constraints. Combinatorial Problem Solving (CPS) Enric Rodríguez-Carbonell (based on materials by Javier Larrosa) February 22, 2019

Global Constraints. Combinatorial Problem Solving (CPS) Enric Rodríguez-Carbonell (based on materials by Javier Larrosa) February 22, 2019 Global Constraints Combinatorial Problem Solving (CPS) Enric Rodríguez-Carbonell (based on materials by Javier Larrosa) February 22, 2019 Global Constraints Global constraints are classes o constraints

More information

16.Greedy algorithms

16.Greedy algorithms 16.Greedy algorithms 16.1 An activity-selection problem Suppose we have a set S = {a 1, a 2,..., a n } of n proposed activities that with to use a resource. Each activity a i has a start time s i and a

More information

Trees : Part 1. Section 4.1. Theory and Terminology. A Tree? A Tree? Theory and Terminology. Theory and Terminology

Trees : Part 1. Section 4.1. Theory and Terminology. A Tree? A Tree? Theory and Terminology. Theory and Terminology Trees : Part Section. () (2) Preorder, Postorder and Levelorder Traversals Definition: A tree is a connected graph with no cycles Consequences: Between any two vertices, there is exactly one unique path

More information

Data Structures and Algorithms

Data Structures and Algorithms Data Structures and Algorithms CS245-2008S-19 B-Trees David Galles Department of Computer Science University of San Francisco 19-0: Indexing Operations: Add an element Remove an element Find an element,

More information

Chapter 16: Greedy Algorithm

Chapter 16: Greedy Algorithm Chapter 16: Greedy Algorithm 1 About this lecture Introduce Greedy Algorithm Look at some problems solvable by Greedy Algorithm 2 Coin Changing Suppose that in a certain country, the coin dominations consist

More information

6. Finding Efficient Compressions; Huffman and Hu-Tucker

6. Finding Efficient Compressions; Huffman and Hu-Tucker 6. Finding Efficient Compressions; Huffman and Hu-Tucker We now address the question: how do we find a code that uses the frequency information about k length patterns efficiently to shorten our message?

More information

How many leaves on the decision tree? There are n! leaves, because every permutation appears at least once.

How many leaves on the decision tree? There are n! leaves, because every permutation appears at least once. Chapter 8. Sorting in Linear Time Types of Sort Algorithms The only operation that may be used to gain order information about a sequence is comparison of pairs of elements. Quick Sort -- comparison-based

More information

Data Compression Techniques

Data Compression Techniques Data Compression Techniques Part 1: Entropy Coding Lecture 1: Introduction and Huffman Coding Juha Kärkkäinen 31.10.2017 1 / 21 Introduction Data compression deals with encoding information in as few bits

More information

CSC 373 Lecture # 3 Instructor: Milad Eftekhar

CSC 373 Lecture # 3 Instructor: Milad Eftekhar Huffman encoding: Assume a context is available (a document, a signal, etc.). These contexts are formed by some symbols (words in a document, discrete samples from a signal, etc). Each symbols s i is occurred

More information

Design and Analysis of Algorithms

Design and Analysis of Algorithms Design and Analysis of Algorithms Instructor: SharmaThankachan Lecture 10: Greedy Algorithm Slides modified from Dr. Hon, with permission 1 About this lecture Introduce Greedy Algorithm Look at some problems

More information

CS521 \ Notes for the Final Exam

CS521 \ Notes for the Final Exam CS521 \ Notes for final exam 1 Ariel Stolerman Asymptotic Notations: CS521 \ Notes for the Final Exam Notation Definition Limit Big-O ( ) Small-o ( ) Big- ( ) Small- ( ) Big- ( ) Notes: ( ) ( ) ( ) ( )

More information

Greedy Algorithms. Textbook reading. Chapter 4 Chapter 5. CSci 3110 Greedy Algorithms 1/63

Greedy Algorithms. Textbook reading. Chapter 4 Chapter 5. CSci 3110 Greedy Algorithms 1/63 CSci 3110 Greedy Algorithms 1/63 Greedy Algorithms Textbook reading Chapter 4 Chapter 5 CSci 3110 Greedy Algorithms 2/63 Overview Design principle: Make progress towards a solution based on local criteria

More information

CS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics

CS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics CS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics 1 Sorting 1.1 Problem Statement You are given a sequence of n numbers < a 1, a 2,..., a n >. You need to

More information

(2,4) Trees. 2/22/2006 (2,4) Trees 1

(2,4) Trees. 2/22/2006 (2,4) Trees 1 (2,4) Trees 9 2 5 7 10 14 2/22/2006 (2,4) Trees 1 Outline and Reading Multi-way search tree ( 10.4.1) Definition Search (2,4) tree ( 10.4.2) Definition Search Insertion Deletion Comparison of dictionary

More information

TU/e Algorithms (2IL15) Lecture 2. Algorithms (2IL15) Lecture 2 THE GREEDY METHOD

TU/e Algorithms (2IL15) Lecture 2. Algorithms (2IL15) Lecture 2 THE GREEDY METHOD Algorithms (2IL15) Lecture 2 THE GREEDY METHOD x y v w 1 Optimization problems for each instance there are (possibly) multiple valid solutions goal is to find an optimal solution minimization problem:

More information

These are not polished as solutions, but ought to give a correct idea of solutions that work. Note that most problems have multiple good solutions.

These are not polished as solutions, but ought to give a correct idea of solutions that work. Note that most problems have multiple good solutions. CSE 591 HW Sketch Sample Solutions These are not polished as solutions, but ought to give a correct idea of solutions that work. Note that most problems have multiple good solutions. Problem 1 (a) Any

More information

Greedy algorithms 2 4/5/12. Knapsack problems: Greedy or not? Compression algorithms. Data compression. David Kauchak cs302 Spring 2012

Greedy algorithms 2 4/5/12. Knapsack problems: Greedy or not? Compression algorithms. Data compression. David Kauchak cs302 Spring 2012 Knapsack problems: Greedy or not? Greedy algorithms 2 avid Kauchak cs02 Spring 12 l 0-1 Knapsack thief robbing a store finds n items worth v 1, v 2,.., v n dollars and weight w 1, w 2,, w n pounds, where

More information

looking ahead to see the optimum

looking ahead to see the optimum ! Make choice based on immediate rewards rather than looking ahead to see the optimum! In many cases this is effective as the look ahead variation can require exponential time as the number of possible

More information

Sorting and Selection

Sorting and Selection Sorting and Selection Introduction Divide and Conquer Merge-Sort Quick-Sort Radix-Sort Bucket-Sort 10-1 Introduction Assuming we have a sequence S storing a list of keyelement entries. The key of the element

More information

An undirected graph is a tree if and only of there is a unique simple path between any 2 of its vertices.

An undirected graph is a tree if and only of there is a unique simple path between any 2 of its vertices. Trees Trees form the most widely used subclasses of graphs. In CS, we make extensive use of trees. Trees are useful in organizing and relating data in databases, file systems and other applications. Formal

More information

CMSC 341 Leftist Heaps

CMSC 341 Leftist Heaps CMSC 341 Leftist Heaps Based on slides from previous iterations of this course Today s Topics Review of Min Heaps Introduction of Left-ist Heaps Merge Operation Heap Operations Review of Heaps Min Binary

More information

Building a network. Properties of the optimal solutions. Trees. A greedy approach. Lemma (1) Lemma (2) Lemma (3) Lemma (4)

Building a network. Properties of the optimal solutions. Trees. A greedy approach. Lemma (1) Lemma (2) Lemma (3) Lemma (4) Chapter 5. Greedy algorithms Minimum spanning trees Building a network Properties of the optimal solutions Suppose you are asked to network a collection of computers by linking selected pairs of them.

More information

Chapter 5. Greedy algorithms

Chapter 5. Greedy algorithms Chapter 5. Greedy algorithms Minimum spanning trees Building a network Suppose you are asked to network a collection of computers by linking selected pairs of them. This translates into a graph problem

More information

Algorithms (VI) Greedy Algorithms. Guoqiang Li. School of Software, Shanghai Jiao Tong University

Algorithms (VI) Greedy Algorithms. Guoqiang Li. School of Software, Shanghai Jiao Tong University Algorithms (VI) Greedy Algorithms Guoqiang Li School of Software, Shanghai Jiao Tong University Review of the Previous Lecture Lengths on Edges BFS treats all edges as having the same length. It is rarely

More information

CS F-11 B-Trees 1

CS F-11 B-Trees 1 CS673-2016F-11 B-Trees 1 11-0: Binary Search Trees Binary Tree data structure All values in left subtree< value stored in root All values in the right subtree>value stored in root 11-1: Generalizing BSTs

More information

CSE 5311 Notes 4a: Priority Queues

CSE 5311 Notes 4a: Priority Queues Chart on p., CLRS (binary, Fibonacci heaps) CSE Notes a: Priority Queues (Last updated 9// : AM) MAKE-HEAP INSERT MINIMUM EXTRACT-MIN UNION (MELD/MERGE) DECREASE-KEY DELETE Applications - sorting (?),

More information

Analysis of Algorithms - Greedy algorithms -

Analysis of Algorithms - Greedy algorithms - Analysis of Algorithms - Greedy algorithms - Andreas Ermedahl MRTC (Mälardalens Real-Time Reseach Center) andreas.ermedahl@mdh.se Autumn 2003 Greedy Algorithms Another paradigm for designing algorithms

More information

(2,4) Trees Goodrich, Tamassia (2,4) Trees 1

(2,4) Trees Goodrich, Tamassia (2,4) Trees 1 (2,4) Trees 9 2 5 7 10 14 2004 Goodrich, Tamassia (2,4) Trees 1 Multi-Way Search Tree A multi-way search tree is an ordered tree such that Each internal node has at least two children and stores d -1 key-element

More information

Multi-way Search Trees. (Multi-way Search Trees) Data Structures and Programming Spring / 25

Multi-way Search Trees. (Multi-way Search Trees) Data Structures and Programming Spring / 25 Multi-way Search Trees (Multi-way Search Trees) Data Structures and Programming Spring 2017 1 / 25 Multi-way Search Trees Each internal node of a multi-way search tree T: has at least two children contains

More information

7. Sorting I. 7.1 Simple Sorting. Problem. Algorithm: IsSorted(A) 1 i j n. Simple Sorting

7. Sorting I. 7.1 Simple Sorting. Problem. Algorithm: IsSorted(A) 1 i j n. Simple Sorting Simple Sorting 7. Sorting I 7.1 Simple Sorting Selection Sort, Insertion Sort, Bubblesort [Ottman/Widmayer, Kap. 2.1, Cormen et al, Kap. 2.1, 2.2, Exercise 2.2-2, Problem 2-2 19 197 Problem Algorithm:

More information

Algorithms (IX) Yijia Chen Shanghai Jiaotong University

Algorithms (IX) Yijia Chen Shanghai Jiaotong University Algorithms (IX) Yijia Chen Shanghai Jiaotong University Review of the Previous Lecture Shortest paths in the presence of negative edges Negative edges Dijkstra s algorithm works in part because the shortest

More information

6. Finding Efficient Compressions; Huffman and Hu-Tucker Algorithms

6. Finding Efficient Compressions; Huffman and Hu-Tucker Algorithms 6. Finding Efficient Compressions; Huffman and Hu-Tucker Algorithms We now address the question: How do we find a code that uses the frequency information about k length patterns efficiently, to shorten

More information

Design and Analysis of Algorithms 演算法設計與分析. Lecture 7 April 6, 2016 洪國寶

Design and Analysis of Algorithms 演算法設計與分析. Lecture 7 April 6, 2016 洪國寶 Design and Analysis of Algorithms 演算法設計與分析 Lecture 7 April 6, 2016 洪國寶 1 Course information (5/5) Grading (Tentative) Homework 25% (You may collaborate when solving the homework, however when writing up

More information

Multi-Way Search Trees

Multi-Way Search Trees Multi-Way Search Trees Manolis Koubarakis 1 Multi-Way Search Trees Multi-way trees are trees such that each internal node can have many children. Let us assume that the entries we store in a search tree

More information

Priority Queues. 1 Introduction. 2 Naïve Implementations. CSci 335 Software Design and Analysis III Chapter 6 Priority Queues. Prof.

Priority Queues. 1 Introduction. 2 Naïve Implementations. CSci 335 Software Design and Analysis III Chapter 6 Priority Queues. Prof. Priority Queues 1 Introduction Many applications require a special type of queuing in which items are pushed onto the queue by order of arrival, but removed from the queue based on some other priority

More information

CMPSCI 240 Reasoning Under Uncertainty Homework 4

CMPSCI 240 Reasoning Under Uncertainty Homework 4 CMPSCI 240 Reasoning Under Uncertainty Homework 4 Prof. Hanna Wallach Assigned: February 24, 2012 Due: March 2, 2012 For this homework, you will be writing a program to construct a Huffman coding scheme.

More information

In this lecture, we ll look at applications of duality to three problems:

In this lecture, we ll look at applications of duality to three problems: Lecture 7 Duality Applications (Part II) In this lecture, we ll look at applications of duality to three problems: 1. Finding maximum spanning trees (MST). We know that Kruskal s algorithm finds this,

More information

8. Sorting II. 8.1 Heapsort. Heapsort. [Max-]Heap 6. Heapsort, Quicksort, Mergesort. Binary tree with the following properties Wurzel

8. Sorting II. 8.1 Heapsort. Heapsort. [Max-]Heap 6. Heapsort, Quicksort, Mergesort. Binary tree with the following properties Wurzel Heapsort, Quicksort, Mergesort 8. Sorting II 8.1 Heapsort [Ottman/Widmayer, Kap. 2.3, Cormen et al, Kap. 6] 9 210 Heapsort [Max-]Heap 6 Binary tree with the following properties Wurzel Inspiration from

More information

CSE 431/531: Algorithm Analysis and Design (Spring 2018) Greedy Algorithms. Lecturer: Shi Li

CSE 431/531: Algorithm Analysis and Design (Spring 2018) Greedy Algorithms. Lecturer: Shi Li CSE 431/531: Algorithm Analysis and Design (Spring 2018) Greedy Algorithms Lecturer: Shi Li Department of Computer Science and Engineering University at Buffalo Main Goal of Algorithm Design Design fast

More information

Advanced algorithms. topological ordering, minimum spanning tree, Union-Find problem. Jiří Vyskočil, Radek Mařík 2012

Advanced algorithms. topological ordering, minimum spanning tree, Union-Find problem. Jiří Vyskočil, Radek Mařík 2012 topological ordering, minimum spanning tree, Union-Find problem Jiří Vyskočil, Radek Mařík 2012 Subgraph subgraph A graph H is a subgraph of a graph G, if the following two inclusions are satisfied: 2

More information

Fundamentals of Multimedia. Lecture 5 Lossless Data Compression Variable Length Coding

Fundamentals of Multimedia. Lecture 5 Lossless Data Compression Variable Length Coding Fundamentals of Multimedia Lecture 5 Lossless Data Compression Variable Length Coding Mahmoud El-Gayyar elgayyar@ci.suez.edu.eg Mahmoud El-Gayyar / Fundamentals of Multimedia 1 Data Compression Compression

More information

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Priority Queues / Heaps Date: 9/27/17

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Priority Queues / Heaps Date: 9/27/17 01.433/33 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Priority Queues / Heaps Date: 9/2/1.1 Introduction In this lecture we ll talk about a useful abstraction, priority queues, which are

More information

Algorithm Theory. 8 Treaps. Christian Schindelhauer

Algorithm Theory. 8 Treaps. Christian Schindelhauer Algorithm Theory 8 Treaps Institut für Informatik Wintersemester 2007/08 The Dictionary Problem Given: Universe (U,

More information

Trees. A tree is a directed graph with the property

Trees. A tree is a directed graph with the property 2: Trees Trees A tree is a directed graph with the property There is one node (the root) from which all other nodes can be reached by exactly one path. Seen lots of examples. Parse Trees Decision Trees

More information

managing an evolving set of connected components implementing a Union-Find data structure implementing Kruskal s algorithm

managing an evolving set of connected components implementing a Union-Find data structure implementing Kruskal s algorithm Spanning Trees 1 Spanning Trees the minimum spanning tree problem three greedy algorithms analysis of the algorithms 2 The Union-Find Data Structure managing an evolving set of connected components implementing

More information

Problem. Input: An array A = (A[1],..., A[n]) with length n. Output: a permutation A of A, that is sorted: A [i] A [j] for all. 1 i j n.

Problem. Input: An array A = (A[1],..., A[n]) with length n. Output: a permutation A of A, that is sorted: A [i] A [j] for all. 1 i j n. Problem 5. Sorting Simple Sorting, Quicksort, Mergesort Input: An array A = (A[1],..., A[n]) with length n. Output: a permutation A of A, that is sorted: A [i] A [j] for all 1 i j n. 98 99 Selection Sort

More information

Greedy Algorithms. Alexandra Stefan

Greedy Algorithms. Alexandra Stefan Greedy Algorithms Alexandra Stefan 1 Greedy Method for Optimization Problems Greedy: take the action that is best now (out of the current options) it may cause you to miss the optimal solution You build

More information

Name: Lirong TAN 1. (15 pts) (a) Define what is a shortest s-t path in a weighted, connected graph G.

Name: Lirong TAN 1. (15 pts) (a) Define what is a shortest s-t path in a weighted, connected graph G. 1. (15 pts) (a) Define what is a shortest s-t path in a weighted, connected graph G. A shortest s-t path is a path from vertex to vertex, whose sum of edge weights is minimized. (b) Give the pseudocode

More information

Lecture 5. Treaps Find, insert, delete, split, and join in treaps Randomized search trees Randomized search tree time costs

Lecture 5. Treaps Find, insert, delete, split, and join in treaps Randomized search trees Randomized search tree time costs Lecture 5 Treaps Find, insert, delete, split, and join in treaps Randomized search trees Randomized search tree time costs Reading: Randomized Search Trees by Aragon & Seidel, Algorithmica 1996, http://sims.berkeley.edu/~aragon/pubs/rst96.pdf;

More information

FINAL EXAM SOLUTIONS

FINAL EXAM SOLUTIONS COMP/MATH 3804 Design and Analysis of Algorithms I Fall 2015 FINAL EXAM SOLUTIONS Question 1 (12%). Modify Euclid s algorithm as follows. function Newclid(a,b) if a

More information

Lecture 15. Error-free variable length schemes: Shannon-Fano code

Lecture 15. Error-free variable length schemes: Shannon-Fano code Lecture 15 Agenda for the lecture Bounds for L(X) Error-free variable length schemes: Shannon-Fano code 15.1 Optimal length nonsingular code While we do not know L(X), it is easy to specify a nonsingular

More information

Binary heaps (chapters ) Leftist heaps

Binary heaps (chapters ) Leftist heaps Binary heaps (chapters 20.3 20.5) Leftist heaps Binary heaps are arrays! A binary heap is really implemented using an array! 8 18 29 20 28 39 66 Possible because of completeness property 37 26 76 32 74

More information

implementing the breadth-first search algorithm implementing the depth-first search algorithm

implementing the breadth-first search algorithm implementing the depth-first search algorithm Graph Traversals 1 Graph Traversals representing graphs adjacency matrices and adjacency lists 2 Implementing the Breadth-First and Depth-First Search Algorithms implementing the breadth-first search algorithm

More information

Discrete mathematics

Discrete mathematics Discrete mathematics Petr Kovář petr.kovar@vsb.cz VŠB Technical University of Ostrava DiM 470-2301/02, Winter term 2018/2019 About this file This file is meant to be a guideline for the lecturer. Many

More information

Multiway Search Trees. Multiway-Search Trees (cont d)

Multiway Search Trees. Multiway-Search Trees (cont d) Multiway Search Trees Each internal node v of a multi-way search tree T has at least two children contains d-1 items, where d is the number of children of v an item is of the form (k i,x i ) for 1 i d-1,

More information

CSE332: Data Abstractions Lecture 21: Parallel Prefix and Parallel Sorting. Tyler Robison Summer 2010

CSE332: Data Abstractions Lecture 21: Parallel Prefix and Parallel Sorting. Tyler Robison Summer 2010 CSE332: Data Abstractions Lecture 21: Parallel Preix and Parallel Sorting Tyler Robison Summer 2010 1 What next? 2 Done: Now: Simple ways to use parallelism or counting, summing, inding elements Analysis

More information

Huffman Codes (data compression)

Huffman Codes (data compression) Huffman Codes (data compression) Data compression is an important technique for saving storage Given a file, We can consider it as a string of characters We want to find a compressed file The compressed

More information

Priority Queues and Huffman Trees

Priority Queues and Huffman Trees Priority Queues and Huffman Trees 1 the Heap storing the heap with a vector deleting from the heap 2 Binary Search Trees sorting integer numbers deleting from a binary search tree 3 Huffman Trees encoding

More information

Minimum Spanning Trees

Minimum Spanning Trees Minimum Spanning Trees Overview Problem A town has a set of houses and a set of roads. A road connects and only houses. A road connecting houses u and v has a repair cost w(u, v). Goal: Repair enough (and

More information

Welcome Back to Fundamentals of Multimedia (MR412) Fall, 2012 Lecture 10 (Chapter 7) ZHU Yongxin, Winson

Welcome Back to Fundamentals of Multimedia (MR412) Fall, 2012 Lecture 10 (Chapter 7) ZHU Yongxin, Winson Welcome Back to Fundamentals of Multimedia (MR412) Fall, 2012 Lecture 10 (Chapter 7) ZHU Yongxin, Winson zhuyongxin@sjtu.edu.cn 2 Lossless Compression Algorithms 7.1 Introduction 7.2 Basics of Information

More information

Greedy algorithms part 2, and Huffman code

Greedy algorithms part 2, and Huffman code Greedy algorithms part 2, and Huffman code Two main properties: 1. Greedy choice property: At each decision point, make the choice that is best at the moment. We typically show that if we make a greedy

More information

Graphs and Network Flows ISE 411. Lecture 7. Dr. Ted Ralphs

Graphs and Network Flows ISE 411. Lecture 7. Dr. Ted Ralphs Graphs and Network Flows ISE 411 Lecture 7 Dr. Ted Ralphs ISE 411 Lecture 7 1 References for Today s Lecture Required reading Chapter 20 References AMO Chapter 13 CLRS Chapter 23 ISE 411 Lecture 7 2 Minimum

More information

15.4 Longest common subsequence

15.4 Longest common subsequence 15.4 Longest common subsequence Biological applications often need to compare the DNA of two (or more) different organisms A strand of DNA consists of a string of molecules called bases, where the possible

More information

Department of Computer Science and Engineering Analysis and Design of Algorithm (CS-4004) Subject Notes

Department of Computer Science and Engineering Analysis and Design of Algorithm (CS-4004) Subject Notes Page no: Department of Computer Science and Engineering Analysis and Design of Algorithm (CS-00) Subject Notes Unit- Greedy Technique. Introduction: Greedy is the most straight forward design technique.

More information

COMP : Trees. COMP20012 Trees 219

COMP : Trees. COMP20012 Trees 219 COMP20012 3: Trees COMP20012 Trees 219 Trees Seen lots of examples. Parse Trees Decision Trees Search Trees Family Trees Hierarchical Structures Management Directories COMP20012 Trees 220 Trees have natural

More information

2-3 and Trees. COL 106 Shweta Agrawal, Amit Kumar, Dr. Ilyas Cicekli

2-3 and Trees. COL 106 Shweta Agrawal, Amit Kumar, Dr. Ilyas Cicekli 2-3 and 2-3-4 Trees COL 106 Shweta Agrawal, Amit Kumar, Dr. Ilyas Cicekli Multi-Way Trees A binary search tree: One value in each node At most 2 children An M-way search tree: Between 1 to (M-1) values

More information

Multi-Way Search Trees

Multi-Way Search Trees Multi-Way Search Trees Manolis Koubarakis 1 Multi-Way Search Trees Multi-way trees are trees such that each internal node can have many children. Let us assume that the entries we store in a search tree

More information

1 Minimum Cut Problem

1 Minimum Cut Problem CS 6 Lecture 6 Min Cut and Karger s Algorithm Scribes: Peng Hui How, Virginia Williams (05) Date: November 7, 07 Anthony Kim (06), Mary Wootters (07) Adapted from Virginia Williams lecture notes Minimum

More information

Assignment 5: Solutions

Assignment 5: Solutions Algorithm Design Techniques Assignment 5: Solutions () Port Authority. [This problem is more commonly called the Bin Packing Problem.] (a) Suppose K = 3 and (w, w, w 3, w 4 ) = (,,, ). The optimal solution

More information

CSCI 136 Data Structures & Advanced Programming. Lecture 22 Fall 2018 Instructor: Bills

CSCI 136 Data Structures & Advanced Programming. Lecture 22 Fall 2018 Instructor: Bills CSCI 136 Data Structures & Advanced Programming Lecture 22 Fall 2018 Instructor: Bills Last Time Lab 7: Two Towers Array Representations of (Binary) Trees Application: Huffman Encoding 2 Today Improving

More information

CS 337 Project 1: Minimum-Weight Binary Search Trees

CS 337 Project 1: Minimum-Weight Binary Search Trees CS 337 Project 1: Minimum-Weight Binary Search Trees September 6, 2006 1 Preliminaries Let X denote a set of keys drawn from some totally ordered universe U, such as the set of all integers, or the set

More information

Thus, it is reasonable to compare binary search trees and binary heaps as is shown in Table 1.

Thus, it is reasonable to compare binary search trees and binary heaps as is shown in Table 1. 7.2 Binary Min-Heaps A heap is a tree-based structure, but it doesn t use the binary-search differentiation between the left and right sub-trees to create a linear ordering. Instead, a binary heap only

More information

CS 161: Design and Analysis of Algorithms

CS 161: Design and Analysis of Algorithms CS 161: Design and Analysis of Algorithms Greedy Algorithms 2: Minimum Spanning Trees Definition The cut property Prim s Algorithm Kruskal s Algorithm Disjoint Sets Tree A tree is a connected graph with

More information

Lecture 4: September 11, 2003

Lecture 4: September 11, 2003 Algorithmic Modeling and Complexity Fall 2003 Lecturer: J. van Leeuwen Lecture 4: September 11, 2003 Scribe: B. de Boer 4.1 Overview This lecture introduced Fixed Parameter Tractable (FPT) problems. An

More information

Acyclic orientations do not lead to optimal deadlock-free packet routing algorithms

Acyclic orientations do not lead to optimal deadlock-free packet routing algorithms Acyclic orientations do not lead to optimal deadloc-ree pacet routing algorithms Daniel Šteanovič 1 Department o Computer Science, Comenius University, Bratislava, Slovaia Abstract In this paper e consider

More information

CS 6783 (Applied Algorithms) Lecture 5

CS 6783 (Applied Algorithms) Lecture 5 CS 6783 (Applied Algorithms) Lecture 5 Antonina Kolokolova January 19, 2012 1 Minimum Spanning Trees An undirected graph G is a pair (V, E); V is a set (of vertices or nodes); E is a set of (undirected)

More information

Greedy Algorithms. Subhash Suri. October 9, 2017

Greedy Algorithms. Subhash Suri. October 9, 2017 Greedy Algorithms Subhash Suri October 9, 2017 1 Introduction Greedy algorithms are a commonly used paradigm for combinatorial algorithms. Combinatorial problems intuitively are those for which feasible

More information

Heapsort. Why study Heapsort?

Heapsort. Why study Heapsort? Heapsort Material adapted courtesy of Prof. Dave Matuszek at UPENN Why study Heapsort? It is a well-known, traditional sorting algorithm you will be expected to know Heapsort is always O(n log n) Quicksort

More information