ADA UNIT 4 Complete Notes
ADA UNIT 4 Complete Notes
Examples:
• sorting: lower bound (n log n)
• searching in a sorted array: lower bound (log n)
• multiplication of n × n matrices: lower bound (n2)
There are a number of methods that can be used to establish lower bounds
• Trivial lower bounds
• Information-theoretic arguments
• Adversary arguments
• Problem reduction
Trivial Lower Bounds
Trivial lower bounds are based on counting the number of items that must be
processed in input and generated as output to solve a problem.
Examples:
Sorting Algorithm
Searching Algorithm
Graph Traversal Algorithm
Matrix Multiplication
Information-Theoretic Arguments
Information-theoretic lower bound is based on the amount of information an algorithm needs to
produce to achieve its solution.
Adversary Arguments
Adversary argument method is a way to estimate the minimum amount of
computational effort that an algorithm would need to solve a problem.
Example
Finding the maximum element
Searching for an element in a sorted list
Problem Reduction
Problem reduction method is a technique used to obtain lower bounds on the number of
operations required to solve a problem.
Examples:
Sorting problem
Graph Connectivity problem
Decision Trees
A decision tree is a powerful model that can be used to represent a sequence of decisions made by
an algorithm in the form of a tree.
A decision tree is composed of nodes and edges. Each internal node represents a decision. The
edges coming out of a node correspond to the possible outcomes of the decision. A leaf node
represents an outcome or a solution.
Structure of Decision tree
Root Node: The root node is the starting point of a tree.
Internal Nodes: Each internal node represents a decision point.
Leaf / Terminal Nodes: Leaf nodes represent the final class of the outcome.
Branches: Branches are connections between nodes. Each branch represents a response such as yes
or no.
Decision Trees for Sorting
Example 1
Most sorting algorithms are comparison based. Consider an example of selection
sorting for three elements in a list using decision tree.
Example 2
Consider an example of insertion sorting for three elements in a list using decision tree.
The worst-case number of comparisons for insertion sort is 𝒏𝒍𝒐𝒈𝟐 𝒏
A problem is solvable in polynomial time if its worst- case time efficiency belongs to
O(p(n)) where p(n) is a polynomial of the problem’s input size n.
Problems that can be solved in polynomial time are called tractable, and problems that
cannot be solved in polynomial time are called intractable.
Class P Problems
DEFINITION: Class NP is the class of decision problems that can be solved by non-
deterministic polynomial algorithms. This class of problems is called nondeterministic
polynomial.
Most decision problems are in NP
P ⊆ NP
Example
1. Travelling Salesman Problem
2. Knapsack Problem
NP-Complete Problems
It belongs to class NP
Every problem in NP is polynomial reducible to D.
1
CHnprrR -6
Bctck bvac k NA d
d
PugF:,"3,
ktmclcinq is ah alaoa;tF,nte tpthn q,r,
Brc
U
ldnwo +'F,t ts t, a@* oltA sot,rlru,r, h '*
N .tte
r"b rD usina
[,a: huh *** q.ytptae h, it brtirb
b,nldr'"f a- s0* d'rotuk'o>r ircfizru::tb
% %'ott
gLnca o.
prJtUn w*uA ha ,u ur>*lroink , 'tFt I of."lfu'v
ro Pi [l ba t*r'P"eA
#at baiL t Salttfil tf,p '
AE
1
U,L
%
ofirmal so lrrh". caD La
u;Mcl,
ryP tf{ baek{roe k i neM,
Solved wt "a 1
1ii) ao *PPU b"ck.lrac k ilta+frwd , #^e goltt*tsr, mtx|
(x,-. - - z..r)
bo LYA*< fblt n- tuPLe .1
a,s Arf)
*no -A fiorlos ,
r. Pxpltc|b
A.?-nplictL
Fx ['ei L br*1r6
t*1&@J
n,l-r
Xi=a@ft or S i=[,o,t )
hll "lupb, +T,at Bah\ - +11, ULP lr'eib u*4 tal nb
bl
4,,* ar' Po4n
DblL sobt}i'oo oru b* t"
L
I
F xanpb,
Ik A - @uu-n PTO
bten , *fv oltntnh "fi lf'e laPlz
rrxrst h ,50,b 8= \,rrr3,4
1lasen
F*tu 1
trornrw
I
I 03
I
3
l-iro-\A.lrA*J
q+r.A. nodr 't*fls ls a b@1, wl,tc h ho.s bon aat eryulnd
at,d qtl uheg c hil&^en &ra r>ok u oL qo-{rr*ha
"fi J (l
rwdt /, ltvo tuod! u) bkc ln/d,tro ate
*n Xo,*""t
d is ealbd tf-rn d,
cu*rontfy
'Dooal
:a.\,1.ms Nr-d,
: fi hrolo ot-rena,lad is ol{F,ev t>ot b" exfdrd
a
$u.v{fvv sl flfie ohE
il* wln'el, a,1/ 'tt, cl, tdtzo herrye
bun aao,rcratLd
fldor"li Y&
E**l EEIfsHs0 NPi
14 It *olttlisrx dfr' f as-'eraro/ in x ( r:n) *rd
a,g Sson aA 'rTreU a^2 aln'fernirvd 7() bill .
fytrilLd
T)td tt,( ao* %I" p*-itu val'at
blaor i+fr,ro : {Z ac k trac k ( n)
q0
k =l
wl"ib ( t, L =o ) alu
{
if ( +t*.," t*tnai >t etn untriod *( x1 e{*t,l,vtrJ
' '. . :c[x ',y and B1r(xtil" xtil)=/,,ue)ll*o
t if, ( xtti -- - xlNi) o'WO
ts a W lo
lxin{:
( xl,I zt rl )
k =k*r
L
J,
8-W
!9 fu=k-t
I
h
l) oritfrm : R Bact<lRnckCK)
$- @,.,ten
E&..F*d\e'se,
Pnobbn
r.*#i..rt#i*.,
SbdhrnenF :
Yrr*'*frdfsr#
Tfu
cksa bsat d
r tfret
SO
obbn ,'J
ho
to plo*
lwo
A-
uze.b
?webs
att
on hvtt
c* oach slfe,
?
bu ba h ti,( gaff t t@a) OT in +f',r ?atfie celumn ot
1
0n +fN Samt olryorul
LvM Lo rx4rai
9* t,,'ci
u l- C-o
r-y--!
rx.lra, nh
-J
--*
:
l. Irlo
L- Alo
4rpo
4wo
1* c-a,r) ho en +fP Sa,tw totD.
bu oh lfrt amt galumn
3 Alb
7*'^ Can S .
lwo
?t/!t
t>r eQ/h L" on +fx s aw dr'oqoml
a
..Il.g cbl.x b"*d
b* +1"e.
9'* 1/* h },ueo
b lam rs
Sk.rrn be-ln*,
3h
@,,run I
L <- @,raen L
3 @,.,un 3
tl
I <- @uu" *
fr"1
t2
I Q,
L
3
4
I
L
Step z
Ada-6+."e
-rk tl*r ol ?ueon shoutd >oF ba in 'tfr''d
*pu P *t*n($o* @l,,mo @, * @=), +fn"d
ftr,rs
cD;, ( Az * 4s olingo'-lt1 oae| drA'
$rs A sLcond
4fitrd cpLumn ( e,L @g sa'ntt e'L*n)
lkrd *oa
, cofo*n ( 64, L@z ar"X orn, ,y
4krd Soul
fieurlB
e+fru, ) E, back t raak irl ond
0-qoh
st;gt Qt obu.
I L 3
't 2-3
,
&r @'
I
L
L L Ar)
3 3 -3
h 4
Lg lo*n
-Tfnn plaw Gs in' #*'ol frsu: s o+n"'l .
l>
I
2
3 Q* -l
q
7
, , Do BacHtack 1
@ t is skSad oncz
r LJ l> 3
I
L
& i
lt I >< @,
3 3
h a1 A
I z 3
I
1
3
lr
I
f ,rst La lunn
z 6>
3 'qt
4
I
Sh 7 pl aw @a in $frmtB
soat tf,trd cok*n
bet *r* ) rtbffi fiso r rs F col,,mn
(e* [ Q,]
In d,ame Cetnnn ) , u) sorpld ee lurt, n
$o", tf''
cro
6, xz= h
3 33 !C3= I
--l q-
l
h j x+ =3
@r 2(1 =3
L @z x>z I
3 6* xz=h
ll &,, 2t4=L
T
+fre *i rs I s alr4ion is I hoon
q
flu e'lafr .e
lau 4ras fio4
bdntt'
'Xl= I
Xr L.
(4r
r
tl
r{ x
N
Ir ]
_llo
4,1 I
I
/, 0,
1
, 6'i
IL
t
a/, I
+
N * t1
It
L?i i
Az
(*,
I
Sob.knn '
G
l0
3--eggl*- .?xb,k*-,
S be,Jnrm nh ,
-4Nb,#
7*o*
b*ru rn tfrL &rrte fr&a, ox in tfrz 3 a rrL{ C'o lan
n
\
0"{ @n +fre (tulw duV" r*t
fx c-) b CorNt
e*,r44.#
rai nh :
ti) At" *
lA ro eyu-tLM (4,h b* @n tfr" Saxtr rsa)
t
+
t'd) Al, Itwo q** czh b tk BN *a'trw coutril)
[ii;;
L
ca.h L* oil 4f" dr"g*t
Ato w)b
1y^ Same
h &?1 Lhm
i6 (n=n) +f"n
vovib (n C t : hJ ).,
o-lse
N Quoun s ( xrr , n) ;
3
\
3
II
n oniffi no : yl"c"(w,i )
4
f, J= | b, b-t d.
r5 txtjl ,
I
)
fr!,f,r!* QlaLx ;
L 6r
3 6a
A &.,
, txc
b 6t
T 6r
g q(
3 o3
It n
4
114
5 6
I
I
L"tg
b
-:
@o
"7
g
t- 6,
G,g
IA
.f", w S,r,bsz
q\,
4,t, , ' ' ' ' sn 1%-n- f rti Fi
va
GSirnn &tuts
inbo6zx aur d (\
P
ssifir,'u i n h*T- -Tk ytobbn t3
.g
.h all a*b+nha "fr tff
tr
ven I ub w bo*e 3 uro
bt'd
h tl,
is
%d
f**wh-'
tri 5={ ll., 13,7+,'1 ) o.nd C = 3t. 'TF,,n &*lwo)
S rrbs,e ft ard L
{", tz,1\ ano) 4'+ ,t \
{, Solvo 'tf*s y, bltm LL{,,7 ' bnc klroc ki ntr:
I wdan
r, 4irsl wu &ott lt'e vo'lnos t r) in evohq
T
,
. s ={ 1, u, t3 ,zlrl
4
tj
iiJ
X
XX
X xX
+ x- rr#;cats d!^d wdto
r,
l" so hr,F, , ',/ uLW "1, lt', tB o'{ ,lr l) l, o j
\
p, . sol^rFton vo-ch"r z{ '/, ah3 o? \r,D,D,r\
r8
:tol* I * s[ it r]) ;
sIi+J--fftu,
S**"bSu,bszFI
e
i+1, **A hb,bob"l -sfi+,J);
\
)
F *(o:tcik. '. I
fxuvcix i pl
^r.frd*\'^".:&*ffi
Step 2 The root node has three adjacent vertices 2,3,5. We select 2, it has two adjacent vertices 5
and 6. We proceed to 5, it has 2 two adjacent 3 and 4. The vertex 3 has one unvisited adjacent
vertex that is 4. The vertex 4 has one unvisited adjacent vertex 6. The vertex 6 has adjacent
vertices 2 and 4, since both of those nodes are already visited therefore node 6 is dead end. Now
backtrack from 6 4, 43 and 35
Step 3
The solution vectors are:
1. 1 264351
2. 1 264531
3. 1 346251
4. 1 354621
5. 1 526431
Complexity
Time Complexity: O(n!)
Space Complexity: O(n)
𝑀𝑎𝑥 ∑ 𝑝𝑖 𝑥𝑖
𝑖=1
𝑛
𝑆𝑢𝑏𝑗𝑒𝑐𝑡 𝑡𝑜 ∑ 𝑤𝑖 𝑥𝑖 ≤ 𝑀
𝑖=1
Where 𝑥𝑖 𝜖 {0,1},1 ≤i ≤n
𝑝1 20 𝑝2 40 𝑝3 35 𝑝4 45
= =1, = = 1.6, = = 3.5, = =3
𝑤1 20 𝑤2 25 𝑤3 10 𝑤4 15
𝒑𝒊
Object Weight Profit
𝒘𝒊
1 10 35 3.5
2 15 45 3.0
3 25 40 1.6
4 20 20 1.0
Root Node
Create the root node. Initially w=0, p=0
UB=0+(40-0)*3.5=140
Node 1
Object 1 is included therefore w=10,p=35
UB=35+(40-10)*3.0 =125
Node 2
Object 1 is excluded therefore w=0,p=0
UB=0+(40-0)*3.0 =120
By considering node 3,4,5 and 6 the highest upper bound is 104 therefore we explore node 3
Node 7
Object 3 is included therefore w=50 ie.,(10+15+25),p=120 ie., (35+45+40)
Weight is greater than knapsack therefore Not feasible
Node 8
Object 3 is excluded therefore w=25, ie.,(10+15),p=80 ie., (35+45)
UB=80+(40-25)*1 =90
By considering node 7 and 8 we explore only node 8 because node 7 is not feasible.
Node 9
Object 4 is included therefore w=45 ie.,(10+15+20),p=100 ie., (35+45+20)
Weight is greater than knapsack therefore Not feasible
Node 10
Object 4 is excluded therefore w=25ie.,(10+15),p=80 ie., (35+45)
UB=80+(40-25)*0 =80
Node 3 is already explored in second level of the tree, therefore by considering the remaining
nodes (4,5,6) explore Node 5 because the highest upper bound is 85
Node 11
Object 3 is included therefore w=40 ie.,(15+25),p=85 ie., (45+40)
UB=85+(40-40)*0 =85
Node 12
Object 3 is excluded therefore w=15, p=45
UB=45+(40-15)*1 =70
Complexity
Time Complexity 𝑂(2𝑛 )
Space Complexity 𝑂(2𝑛 )