0% found this document useful (0 votes)
17 views

Assignment 4 PDF

The document describes an algorithm to find the maximum number of thieves that can be caught by policemen in an array. Each policeman can catch one thief within K units. The algorithm involves: 1. Storing policeman and thief indices in vectors 2. Comparing the lowest policeman and thief indices and making an allotment if within K units 3. Incrementing the minimum index and repeating until all allotments are made The time complexity is O(N) where N is the array size, as the algorithm makes a single pass through the array.

Uploaded by

fatemahatem2
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Assignment 4 PDF

The document describes an algorithm to find the maximum number of thieves that can be caught by policemen in an array. Each policeman can catch one thief within K units. The algorithm involves: 1. Storing policeman and thief indices in vectors 2. Comparing the lowest policeman and thief indices and making an allotment if within K units 3. Incrementing the minimum index and repeating until all allotments are made The time complexity is O(N) where N is the array size, as the algorithm makes a single pass through the array.

Uploaded by

fatemahatem2
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

1.

a) Policemen catch thieves

Given an array of size n that has the following specifications:

Each element in the array contains either a policeman or a thief.

Each policeman can catch only one thief.

A policeman cannot catch a thief who is more than K units away from the policeman.

We need to find the maximum number of thieves that can be caught.

Examples:

Input : arr[] = {'P', 'T', 'T', 'P', 'T'},

k = 1.

Output : 2.

Here maximum 2 thieves can be caught, first policeman catches first thief and second policeman can catch
either second or third thief.

Input : arr[] = {'T', 'T', 'P', 'P', 'T', 'P'},

k = 2.

Output : 3.

b)

“For each policeman from the left catch the farthest possible thief”. A symmetric argument can be applied to
show that traversing for these from the right side of the array also fails. We can observe that thinking
irrespective of the policeman and focusing on just the allotment works:

1. Get the lowest index of policeman p and thief t. Make an allotment

if |p-t| <= k and increment to the next p and t found.

2. Otherwise increment min(p, t) to the next p or t found.

3. Repeat above two steps until next p and t are found.

4. Return the number of allotments made.

c) Below is the implementation of the above algorithm. It uses vectors to store the indices of police and thief in
the array and processes them.

// Returns maximum number of thieves that can

// be caught.

int policeThief(char arr[], int n, int k)

{
int res = 0;

vector<int> thi;

vector<int> pol;

// store indices in the vector

for (int i = 0; i < n; i++) {

if (arr[i] == 'P')

pol.push_back(i);

else if (arr[i] == 'T')

thi.push_back(i);

// track lowest current indices of

// thief: thi[l], police: pol[r]

int l = 0, r = 0;

while (l < thi.size() && r < pol.size()) {

// can be caught

if (abs(thi[l] - pol[r]) <= k) {

res++;

l++;

r++;

// increment the minimum index

else if (thi[l] < pol[r])

l++;

else

r++;

return res;

d) The time complexity of the above approach in O(N) where N is the size of the array. In the pseudocode, the
for statement loop will run n times while the if statement will run only single times and while will also be
implemented single time.
1) The initial set will be empty and keys are assigned to vertices are {0, inf, inf, inf, inf, inf, inf, inf}.

Step1: Initial set will be null. Now, the vertex with minimum key value will be selected. The vertex is ‘a’.
Updated key value of adjacent of ‘a’ will be

Visited vertex= {a}


b Unvisited= {b, c, d, e, f, g}
1 Key value
a b c d e f g
a 0 1 5 ∞ ∞ ∞ ∞

5
c

Step 2: The vertex with minimum key value from unvisited vertices will be selected. The vertex is ‘b’.

Visited vertex= {a, b}


7
b e Unvisited= {c, d, e, f, g}
1 8

a 4 d

Step 3: The vertex with minimum key value from unvisited vertices will be selected. The vertex is ‘c’.

7 Visited vertex= {a, b, c}


b e Unvisited= {d, e, f, g}
1

a 4 d
6
c 2 f

c-d edge is selected instead of b-d due to better key value.

Step 4: now, ‘f’ is the vertex with minimum key value which is 2.
Visited vertex= {a, b, c}
b e
Unvisited= {d, e, g}
1

a 4 d 3 g

6
12
c 2 f

Step 5: Accordingly, the same process will be continued to get the unvisited set empty. Here, the minimum
spanning will be following:

b e
1 10

a 4 d 3 g

6
c 2 f
2) Kruskal’s algorithm:

Step 1: Sort all the edges in non-decreasing order of their weight. After sorting, the edges will be like following:

weight Source vertex Destination vertex


10 6 1
12 4 3
14 7 2
16 2 3
18 7 4
22 5 4
24 5 7
25 6 5
28 1 2

Step 2: Select the smallest edges one by one. Check if it forms a cycle with the spanning-tree formed so far. If
the cycle is not formed, include this edge. Else, discard it.

a) 6-1 edge doesn’t make any cycle. So, it will be included.

1
10

b) 4-3 edge doesn’t make any cycle. So, it will be included.

1
10

6
3

12
4

c) 7-2 edge doesn’t make any cycle. So, it will be included.

1 2
10
14
6 7
3

12
4
d) 2-3 edge doesn’t make any cycle. So, it will be included.

1 2
10 16
14
6 7 3

12
4

e) 7-4 edge makes a cycle. So, it will be discarded.

f) 5-4 edge doesn’t make any cycle. So, it will be included.

1 2
10 16
14
6 7 3

12
5 4
22

g) 5-7 edge makes a cycle. So, it will be discarded.

h) 6-5 edge doesn’t make any cycle. So, it will be included.

1 2
10 16
14
6 7 3
25
12
5 4
22

Since, all vertexes are connected with the edges. This is the minimum spanning tree.
3) Serving the customer having less service time first to minimize the waiting time.

By solving the given data, it can be shown.

All customers Anna (A), Bella (B), and Chloe (C) have arrived and are waiting for the service. So, arriving time is
0 for all of them.

Customers Arrival time Service time (min) Completion time Waiting time
(min) (min)
A 0 5 5 5-5=0
C 0 10 15 15-10=5
B 0 20 35 35-20=15
Total waiting 20 min
time
Here, the customer with lowest service time will be served first, then the customer (C) with next lowest service
time. Finally, 20 minutes is the total waiting time.

Now, for the traditional method the first come, first serve basis will be considered.

Customers Arrival time Service time (min) Completion time Waiting time
(min) (min)
A 0 5 5 5-5=0
B 0 20 25 25-20=5
C 0 10 35 35-10=25
Total waiting 30 min
time

Hence, the waiting time is 30-20= 10 minutes more in case of traditional method.

So, in greedy method, the total waiting time should be 20 minutes.

Hence, Kate Young should use this approach.


4) A job sequence problem that can be followed the approach of Greedy algorithm. It is used o know about the
select value that gives the solutions is optimal format.

Step 1: Start

Step 2: Sort the table allow to the deadline.

a) If the two jobs have equal deadline, then sort the jobs in the form of increasing order profit.

Step 3: Select the job coming last in the sorted table.

a) Let us consider Job number 1 or J1.

Step 4: Now coming from right hand side, then select the job with deadline at least one less than the deadline
of the previous step and it contains maximum profit among them.

Step 5: Repeat step 4 till get the number of jobs, those number of jobs are equal to maximum deadline.

Now, let us take J1, it has maximum deadline (3) with maximum profit.

Deadline =3 then profit= 70

let us take J3, it has maximum deadline ≥3-1 then,

profit= 70+50= 120

let us take J4, it has maximum deadline ≥2-1 then,

profit= 70+50+40= 160

Hence, the maximum profit will be 160

Sequence of job will be first in the form of 𝐽𝐽4 → 𝐽𝐽3 → 𝐽𝐽1


5)

a) Here weight and values are given. In this case, number of people the vaccine can save and given ‘n’ items.

b) A subset of items to include in a collection have to be determined.

c) The total weight is less than or equal to a given limit and the total values should be large.

Item smallpox Measles Yellow fever Chickenpox


No. of people 50 K 50 K 50 K 20 K
can be saved
‘N’
Weight ‘w’ 10 lbs 20 lbs 50 lbs 40 lbs
Ratio (N/w) 5 2.5 1 0.5

The maximum weight can be carried is 100 lbs. In this table, the items are organized according to the
descending order of ration (N/w).

So first we will choose Smallpox vaccine as its weight is < Maximum weight (W) and ratio is highest. Maximum
weight (W) left =(100-10) lbs= 90 lbs

Next Measles vaccine is chosen as available maximum weight is still greater than the Measles vaccine.
Maximum weight (W) left= (90-20) lbs= 70 lbs

Next Yellow Fever vaccine is chosen as available maximum weight is still greater than the Yellow Fever vaccine.
Maximum weight (W) left= (70-50) lbs=20 lbs

Next Chickenpox vaccine is taken , but the whole item cannot be taken as the remaining maximum is less than
the weight of Chickenpox vaccine. Hence fraction of Chickenpox vaccine is chosen i.e. 20/40=1/2

Now the maximum weight (W) is achieved ie. 100lbs. Hence no more vaccines can be carried.

The total weight of the selected vaccines is 10 + 20 + 50+40 * (1/2) = 100 lbs

So, total number of people that can be saved are : 50K + 50K + 50K +20*(1/2)K= 160K

This is the optimal solution. Marty McFly can save maximum of 160K people even if he any different
combination of vaccines.
6)

a) In fixed length encoding each word is encodes with same length code.

Here there are 6 characters are there, thus minimum 3 bits are required to represent the characters.

Character Fixed length encoding


a 000
b 001
c 010
d 011
e 100
f 101

b) The Huffman coding requires to dear the Huffman tree.

For that first sort the frequency table as given below:

Character Frequency
f 50
d 20
a 15
e 12
c 9
b 5

Step 1: Take the two least frequent characters and add them in frequency and place accordingly in the sorted
frequency table. Also draw the intermediate Huffman tree:

Character Frequency
f 50
d 20
a 15
b+c 14
e 12

The intermediate tree:

14
0 1

b c
Step 2: Now repeat the process, select the two least frequent characters of character groups,

Character Frequency
f 50
b+c+e 26
d 20
a 15

The intermediate tree:

26
0
1

14 e
0 1

b c

Step 3: Again repeat the same process, take the least frequent two characters or groups and create the
corresponding tree and update the table:

Character Frequency
f 50
a+d 35
b+c+e 26

26
0
1
35
14 e 1
0 1
0

b c a d

Step 4: Again take the least frequent groups and create the tree by joining them and update the table.

Character Frequency
a+b+c+d+e 61
f 50
The intermediate tree:

61
0 1

26 35
0 0 1
1

14 e a d
0 1

b c

Step 5: Now only two entries left, create the final tree by combining them, and label the branches to left as “0”
and to right as “1” or reverse. And trace the path to each character, it will be the code for that specific
character.

Final Huffman tree:

111
0 1

61 f
0 1

26 35
0 0 1
1

14 e a d
0 1

b c

The code words are:

Character Fixed length encoding


a 010
b 0000
c 0001
d 011
e 001
f 1

c) Total space needed for fixed length encoding= Σ (length of codes*frequency)

=3*(15+5+9+20+12+50) = 111*3=333.

Total space needed in fixed length coding = 333

Total space needed for variable length encoding= Σ (length of codes*frequency)

=15*3+5*4+9*4+20*3+12*3+50*1 = 45+20+36+60+36+50 = 247

Total space needed in variable length coding = 247

You might also like