DAA Assignment 3
DAA Assignment 3
[ 1,3 ] ,
[ 1,2] ,
[ 3,4 ] ,
[ 4,6 ] & [ 2 5)
,
Time
I 2 3 4 5 6
I 2 3 4 5 6
class 2
④
class 1-
class 1
class I
Assigningremaining to class 2 ,
I 2 3 4 5 6
class 1
②
class 2
class 1
class 1
class 2
Nov class gets activities before However class
,
2 3 ,
same as .
, 2
gets
2 activities ,
1 more than before .
Pseudocode : 11 A =
activity .
Gannon 1 = 1st
activity with least ending time
for i =3 → n :
Let ai , ah . . .
,
ak be activities selected by greedy .
Let 01 , 02, . . . .
,
0m be activities by some other optimal solution .
where Al =
01 ,
a 2=02 ,
. . .
,
ar = Or for the largest possible value
of t.
Now , for activity Or+ i & are , , following cases are possible -
This means
they are not overlapping with both classes and are
assigned to different
classes .
Time between scorn ) & its last activity is either equal or more '
fa -
end tone of greedy's lost activity
to =
end time of qntimal 's last activity
to Ffa
Now we assume a
process An with start tire between to and fa and
,
end time
after Orn or Arti .
If
"
Hence ,
no
of processes in the optimal solution is less than the number of processes
in
greedy > which is a contradiction .
: .
Greedy is the
tmeoptimalsobtion
Twine Complexity :
Olnlgn )
for hop ( 3 to n ) =
01h )
Overall
0lNg#
: .
=
22dg Given n customers
Given : All the customers arrive at the same time and no more customers arrive
later .
We have t 1 1-2 t 3,
, ,
. . . .
,
tn .
To to put
minimize the waiting time for everyone .
we would always want
the person with the maximum service time at the end .
Lets take an
example .
ya = 3
X2 = I
23 =
4
✗4 =
7
Xs =
6
start [ - - - -
✗4 ] end
start [ - - - Xs ✗ 4 ] end
We repeat the process until the line is filled .
[ i-z.ie ii. is II ]
, , .
Tune wait
I ¥ ! I ¥
27¥
5
If we
change the position of any customer and swap with another ,
the average
tire will increase .
eg [ ii. I. is ii. E. ] .
Waiting time [ 0, 1
,
4 ,
10 , 14 ]
Hence ,
we have to put customer with max service time at the end .
P.TO
Algorithm :
Time Complexity =
Olnlgn )
=
237 Shan that the 0-1 knapsack problem can be solved in 0 Cnw)
Given : Given n -
items to fit and a weight -
limit of W .
In a 0-1 problem ,
we have only one of each item available .
So there
,
are
only two possibilities .
item = 0 Or item = 1
v v
item not added item added to
to the bag the
bag
D. TO
0-1 knapsack DP Approach
The logic to choose whether to pick up or not pick up each item ,
but
using
an iterative bottom-up approach .
Algorithm :
,
n is no
conclusive )
including items from 1- → i
↳ dp [07-10] , dp [01-4] ,
. . .
, dp to] [ w] =
0 OCW)
> as there are no items included .
37 dp [07-10] , dp [ 1) to] ,
. . .
, dp Eh ] [0]
=
0 0 Cn)
since total allowed
> weight is 0 .
47 To compute dp[i7[j ] ,
we either include the ith item or do not
include the ith item .
)
i For including ,
=
pi + it [ j Wi ]
dp [ i -
-
( if dp[i -
i ] [ j wit exits ) -
i
for weight (j -
wi ) when including items 1- → (i i ) ) -
ii
) Dorit include
dp [ i i [ j ]
= -
( '
: It is
equivalent to
profit for weight j when including items
1- → e- 1.)
5) dp [ i ] [j ) =
Max (pit dp [i -
i [j -
wi ] , dp [i -
i ] [j))
( check if (j -
Wi ) >a) 0>4)
67 Iteratively compute the above for i =
1 → n
, j = 1-→ w Ocnw)
7) return dp In ] [ w ]
Time Complexity
The time complexity for each line of code gñon above is ,
=
20GW ) + OCW) + 0 Cn)
ocnwl-P.to
=
Example :
Let W=4
b) dp = O O O O O
O
O
O
2-78=1,8--1
j -
Wi = 1-2<0
i.
dp [01-4]=0
dp = O O O O O
O O
O
O
3) 0--1,8=2
j -
Wi =
2-220
dp [03-12]=0
dp = O O O O O
O O 190
0
0
40) I -4,8--3
dp = O O O O O
O O 190 190
0
0
57 it ,j=4
dp = O O O O O
67 E-
2,8=1
1-2<0
j Wi
- =
dp = O O O O O
8D i=2 j=3 ,
dp = O O O O O
97 E- 2,8=4
dp = O O O O O
107 0--3,8--1
dp = O O O O O
dp = O O O O O
1207 I =3 , 8--3
dp = O O O O O
137 E- 3
, j=4
dp = O O O O O
Final answer
dp -145147 =
37¥
247 Hoffmann code :
A : 15 ,
6:10 ,
C: 13
,
d :3
,
e :
18
, f : 41
17 13
3 10
d b
27 26
13
13
C
3 10
d b
37 26 33
13 15 18
13
c a e
3 10
d b
4) 59
26 33
13 15 18
13 a e
C
3 10
d b
5) 100
0 1
59
41 1-
0
26 33
0 0 1-
1
13 15 18
13 a e
C
O
1
3 10
d b
P
Codewords and their sizes
A 110 15 45
b 101 1 10 40
C 100 13 39
d 1010 3 12
e 11 1 18 54
f o 41 41
Total 231
If i =
length i 1 2 3 4
l 20 33 36
price pi
9
pili 1 10 11
According to our
greedy algorithm ,
we will
fist cut out piece of length 3 ( price is
a 33 &
pili =
11 ) & thus we
However there better cut the bed We cut the rod into
way to
,
is a .
can
two pieces of length thus giving us a total of 20+20--40 dollars
2 , price .