0% found this document useful (0 votes)
43 views14 pages

Algo Merged

The document contains five questions for a midterm exam on algorithms. The questions cover topics like analyzing recursive relations, applying coding algorithms like Hoffman coding and Rabin-Karp, and designing algorithms to solve problems like finding the maximum subarray sum and missing numbers in a sorted array.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views14 pages

Algo Merged

The document contains five questions for a midterm exam on algorithms. The questions cover topics like analyzing recursive relations, applying coding algorithms like Hoffman coding and Rabin-Karp, and designing algorithms to solve problems like finding the maximum subarray sum and missing numbers in a sorted array.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

November 16, 2021

EAST WEST UNIVERSITY


Department of Computer Science and Engineering
B.Sc. in Computer Science and Engineering Program
Mid 1 Examination, Fall 2021 Semester
Course: CSE246 Algorithm, Section-02
Instructor: Jesan Ahammed Ovi, Senior lecturer, CSE Department
Full Marks: 25 (15 will be considered for final grading)
Time: 1 Hour 20 Minutes (Writing) plus 10 Minutes (Uploading)

Note: There are FIVE questions, answer ALL of them. Course Outcome (CO) and Mark of each
question are mentioned at the right margin.

1. Consider the following recursive relation. Analysis the complexity of the relation [CO4, C4,
using a suitable technique. Marks: 5]

𝐶 ;𝑛 = 1
𝑇(𝑛) = { 𝑛
9 ∗ 𝑇( ) + 𝑛 + 𝐶 ;𝑛 > 1
3
Here C is constant and n is the input.
2. Mr. Jonathan is a senior software engineer in a renowned company. As a part of his [CO3, C4,
job responsibility he has to arrange several online meeting with different stockholders EP1,
via different platform. Unfortunately, he forget his zoom password before an Marks: 5]
important meeting. But he remember the number of digits N as well as the sum S of
all the digits of his password. He know that his password is the largest number of N
digits that can be made with given sum S. As he is busy doing his homework, help
him retrieving his password. Consider the following example for better
understanding.

Example 1:
Input:
N = 5, S = 12
Output:
93000
Explanation:
Sum of elements is 12. Largest possible
5 digit number is 93000 with sum 12

Example 2:
Input:
N = 3, S = 29
Output:
-1
Explanation:
There is no such three digit number
Whose sum is 29

Now design an algorithm using suitable design approach that takes N and S as input
and return the password if possible otherwise print -1.

Page 1 of 2
November 16, 2021

3. Consider the following data. Apply Quick sort algorithm to sort them in increasing [CO3, C3,
order. Consider last value as pivot. Show details of your calculation. Marks: 5]
7, 13, 2, 18, 5, 9

4. Consider the following code segment. Analysis the complexity of the code in terms [CO4, C3,
of big-O. Show details of your calculation. Marks: 5]

for(i = -10 ; i <= n/3 ; i++){


for(j = 1 ; j <= m ; j = j*2){
sum++ ;

}
}
5. Consider the following String S and Pattern P. Apply Rabin-Karp algorithm to find [CO2, C3,
the each occurrence of pattern P in string S. Marks: 5]

S = 0011001011000101
P = 1100

Use Horner’s rule for generating integer.

Page 2 of 2
March 9, 2022

EAST WEST UNIVERSITY


Department of Computer Science and Engineering
B.Sc. in Computer Science and Engineering Program
Mid 1 Examination, Spring 2022 Semester
Course: CSE246 Algorithm, Section-03
Instructor: Jesan Ahammed Ovi, Senior lecturer, CSE Department
Full Marks: 25 (15 will be considered for final grading)
Time: 1 Hour 20 Minutes (Writing) plus 10 Minutes (Uploading)

Note: There are FIVE questions, answer ALL of them. Course Outcome (CO) and Mark of each
question are mentioned at the right margin.

1. Consider the following recursive relation. Analyze the complexity of the following [CO4, C4,
recursive relation using a suitable technique. Marks: 5]

𝐶 ;𝑛 = 1
𝑇 (𝑛 ) = { 𝑛
3∗ 𝑇( )+ 𝑛 +𝐶 ;𝑛 > 1
3
Here C is constant and n is the input size.
2. Consider the following statement. Execute Hoffman algorithm to generate variable [CO3, C3,
length code for each character. Show details of your calculation. Marks: 5]
TOO COOL TO SLEEP

3. Mr. Thorin a famous researcher who visits different place and record the temperature [CO3, C4,
of that place. As he uses to visit different place of the world, temperature he recorded Marks: 5]
can be both positive and negative. He stored the value of the temperature in
consecutive way like {-1, 5, 20, -18, -5, 33, 24}. Now he wants know maximum
continuous subset that has maximum sum. See following example for better
understanding.

For example, if the given array is {-2, -5, 6, -2, -3, 1, 5, -6}, then the maximum
subarray sum is 7 that is made by {6, -2, -3, 1, 5}.

You are given the temperature of n places. Your task is to design an efficient
algorithm using a suitable design technique that find the continuous subset having
maximum sum. Worst case run time complexity of your algorithm can not be more
than 𝑛𝑙𝑜𝑔𝑛.
4. Consider the following String S and Pattern P. Use Rabin-Karp algorithm to find each [CO2, C3,
occurrence of pattern P in string S. Marks: 5]

S = 1011001001000101
P = 1001

Use Horner’s rule for generating integer.

Page 1 of 2
March 9, 2022

5. For solving a particular problem, two students come with their own solutions. Core [CO4, C4,
part of their solution is given bellow. Choose more efficient solution between them. Marks: 5]
Show details of your calculation.
Solution 1: Solution 2:
for(i = -10 ; i <= n/3 ; i++){ for(i = -1 ; i <= n/3 ; i++){
for(j = 1 ; j <= m ; j = j*2){ j=m;
sum++ ; while(j >= 1){
sum++ ;
} j = j-2 ;
} }
}

Page 2 of 2
November 17, 2021

EAST WEST UNIVERSITY


Department of Computer Science and Engineering
B.Sc. in Computer Science and Engineering Program
Mid 1 Examination, Fall 2021 Semester
Course: CSE246 Algorithm, Section-03
Instructor: Jesan Ahammed Ovi, Senior lecturer, CSE Department
Full Marks: 25 (15 will be considered for final grading)
Time: 1 Hour 20 Minutes (Writing) plus 10 Minutes (Uploading)

Note: There are FIVE questions, answer ALL of them. Course Outcome (CO) and Mark of each
question are mentioned at the right margin.

1. Consider the following sentence. Apply Hoffman algorithm to generate variable [CO3, C3,
length code for each character. Show details of your calculation. Marks: 5]

“TOO HOT TO RUN”

2. Consider the following recursive relation. Analysis the complexity of the relation [CO4, C4,
using a suitable technique. Marks: 5]

𝐶 ;𝑛 = 1
𝑇(𝑛) = { 𝑛
4 ∗ 𝑇 ( ) + 𝑛/2 + 𝐶 ; 𝑛 > 1
2
Here C is constant and n is the input.
3. Xander Budnick is a famous you tuber. Currently he is in a mission to visit several [CO3, C4,
tourist spots and stay distinct number of days in each spot. So while he is visiting a EP1,
particular spot, he is a problem to find out the number of days he should stay in that Marks: 5]
spot. You are given a sorted non-negative distinct integers, your task is to design an
algorithm that finds the smallest missing non-negative element on it. Your solution
should not takes more than logn instructions in worst case. Consider following
examples for better understanding.

Example:
Input: nums[] = [0, 1, 2, 6, 9, 11, 15]
Output: The smallest missing element is 3

Input: nums[] = [1, 2, 3, 4, 6, 9, 11, 15]


Output: The smallest missing element is 0

Input: nums[] = [0, 1, 2, 3, 4, 5, 6]


Output: The smallest missing element is 7

Page 1 of 2
November 17, 2021

4. Consider the following code segment. Analysis the complexity of the code in terms [CO4, C3,
of big-O. Show details of your calculation. Marks: 5]

for(i = 2 ; i <= n/2 ; i++){


j=1;
while(j <= m ){
sum++ ;
j *= 10 ;
}
}
5. Consider the following String S and Pattern P. Apply Rabin-Karp algorithm to find [CO2, C3,
the each occurrence of pattern P in string S. Marks: 5]

S = 0011011011000101
P = 1101

Use Horner’s rule for generating integer.

Page 2 of 2
December 14, 2021

EAST WEST UNIVERSITY


Department of Computer Science and Engineering
B.Sc. in Computer Science and Engineering Program
Mid II Examination, Fall 2021 Semester
Course: CSE246 Algorithm, Section-02
Instructor: Jesan Ahammed Ovi, Senior lecturer, CSE Department
Full Marks: 20 (20 will be considered for final grading)
Time: 1 Hour 20 Minutes (Writing) plus 10 Minutes (Uploading)

Note: There are FIVE questions, answer ALL of them. Course Outcome (CO) and Mark of each
question are mentioned at the right margin.

1. Answer the following questions with proper explanation. Provide suitable examples [CO1, C2,
if needed. Marks:
a. If the graph is undirected only tree edge and back edge will be found – do you 2+2]
agree? Why or why not? Explain.
b. Does BFS always ensure shortest path from source node to all others node?
Explain with proper reasons.
2. Consider the following graph. Apply DFS algorithm to find tree, back, cross and [CO1, C3,
forward edges from it. Show each significant steps of your calculation. Marks: 4]

3. A thief enters an electronics shop with a knapsack of capacity W. there are n numbers [CO3, C4,
of different products. Each product has its own value and weight and there are exactly Marks: 4]
2 copies of each product. Now design an efficient algorithm that finds those products
list having maximum value with the given knapsack capacity. Worst case run time
complexity of your algorithm will not be more than O(nW).
[Note: Product cannot be taken partially]

Page 1 of 2
December 14, 2021

4. Ali and Alisa are two sibling. They live happily with their parents. During Eid their [CO3, C4,
relatives use to visit them and bring gifts for them. Problem is that each relative brings EP1,
exactly 1 gift. That’s why their mother is in problem to share the gifts equally (based Marks: 4]
on price) between them. Because one gift cannot be shared or partially given. Your
task is to determine whether gifts can be divided into two equal share or not. See
following example for better understanding.

Example 1:
Input
Number of relatives 5
Price 17 3 5 5 10

Output
Yes possible
One sibling will receive {17 and 2}
Other sibling will receive {5, 5 and 10}

Example 2
Input
Number of relatives 7
Price 2 7 3 3 1 3 2
Output
Not possible

You are given number of relatives and the price of each gift they have taken. Design
an efficient algorithm that determine whether it possible to equally divide the gifts or
not. If yes find the set of each sibling otherwise just print not possible.
5. Consider the sequences S1: AGCTGCC and S2: GGCTCACC. Find LCS between [CO3, C3,
them using top down approach. Marks: 4]

Page 2 of 2
December 15, 2021

EAST WEST UNIVERSITY


Department of Computer Science and Engineering
B.Sc. in Computer Science and Engineering Program
Mid II Examination, Fall 2021 Semester
Course: CSE246 Algorithm, Section-03
Instructor: Jesan Ahammed Ovi, Senior lecturer, CSE Department
Full Marks: 25 (15 will be considered for final grading)
Time: 1 Hour 20 Minutes (Writing) plus 10 Minutes (Uploading)

Note: There are FIVE questions, answer ALL of them. Course Outcome (CO) and Mark of each
question are mentioned at the right margin.

1. Consider the following graph and answer the question bellow with proper [CO1, C2,
explanation. Marks:
2+2]

a. If BFS is applied on the above graph from node ‘a’, does it possible to have a
path a->h->i->c->d? Why or why not?
b. If DFS is applied is above graph, does it always ensure shortest path from
source node to all other nodes? Explain.

2. Apply DFS algorithm in the following graph to topologically sort the node of the [CO1, C3,
graph. Show details of your calculation. Marks: 4]

Page 1 of 2
December 15, 2021

3. Ali and Alisa are two sibling. They live happily with their parents. During Eid their [CO3, C4,
relatives use to visit them and bring gifts for them. Problem is that each relative brings EP1,
exactly 1 gift. That’s why their mother is in problem to divide the gifts in two portion Marks: 4]
in such a way so that there will be a minimum price gap between them. Because one
gift cannot be shared or partially given. Your task is to determine two such portion of
the gifts so that price gap will be minimum.

Example
Input
Number of guest 5
Price 4 6 2 3 10

Output
1st portion {10, 2}
2nd portion {4, 6, 3}

You are given number of relatives and the price of each gift they have taken. Design
an efficient algorithm that determine the two set of gifts having minimum price gap.

4. Consider the sequences S1: ACCTGCC and S2: GCCTCACC. Find LCS between [CO3, C3,
them using top down approach. Marks: 4]

5. A thief enters an electronics shop with a knapsack of capacity W. there are n numbers [CO3, C4,
of different products. Each product has its own value and weight and there are exactly Marks: 4]
2 copies of each product. Now design an efficient algorithm that finds those products
list having maximum value with the given knapsack capacity. Worst case run time
complexity of your algorithm will not be more than O(nW).
[Note: Product cannot be taken partially]

Page 2 of 2
January 18, 2022

EAST WEST UNIVERSITY


Department of Computer Science and Engineering
B.Sc. in Computer Science and Engineering Program
Final Examination, Fall 2021 Semester
Course: CSE246 Algorithm, Section-02
Instructor: Jesan Ahammed Ovi, Senior lecturer, CSE Department
Full Marks: 20 (20 will be considered for final grading)
Time: 1 Hour 20 Minutes (Writing) plus 10 Minutes (Uploading)

Note: There are FIVE questions, answer ALL of them. Course Outcome (CO) and Mark of each
question are mentioned at the right margin.

1. Consider the following weighted graph. Apply a suitable shortest path algorithm to [CO1, C3,
find shortest path from source node to all other nodes. Also, justify your choice. Marks: 4]
Show significant steps of your calculation. Consider node ‘4’ as source node.

2. A group of engineers is working in a project to connect each house using a gas line [CO1, C3,
exactly once. Map of the city each is giving bellow. Here, node indicates the house Marks: 4]
and edge represent the road between the houses. Weight of each road is the length of
that road. Engineers have to put the gas line under the road. Your task is to find the
best way to connect each house exactly once so that minimum gas pipe is needed
from the bellow graph. Show details of your calculation.

3. Briefly explain the following questions. Provide suitable example if required. [CO1, C2,
a. Dijkstra can be used to find single source shortest path from a graph having Marks:
negative weighted edges, if we add a positive value to the weight of all edges 2+2]
so that every edges become positive weighted – do you agree with it? Why
or why not?
b. If there exists multiple minimum spanning tree (MST) of a particular graph,
does it necessary to calculate 2nd best MST from it? Explain with a suitable
real life example.

Page 1 of 2
January 18, 2022

4. Consider the following network which represents the relation between the different [CO1, C3,
warehouses of a company where each node represents the warehouse and the edge Marks: 4]
reflects the connection between them. Value in the edge reflects the maximum
capacity that can be carried at a time.

Apply Ford-Fulkerson method to find the maximum flow from source node s to sink
node t from above network. Show each significant steps of your calculation. Note
that warehouse 3 and 4 has storing capacity 2 and 4 respectively. Choose the path
that has fewest edges while considering augmenting path. Show details of your
calculation.

5. Suppose in a city there are n number of houses. Each house is connected to each [CO3, C4,
other with a road (2 way road) having length m > 0. In P (P < n) number of houses EP1,
there lives a mouse (one mouse in each house) and one house there is a chees. Every Marks: 4]
mouse is looking for that chees. You are given number of house n and the distance
between houses m as input. You have to find which mouse will find the chees first.
Input will be given as u v w format where u and v is the house number and w is the
distance between them. You will be given the house number where the chees is and
also the house number where are the mice are. Now write an algorithm to do the job.
Have a look in sample input output for better understanding.

Sample input Sample output

Number of house n: 6 Mouse in house 0 will find the chees


Number of road r: 8 first.
047
2 5 12
013
326
0 3 10
217
0 5 20
453
Mice house: 0 1 4
Chess: 5

Page 2 of 2
January 18, 2022

EAST WEST UNIVERSITY


Department of Computer Science and Engineering
B.Sc. in Computer Science and Engineering Program
Final Examination, Fall 2021 Semester
Course: CSE246 Algorithm, Section-03
Instructor: Jesan Ahammed Ovi, Senior lecturer, CSE Department
Full Marks: 20 (20 will be considered for final grading)
Time: 1 Hour 20 Minutes (Writing) plus 10 Minutes (Uploading)

Note: There are FIVE questions, answer ALL of them. Course Outcome (CO) and Mark of each
question are mentioned at the right margin.

1. Explain the following questions with suitable examples. [CO1, C2,


a. Shortest path can have negative cycle as it reduces cost but no positive cycle Marks:
– do you agree? Why or why not? 2+2]
b. Does it possible total outgoing flow of source node and total incoming flow
of sink node is not equal of a network in any case? Explain.

2. Consider the following graph. Apply Floyd Warshall algorithm to calculate D3 from [CO1, C3,
the graph. Marks: 4]

Explain significant steps of your simulation.

3. A group of engineers is working in a project to connect each house using a gas line [CO1, C3,
exactly once. Map of the city each is giving bellow. Here, node indicates the house Marks: 4]
and edge represent the road between the houses. Weight of each road is the length of
that road. Engineers have to put the gas line under the road. Your task is to find the
best way to connect each house exactly once so that minimum gas pipe is needed
from the bellow graph. Show details of your calculation.

Page 1 of 2
January 18, 2022

4. Consider the following network which represents the relation between the different [CO1, C3,
warehouses of a company where each node represents the warehouse and the edge Marks: 4]
reflects the connection between them. Value in the edge reflects the maximum
capacity that can be carried at a time.

Apply Ford-Fulkerson method to find the maximum flow from source node s to sink
node t from above network. Show each significant steps of your calculation. Note
that warehouse D and E has storing capacity 2 and 1 respectively. Choose the path
that has fewest edges while considering augmenting path.
5. Suppose there are n cities connected with each other via particular road. Each road [CO1, C4,
has a length w. Exactly in one city m there is a tiger. Mayor of other cities want to EP1,
capture that tiger. Mayor who comes first in m city will capture the tiger. Your task Marks: 4]
is to design an algorithm that finds which city mayor will capture the tiger first.
You are given number of city n, number of road r, length of each road w and the
location of the city m that holds the tiger as input. Input will be given in following
format:

n
r
uvw

Here, u v w denotes the road between city u and v, length of that road is w. See
sample input and output for better understanding.
Sample input Sample output

n: 4 Mayor in city 1 will capture


r: 4 first.
017
235
0 3 10
211
m: 2

Page 2 of 2

You might also like