Professional Documents
Culture Documents
DAA Unit 2
DAA Unit 2
of
ALGORITHMS
unit-2
-
When in doubt ,
use brute force -
Ken
Thompson
to to mind)
naturally come
Exhaustive search or
generate and test
°
candidates for
Systematically enumerating all
possible the solution
°
°
Check whether each candidate satisfies the solution
.
Inefficient approach
.
enumerate all natural numbers less than n and check if
.
I to n must be checked exhaustive
;
Os : 8 board ,
queens puzzle : in a 64×64 all 8
queens
and
placed such that
they do not dash ( diff
diagonals ,
rows
,
diagonals) .
•
check all enumerations ( possibilities) of the S
queens
selection sort
see unit 1 3
,
page
can -
-
Ei :& .
' =
:{ cnn.it#=i..En-i-i
= (n t ) -
+ ( n 2)
- t . . .
t I
un) = n Cn -
t)
E Ocn
'
)
2
bubble sort
Algorithm
for i -
-
O to i -
- n -
2 :
for j
-
- o to j
-
- n -
i -
l :
if arrcjtl ] Carr
Cj ] :
a
5 2 8 3
pass
I 3
j : O to 3
-
-
, , , ,
3 2 5 8 3
, , ,
3 ,
2
,
5 ,
3 ,
8
I
→
3 5 8 to
pass
-
- 2 2 , , ,
3
, j : O 2
2 ,
3 3
,
5 ,
8
,
2 3 3 5 8
pass =3 ,
, , ,
=4
2 3 3 5 8
pass ,
, , ,
5 2 3 3 5 8
pass
-
-
,
, , ,
using a to be at
-
O to i -
- n -
2 :
sorted = true
for j
-
- o to j
-
- n -
i -
l :
if arrcjtl ] Carr
Cj ] :
if sorted is true :
break
Time
complexity
can .EE?Ei-iz--Ein.i.i
=
(n -
t) t (n - 2) t . . . t I
Ccn) =
nC# E O ( NZ )
2
BRUTE FORCE
searching algorithms
search for in text
a
pattern a
given
°
seaential search
linear search
page 3
-
string matching
"
) text )
"
eg :
pattern : TEXT (
length m : below Clength n
- -
-
-
° I 2 3 45 6 7 8 9 10 11 12 13
R E A D T E X T B O O K s
i -
- O T E X T
i -
- l T E X T align and
slide until
i -
- 2 T Ext
match found
i -
-
3 TE XT s
i -
- 4 TEXT
i -
- 5 T E X T
iterations
worst case text
length pattern length -11
°
-
=
•
0 to n -
m or n -
Mtl trials
String matching
int find(char *text, char *pattern) {
int n = strlen(text);
int m = strlen(pattern);
int i, j;
if (j == m) {
break;
}
}
if (i > n-m) {
return 0;
}
return 1;
}
output
EXHAUSTIVE SEARCH
set of all
.
generate a
potential solutions
systematically
compute cost of each
permutation
.
•
find optimal solution
by comparing every solution with
solution
every other
travelling salesperson
°
a
salesperson has to travel from source to dest with
distances)
minimum tour cost (
weights (
find every of
°
brute force solution -
possible permutation
I]
n -
l cities [ I ,
.
-
. . . .
n
,
-
all possibilities
exponential growth -
NP
problem (n D !
-
°
Find shortest Hamiltonian circuit in a
weighted connected
graph
O : Find optimal circuit
start q end
I 2
A b
5
3
3! = 6
permutations
8 4
c d
7
a → b → c → d → a =
2+31-71-5=170
a → b → d → c → a =
2+4+7-18 =
21
a → c -7 b → d → a =
8-13+41-5 =
20
a → c → d → b → a =
8-171-4-12=21
a → d → b → c → a
=
5-14-131-8 =
20
a → d → c → b → a =
5 -17+3+2=170
Optimal paths :
a → b→c→d→a
a → d → c → b. → a
O : Find circuit
start qeud
I 2
A
b
7
5
38
c d
l
a → b → c -7 d → a =
2+81-1+7 =
18
a → b → d → c → a
'
-
2+3+1+5 =
④
a → [→ b → d → a =
5-181-31-7 =
23
a → c → d → b → a =
5-111-31-2 =
④
a → d → b → c → a = 71-3+81-5 =
23
d b → a
=
a → → c →
7-11+8-12 =
18
Optimal solution
a→b→d→c→a
a -7C -
id Sb
-
→a
KNAPSACK PROBLEM
°
0/1 knapsack ; object either picked up or not [no fractions)
eg
: a thief tries to maximise profit with finite
bag size
l and
weight value)
☐
exhaustive search call subsets found ,
value and weight
calculated , optimised subset found)
•
2^ subsets
0 :
knapsack capacity w= 16
value
Item weight
1 2 20
2 5 30
3 10 50
4 5 10
1 I} o o
2 { I} 2 20
3 {21 5 30
4 {3} to 50
5 {4 } 5 10
.
6 { 1,2} 7 50
7 { 1,33 12 70
8 11,43 7 30 optimal
←
912,33152€
10 { 2,43 10 40
it { 3,4} 15 60
•
Exhaustive search : run )
n
jobs
Minimisation problem
°
J, Jz - . . .
.
Jn
P, C
,,
. i '
: . .
"
<
-
(
Pn '
Os : Jl J2 Js 54 n ! permutations
Pl 9 2 7 8
P2 6 4 3 7
P3 5 8 I 8
124 7 6 9 4
4! 24 possibilities
-
Ji -
J2 -53-54
I -2-3-4 3-1-2-4
I -2 -
4 -
3 3 -
I -
4-2
I -3-2-4 3-2 -
I -4
I -
3-4-2 3-2-4 -
l -
4 -
2-3 3 -
4 -
I -2
( -
4-3-2 3-4-2 -
I
2- I -3-4 4 I 2-3
-
-
2- I 4-3 -
4 -
I -
3 -
2
2- 3 -
I -4 4 -
2 -
I -3
2- 3 -
4 -
I 4 -
2
-
3 -
I
2 -
U -
I -3 4 -
3 -
I -2
2-4-3 -
I 4-3-2 -
I
DIVIDE u CONQUER
divided into smaller sub problems of
big problem
° -
same nature
°
solve smaller
problems recursively
°
combine the solutions
instance of
size n
sub -
problem sub -
problem
of size 42 of size nlz
of of
problem problem
size I size 1
Solution
to
original
problem
time spent
T
Tcn) = at Tcnlb) tfcn>
on
and
dividing
n
combining
lb instances
Master 's theorem
i÷÷÷÷÷÷÷÷÷÷
35 unit I
pg ,
merge sort
EBoETaotZgEgaggggg
Mfa
°
Merge :
merge a sorted sets to form a
larger merged set
SI S2
10 30 50 20 25 26
Divide : size of
keep dividing into halves until array l
-
• -
25,1548
/
,
16
-
25415 8,116
/ h
④ ⑤⑧ → elementary case ( size =D
Algorithm Merge sort CAIO . . . n -
D)
H recursive
11 input ACO I]
:
array n -
. . -
I]
11 output : sorted array Aco . . . - n -
if n 70
copy
A CO . . . Lnlz) -
I] to B [0 .
.
.
Lnlz) -
I ]
I]
copy Al Lnm I ] to CCO Lnlz ) -
n
-
. . . . . . .
Merge CB C A) , ,
,
-
11 output :
array Alo . . .
ptq I ]
-
of both
arrays
'
elements ,
sorted
i K
O
j O O
- - -
- -
,
-
-
Ccj ]
j -
jet
K -
- K -11
if i =p
ACK
copy Ccj . . .
g- IT to . . .
ptg
-
I ]
else
BC i p IT to ACK I]
copy ptq
- -
. - . .
. .
Q: Show sort of 8,3 , 2,9 , 7,1 , 5,4
merge
low -
- O
7
O l 4 5 6 7 high
-
3
-
2
8 3 2 9 7 I 5 4 mid -
-
lo -177/2
, , , , , ,
,
I I =3
8
,
3 2 9 7 I 5 4
, , , , ,
I t t t
8 3 2 ,
9 7
,
I 5
,
4
,
d d t t t l l l
8 3 2 9 7 I 5 4
↳ t b d → ← → ←
3, 8 2
,
9 I, 7 4,5
- I → a
2 3 8 9 I
,
4 5 7
, , , , ,
→ a
1 , 2 3 4 5 7
,
8
,
9
, , , ,
← merge S
,
71
, 712×3
time
Tcm 217427 t I
11/2/34/15
=
n
-
(
comparisons) Sz Ky " s "6
5
comparisons
Mater 's theorem: a 2 d I b 2
- - -
- -
-
, ,
a = bd ( 2=27
← improvement
tch) E
Olnlogn)
:
.
over 01h27
implementation in C
merge_sort(A, l, m);
merge_sort(A, m+1, h); for 1,000 elements
merge(A, l, m, h); ~
0.2ms
}
}
int i = l, j = m + 1, k = 0;
while (i <= m && j <= h) {
if (A[i] <= A[j]) { for 100,000 elements
B[k++] = A[i++];
~
20ms
}
else {
B[k++] = A[j++];
}
for 1,000,000 elements
}
while (i <= m) {
~
200 MS
B[k++] = A[i++];
}
while (j <= h) {
B[k++] = A[j++];
}
k = 0;
for (i = l; i <= h; ++i, ++k) {
A[i] = B[k];
}
}
quick sort
•osoEsaoooogFB
5 3 1 9 10 4 6 28
°
Hoare's partition method
.
pivot element
•
variations of as
pivot
pivot : first element in
array !
°
5 3 1 9 10 4 6 28 n -
-
8
p i j
is low -11
j high
-
-
p
-
-
pivot
5 3 1 9 10 4 6 28
p i i i
j j j
355 K5 975 455675 2875
✓ ✓ r
stop stop
v
swap
5 3 1 4 10 9 6 28
i ij j
j 1075
stop
495 1075
r
Stop
swap jth and pivot when i and j cross
P i j
4 3 I ⑤ to 9 6 28
partitioning
-
- T successful
LHS RHS
pivot
sort from quick sort from
quick
low to
j I jet to
high
-
I 3 4 6 9 10 28
g. sort
5 28 7 14 10 3 2 I
i j
p
stop stop
2845 145
icj →
swap iqj
5 1 7 14 10 3 2 28
i j
stop stop
745 27/5
icj →
swap ieej
5 1 2 14 10 3 7 28
i j
stop stop
1445 345
icj →
swap
5 I 2 3 10 14 7 28
g- stop i top
jci →
swap p and j
3 I 2 ⑤ to 14 7 28
- -
3 1 2 10 14 7 28
i i
p ji p i j j
✓
j stop stop stop stop 28710
2 I 3 10 7 14 28
-
j i
ij stop
stop stop
p > 0013
j stop swap pg j
swap jeep 7 ④ 14 28
-
I 2 3 14 28
p ij
Final array
I 2 3 7 10 14 28
HI )
Algorithm Quick sort CALL . .
-
11 Sorts sub
a
array by quick sort
11 Input : a sub
array ACL . . - h] of ACO .
. .
n -
I]
11 sorted in
Output : a sub
array All . . . h]
ascending order
if L C h
s = Partition ( ACL . . . HI )
Quicksort CALL . . .
s D)-
11
Input : a sub
array ACL . .
h] . of Alo . . .
n
-
I]
11 Output : a
partition of ACL . . . h] ,
with the split position returned
All ]
p
=
i -
- L
j
-
- h
repeat
repeat i it I until ACIIZP
=
if j t l
return j
IMPLEMENTATION in C
while (i <= j) { ← be
0
while (i <= high && a[i] <= pivot) {
strict
( repetitions)
++i;
} ←
O
while (j > low && a[j] >= pivot) {
--j;
}
if (i < j) {
// Swap
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
// Crossover
if (j != low) {
a[low] = a[j];
a[j] = pivot;
}
return j;
}
efficiency class
best CASE
°
Best case : split 50-50
5 4 6 8 9
②2③
3 10 9 n
-
-
i
stops
comparisons of pivot)
htt Cho
repeating
-
-
5 ① I 2 3 5 6 10 9 8 n -
- 9
comparisons n
-
-
a -_ 2
←
basic
op b -
- 2 a
-
-
bd
( Ch) = 2 ( ( M2) t n d -
-
I
best best
Oln
Tcn)
login)
=
worst CASE
On
2 4
④3 6
5
⑤ ③
[
comparisons = ntl
C.
=
CHI ) t ht -13 = CntDCnt# -
3
worst
.
. .
Tcn) = 0cm )
CASE
average
take different locations
partitioning can place at n
.
Cavgcn) ht
-
f [ Cn -117 t
Cavgcs ) t
Cavgcn
-
I -
s )] for n> 1
( (n ) = 2n lncn) = 1.38 n
logan
avg
E'iiiiiitttYEEHiitt.it
'
I size decreases
(only I sub instance)
decrease and
conquer
a
•
two conditions
1- the elements must be in order ( sorted array)
example
21 8 21 32 89
key 65 72 100
-
0 I 2 3 4 5 6
Low high
mid -
-
low
thigh =3
2
array (mid ]
L
key
-
8 21 32 65 72 89 100
0 I 2 3 4 5 6
Low high
Algorithm Binary Search ( Allow . - -
high ] ,
K)
11 Implements recursive
binary search
11 Input array sorted
: ACO n I] in
ascending order
-
.
. .
if low s
high
mid -
-
(high-low ) 12
if AC mid ] =
-
- K
return k
else if A [ mid ] 7k
return
Binary Search ( Allow mid I ] K)
-
. . . .
else
return
Binary Search ( Acmidtl n I ] K)
-
. . .
else
return -1
,
.
11 Implements non -
recursive
binary search
11 Input array sorted
: ACO n I] in
ascending order
-
.
. .
return k
else if A [ mid ] 7 k
high mid I
-
-
-
else
low -
- mid -11
return -
I
TIME COMPLEXITY
d
Tcn) = Tcnlz) t t a =L a = b
b -
- 2
d- - O
Tcn) OC )
log n
-
unsorted set
Tcn) =
nlogn
IMPLEMENTATION IN C
iterative recursive
int bs_iter(int *a, int n, int key) { int bs_recur(int *a, int low, int high, int key) {
int low = 0, high = n - 1, mid; int mid;
°
non -
linear data structure disjoint sets
( both binary
°
finite set of nodes T t trees)
height of a
true
of empty
height = I
-
Algorithm Height CT )
if T= 0 11 empty tree
return -
I
return It Max (
Height LTD Height Ctr)) ,
Recurrence Relation
non -
symmetric ;
not
straightforward
basic ( F- 0)
operation : comparison
int
f
n → int -
-
l
x → ext -
- 2
c- ext
n = 2
x =3
x -
- htt
=
ntltn
= 2h -11
= Ocn)
number of additions = n
MULTIPLICATION of LARGE INTEGERS
"
42
IX - l
X 3 5
total : 4 multiplications
20 o
(basic
I 2 6 0 operation)
I 4 t O
axb
divide
it digits
a -_
9,90 ( even) 9=91×10%+90 pad with
← zeroes
b -
- b , bo b -
- b, X 1042 tho if odd
ax b = Ca , x 10^12-190) ( b xlonkt , bo )
=
(a , xb ,) x 10
"
t ( a , xbutaoxb ,)x 10^12 t
aoxbo
10h12
"
10 t
=
Cry x C, X + Co
① Co = aoxbo
② ca
-
-
alibi
← rewritfedtuoce # of multiplications
③ C
,
= Ca , -190) xlbitbo) -
Ccztco)
Q: 33×24 n -
- 2
A , ao bi bo
Cz = 9, X b, = 6
Co =
aoxbo = 12
C, = Ca , tao) x ( bilbo) -
Ccztco)
= 6×6 -
18
= 18
Q: 1233×1124
A, ao b, bo
12×11 → (1)
Cz
=
Co = 33×24 → (2)
c, =
(12-133)×(11-124) -
C 12×11 -133×24)
=
(45×35) ( 12×11 -
+33×247
→ ( 37
(1) 12 X 11
9 au b , bo
,
Cz 1×1 I →
(4)
-
- -
-
Co = 2×1 = 2 → (5)
C
,
= 3×2 -
( I -12 )
(6)
=
6-3=3-3
12×11 = 1×102-1 3×10 t 2 = 132
(2) 33 X 24
a
, ao b, bo
Cz = 3×2 = 6 →
(7)
Co
-
-
3×4=12 - 7
(8)
c, = 6×6 -
( 12+67
=
18
#(9)
33×24 = 6×102-118×10 t 12
= 792
(3) 45 X 35
91 90 b, bo
Cz = 4×3 = 12 → (IO)
co = 5×5 -
- 25 → 41 )
c, = 9×8 -
(12-125)
= 72-37-42)
=
35
12×11 =
132
Cz
=
Co = 33×24=792
(132-1792)=651
1575
c, = -
- I
Monk)
"
=3 M( 2 )
"
2)
-
=3 3 -
Mca
3.3 ( 2K 3)
-
= -
3 M
't i
mean )
-
=3
"
i-k =3 MCD
"
=3
31092N ) 10923
Mcn) E O( @ (n ) O ( ht 585g
.
= ,
-585
Acn) E O ( ht )
°
For two 2×2 matrices ,
seven
multiplications
instead of 8 ( brute force) ( 21-2-121-2)
( Mit my My Mzt Ms
) 8 additions
= -
Ms +
Mz
=
doo * ( boi -
bi , )
my =
all
*
( bio -
boo)
Ms = (doo t Aoi)
*
bi ,
law doo)
* ( boot bo , )
Mo
-
- -
My =
Cao , -
ah ) * ( bio tbh)
.
10 additions t 8 additions =
18 additions
°
divide matrices into sub matrices
K : i :H : : :÷H :O: : :L
Mln) - 7 Mcnlz) MCI ) =/
2K
n K
logan
-
-
- -
""
MC2
"
) = 7 Mca ) theory :
7.7 M ( 2K 2)
can
get
✓
-
=
n2 not
i
'
2K ) ,
-
= 7 MC yet
807
7109 an n' 0927 nd n3
-
= 7k = = = c found
807
m2 )
-
Tcn ) f @(
°:
x
shown steps
.
A B
M, = (A oo t
Aa) * ( Boo t Bil)
Ht : H : :D Cl : :3 xl 's :D *
e-
lO
-
-
(O ll ll
boot bn)
M '
I % off ? "
mi
: as :{ 717 ? iz
'
and so on
Q: to find of
Design an
algorithm the max elements
using
divide and conquer strategy .
11 input : of elements
array
n
mid -
-
(Ot n -
1) 12
if size CA ) -
- I
return ACO ]
right - Max =
Find Max ( Acmidtl . . .
)
n -13
high
left
if - Max >
right - Max
return left -
Max
else
return
right - Max
52
O l 2 3 4
8 9 4 6 I
- -
° I 2 4 6 I
U
2 8 9 -
- u
4 6 I
Sy ! ↳
2
6
/
✓
q
c
\ g
Code in C
both
like
binary search
,
but search halves
,
n
n
>
- I
,
Q: Tch ) -
-
41742) th Tcl) =/
,
a -
- 4
b -
-
2
d- - I
d
a 7 b
4 72
logba )
Tcn) E OC n
Tch) E O ( m2 )
Q: Tch) -
- 4TCn/2) th
'
Tcl) =/
,
a=4 b -
-
2 d -
- 2
d
a = b
4=4
Ch' )
Tcn) E O
log n
'
Q: Tch) = 4 TCM 2) t n
,
Tcl ) =/
a
-
- 4 b -
- 2 d =3
d
a c b
Tcn) C- O( nd )
Tch) C- O(n3 )
Q : Given n
positive integers , partition them into 2 disjoint
subsets with the same sum of their elements (Partition
Problem)
I 2 3 4 5 6
eg :
2,4 4 6
1) 3,4 Eg 3,5
Tcn) E OCI)
Q : Dutch white
flag problem : red , ,
blue balls to be
rearranged
into order of red white and then blue
,
eg :
input : O l 2 0 I 2
output : O O l l 2 2
Algorithm #I :
traverse
through array once and
count number of O 's I 's and 2 's and then
,
#2: partition
Algorithm 3-
way
000 . - .
111 . . . Unknown 222 . .
.
low
high
start
mid
acmid ] o → swap with low and increment
-
-
mid Ee low
I → correct
acmid ] =
partition
move mid
right
acmid 3=2 →
swap with end and decrement
end
https://www.geeksforgeeks.org/sort-an-array-of-0s-1s-and-2s/
D: Quick sort
using Dutch
flag algorithm (3 -
way sort)
2 6 5 2 6 8 6 I 2 ⑥ ←
pivot
=6
pivot
normal ⑥ _Qs
⑥ ⑥
QSO
Dutch
Index inversion
:
problem
if aCjJ=i
Output : count of II and act J j
-
-
-
I O
O l 2 3 4 5 6 7
I O 3 2 4 6 5 8
, , , , ,
, ,
- -
f b t b
I O 3 2 4 6 5
,
8
, , ,
- -
- -
t b t b t b t b
4 5 6 7
I 0 3 2 4 6 5 8
i i
j i j i j j
ago
-
- i acj ] -
-
i aged -
-
i aCjT=i
T T T
# inv # inv # inv # inv t
merge normally
3 4 5 6
7 18
°
I 2
3 2 O 4 ,
6 5
,
, ,
ini j
i
j
if ali ] else if ali ]
j Cj
-
-
acj ] -
- i inc i
# inv p
inc both