Docsity

Greedy Algorithms: Kruskal's Minimum Spanning Tree and Huffman Codes, Study notes of Computer Science

Partial preview of the text

Download Greedy Algorithms: Kruskal's Minimum Spanning Tree and Huffman Codes and more Study notes Computer Science in PDF only on Docsity!

Greedy Algorithm

• A greedy algorithm always makes the choice that

looks best at the moment

• Key point: Greed makes a locally optimal choice in

the hope that this choice will lead to a globally

optimal solution

• Note: Greedy algorithms do not always yield

optimal solutions, but for SOME problems they do

Greed

• When do we use greedy algorithms?

– When we need a heuristic (e.g., hard problems

like the Traveling Salesman Problem)

– When the problem itself is “greedy”

  • Greedy Choice Property (CLRS 16.2)• Optimal Substructure Property (shared with DP) (CLRS 16.2)

• Examples:

– Minimum Spanning Tree (Kruskal’s algorithm)

– Optimal Prefix Codes (Huffman’s algorithm)

Greedy Algorithm: Minimum Spanning Tree

  • Kruskal’s minimum spanning tree algorithm – INPUT: edge-weighted graph G = (V,E), with |V| = n
    • OUTPUT: T a spanning tree of G (touches all vertices, and thereforehas n-1 edges) of minimum cost (= total edge weight)
    • Will grow a set of edges T until it contains n-1 edges– Algorithm: Start with T empty, then iteratively add to T the smallest (minimum-cost) remaining edge in the graph if the edge does notform a cycle in T Claim: Greedy-MST is correct. Proof (by induction): ß Define athat is a subgraph promising of some MST. We will show by edge set E’ ⊆ E to be any edge set induction that as T is constructed, T is always promising.

Proof of Kruskal’s Algorithm

ß Basis: |T| = 0, trivial.

ß Induction Step: T is promising by I.H., so it is a

subgraph of some MST, call it S. Let ei be the

smallest edge in E, s.t. T∪{ei} has no cycle, ei∉T.

ß If ei∈S, we’re done.

ß Suppose ei∉S, then S’ = S ∪ {ei} has a unique

cycle containing ei, and all other arcs in cycle ≤ ei

(because S is an MST!)

ß Call the cycle C. Observe that C with ei cannot

be in T, because T ∪ {ei} is acyclic (because

Kruskal adds ei)

Greedy Algorithm: Huffman Codes

• Prefix codes

– one code per input symbol

– no code is a prefix of another

• Why prefix codes?

  • Easy decoding
  • Since no codeword is a prefix of any other, the codewordthat begins an encoded file is unambiguous
  • Identify the initial codeword, translate it back to theoriginal character, and repeat the decoding process on the remainder of the encoded file

Greedy Algorithm: Huffman Codes

• Huffman coding

  • Given: frequencies with which with which sourcesymbols (e.g., A, B, C, …, Z) appear in a message
  • Goal is to minimize the expected encoded messagelength

• Create tree (leaf) node for each symbol that occurswith nonzero frequency

  • Node weights = frequencies

• Find two nodes with smallest frequency

• Create a new node with these two nodes aschildren, and with weight equal to the sum of the

weights of the two children

• Continue until have a single tree

Greedy Algorithm: Huffman Codes

Step 1: Step 2:

Step 3: Step 4:

AA MM

TT^ UU

AA MM

VV YY

AA MM

TT^ UU^22

TT^ UU

NN

VV YY

AA MM

Greedy Algorithm: Huffman Codes

Step 5:

Step 6:

TT^ UU

NN

VV YY

AA MM

OO RR

TT^ UU

NN

VV YY

AA MM

OO RR

SS GG

Greedy Algorithm: Huffman Codes

• Step 9

II EE

SS GG

VV YY

AA MM

TT^ UU

NN

OO RR

Greedy Algorithm: Huffman Codes

Finally:

• Note that the 0’s (left branches) and 1’s (right

branches) give the code words for each symbol

II EE

SS GG

VV YY

AA MM

TT^ UU

NN

OO RR

00^2424 11 1515

Dynamic Programming

• Dynamic programming: Divide problem into

overlapping subproblems; recursively solve each

in the same way.

• Similar to DQ, so what’s the difference:

– DQ partition the problem into independent

subproblems.

– DP breaking it into overlapping subproblems,

that is, when subproblems share subproblems.

– So DP saves work compared with DQ by solving

every subproblems just once ( when

subproblems are overlapping ).

Elements of Dynamic Programming

• Optimal substructure: A problem exhibits optimal

substructure if an optimal solution to the problem

contains within it optimal solutions to subproblems.

Whenever a problem exhibits optimal substructure, it is a good clue that DP might apply.(a greedy method mightapply also.)

• Overlapping subproblems: A recursive algorithm for

the problem solves the same subproblems over

and over, rather than always generating new

subproblems.

Dynamic Programming: Matrix Chain Product

• How do we build bottom-up?

1) From last example:

• Best way for triple: Cost ( A1 , A 2 ) + Cost(( A 1

A 2 ) × A 3 ) or Cost ( A2 , A 3 ) + Cost( A 1 (A 2 × A 3 )).

• Save the best solutions for contiguous groups

of Ai.

2) Cost of ( i × j )( j × k ) is ijk.

E.g., 3

Each of 3•10 entries requires 5 multiplies (+ 4 adds)

• Cost of final multiplication?

A 1 • A 2 • A 3 • … • Ak-1 • Ak • …• An.

• Each of these subproblems can be solved

optimally – just look in the table

d 1 × dk dk×dn+

Dynamic Programming: Matrix Chain Product