ADSA Notes
ADSA Notes
Structures and
algorithms
Notes
Mus
>
Splay Notes :
-
Partitioning
>
-
stays at
different levels of partitioning
h or
hypergraph
-usually with vertex
weight/size
>
usually weighted edges
- .
straintspartitioning
s
(K-way partitioning)
>
-
MaximumCapacity of
each
partitiono
maximum allowable
difference between
partition
.
objective odes to
partitions subject
.
to constraints such that
there
cutsize minimized
is
Fractabil,ity p-complete .
calculation.
Gain
>
-
Fontusios -
Matthesus
algorithm
>
-
Weighted event
scheduling
> Matrix chain
Multiplication
-
Booth
-
& Wallace
multiplies
>
-
storisius matrix
multiplication ?
> show that no
of paranthesis grows
at
(4/ ?
-
Longest subsequence
>
-
common
The
knapsack problem
>
-
O knapsack problem
Randomized Las
Vegas (Always
correct solution)
>
Algorithm >
gives
-
-min-cut
algorithm
-
Kargar algorithm partition
>
-
>
-
Simulated Evolution
>
probability computing Michael M
.
-
Markov's
>
Inequality
.
-
Chebyshev's Inequality
>
-
Chernoff's bound
.
>
-
-> Eulerian
graph
The
K-clustering problem
>
-
# knapsack problem
- circuit SAT
(Questions willcome
Sop POS
format
>
polynomial Reducibility
-
NP-complete problems
>
-
>
-
3- SAT
>
3-colorability problem
-
>
- The
clique problem
>Vertex cover
problem
-
shortest path with Hedge
b for v
path s-x
① => every
,
③ b
#min
C -
+ 1 = 33
, = 1
13 11 10
-
②
---
⑤ b
⑦ C
111/
⑧ a Randomized Select
Algorithm Oct.
worst A
but case
- 0(n) .
⑨ (9) True
every bipartite
(b) False becoz Not
graph
is tree But tree is
every
.
bipartite graph .
false plogs
es as => n)
logn .
Oth
complexity
So , is Time
False
(f) it solved .
DP
as is
using 2-D ,
True
(g)
as minimum
M5T
edge is the
part
of
(h) True
i True
⑩
Common
LCS7
Longest Subsequence
.
Time
=>
Complexity of LCS
for Dynamic method : -MXn
if
S
k or t
[FCs (KIst
= 0 = 0
Les (1 A]
,
=
+]
if 10 , 30, A
max[LC5 (k-1 , AJ ,
LCS (k -1)
,
if 170 , 130 , Un = ya
t
↓
max
subsequent
length
.
A
Subsequence :
=> Time
complexity of LCS
for recursive method :
nxzm or mx2"
E
1 + (C8(i 1) if i
j j
- 1 -
=
,
mar (LCS (i -1 j)
LCS ,
if i
j
= ,
225(i
, j 1))
-
123456
if i = 0
,
j
.
= 0
Q X =
BDGABA
Y = ABC BDA
⑭ 3
13
5)
[S2
LCs (6 6) 1+ LC5 (5 - 25
,
=
, , 2) = man
3
(LCS
LCs (5 5) ,
= mar (4, 53
, 2
I
LC5 (5 4) 3 2C5 (2 3)
GLC5(1 3) 1
,
,
=
man ,
2
[25(3
5)2 LC5 (2, 2) I
LOS (4 5) ,
= may ,
↑
S
LC5 (4 , 4)2 (C5 (4 , 1) = 1 + LC5 (3 07
,
3 # ⑧
LCs (5 , 4) =
1 + LCS (4 3) 2
,
-
LC5(1 , 2) = 1 + LC5(0, 1) -
z O
LCs (3 5) ,
=
man
((C512
LC5 (3
, 5)
4)2
,
2 LC5(2 , 1) =
man
&LC5
LC5 (2, 0)
(1 1178 ,
o
,
z O
(C5 (44)
(105(3 4)2 LC5(3 , 1)
man Las
=
,
= man ,
LCS (4, 31 2
I
(2C5 (2C510
225 (3 ,) =
man (3 3)2 , LC5(1 / 3) = man , 3)0
LC5 (2 , 4) I LC5 (1 , 2) 1
z
3)
E (LCSto
LC5 (4 , 3) =
mas LCS (3 3) .
2
LC5 (1 , 1) = man ,
28(10)
2C5(4, 2) 1
[15(1
*
431
length
=4
LCs (2 , 4) = mar , .
LC5 (2 , 3) 1
205" (4 2)
(LC5(b 2)
Substringe BCBA
=
, man ,
(C8(4 1) ,
I
I p
225 (2 2) man
&Les c,0
=
,
chain
=> Given a
of matrices (A ,, Az -
--Ax]
where A has dimensions Pix XPi ,
Fully
parenthesize the
product A2---An Al ,
in a
way that minimizes the number
of multiplications
scalar . Give the recursive
solution
for minimizing the cost of
parenthesizing the Matrix Ai Ai 1---Aj and .
+
compute
Al XA2 x A3 X A4
wher ,
Al : 2X4 A2 : 4x12 A3 : 12X4 A4 : 4x10
- The
following recursive solution is
Emin
i
[i
g if =
j
e
j]
=
,
[c(i , ) +
e (k ,
j
+
Pipe
AiXA2 X Az XA4
-
2x44X12 12x44X10
↑
=
min [0 + 0 + 2x4x123 96 =
min(e (2 p xpzXP33
t
c (3 37
↓ (2 3)
,
=
, 2) +
, +
,
=
min[0 + 0 +
4x12X43 = 192 .
mintboxbo- (1 , 3)
,A
= = 0 + 192 + 2x4X4 = 224
c (1 , 23 + c (3 3)
, + prxb2xP3
1 < 3 -1
C(
2k4
,) =
meet
E c (2 3) , + c(4, 4) + P
, XPzXP4
0 = 480 + 48 = 96
192 + 0 4X4X10
=
+ = 192 + 160 =
352
min
S F
0 + 352 + 2 x4 X 1 0 = 352 + 80 =
432
e ,) =
9) + 480 + 2x12x10 = 96 + 480 + 240 = 816
1 < k <4 c (1 , 3) + c (4 4]
,
+ Po XPzX Pr
2 + 00 = 22
Parenthesization .
S 3
(2A , xAcxAs) X A
An
Que Karger min-cut algorithm : -
o
>
To collapse the edge Cr v) create node
-
we a new
, ,
En wa or
, Ev ws ,
with a new
edgecur ,
we
and
Mand
v
then delete the
original virtices .
Min cut
>
Karger :-
-
1: Run
return
the
the
guessing
smallest
algo many . times and
guess
.
in a row
.
mink
i
for 1 to N .:
=
mink
if 1x1 < .
mink = 1x1
mina = X .
return mink .
Applying min-cut-algo to
given ques :
rapsing↓
( , 4) Yater pai
- collapsing 20
collapsing Ga
#D a re
final graph
after applying
the
algorithm
.
Qubi-
F y2
=> expression for
firstly deriving its Boolean the above circuit :
Yo yay - %
=
Now * 5
y
=
,
yy
=
y5xys
-
y
= x , nx4 , y2
=
T
yz
=
nxn T yq
=
(4 144)
,
*
42
T
-
②
y5
=
77 2
x(uznx)
=
>5]
Now , (((4 , 2x4Xx2) /Ev(Uza ** )) <
ye
=
(an b) y c =
(avc)n(byc) .
%o
=
((k xx2) n (44YX2)]
, /zvU3) NLT2 vn] nT5
the circuit
Now for to be satisfiable following conditions
must be hold true :
I Tre
(k2VU , ) >
-
must be true
True True
(V *) >
-
True
then for (x2X4 , ) >
-
true
considering/
true l = then , I, can be
anything
(O2Vx) >
-
(v) >
-
-False >
is >
-
true
so,
Iuse
(2 YUz) >
-
y has to be true
#-
true .
Hence , 41 >
-
True/false
42 - True
3 - True
44 >
-
False
45 - False
Ques +
WSCP
by pricing method : -Given
diagram
is :
w
b
w(S1) =
So the WSCP that :-
,
says
i/p = a set X =
Ex , x2 ,
- - - - - -
kn] of n elements .
a
family f =
of u and w(f) =
weights of each
subset If .
w(f) > 0 .
Pre-condition =f covers a i .
.
e
w(e)= was
Now
coming
to our
question : .
-
w(s)
⑭
-
·
&
③>
- 12
>
-
15
⑭
⑮ > 10
⑳
-
Now the optimal solution :
Copt
=
[S2 , Az , 15]
W(Coptl = 9 + 12 + 10 = 31 .
the
algorithm as
follows :
E
N = ( *
uncovered elements)
c d
= ( set cover)
while U :
=it
a
noo
s demn
u = v -
S
c = c S
return C
#
Running the above
algorithm ,
ituati
#
on
p
=
2475 , >
-
% = 2 -
X-
Ya
v
2
= .
2 *4 Ss
> 12/3 4
-
X5
Sk > 15/2 = 7 5
X6
- .
X7 Sq
Xf
>
-
1 % =
3 .
33
itration o
d
& p =
u
2 K 1, a
,
2
9/2 = 4 5 .
12/1 = 12
15/1 = 15
4 5 .
Us
4 .
5 s
1 %
/r = 5
RT
Rf
iteration
Erd
ice x
p =
,
Si a
2
R2
52 es
5i as
Sy
Sk
12/1 =
12
4 5 1/1 =
15
XS
.
x7 S5 10/1 = 10
E
1048
-
4th iteration
-
Price : -
24 ,
Si
D
2xz S2 s
e-in
s
53 e
In -
12x7
10 Up
EPi(ki) w(l) 4
2 + 2 + 54-
Now 2 + 2
= = + .
4
. 5 + 12+ 10
(c)
39 =
algorithm
using
Etopt = 31
.
-Given graph :-
a
Step
I
-p
I
find the pair of verti where
cut size decreases :
Let us take :
vertise pair (H , ED
se
Now cut =
2 .
⑭
Now cut =
1
So 2-1
gain
=
= 1 .
So ,
here also : -cut has been decreased
Hence also accepted
, this
swapping is .
Now if we
swap more
pair of vertices
,
will
definitely the
then cut increase
.
This
I is the minimum that we can
get .
proteo
verters cover
for
-
above =
⑦2
[B(4) ,
C(2) ,
e(2)}.
↓
*
total weight = 8 .= c
c =
0 .
Now ,
approximation algo, verte cover : -
1st
edge > (A C)
-
e =
qU[A CY ,
⑦-----
·
2nd
edge ,
c =
&A CSU (B D)
,
,
C =
[A , C , B D3 > total
weight
-
>
-
3 + 2 + 4# 4
·
> 13 = C
⑰
-
----------
des
Here
,
co =
13
*
C = 8
s
>
pans
-
C cost of solution
of optimal
*
c >
-
cost solution
For minimization : -
i
for
maximizati
I
STATION
·
·
S1 ,5
LINE-I
e Exit
↑
Entry
- ⑫
⑫
5
X
5
654
S 52 ,
STATION
2 S2 , 3
STATION
52 , 4
STATION
S2 ,
STATION
5
if j
detai
=
2
Fli] =
[F/j-1)
min +
Rej F2/j-c] + +
, ,
+
is a] if j22
E/j) =Let Feli-t] + + y
,
-
1
+
azifj22
For j = 1
# 11] 8
= e + au =
2+ 6
,
=
5+ 8 13
F(1] e + 22
=
= =
For j = 2
= min
[0 + 8 ,
13 + 2 + 8]
= 16
= min
[15 + 5 0 + + 6),
2
= 16