Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

CS 170 Discussion 7: Greedy Algorithms and Huffman Encoding, Lecture notes of Algorithms and Programming

Notes from a discussion on greedy algorithms and Huffman encoding in CS 170 at UC Berkeley in Fall 2017. The discussion covers approximating set cover, proving the optimality of greedy algorithms, and the basics of Huffman encoding. The notes include examples and explanations of the concepts discussed.

Typology: Lecture notes

2016/2017

Uploaded on 05/11/2023

damyen
damyen 🇺🇸

4.4

(27)

35 documents

1 / 10

Toggle sidebar

Related documents


Partial preview of the text

Download CS 170 Discussion 7: Greedy Algorithms and Huffman Encoding and more Lecture notes Algorithms and Programming in PDF only on Docsity! CS 170 DISCUSSION 7 GREEDY ALGORITHMS Raymond Chan UC Berkeley Fall 17 Raymond Chan, UC Berkeley Fall 2017 APPROXIMATING SET COVER • See board or notes/video Raymond Chan, UC Berkeley Fall 2017 GREEDY IS AT LEAST BETTER THAN OPTIMAL • Suppose you have an optimal solution S* and greedy solution S. • Look at the first value in which greedy and optimal differ. Usually talk about an ordering. • Prove that at this point, the greedy solution is at least as better as the optimal solution. And by induction, the greedy solution is better at each differing value. • Prove optimality via contradiction. Assume greedy is not optimal. Use above point to derive a contradiction. • Discussion today: Meeting Scheduling Raymond Chan, UC Berkeley Fall 2017 APPROACH 1 VS 2 • Use exchange argument (1) instead of contradictory proofs (2) because there could be multiple optimal solutions. • Both require induction. • Nuanced difference. Exchange argument takes some optimal ordering and swaps elements in the ordering that violate greedy heuristic. We will reach greedy without increasing cost. • Approach 2 compares specific differing values in greedy and optimal solutions. Choosing the greedy value at that point is at least as good as optimal. Raymond Chan, UC Berkeley Fall 2017 HUFFMAN ENCODING • Motivation: Given symbols and their respective probabilities, can we encode the symbols to prevent ambiguity and minimize length of longest encoding? • Prefix-free encoding: no codeword can be a prefix of another codeword. • Use full binary tree. Nodes have 0 or 2 children. • Leaves are symbols. • Internal nodes (v) have 2 children (u, w) with probability (or frequency) equal to sum of children’s probabilities. f(v) = f(u) + f(w)
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved