CS251 Unit4 Slides
CS251 Unit4 Slides
Unit -4
Bharathi R
Department of Computer Science & Engineering
DESIGN AND ANALYSIS OF ALGORITHMS
Bharathi R
Department of Computer Science & Engineering
Design and Analysis of Algorithms
Space and Time Tradeoffs- Introduction
a) Hashing b) B-Trees
a) Counting b) String
Methods for matching
Sorting Algorithm
Design and Analysis of Algorithms
1.Input enhancement
1. Find the numbers that are less than a[0] i.e, 62, by scanning the array from the index 1 to 5
2. Maintain another array called Count the elements that are lesser than 62
Array a 62 31 84 96 19 47
Array 3
Count
Design and Analysis of Algorithms
Bharathi R
Department of Computer Science & Engineering
[email protected]
Design and Analysis of Algorithms
Unit -4
Bharathi R
Department of Computer Science & Engineering
DESIGN AND ANALYSIS OF ALGORITHMS
Bharathi R
Department of Computer Science & Engineering
Design and Analysis of Algorithms
1.Input enhancement
a)Sorting by Counting
1. Comparison Counting Sorting
I. For each element of the list, count the total number of
elements smaller than this element.
II. These numbers will indicate the positions of the elements in
the sorted list.
Consider sorting the array whose values are known to come from the set
{11, 12, 13} and should not be overwritten in the process of sorting. The
frequency and distribution arrays are as follows:
=
Design and Analysis of Algorithms
Example of sorting by distribution counting.
Bharathi R
Department of Computer Science & Engineering
[email protected]
Design and Analysis of Algorithms
Unit -4
Bharathi R
Department of Computer Science & Engineering
DESIGN AND ANALYSIS OF ALGORITHMS
Bharathi R
Department of Computer Science & Engineering
Design and Analysis of Algorithms
String Matching
Step 3 While a mismatch is detected and the text is not yet exhausted,
realign pattern one position to the right and repeat Step 2
Always makes a shift based on the text’s character c aligned with the
last compared (mismatched) character in the pattern according to the
shift table’s entry for c
Design and Analysis of Algorithms
How far to shift?
By scanning pattern before search begins and stored in a table called shift
table. After the shift, the right end of pattern is t(c) positions to the right of
the last compared character in text.
Shift table is indexed by text and pattern alphabet Eg: for BAOBAB:
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
1 2 6 6 6 6 6 6 6 6 6 6 6 6 3 6 6 6 6 6 6 6 6 6 6 6
Design and Analysis of Algorithms
Shift Table
Design and Analysis of Algorithms
Horspool Matching
Design and Analysis of Algorithms
Horspool Matching -Example
Design and Analysis of Algorithms
Example of Horspool’s algorithm
_
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
1 2 6 6 6 6 6 6 6 6 6 6 6 6 3 6 6 6 6 6 6 6 6 6 6 6 6
Bharathi R
Department of Computer Science & Engineering
[email protected]
Design and Analysis of Algorithms
Unit -4
Bharathi R
Department of Computer Science & Engineering
DESIGN AND ANALYSIS OF ALGORITHMS
Bharathi R
Department of Computer Science & Engineering
Design and Analysis of Algorithms
The Boyer-Moore algorithm
In this situation, the Boyer-Moore algorithm determines the shift size by considering
two quantities. The first one is guided by the text’s character c that caused a
mismatch with its counterpart in the pattern. Accordingly, it is called the bad symbol
shift.
Design and Analysis of Algorithms
Bad-symbol shift in Boyer-Moore algorithm
If c is not in the pattern, we shift the pattern to just pass this c in the text.
Conveniently, the size of this shift can be computed by the formula
t1(c) − k where t1(c) is the entry in the precomputed table used by
Horspool’s algorithm and k is the number of matched characters:
Design and Analysis of Algorithms
Bad-symbol shift in Boyer-Moore algorithm- Example
Design and Analysis of Algorithms
Bad-symbol shift in Boyer-Moore algorithm- Example
Design and Analysis of Algorithms
Good-suffix shift in Boyer-Moore algorithm and when to use?
• d2(k) = the distance between (the last letter of) the matched
suffix of size k and (the last letter of ) its rightmost occurrence in
the pattern that is not preceded by the same character
preceding the suffix
Bharathi R
Department of Computer Science & Engineering
[email protected]
Design and Analysis of Algorithms
Unit -4
Bharathi R
Department of Computer Science & Engineering
DESIGN AND ANALYSIS OF ALGORITHMS
Bharathi R
Department of Computer Science & Engineering
Design and Analysis of Algorithms
The Greedy Approach
● Coin-change problem
● Huffman codes
Design and Analysis of Algorithms
Coin Change Problem
• Let’s assume that we’re working at a cash counter and have an infinite supply of
D ={1,2,5,10} valued coins.
• A person buys things worth Rs. 72 and gives a Rs. 100 bill. How does the cashier
give change for Rs. 28?
Design and Analysis of Algorithms
Change Making Problem
Change-making problem:
How can a given amount of money be
made with the least number of coins
of given denominations?
Option Choosen Coins
Example:
Change for Rs. 28 28-10 = 18 10
18-10 =8 10 10
8-5 =3 10 10 5
3-2 = 1 10 10 5 2
1-1 =0 10 10 5 2 1
Design and Analysis of Algorithms
Change Making Problem
Iteration 1 Iteration 5
D ={1,2,5,10} D ={1,2,5,10}
n = 28 and i=4 n = 1 and i=0
Select D[3] =10 as 28>= 10 Select D[0] =1 as 1>= 1
Decrease n by 10 ( n= 18) Decrease n by 1 ( n= 0)
Add 10 to set S = {10} Add 1 to set S = {10, 10, 5,2,1}
Iteration 2
Iteration 4
D ={1,2,5,10}
D ={1,2,5,10}
n = 28 and i=3
n = 3 and i=1
Select D[3] =10 as 18>= 10
Select D[1] =2 as 3>= 2
Decrease n by 10 ( n= 8)
Decrease n by 2 ( n= 1)
Add 10 to set S = {10, 10}
Add 2 to set S = {10, 10, 5,2}
Iteration 3
D ={1,2,5,10}
n = 8 and i=2
Select D[2] =5 as 8>= 5
Decrease n by 5 ( n= 3)
Add 5 to set S = {10, 10,5}
Design and Analysis of Algorithms
Summary
On each step, the choice made must be feasible, locally optimal, and
irrevocable.
Design and Analysis of Algorithms
Text Books
Bharathi R
Department of Computer Science & Engineering
[email protected]
Design and Analysis of Algorithms
Unit -4
Bharathi R
Department of Computer Science & Engineering
DESIGN AND ANALYSIS OF ALGORITHMS
Bharathi R
Department of Computer Science & Engineering
Design and Analysis of Algorithms
Minimum Spanning Tree : DEFINITIONs
Example
a
Design and Analysis of Algorithms
Prim’s Algorithm
Design and Analysis of Algorithms
Prim’s Algorithm
Design and Analysis of Algorithms
Prim’s Algorithm
Design and Analysis of Algorithms
Prim’s Algorithm
Design and Analysis of Algorithms
How efficient is Prim’s Algorithm
• The answer depends on the data structures chosen for the graph
itself and for the priority queue of the set V − VT whose vertex
priorities are the distances to the nearest tree vertices.
Both algorithms use the greedy approach - they add the cheapest
edge that will not cause a cycle. But rather than choosing the
cheapest edge that will connect any pair of trees together, Prim's
algorithm only adds edges that join nodes to the existing tree.
Bharathi R
Department of Computer Science & Engineering
[email protected]
Design and Analysis of Algorithms
Unit -4
Bharathi R
Department of Computer Science & Engineering
DESIGN AND ANALYSIS OF ALGORITHMS
Bharathi R
Department of Computer Science & Engineering
Design and Analysis of Algorithms
Minimum Spanning Tree : Kruskal’s Algorithm
Kruskal's algorithm initially places all the nodes of the original graph
isolated from each other, to form a forest of single node trees, and
then gradually merges these trees, combining at each iteration any
two of all the trees with some edge of the original graph.
Before the execution of the algorithm, all edges are sorted by weight
(in non-decreasing order).
Then begins the process of unification: pick all edges from the first to
the last (in sorted order), and if the ends of the currently picked edge
belong to different subtrees, these subtrees are combined, and the
edge is added to the answer.
After iterating through all the edges, all the vertices will belong to
the same sub-tree, and we will get the answer.
Design and Analysis of Algorithms
Kruskal’s Algorithm
1
b c
6
3 4 4
5
a 5
f d
2
6 8
e
Design and Analysis of Algorithms
Kruskal’s Algorithm
1 Edge(u,v) Weight
b c
6 bc 1
3 4 4
ef 2
5 ab 3
a 5
f d
bf 4
cf 4
2
6 8
af 5
df 5
e
ae 6
cd 6
de 8
Design and Analysis of Algorithms
Kruskal’s Algorithm
1 Edge(u,v) Weight
b c
bc 1
ef 2
a f d ab 3
bf 4
cf 4
af 5
e df 5
ae 6
cd 6
de 8
Design and Analysis of Algorithms
Kruskal’s Algorithm
1 Edge(u,v) Weight
b c
bc 1
ef 2
a f d ab 3
bf 4
2 cf 4
af 5
e df 5
ae 6
cd 6
de 8
Design and Analysis of Algorithms
Kruskal’s Algorithm
1 Edge(u,v) Weight
b c
3 bc 1
ef 2
a f d ab 3
bf 4
2 cf 4
af 5
e df 5
ae 6
cd 6
de 8
Design and Analysis of Algorithms
Kruskal’s Algorithm
1 Edge(u,v) Weight
b c
3 bc 1
4
ef 2
a f d ab 3
bf 4
2 cf 4
af 5
e df 5
ae 6
cd 6
de 8
Design and Analysis of Algorithms
Kruskal’s Algorithm
1 Edge(u,v) Weight
b c
3 bc 1
4
ef 2
a f d ab 3
bf 4
2 Cyclic cf 4 X
af 5
e
df 5
ae 6
cd 6
de 8
Design and Analysis of Algorithms
Kruskal’s Algorithm
1 Edge(u,v) Weight
b c
3 bc 1
4
ef 2
a f d ab 3
bf 4
2 cf 4
Cyclic af 5 X
e df 5
ae 6
cd 6
de 8
Design and Analysis of Algorithms
Kruskal’s Algorithm
1 Edge(u,v) Weight
b c
3 bc 1
4 ef 2
5
a f d ab 3
bf 4
2 cf 4
af 5
e Acyclic df 5
ae 6
cd 6
de 8
Design and Analysis of Algorithms
Data Structure
Before formalizing the above idea, lets quickly review the disjoint-set
data structure.
Bharathi R
Department of Computer Science & Engineering
[email protected]
Design and Analysis of Algorithms
Unit -4
Bharathi R
Department of Computer Science & Engineering
DESIGN AND ANALYSIS OF ALGORITHMS
Bharathi R
Department of Computer Science & Engineering
Design and Analysis of Algorithms
Dijkstra’s Algorithm
Example
Design and Analysis of Algorithms
Dijkstra’s Algorithm-Example
Bharathi R
Department of Computer Science & Engineering
[email protected]
Design and Analysis of Algorithms
Unit -4
Bharathi R
Department of Computer Science & Engineering
DESIGN AND ANALYSIS OF ALGORITHMS
Bharathi R
Department of Computer Science & Engineering
Design and Analysis of Algorithms
Huffman Trees
Step 1: Initialize n one-node trees (forest) and label them with the
symbols of the alphabet given. Record the frequency of each
symbol in its tree’s root to indicate the tree’s weight. (Generally,
the weight of a tree will be equal to the sum of the frequencies in
the tree’s leaves.)
Bharathi R
Department of Computer Science & Engineering
[email protected]
UE19CS251 DAA
Assignment II
Max Marks: 20(will be scaled down)
Marks distribution