Space-Time Tradeoff in Algorithms
Space-Time Tradeoff in Algorithms
m - CS5
o rit h
A lg
ysis of
A n a l
a n d
sig n Dr. Jeno
De Dept. of CSE
39 51 67 83 87 104 116
Sorting by counting
O(n2)
Sorting by counting - To think
u = 7, v=8
u u+v 7 + 8 = 15
v u-v 15 - 8 = 7
u u-v 15-7 = 8
u = 8, v = 7
Sorting by counting - Drawback
• The counting idea does work productively in a situation
in which elements to be sorted belong to a known small
set of values.
unique values
23 21 22 23 22 22
22
22
23
22
21
23
Distribution counting sort
3 3 P
m - CS5
o rit h
A lg
ysis of
A n a l
a n d
sig n Dr. Jeno
De Dept. of CSE
• A [ 80,23,76,234,100,9,56]
De Dept. of CSE
comparision
A-7
Horspool Algorithm - shift table construction
Last character - don’t do
N-6
G-5
A-4
L-3
O-2
R-1
Horspool Algorithm - shift table construction
B A R B E R m=6
B A R B E R
Excellence and
Service 22
CHRIST
Deemed to be University
B A R B E R m=6
B A R B E R
5 4
Excellence and
Service 23
CHRIST
Deemed to be University
B A R B E R m=6
B A R B E R
5 4 3
Excellence and
Service 24
CHRIST
Deemed to be University
B A R B E R m=6
B A R B E R
5 4 3 2
Excellence and
Service 25
CHRIST
Deemed to be University
B A R B E R m=6
B A R B E R
5 4 3 2 1
Excellence and
Service 26
CHRIST
Deemed to be University
B A R B E R m=6
B A R B E R
5 4 3 2 1
B A R E
2 4 3 1
Excellence and
Service 27
CHRIST
Deemed to be University
J I M - S A W - M E - I N - A - B A R B E R S H O P
B A R B E R
B A R B E R
B A R B E R
B A R B E R
B A R B E R
Excellence and
Service 28
CHRIST
Deemed to be University
J I M - S A W - M E - I N - A - B A R B E R S H O P
B A R B E R
B A R B E R
B A R B E R
B A R B E R
B A R B E R
B A R B E R
Excellence and
Service 29
CHRIST
Deemed to be University
B A 0 B A B m=6
B A 0 B A B
Excellence and
Service 30
CHRIST
Deemed to be University
B A O B A B m=6
B A 0 B A B
5 4
Excellence and
Service 31
CHRIST
Deemed to be University
B A O B A B m=6
B A 0 B A B
5 4 3
Excellence and
Service 32
CHRIST
Deemed to be University
B A 0 B A B m=6
B A 0 B A B
5 4 3 2
Excellence and
Service 33
CHRIST
Deemed to be University
B A 0 B A B m=6
B A 0 B A B
5 4 3 2 1
Excellence and
Service 34
CHRIST
Deemed to be University
B A 0 B A B m=6
B A 0 B A B
5 4 3 2 1
B A O
2 1 3
Excellence and
Service 35
CHRIST
Deemed to be University
B E S S - K N E W - A B O U T - B A O B A B S
B A O B A B
Excellence and
Service 36
CHRIST
Deemed to be University
B E S S - K N E W - A B O U T - B A O B A B S
B A O B A B
Excellence and
Service 37
CHRIST
Deemed to be University
B E S S - K N E W - A B O U T - B A O B A B S
B A O B A B
B A O B A B
Excellence and
Service 38
CHRIST
Deemed to be University
B E S S - K N E W - A B O U T - B A O B A B S
B A O B A B
B A O B A B
Excellence and
Service 39
CHRIST
Deemed to be University
B E S S - K N E W - A B O U T - B A O B A B S
B A O B A B
B A O B A B
B A O B A B
Excellence and
Service 40
CHRIST
Deemed to be University
B E S S - K N E W - A B O U T - B A O B A B S
B A O B A B
B A O B A B
B A O B A B
Excellence and
Service 41
CHRIST
Deemed to be University
B E S S - K N E W - A B O U T - B A O B A B S
B A O B A B
B A O B A B
B A O B A B
B A O B A B
Excellence and
Service 42
CHRIST
Deemed to be University
B E S S - K N E W - A B O U T - B A O B A B S
B A O B A B
B A O B A B
B A O B A B
B A O B A B
Excellence and
Service 43
CHRIST
Deemed to be University
B E S S - K N E W - A B O U T - B A O B A B S
B A O B A B
B A O B A B
B A O B A B
B A O B A B
B A O B A B
Excellence and
Service 44
CHRIST
Deemed to be University
Puzzle time
Excellence and
Service
CHRIST
Deemed to be University
O N E T W O T H R E E
T H R E E
T H R E E
T H R E E
Excellence and
Service 46
Horspool’s Algorithm CHRIST
Deemed to be University
T C C T A T T C T T
9 8 7 6 5 4 3 2 1
A C T
5 2 1
Excellence and
Service 48
CHRIST
Deemed to be University
A
Horspool’s Algorithm:
C T
Example
Pattern String: TCCTATTCTT
5 2 1
T T A T A G A T C T C G T A T T C T T T T A T A G A T C T C C T A T T C T T
T C C T A T T C T T
T C C T A T T C T T
T C C T A T T C T T
T C C T A T T C T T
T C C T A T T C T T
T C C T A T T C T T
T C C T A T T C T T
T C C T A T T C T T
T C C T A T T C T T
T C C T A T T C T T
T C C T A T T C T T
T C C T A T T C T T
T C C T A T T C T T
T C C T A T T C T T
Excellence and
Service 49
Brute force
3 3 P
m - CS5
o rit h
A lg
ysis of
A n a l
a n d
sig n Dr. Jeno
De Dept. of CSE
Note:
Value of * - length of pattern
U =7-0-1=6
N = 7- 1-1 =5
I = 7- 2-1 = 4
V = 7-3-1=3
E= 7-4-1 =2
R = 7-5-1 = 1
S = length of pattern = 7
Boyer Moore string matching
C H R I S T U N I V E R S I T Y B A N G L O R E
U N I V E R S
U N I V E R S
U N I V E R S
De Dept. of CSE
U4- Hashing
Hashing
- Useful for searching purpose.
Introduces hashing
Hashing
Store the element upon its index. Take the value of the element as its index , store in
that index
Search time
O(1)
Hashing
Time reduced
Space wasted
List of elements
Hashing
Properties of functions : one - one , Many – one
h( x) = x - one-one
Modify the function to efficiently utilize the space in the hash table.
= [ 23%10 + 0] % 10
= [3 + 0] % 10
=3
= [ 23%10 + 12] % 10
= [3 + 1] % 10
=4
= [ 23%10 + 22] % 10
= [3 + 4] % 10
=7
Avoid clustering,
improve searching
time
Hashing- Try it later
input {4371, 1323, 6173, 4199, 4344, 9679, 1234, 6788, 3445,
3421, 4567, 6792 and 1989} and a hash function H(X) = X (mod13)
show the resulting hash table when each of the collision resolution
technique is used:
Organizing the data directly in to the memory that is used by the program,- Data structure
Organizing the data on the disk efficiently , so that it can be accessed easily - DBMS
How data stored on the disk
Self managed indexing - automatic addition and deletion of index based on table size
B trees are originated from M-way search trees
Creation process
Creation of B Trees
Insert keys . Degree
Is 4 hence 3 keys
Insert 50 , no space.
Split the node.
Insert 35. split
BT are useful in
Multilevel indexing.
Insert 5
Insert 60,70,80
Insert 15 . Split
3 levels of indices. BT node has child pointer(block pointer , record pointer) -> DB
Prims algorithm
• SAK
Greedy Technique : Dijkstra’s Algorithm
Single path
Greedy Technique : Dijkstra’s Algorithm
Greedy Technique : Dijkstra’s Algorithm
Greedy Technique : Dijkstra’s Algorithm
Greedy Technique : Dijkstra’s Algorithm
Greedy Technique : Dijkstra’s Algorithm
Greedy Technique : Dijkstra’s Algorithm
Greedy Technique : Dijkstra’s Algorithm
t h m -
l gori
o f A
l y s is
A na
and 5 3 3 P
s i gn C S
De Dr. Jeno
Dept. of CSE
Prepare a matrix
Dynamic programming
Warshall’s - Floyd’s Algorithm
Prepare a matrix by referring the previous matrix, consider 1 as intermediate vertex.
All the paths that belong to vertex 1 remain unchanged. Diagonals also unchanged
Time complexity
3 3 P
m - CS5
o rit h
A lg
ysis of
A n a l
a n d
sig n Dr. Jeno
De Dept. of CSE
Try it !!!!
3 3 P
m - CS5
o rit h
A lg
ysis of
A n a l
a n d
sig n Dr. Jeno
De Dept. of CSE
Solution
Condition:
1. sum of the weight is max
2. sum of the weight < = bag capacity.
Knapsack - Dynamic programming
1. Otimization problem
2. Every stage, decision- sequence of decision
3. All possible solution -pick up the best
weights --->
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
p w
0 0 1 1 1 1 1
capacity <------
1 1 1
1 2
2 0 0 1 2 2 3 3 3 3
2 3 0 1 2 5 5 7
3 0 6 7
4 0
5 4
6 5
Knapsack - Dynamic programming
weights --->
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
p w
0 0 1 1 1 1 1
capacity <------
1 1 1
1 2
2 0 0 1 2 2 3 3 3 3
2 3 0 1 2 5 5 7
3 0 6 7
4 0 0
5 4
6 5
Knapsack - Dynamic programming
weights --->
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
p w
0 0 1 1 1 1 1
capacity <------
1 1 1
1 2
2 0 0 1 2 2 3 3 3 3
2 3 0 1 2 5 5 7
3 0 6 7
4 0 0 1 2 5
5 4
6 5 till before 5th weight , will get negative weight, fill with previous
values
Knapsack - Dynamic programming
weights --->
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
p w
0 0 1 1 1 1 1
capacity <------
1 1 1
1 2
2 0 0 1 2 2 3 3 3 3
2 3 0 1 2 5 5 7
3 0 6 7
4 0 0 1 2 5
5 4
6 5
Knapsack - Dynamic programming
weights --->
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
p w
0 0 1 1 1 1 1
capacity <------
1 1 1
1 2
2 0 0 1 2 2 3 3 3 3
2 3 0 1 2 5 5 7
3 0 6 7
4 0 0 1 2 5 6
5 4
6 5
Knapsack - Dynamic programming
weights --->
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
p w
0 0 1 1 1 1 1
capacity <------
1 1 1
1 2
2 0 0 1 2 2 3 3 3 3
2 3 0 1 2 5 5 7
3 0 6 7
4 0 0 1 2 5 6 6
5 4
6 5
Knapsack - Dynamic programming
weights --->
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
p w
0 0 1 1 1 1 1
capacity <------
1 1 1
1 2
2 0 0 1 2 2 3 3 3 3
2 3 0 1 2 5 5 7
3 0 6 7
4 0 0 1 2 5 6 6 7
5 4
6 5
Knapsack - Dynamic programming
weights --->
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
p w
0 0 1 1 1 1 1
capacity <------
1 1 1
1 2
2 0 0 1 2 2 3 3 3 3
2 3 0 1 2 5 5 7
3 0 6 7
4 0 0 1 2 5 6 6 7 8
5 4
6 5
Knapsack - Dynamic programming
weights --->
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
p w
0 0 1 1 1 1 1
capacity <------
1 1 1
1 2
2 0 0 1 2 2 3 3 3 3
2 3 0 1 2 5 5 7
3 0 6 7
4 0 0 1 2 5 6 6 7 8
5 4
1 1 1
1 2
2 0 0 1 2 2 3 3 3 3
2 3 0 1 2 5 5 7
3 0 6 7
4 0 0 1 2 5 6 6 7 8
5 4
x1 x2 x3 x4
profit
6 = 25 , profit of this object = 2
remaining profit = 2-2 = 0 1
1 0
Knapsack - Dynamic programming
profit = 2 , profit of this object = 2 remaining profit = 2-2 = 0
1 1 1
1 2
2 0 0 1 2 2 3 3 3 3
2 3 0 1 2 5 5 7
3 0 6 7
4 0 0 1 2 5 6 6 7 8
5 4
x1 x2 x3 x4
6X2 and5 x4 are included in the bag
0 0 1
1
Knapsack - Dynamic programming
• Tabulation method
• Set method - self study
Knapsack - Dynamic programming
For the given set of items and knapsack capacity = 5 kg, find the optimal solution
for the 0/1 knapsack problem making use of dynamic programming approach.
Knapsack - Dynamic programming
Step-01: Draw a table. Fill all the boxes of 0th row and 0th column with 0.
Knapsack - Dynamic programming
Step-02:
• Start filling the table row wise top to bottom from left to right using the formula-
The last entry represents the maximum possible value that can be put into the
knapsack.
So, maximum possible value that can be put into the knapsack = 7.
Knapsack - Dynamic programming
Step-03: Identifying Items To Be Put Into Knapsack
• Following Step-04,