Sei sulla pagina 1di 4

The University of Lahore

Subject: Design & Analysis of Algorithms Assignment#: 01


Due Date / Time: Oct 10, 2019 Max Marks: 15+15
1. A fisherman has permit to catch fish from a NxN area of river Indus. He has equipment to cover AxA
area in a day (N is integer multiple of A).
i. Write an algorithm that fisherman follows to cover all its area.

Sample Solution:
proc cover_area(N, A)
for len  1 to N step A do
for wid  1 to N step A do
catch_fish(len, wid)
next
next
end proc

Complexity:
Cost of Line 1: N/A + 1
Cost of Line 2: N/A (N*A+1)
Cost of Line 3: N/A * N/A
T(N) = N/A +1 + N/A (N*A+1) + N/A * N/A
= O(N2) // A is some factor of N

ii. Fisherman has cover width of river first before going towards length. Rewrite the algorithm
(as part i. of this question) while covering this constraint.
Sample Solution:
proc cover_area_width_first(N, A)
for wid = 1 to N step A do
for len = 1 to N step A do
catch_fish(wid, len)
next
next
end proc

Complexity:
Cost of Line 1: N/A + 1
Cost of Line 2: N/A (N*A+1)
Cost of Line 3: N/A * N/A
T(N) = N/A +1 + N/A (N*A+1) + N/A * N/A
= O(N2) // A is some factor of N

iii. Fisherman works six days and rests for seventh day of week. Re-write your algorithm (as part
i. of this question) while covering this constraint.
Sample Solution:
proc cover_area_six_days_per_week(N, A)
day  1
Section C
The University of Lahore
Subject: Design & Analysis of Algorithms Assignment#: 01
Due Date / Time: Oct 10, 2019 Max Marks: 15+15
len  1
while len <= N do
wid  1
while wid <= N do
day  day + 1
if day mod 7 = 0 then
continue // (don’t work)
else
catch_fish(len, wid)
end if
wid  wid + A
next
len  len + A
next
end proc
Complexity:
Cost of Line 1: 1
Cost of Line 2: 1
Cost of Line 3: N + 1
Cost of Line 4: N
Cost of Line 5: N * (7/6 * N + 1)
Cost of Line 6: N * (7/6 * N)
Cost of Line 7: N * (7/6 * N)
Cost of Line 8: N * (1/6 * N)
Cost of Line 9: N * N
Cost of Line 10: N * (7/6 * N)
Cost of Line 11: N

T(N) = Add all costs and simplify


= O(N2) // A is some factor of N

iv. If the fisherman catches X units of fish per day and fish earns him R rupees per unit. Calculate
the overall money that he earns.

Sample Solution:
proc calculate_cost(X, R, N, A)
work_days  N/A * N / A
total_units_of_fish_caught  work_day * X
earning_per_unit  R
income  earning_per_unit * total_units_of_fish_caught
return income
end proc
Section C
The University of Lahore
Subject: Design & Analysis of Algorithms Assignment#: 01
Due Date / Time: Oct 10, 2019 Max Marks: 15+15
Note: Calculate the complexity of first three parts of question 1.

2. A manufacturer manufactures three types of products type-A, type B and type C. All three products cost
him same per unit. He has N workers. 20% of workers can manufacture type-A goods only, 20%
workers can manufacture type type-B goods only, 20% workers can manufacture type type-C goods
only, 10% workers can manufacture type A & B goods, 10% workers can manufacture type B & C goods
and remaining 20% workers can manufacture all types of goods. Following assumptions hold for sake
of simplicity:
 Every worker produces X units of goods per day
 Production cost for type-A goods is C per unit

i. Write down an algorithm such that the manufacturer produces R units of all three types each (R is
large integer). All three products start producing and end producing at same time. Calculate its
complexity.

Sample Solution:
proc produce_products(N, R, X)
man_days_per_product  R / X
workers_per_team  N/3
workers_type_A_only  0.2 * N
workers_type_B_only  0.2 * N
workers_type_C_only  0.2 * N
workers_type_A_and_B  0.1 * N
workers_type_B_and_C  0.1 * N
workers_all_types  0.2 * N
team_A_workers  workers_type_A_only + workers_type_A_and_B +
workers_all_types * 1 / 6
team_B_workers  workers_type_B_only + workers_type_B_and_C +
workers_all_types * 1 / 6
team_C_workers  workers_type_C_only + workers_all_types * 4 / 6

for k  1 to man_days_per_product / team_C_workers do


work N/3 Men for product A
work N/3 Men for product B
work N/3 Men for product C
next
end proc

ii. Half of the workers that could manufacture all three types of products left the company. Now re-
write the algorithm as above covering this constraint.

Sample Solution:
Section C
The University of Lahore
Subject: Design & Analysis of Algorithms Assignment#: 01
Due Date / Time: Oct 10, 2019 Max Marks: 15+15
proc produce_products_few_workers_left(N, R, X)
//Half of workers who could produce all types of products are left i.e. 10% of all
employees gone

man_days_per_product  R / X
workers_per_team  N/3
workers_type_A_only  0.2 * N
workers_type_B_only  0.2 * N
workers_type_C_only  0.2 * N
workers_type_A_and_B  0.1 * N
workers_type_B_and_C  0.1 * N
workers_all_types  0.1 * N
team_A_workers  workers_type_A_only + workers_type_A_and_B
team_B_workers  workers_type_B_only + workers_type_B_and_C
team_C_workers  workers_type_C_only + workers_all_types

for k  1 to man_days_per_product / team_C_workers do


work 0.3 * N Men for product A
work 0.3 * N Men for product B
work 0.3 * N Men for product C
next
end proc

iii. If the manufacturer earns 50% more money per product than he pays to his workers, how much
money does he make as per situation is section i. of this question.

Sample Solution:
proc earned_money(R, C)
cost_per_product  C
earning_per_product  0.5 * C
total_products  R
total_income  R * earning_per_product
return total_income
end proc

Section C