0% found this document useful (0 votes)
61 views14 pages

DAA - Lecture 21 - Optimal Merge Patterns

The document discusses Optimal Merge Patterns as part of a course on Design and Analysis of Algorithms, focusing on merging sorted files efficiently using a greedy method. It outlines procedures, examples, and applications such as Huffman Coding, demonstrating how to minimize the cost of merging files. The document also includes an algorithm for constructing a binary tree for merging and highlights the time complexity involved.

Uploaded by

kanchanaketha06
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)
61 views14 pages

DAA - Lecture 21 - Optimal Merge Patterns

The document discusses Optimal Merge Patterns as part of a course on Design and Analysis of Algorithms, focusing on merging sorted files efficiently using a greedy method. It outlines procedures, examples, and applications such as Huffman Coding, demonstrating how to minimize the cost of merging files. The document also includes an algorithm for constructing a binary tree for merging and highlights the time complexity involved.

Uploaded by

kanchanaketha06
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/ 14

Topic: Optimal Merge Patterns Course: Design and Analysis of Algorithms

Design and Analysis of Algorithms


(VR17) III B.Tech – I Semester
UNIT-3
Lecture: 21
Topic: Optimal Merge Patterns

COURSE INSTRUCTOR:
B.VAMSI
Assistant Professor
CSE Department
Department of Computer Science and Engineering Slide No. 1
Topic: Optimal Merge Patterns Course: Design and Analysis of Algorithms

Unit -3 Syllabus:
Greedy method: General method, Applications: Job sequencing with
deadlines, knapsack problem, Single source shortest path problem,
Optimal Merge Patterns

Lecture Objective:
Lecture Contents: Understand the two-way
merging technique
• Procedure

• Example
Lecture Outcome:
• Application Analyze the Huffman Coding
using Optimal Merge
Patterns

Department of Computer Science and Engineering Slide No. 2


Topic: Optimal Merge Patterns Course: Design and Analysis of Algorithms

Procedure:
• Merging means combining of two sorted lists in to one.
• Example:
A 1 2 3 4

B 5 6 7 8

C 1 2 3 4 5 6 7 8

• These two sorted files containing ‘n’ and ‘m’ records respectively
could be merged together in time is O(n+m).
• When “more than two” sorted files are to be merged together, then
the merge can be accomplished by repeatedly merging sorted files
in pairs is called as “TWO-WAY MERGING”.

Department of Computer Science and Engineering Slide No. 3


Topic: Optimal Merge Patterns Course: Design and Analysis of Algorithms

Case 1: A B C D
6 5 2 3
Files A B C D
Sizes 6 5 2 3 11

13

Procedure:
16
Case-1 follows ‘linear merging’:
• A followed by B and
Therefore Total time
• B followed by C and = O(11+13+16)
• C followed by D = 40

Department of Computer Science and Engineering Slide No. 4


Topic: Optimal Merge Patterns Course: Design and Analysis of Algorithms

Case 2: A B C D
6 5 2 3
Files A B C D
Sizes 6 5 2 3 11 5

16
Procedure:
Case-2 follows: Therefore Total time
= O(11+5+16)
• Merge ‘A’ with ‘B’ = 32
• Merge ‘C’ with ‘D’

Department of Computer Science and Engineering Slide No. 5


Topic: Optimal Merge Patterns Course: Design and Analysis of Algorithms

Case 3: A B C D
6 5 2 3
Files A B C D
Sizes 6 5 2 3 5

10
Procedure:
Case-3 follows: 16
• Merging minimum length of files
Therefore Total time
= O(5+10+16)
= 31

Department of Computer Science and Engineering Slide No. 6


Topic: Optimal Merge Patterns Course: Design and Analysis of Algorithms

• So, in the above 3 cases, case-3 gives minimum cost to merge four
files, because of merging of minimum length of files first.
• The ‘greedy method’ which we follow always says that merging a
pair of small files to get the best solution.

Example: Optimal Merge Pattern


Files  x1 x2 x3 x4 x5
Sizes 20 30 10 5 30

Now, apply greedy method to merge all files in increasing order.


Files  x4 x3 x1 x2 x5
Sizes 5 10 20 30 30

Department of Computer Science and Engineering Slide No. 7


Topic: Optimal Merge Patterns Course: Design and Analysis of Algorithms

Binary tree representation of merge pattern


In the binary tree, the leaf nodes
are drawn as ‘squares’ known as
Z4 “External Nodes”.
95
And the nodes are drawn as
‘circles’ known as “Internal
Z2
35 Nodes”.

Each ‘Internal Node’ has exactly


Z1 two children
15 Z3 60
-Total cost = O(15+35+60+95)
= 205
5 10 20 30 30 (or)
The external node ‘x4’ is at a
distance 3 from the root node
x4 x3 x1 x2 x5 ‘Z4’. Therefore: Ʃdi*xi
= 5*3 + 10*3 + 20*2 + 30*2 +
30*2
= 15+30+40+60+60 = 205

Department of Computer Science and Engineering Slide No. 8


Topic: Optimal Merge Patterns Course: Design and Analysis of Algorithms

Algorithm
treenode = record Algorithm Tree(n)

{ {
for i := 1 to n-1 do
treenode *lchild;
{
treenode *rchild;
pt:= new treenode; //create a new node
integer weight;
(pt lchild) := least(list);
}; (pt rchild) := least(list);
(pt weight) := ((pt lchild)weight) + (pt lchild)weight));
Insert(list,pt);
}
return least(list;)
}
Time Complexity: O(n2)

Department of Computer Science and Engineering Slide No. 9


Topic: Optimal Merge Patterns Course: Design and Analysis of Algorithms

Application: Huffman Coding


Example:
Message  BCCABBDDAECCBBAEDDCC
• The length of message = 20 Alphabets
• To store the above message we need ASCII coding.
• Each alphabet need 8-bits of memory, so that 20*8 = 160 bits.
• By “Huffman Coding”, instead of using ASCII codes we are
using Huffman codes to be constructed by binary tree.

Department of Computer Science and Engineering Slide No. 10


Topic: Optimal Merge Patterns Course: Design and Analysis of Algorithms

Application: Huffman Coding


Example: Message  BCCABBDDAECCBBAEDDCC
No. of No. of
Character Count Code Character Count Code
bits bits
A 3 A 3 001 3*3 = 9
B 5 B 5 10 5*2 = 10
C 6 C 6 11 6*2 = 12
D 4 D 4 01 4*2 = 8
E 2 E 2 000 2*3 = 6
20
0 1 Total = 45bits
0 9
11
5 1
0 0 1 The above defines the
1
Encoding process to
2 3 4 5 6 compress the data
E A D B C
Department of Computer Science and Engineering Slide No. 11
Topic: Optimal Merge Patterns Course: Design and Analysis of Algorithms

Decoding Process:
• For decoding process, we must carry the “codes” and “Table”.
Character Code
In the table, total alphabets/characters are “5”.
A 001
B 10 So total memory occupies for alphabets in ASCII is
C 11 5*8 = 40 bits.
D 01
Now, the total memory for codes is: 001, 10, 11,
E 000 01, 000  12 bits.

Therefore, total memory by Huffman coding


ASCII code – 160 bits
for both “Encoding” and “Decoding” is:
Huffman code – 97 bits
= 45 bits + 40 bits + 12 bits
= 97 bits

Department of Computer Science and Engineering Slide No. 12


Topic: Optimal Merge Patterns Course: Design and Analysis of Algorithms

Summary:
• Procedure
• Algorithm
• Example
• Application: Huffman Coding

Exercise:
• Find the time complexity for the files A,B,C,D,E,F
with sizes are: 2, 3, 5, 7, 9, 13 using optimal merge patters?

Department of Computer Science and Engineering Slide No. 13


Topic: Optimal Merge Patterns Course: Design and Analysis of Algorithms

Thank You

Department of Computer Science and Engineering Slide No. 14

You might also like