Docsity

Algorithm Design - Greedy Algorithm, Huffman Coding | ECS 122A, Assignments of Computer Science

Partial preview of the text

Download Algorithm Design - Greedy Algorithm, Huffman Coding | ECS 122A and more Assignments Computer Science in PDF only on Docsity!

Discussion # 5

Yuanbo Zhu

Greedy algorithm• Huffman coding• Gas fulfill• Knapsack• MST Review

  • Hw#

Huffman Coding • Input: Frequency list • Output: code for each

Greedy Algorithm • Midas drives a car cross-country

  • Gas tank can hold enough gas to drive k miles.- Gas stations g1, g2, … gn are separated bydistances d1, d2, …, dn- Whenever he stops, he fills up his tank^ Which stations should he stop at so as tominimize the total number of stops?

Greedy alg. for Midas: Drive car as far as possible

. Only stops for gas if he could not make it to the next stop otherwise.

How to prove it?

the choice set of Madis^ >= the choice set of OPT Excel for the first step => excel for all

Solution – Greedy Algorithm • Define : v/w ratio = value/weight • Sorting v/w in decreasing order • Pick the one with high v/w until meetweight constraint

Minimum Spanning Trees • Given a^ connected

,^ undirected graph

, a spanning tree^ of that graph is a subgraph

which is a tree and connects all the

vertices^ together.

-^ A single graph can have many different spanningtrees.•^ A^ minimum spanning tree

(MST) is the spanning tree withminimum sum weights onedges.

Minimum Spanning Trees