0% found this document useful (0 votes)
17 views9 pages

Final Exam

The document is a midterm exam for the CS 2315 Algorithm Fundamentals course, consisting of 5 questions worth a total of 30 marks. It includes multiple-choice questions, coding problems, and theoretical questions related to algorithms and complexity classes. The exam is administered by Dr. Ahlem Walha and is scheduled for 28/05/2024.

Uploaded by

ahlem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views9 pages

Final Exam

The document is a midterm exam for the CS 2315 Algorithm Fundamentals course, consisting of 5 questions worth a total of 30 marks. It includes multiple-choice questions, coding problems, and theoretical questions related to algorithms and complexity classes. The exam is administered by Dr. Ahlem Walha and is scheduled for 28/05/2024.

Uploaded by

ahlem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Midterm Exam– 3rd term (1445) Time: 2 hours

Student Name:_________________ Student Number:___________

Notes:
 Attempt ALL questions.
 Show all details of your answers.
 If you need to make any reasonable assumptions, write them down as part of your
answers.

Make Sure the Exam Contains 5 Questions in 8 pages.

CS 2315 Algorithm
Fundamentals
Question # Score Total
Q1 10
Q2 3
Q3 2
Q4 10
Q5 5
Total points: 30 Marks

Instructor: Dr. Ahlem Walha

Date: 28/05/2024
Q1. [10 Marks] Choose the correct alternative:

Dr: Ahlem Walha 1


1. String in Java is a? 5) Suffix(S, i) = Substring(S, i-1,
a) class length(n))
b) object a) true
c) variable b) false
d) character array 6) In which of the following cases will the
2. Which of these method of String class edit distance between two strings be zero?
is used to obtain character at specified a) When one string is a substring of
index? another
a) char() b) When the lengths of the two
b) Charat() strings are equal
c) charat() c) When the two strings are equal
d) charAt() d) The edit distance can never be
3. What will be the output of the following zero
Java program? 7) Suppose each edit (insert, delete,
replace) has a cost of one. Then,
the maximum edit distance cost
between the two strings is equal to
the length of the larger string.
a) True
b) False
8) Consider the strings “monday” and
“tuesday”. What is the edit distance
a) I between the two strings?
b) L a) 3
c) K b) 4
d) E c) 5
4) What is the time complexity of prefix d) 6
algorithm ? 9) Consider the two strings “”(empty
a) O (log n!) string) and “abcd”. What is the edit
b) O (n) distance between the two strings?
c) O (n2) a) 0
d) O (n log n) b) 4
c) 2
d) 3
10) Consider the following implementation of the edit distance problem:

#include<stdio.h>
#include<string.h>
int get_min(int a, int b)
{
if(a < b)
return a;

Dr: Ahlem Walha 2


return b;
}
int edit_distance(char *s1, char *s2)
{
int len1,len2,i,j,min;
len1 = strlen(s1);
len2 = strlen(s2);
int arr[len1 + 1][len2 + 1];
for(i = 0;i <= len1; i++)
arr[i][0] = i;
for(i = 0; i <= len2; i++)
arr[0][i] = i;
for(i = 1; i <= len1; i++)
{
for(j = 1; j <= len2; j++)
{ min = get_min(arr[i-1][j],arr[i][j-1]) + 1;
if(s1[i - 1] == s2[j - 1])
{
if(arr[i-1][j-1] < min)
min = arr[i-1][j-1];
}
else
{
if(arr[i-1][j-1] + 1 < min)
min = arr[i-1][j-1] + 1;
}
_____________;
}
}
return arr[len1][len2];
}
int main()
{
char s1[] = "abcd", s2[] = "defg";
int ans = edit_distance(s1, s2);
printf("%d",ans);
return 0;
}
Which of the following lines should be added to complete the above code?

Dr: Ahlem Walha 3


a) arr[i-1][j] = min
b) arr[i][j-1] = min
c) arr[i-1][j-1] = min
d) arr[i][j] = min
11) What is the output of the following code?
#include<stdio.h>
#include<string.h>
int get_min(int a, int b)
{
if(a < b)
return a;
return b;
}
int edit_distance(char *s1, char *s2)
{
int len1,len2,i,j,min;
len1 = strlen(s1);
len2 = strlen(s2);
int arr[len1 + 1][len2 + 1];
for(i = 0;i <= len1; i++)
arr[i][0] = i;
for(i = 0; i <= len2; i++)
arr[0][i] = i;
for(i = 1; i <= len1; i++)
{
for(j = 1; j <= len2; j++)
{
min = get_min(arr[i-1][j],arr[i][j-1]) + 1;
if(s1[i - 1] == s2[j - 1])
{
if(arr[i-1][j-1] < min)
min = arr[i-1][j-1];
}
else
{
if(arr[i-1][j-1] + 1 < min)
min = arr[i-1][j-1] + 1;
}
arr[i][j] = min;

Dr: Ahlem Walha 4


}
}
return arr[len1][len2];
}
int main()
{
char s1[] = "abcd", s2[] = "defg";
int ans = edit_distance(s1, s2);
printf("%d",ans);
return 0;
}
a) 1
b) 2
c) 3
d) 4
12) What does NP stands for in b) The P vs NP problem is focused on
complexity classes theory? how quickly problems can be solved,
a) Non polynomial ignoring the verification process.
b) Non-deterministic polynomial c) The P vs NP problem is to
c) All of the mentioned determine whether every problem
d) None of the mentioned whose solution can be quickly verified
13) Travelling sales man problem belongs (NP) can also be solved quickly (P).
to which of the class? d)The P vs NP problem refers to the
a) P task of finding a universal algorithm
b) NP that can solve and verify all problems
c) Linear instantly.
d) None of the mentioned 16) Edit distance is a P problem
14) If a problem X is in NP and a a) True
polynomial time algorithm for X could b) false
also be used to solve problem Y in 17) TRUE or FALSE: A constant time
polynomial time, then Y is also in NP. algorithm is in P.
a) true a) True
b) false b) false
15) What is the P vs NP problem in 18) TRUE or FALSE: A constant time
computer science? algorithm is in P.
a) The P vs NP problem is about a) True
finding out if all problems can be b) false
solved and verified within non-
polynomial time. 19) what is the time complexity of the
Longest increasing subsequence in Naïve
case:

Dr: Ahlem Walha 5


a) O (log n!) a) O (log n!)
b) O (n) b) O (n)
c) O (n · 2n) c) O (n · 2n)
d) O (n log n) d) O (n log n)
20) what is the time complexity of the
Shortest path Problem in the best case:

Q2 [2 Marks] Given tow strings: str1="apiskal" and str2="vishal"


Complete:

Minimum number of edit operations=…………………………………………………


deleting ‘………..’, replacing ‘………..’ with ‘………..’ and
replacing ‘………..’ with ‘………..’

Q3[2 Marks] What is a suffix function?


………………………………………………………………………………………………………
………………………………………………………………………………………………………
………………………………………………………………………………………………………
……………………………………………………………………………………………………

calulate:
σ(abcdab, ababcd) = ……………
………………………………………………………………………………………………………
………………………………………………………………………………………………………

σ(daabac, abacac) =…………….


………………………………………………………………………………………………………
………………………………………………………………………………………………………

Q4[10 Marks] DIET PROBLEM

Dr: Ahlem Walha 6


Vitamins B1 and B2 are found in two foods F1 and F2. 1 unit of F1 contains 3 units of B1 and 5
units of B2. 1 unit of F2 contains 5 units of B1 and 7 units of B2 respectively. Minimum daily
prescribed consumption of B1 & B2 is 30 and 40 units respectively. Cost per unit of F1 & F2 is
Rs. 6 & Rs. 3 respectively.

Formulate as LPP.
a) Complete the following table [1 mark]

Vitamins Foods Minimum


F1 F2 Comsumption
B1
B2

b) Identify decision Variables [1 mark]:


 ………………………………………………………………….
 ………………………………………………………………….
c) Give the Objective function [1 mark]:
…………………………………………………………………
d) Give resource constraints [1 mark]:
…………………………………………………………………
…………………………………………………………………
e) Give Non-Negativity constraint [1 mark]
…………………………………………………………………
f) Give a faisible solution [1 mark]
……………………………………………………………………...
……………………………………………………………………...
……………………………………………………………………...
……………………………………………………………………...
g) Give An infeasible solution [1 mark]
……………………………………………………………………...
……………………………………………………………………...
……………………………………………………………………...
……………………………………………………………………...
……………………………………………………………………...
h) Plot the graphical solution [2 mark]

Dr: Ahlem Walha 7


g) What is the optimal solution [1 mark]?
…………………………………………………………………………….
…………………………………………………………………………….
…………………………………………………………………………….
…………………………………………………………………………….

Q5[3 Marks]Give 3 P Problems


 …………………………………………………….

Dr: Ahlem Walha 8


 ……………………………………………………
 ……………………………………………………..
Q6[2 Marks]Give 2 NP Problems
 …………………………………………………….
 ……………………………………………………

Dr: Ahlem Walha 9

You might also like