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

Final Exam

Uploaded by

Tsedi Teka
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)
23 views9 pages

Final Exam

Uploaded by

Tsedi Teka
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/ 9

Addis Ababa University

Department of Computer Science


Analysis of Algorithms
COSC 4111 – Final Exam

Name: ___________________________________ ID No: / / .

1. Analyzing algorithms essentially refers to calculating the total amount of resource


consumed by the algorithm during its execution. For most sorting algorithms this time
depends on the size of the input (n) and the status of the input. With these in mind, for
bubble sort algorithm provided here

procedure bubbleSort(A) Time Number of Times


for i = 0 to n do
swapped = false
for j = 0 to n do
if A[j] < A[j+1]
swap(A[j],A[j+1]);
swapped = true;
end if
end for
if (!swapped)
Break
end if
end for
end procedure

I. Calculate the running time (2 point)

1
II. Calculate the best case running time Tb(n) (2 Point)

III. Calculate the worst case running time Tw(n) (2 Point)

2. Proving correctness of algorithms through mathematical induction requires showing


three things to be correct (i.e., the Initiation, Maintenance, and Termination). Using
mathematical induction show that the bubble sort algorithm given here is correct. (4
points)

Loop Invariant:

Initiation:

Maintenance:

Termination: ,

2
3. For each of the graphs below in a pictorial representation, provide the list of vertices V
and edges E, perform BFS and DFS starting from node A in each graph (2.5 points per
graph).

Graph 1

I. List the vertices and edges of the graph

V=
E=

II. BFS

A ® ® ® ® ® .
III. DFS

A ® ® ® ® ® .

Graph 2

I. List the vertices and edges of the graph

V=
E=

3
II. BFS

A ® ® ® ® ® ® .
III. DFS

A ® ® ® ® ® ® .

Graph 3: Note it is a directed graph

I. List the vertices and edges of the graph

V=
E=

II. BFS

A ® ® ® ® ® ® .
III. DFS

A ® ® ® ® ® ® .

4. For the directed Graph 3 in question 3. Find the strongly connected components using
DFS. First label the graph with the discovery and finish time
List the vertices and edges of the transpose graph G’

V’ =
E’ =

List of vertices in descending finish time of DFS

4
What are the connected components C1, C2, C3, ….

C1 => V1 =
E1 =

C2 => V2 =
E2 =

C3 => V3 =
E3 =

5. Guess an asymptotic bound Θ for running time T(n) = T(n/4) + n3 and use substitution
method proof your guess (5 points)

Guess:

Proof for lower bound Proof for upper bound

5
6. Use master method to fill in the following table (1 point each)

Recurrence
A b f(n) log ba Case Θ

𝒏
𝑻(𝒏) = 𝟒𝑻 ( * + √𝒏
𝟐

𝒏
𝑻(𝒏) = 𝟒𝑻 ( * + 𝒏𝟑 + 𝒏
𝟒

𝒏 𝒏
𝑻(𝒏) = 𝟑𝑻 ( * + 𝟑 + 𝟏
𝟐 𝟒

𝒏
𝑻(𝒏) = 𝟑𝑻 ( * + 𝒔𝒊𝒏 𝒏
𝟐

𝒏
𝑻(𝒏) = 𝟒𝑻 ( * + 𝐧 𝐥𝐨𝐠 𝒏
𝟐

7. Fractional Knapsack Problem (5 Points)


Given weights and values of n items, we need put these items in a knapsack of capacity W to get
the maximum total value in the knapsack. In the 0-1 Knapsack problem, we are not allowed to
break items. We either take the whole item or don’t take it. We have discussed a dynamic
programming solution for the 0-1 Knapsack problem in class.
Example
Input:
Items as (value, weight) pairs
arr[] = {{60, 10}, {100, 20}, {120, 30}}
Knapsack Capacity, W = 50;
Output:
Maximum possible value = 220 by taking items of weight 20 and 30 kg
In Fractional Knapsack, we can break items for maximizing the total value of knapsack. This
problem in which we can break item also called fractional knapsack problem. This problem could
be solved using greedy approach with a running time of Θ(𝑛 log 𝑛). Write the greedy algorithm
to solve the Fractional Knapsack problem.

6
8. Ugly Numbers (5 Points)
Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence 1, 2, 3, 4, 5, 6,
8, 9, 10, 12, 15, … shows the first 11 ugly numbers. By convention, 1 is included.
Given a number n, the task is to find nth Ugly number.
Example
Input: n = 7 Input: n = 15
Output: 8 Output: 24

Input: n = 10 Input: n = 150


Output: 12 Output: 5832

Write a dynamic programming algorithm to find the nth ugly number

7
9. Largest Sum Contiguous Subarray (5 Points)
Write a dynamic programming algorithm to find the sum of contiguous subarray within a
one-dimensional array of numbers which has the largest sum.

8
10. Coin Change (5 Points)
Given a value N, if we want to make change for N cents, and we have infinite supply of
each of S = {S1, S2, ..., Sm} valued coins, how many ways can we make the change? The
order of coins doesn’t matter.
For example, for N = 4 and S = {1,2,3}, there are four solutions: {1,1,1,1},
{1,1,2},{2,2},{1,3}. So, output should be 4. For N = 10 and S = {2, 5, 3, 6}, there are five
solutions: {2,2,2,2,2}, {2,2,3,3}, {2,2,6}, {2,3,5} and {5,5}. So, the output should be 5.
Write a dynamic programming algorithm for the coin changing problem.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy