0% found this document useful (0 votes)
17 views26 pages

323 Lecture Notes 8 Part 2

This document discusses the Greedy Technique in algorithms, focusing on Huffman Codes for data compression and Minimum Spanning Trees (MST). It explains the construction of Huffman Codes using a min-priority queue and outlines algorithms like Kruskal's and Prim's for finding MSTs. The document highlights the efficiency of these algorithms and their time complexities.

Uploaded by

ozenedabusee
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views26 pages

323 Lecture Notes 8 Part 2

This document discusses the Greedy Technique in algorithms, focusing on Huffman Codes for data compression and Minimum Spanning Trees (MST). It explains the construction of Huffman Codes using a min-priority queue and outlines algorithms like Kruskal's and Prim's for finding MSTs. The document highlights the efficiency of these algorithms and their time complexities.

Uploaded by

ozenedabusee
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

LECTURE 8: (Part 2)

Greedy Technique

CMPE 323 Algorithms


Based on
Levitin, “Introduction to the Design & Analysis Algorithms“,
Pearson, 2012

Cormen, Leiserson, Rivest, Stein, “Introduction to Algorithms“,


The MIT Press, 2009
Huffman Codes
 Coding of characters by binary strings
 Used for data compression
 Depending on data 20%-90% saving is possible
 Fixed-length code vs. variable-length code (better)
a b c d e f
 Example: Frequency 45 13 12 16 9 5
Fixed-length
000 001 010 011 100 101
Total cost = 300 bits coding codewords
Variable-length
0 101 100 111 1101 1100
Total cost = 224 bits coding codewords
Huffman Codes
 Prefix code: No codeword is also a prefix of some other
codeword
 Optimal data compression is achieved via prefix code
 Prefix codes are preferred since they simplify decoding (i.e.
unambiguous decoding)
 Example: uniquely decoded as . No other decoding is
possible!
Huffman Codes
 A binary tree whose leaves are the given characters, a suitable
representation
 Tree for the
optimal coding
is a full binary
tree
Not
optimal Optimal
Huffman Codes
 Full binary tree: every non-leaf node has two children
 Tree for an optimal prefix code has leaves and non-leaf
 Given a tree corresponding to a prefix code, let for a character ,
be the frequency of the character in the file and be the depth of ’s
leaf in the tree
 Total number of bits required for encoding Cost of the tree
Huffman Codes
 Constructing Huffman Code:
1. Put characters into a min-priority queue keyed on (frequency)
2. Take least frequent two objects from the queue
3. Merge them
4. Put the result into the queue back
Huffman Codes
Huffman Codes
Huffman Codes
Time complexity:
BUILD-MIN-HEAP :

𝜽(𝒍𝒈𝒏)
𝜽(𝒍𝒈𝒏)
Minimum Spanning Tree
 Given a weighted graph find an acyclic subset that connects all
of the vertices and whose total weight is minimized.

A spanning tree with total cost = 15 A spanning tree with total cost = 13
1 3 1 1 3

7 5 2 7 5 2 7

2 2
Given graph𝐺=(𝑉 , 𝐸) 1 3

2
Minimum Spanning Tree with total cost = 8
Minimum Spanning Tree
 Growing a Minimum Spanning Tree (MST):
 Grow the MST one edge at a time, iteratively without violating
the following loop invariant
 Before each iteration, is a subset of some MST
 Definition: At any time of iteration current MST is a set and if
is also a subset of a MST then is said to be a safe edge.
Minimum Spanning Tree
 Below algorithm uses Greedy strategy
 Question: How to find safe edges ?
Minimum Spanning Tree
 Example:
Minimum Spanning Tree
 Definition: A cut of an undirected graph is a partition of .
 Definition: An edge crosses the cut if one of its endpoints is
in and the other is in .
 Definition: An edge is a light edge crossing a cut if its weight
is the minimum of any edge crossing the cut.
 More than one light edge is possible.
Minimum Spanning Tree
 Definition: A cut respects a set of edges if no edge in
crosses the cut.
 Theorem:
 Let be a connected undirected graph with a real-valued weight
function defined on .
 Let be a subset of that is included in some Minimum Spanning Tree
for .
 Let be any cut of that respects
 Let be a light edge crossing

Then, the edge is safe for .


Minimum Spanning Tree
Minimum Spanning Tree
Minimum Spanning Tree: Kruskal’s
Algorithm
 A Greedy algorithm

 The safe edge added to A is always a least-weight edge in the


graph that connect two distinct components
 : Creates set with an element
 : Finds a set whose element is
 : Unions two sets whose elements are and
Minimum Spanning Tree: Kruskal’s
Algorithm
Minimum Spanning Tree: Kruskal’s
Algorithm
Minimum Spanning Tree: Kruskal’s
Algorithm
𝜽 ( 𝟏)
𝐎 (𝒍𝒈𝑽 ) 𝑶(𝑬𝒍𝒈𝑬)
𝑶(𝑬𝒍𝒈𝑬)

𝑶(𝒍𝒈𝑽 )
𝑶(𝒍𝒈𝑽 )
Minimum Spanning Tree: Kruskal’s
Algorithm
 Since graph is connected

 If then .
 So, time complexity is
Minimum Spanning Tree: Prim’s
Algorithm
 A Greedy algorithm

 The edges in the set always form a single tree.


 Starts with arbitrary vertex
 Grows until the tree spans all the vertices in
 Uses min-priority queue
Minimum Spanning Tree: Prim’s
Algorithm
Minimum Spanning Tree: Prim’s
Algorithm
Minimum Spanning Tree: Prim’s
Algorithm

𝐎 (𝑽 )
Time complexity:

𝜽( 𝑽 )
𝑶(𝑽𝒍𝒈𝑽 )
𝑶(𝒍𝒈𝑽 )
𝜽( 𝑬 )

𝑶(𝑬𝒍𝒈𝑽 )
𝑶(𝒍𝒈𝑽 )
Dominant
Term
Decrease Key

You might also like