Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

Assignment -3

217 a>Two classrooms problem


Consider the algorithm where finish time and then
sort
we
by assign as

many as possible to class 1 and the remaining to class 2 .

Lets take an example of following activity ranges .

[ 1,3 ] ,
[ 1,2] ,
[ 3,4 ] ,
[ 4,6 ] & [ 2 5)
,

Time

I 2 3 4 5 6

Sorting by finish time


[1,2 ]
[ 1,3]
[3/4]
[2,57
[4,6 ]
Assigning as
many as possible to classroom 1 & then to classroom 2 .

I 2 3 4 5 6

class 2


class 1-

class 1

class I

do class I will get > [ 1,2 ] ,


[3/4] and [ 4,6 ]

Assigningremaining to class 2 ,

class 2 will only get [ 1,37 and [2,5 ] is


remaining and is not
to
assigned any class .

do class 1 gets 3 schedules activity


class gets only
2 1
activity .

However , if we assign [ 1,3 ] to class 1 & [1,2) to class 2 then ,

class 2 will have 2 activities .

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 .

Hence the algorithm to sort


, by finish time and assign does not always work
as not all classes were
assigned as seen in ① .

b) Greedy algorithm to solve this problem


First ,
we will s.at all the activities with end time .

[ fio fin fiz


, , ,
. . .
, for ]
<
decreasing
For 2 activities with same end time ,
we will sort wilt start tire .

Pseudocode : 11 A =
activity .

Gannon 1 = 1st
activity with least ending time

Classroom 2 = 2nd with least ending time

for i =3 → n :

p, = last activity in a 11 classroom


pz
=
last activity in cz

If Si < flpi ) && Si < flpz ) :


Skip A
else :
Assign A ; to classroom where it is not overlapping & the
difference b/w bi & P, or E) least ( .
# Proof :

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 -

to> Both activities different


are -

then ) < f (0*+1)


flare ,

Greedy is still feasible , irrespective of which class you assign to it .

27 Orel and Arti are the same activity .

This means
they are not overlapping with both classes and are
assigned to different
classes .

Now time between last activity (f) and scare , ) is minimized


for art , , .

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 .

This still waiting line to be considered


activity is in .

If
"

ahead with the optimal activity


"

solution then An can never be


we
go ,

considered as it will overlap in both classes .

But An Can be added to other class in


greedy if time remains without any
other activity overlapping .

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 :

doting by finish time =

Olnlgn )
for hop ( 3 to n ) =
01h )

Overall
0lNg#
: .
=
22dg Given n customers

ith customer d s is n ) needs service time ti .

Task : Order customers late such that average time is minimized


in
waiting .

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

We will put ✗4 at last ,


so it will reduce time for everyone
else .

start [ - - - -
✗4 ] end

Then the next worst


,
is xs .

start [ - - - Xs ✗ 4 ] end
We repeat the process until the line is filled .

[ i-z.ie ii. is II ]
, , .

Tune wait
I ¥ ! I ¥

Avg Wait Twine =


0+1+4+8 -114 =

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 ]

Although we can see that the


waiting time of the last customer remains
unchanged ,
the overall waiting time will increase for the second last customer .

Avg Waiting time


. 2915
-

Previously it was 2715 .

Hence ,
we have to put customer with max service time at the end .

P.TO
Algorithm :

Given Data : te tu,


.
. . . .tn

Sort the array order of time


in
ascending service .

This will be the qotimd solution .

Time Complexity =

Olnlgn )
=

This will always work ,


as
changing any position will the waiting time
increase

for the next customer in line and the average


, waiting time will increase .

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 :

107 Create dp of six In-11) ✗


matrix (Wti) where the of elements
" "

,
n is no

and W is the total weight 0 Cnw) .

Note dp [ it Ij ] gives the profit possible for weight j when


' '
max

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 ) -

( : included profit of Cpi) and added what was the profit


'

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 ,

Tcn ) = 0 ( nw )OCW ) + 0 Cn) + 0 Cn) OCW) 04 )


+ .
.

=
20GW ) + OCW) + 0 Cn)

ocnwl-P.to
=
Example :

Item Weight ( Wi ) Profit Cpi )


A Zpd 190
B Zpd 180
C Bpd 300

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

O O 190 190 190


0
0

67 E-
2,8=1
1-2<0
j Wi
- =

dp = O O O O O

O O 190 190 190


O O
O
7) 6--2,8--2
dp = O O O O O

O O 190 190 190


0 0 190

8D i=2 j=3 ,

dp = O O O O O

O O 190 190 190


0 0 190 190
0

97 E- 2,8=4

dp = O O O O O

O O 190 190 190


0 0 190 190 370
0

107 0--3,8--1

dp = O O O O O

O O 190 190 190


0 0 190 190 370
0 0
1101 E- 3,8=2

dp = O O O O O

O O 190 190 190


0 0 190 190 370
0 0 190

1207 I =3 , 8--3

dp = O O O O O

O O 190 190 190


0 0 190 190 370
0 0 190 300

137 E- 3
, j=4
dp = O O O O O

O O 190 190 190


0 0 190 190 370
0 0 190 300 370

Final answer
dp -145147 =

37¥
247 Hoffmann code :

A : 15 ,
6:10 ,
C: 13
,
d :3
,
e :
18
, f : 41

Taking small values first ,

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

Character Code Freq .


Bits (six )
.

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

i. The size of the compressed code is


221A .

257 Consider the


following greedy strategy for the rod
cutting problem .
For a rod of
find { 1,2 n} n then
length h, i c-
,
with
. . . maximum pili value .

If i =

stop ; otherwise cut off a piece of length i and


repeat .

Assure this greedy approach is


optimal .

Consider the following example ,

length i 1 2 3 4
l 20 33 36
price pi
9
pili 1 10 11

Let the length of the rod be 4 .

According to our
greedy algorithm ,
we will
fist cut out piece of length 3 ( price is
a 33 &
pili =
11 ) & thus we

are left with a piece of length 1 ( price is 1) .


The total price after the rod would be 34 dollar
cutting
i.
.

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 .

Our greedy approach is not the optimal solution .

Thus by proof by contradiction , have shown that the solution not


, we
greedy is
the optimal solution .

You might also like