Uploaded by Imran Zafar

DAA ASIGNMENT 2

advertisement
Design and Analysis of Algorithms
CSC-321
Assignment # 02
GROUP MEMEBERS
Muhammad Ahmed Tariq (01-134202-121)
Imran Zafar (01-134202-027)
Department of Computer Science
BAHRIA UNIVERSITY, ISLAMABAD
BAHRIA UNIVERSITY
Islamabad Campus
Department of Computer Science
Design and Analysis of Algorithms
Assignment #2
Total Marks: 50
Q. 1
BSCS-5 A & B, Fall 2022
Due Date:7th Nov, 2022
(20 Marks)
Which of the following equalities are correct? What values for n0, c1, and c2 will work? Show
complete working.
a) n! = O(nn) b) 33 n3 + 4 n2 = Ω(n2)
c) n2 log n = (n2)
d) n = (log2 n)
Q. 2
(15 Marks)
Consider the following recursive algorithm:
ALGORITHM Mystery(D)
Input: An array D[0..n − 1] of n
numbers if n = 1 return D[0]
else temp ← Mystery (D[0..n −
2]) if temp ≤ D[n − 1]
return temp else return
D[n − 1]
a) What does this algorithm compute?
b) Formulate a recurrence relation for the algorithm’s key operation count.
c) Solve the above recurrence and find the complexity class of this algorithm
Q. 3
(15 Marks)
Find the optimal solution of following problem using Exhaustive search. Submit complete working.
Suppose you have an assignment with different parts labeled A through G. Each part has a “value”
(in points) and a “size” (time in hours to complete).
For example, the values and times for the assignment are as given below:
a) If you have a total of 15 hours to do the assignment, which parts should you do?
b) What is the best total value (in points) possible?
Question # 01
Question # 02
a) The Given algorithm will compute the smallest value in the given array of size “n” with the help
of the concept of the recursion . After every Recurrence array size will be reduced by “1” and
until the size of the array reaches “1” ( n=1 ). After every recurrence the returned value will
store in the temp for the further comparison.
b) The recurrence relation of the following algorithm is
T(n) = T(n-1)+1
For n>1 , T(1) = 0
T(n-1) because it is decreasing the size of the array by “1” after every recurrence
c)
Question # 03
#include <iostream>
using namespace std;
void allPossibleSubset(int arr[], int n , int Time[])
{
int count = pow(2, n);
int time_count = 0;
int value_count = 0;
int space_count = 0;
cout <<
"============================================================================================
=============================" << endl;
cout << "SUBSETS\t\t\t\t\t\tTOTAL TIME\t\t\t\t\tTOTAL VALUE"<< endl;
cout <<
"============================================================================================
=============================" << endl;
for (int i = 0; i < count; i++) {
time_count = 0;
value_count = 0;
space_count = 0;
for (int j = 0; j < n; j++) {
if ((i & (1 << j)) != 0) {
space_count++;
value_count += arr[j];
time_count += Time[j];
cout << arr[j] << " ";
}
}
if(time_count == 15)
cout << "\t\t\t\t\t\t" << time_count << "\t\t\t\t\t\t" << value_count;
else
cout << " " << "\t\t\t\t\t" << "NOT FEASIBLE" << "\t\t\t\t\t" << value_count;
cout << "\n";
}
}
int main()
{
int arr[7] = {7 , 9 , 5 , 12 , 14 , 6 , 12};
int Time[7] = { 3 , 4 , 2 , 6 , 7 , 3 , 5 };
allPossibleSubset(arr, 7 , Time);
return 0;
}
Download