You are on page 1of 6

Analysis of Algorithm

Assignment # 5

Submitted by
Name waqar ullah
Student ID: 13010
Course: BS (Computer Science)

Instructor
Dr. Abdus salam

Department of Computing
Abasyn University, Peshawar Campus
1) Write algorithm for each of the following problem.
i. Read in any three characters (a-z) and print them in alphabetical order.

Problem:
Read any three alphabets from (a-z) and print them in alphabetical order.
Input:
Enter any three alphabets from a-z.
Output:
Print the alphabets in Alphabetical Order.
Algorithm:
1) def isAlphabaticOrder(s):
2) n = len(s)
3) c = [s[i] for i in range(len(s))]
4) c.sort(reverse = False)
5) for i in range(n):
6) if (c[i] != s[i]):
7) return False
8) return True

ii. Reverse the elements of the array of n elements.


Problem: Reverse the elements of the array of n elements.
Input: Enter an array
Output: The output is to be reverse.
Algorithm:
a. Start
b. Declare an array or read from the user.
c. Array=[1,2,3,4,5]
d. Print(Reverse(array))
e. Exit

iii. Write an algorithm that finds the largest number in a list (an array) of n
numbers.

Problem: Write an algorithm that finds the largest number in a list (an array)
of n numbers.

Input: Enter the array.

Output: Largest should be printed in an array.

Algorithm:
a. Start
b. Input the list from user
c. Print(max(list))
d. Exit
iv. Write an algorithm that finds the m smallest numbers in a list of n
numbers.
Problem: Write an algorithm that finds the smallest number in a list (an array)
of n numbers.
Input: Enter the list.
Output: Largest should be printed in a list.
Algorithm:
a. Start
b. Input the list from user
c. Print(min(list))
d. Exit

v. Write an algorithm that prints out all the subsets of three elements of a
set of n elements.
Problem: Write an algorithm that prints out all the subsets of three elements of a set
of n elements.
Input: Input the three elements in the set.
Output: Print the subset of set.
Algorithm:
a. Start
b. Declare variable that contain three element in the set s = {1, 2, 3} c. Initialize the
variable.
d. Using import method import itertools
e. Now defining function
def findsubsets(s, n):
return list(itertools.combinations(s, n))
f. Calling the function
s = {1, 2, 3} n = 2
print(findsubsets(s, n))
2. Solve the following recurrence equations:

i. T(n) = T(n-3) + n2
Solution:
Now we know that
T(n) = T(n-3) + n2
Next It will become T(n-3)= T(n-6)+ 1n2
T(n-6)= T(n-9)+2n2
T(n-9)= T(n-12)+ 3n
Now at one time it will be equal to zero, So
T(n)= T(n-i)+ i*n2 :- i-n=0 i=n
T(n)=T(0)+nn2

So the order is Θ(n)

ii. T(n) = 3T(n/4) +


n2 Solution:
T(n) = 3T(n/22) + n2
Next time it will be T(n/4) = 34T(n/24)+n2
T(n/16)= 316T(n/216)+2n2
Now one time it will be equals to 1, So
T(n)=3iT(n/4i)+i*n2 :- n/4i=1 so n=2i now taking log so i=log n
T(n)=n +log n
So the order is Θ(log n)

iii. T(n) = 2T(n/2) +


n2 Solution:
T(n) = 2T(n/2) + n2
Now next time T(n/2)= 22T(n/22)+2n2
T(n/4)= 23T(n/23)+3n2
T(n/8) = 24T(n/24)+4n2
On time it will be like
T(n)= 2iT(n/2i)+i*n2 :- n/2i=1 so n=2i now taking log so i=log n
T(n)= n + log n
So the order is Θ(log n)

iv. T(n) = 2T(n-1) + n

Solution:
T(n) = 2T(n/2) + n2

Now next time T(n-1) = 21T(n-2) + n2 T(n-2)= 23T(n-3)+2 n


T(n-3)= 24T(n-4)+3 n
T(n-4) 25T(n-5)+4 n
T(n)= 2iT(n/2i)+i*n2
T(n)= n + n
T(n)= 2n
So the order is Θ(n)

v. T(n) = T(n/2) + 1
Solution:
T(n) = 2T(n/2) + n2
Now next time T(n/2)= T(n/22)+2
T(n/4)= T(n/23)+3
T(n/8)=T(n/24)+4
On time it will be like
T(n)= T(n/2i)+i
T(n)= log n
So the order is Θ(log n)

3. What is the value returned by the following functions,


when n = 5? i. function mystery(n)
1. r := 0;
2. for i := 1 to n - 1 do
3. for j := i + 1 to n do
4. for k := 1 to j do
5. r := r + 1
6. return(r)
Time Complexity= O((n^3)/3)
ii. function pesky(n)
1. r := 0;
2. for i := 1 to n do
3. for j := 1 to i do
4. for k := j to i + j do
5. r := r + 1
6. return(r)
Time Complexity= O((n^3)/3)
iii. function pestiferous(n)
1. r := 0;
2. for i := 1 to n do
3. for j := 1 to i do
4. for k := j to i + j do
5. for . := 1 to i + j - k do
6. r := r + 1
7. return(r)

Time Complexity = O(n4)


iv. function conundrum(n)
1. r := 0;
2. for i := 1 to n do
3. for j := i + 1 to n do
4. for k := i + j - 1 to n do
5. r := r + 1
6. return(r)
Time Complexity=O(n3)

You might also like