Exam questions Flashcards
Analysis of binary search algorithm
function binarySearch(š[1. . š], š„) š ā1 j āš while š < š do š ā ā(š+š)/2ā case š„ < š[š]: š ā š ā 1 š„ = š [š]: š ā š; š ā š š„ > š[š]: š ā š + 1 return š
find a function of the variable whose value decreases each time through the loop.
what decreases is the number of elements to be considered:
initially š = š, with š = š and š = 1
let š = š ā š + 1
when does the loop terminate?
when š ā¤ 1
Three possibilities in the loop (cases):
- Let š and šā be the values of š ā š + 1 before and after the iteration under consideration
- Let š,š and šā²,šā² be the values before and after the iteration
ā¦.
see pg 169 of VLs
BONUS QUESTION
ECCENTRICITY
The eccentricity of a node is the length of the shortest path to the most distant node in the graph.
The diameter of a graph is the largest eccentricity of all nodes.
The radius of a graph is the smallest eccentricity of all nodes.
Describe an algorithm (pseudo code) to find the diameter and the radius of a graph. What is the complexity of the solution?
(Zoran 2020, question 6)
Proof by induction:
Prove that for any positive integer n, n ā„ 0, it holds that:
0 ā 0! + 1 ā 1! + ā¦ + n ā n! = (n + 1)! ā 1
(Zoran 2020)
QUESTION 2 or 3
Assume that we can find the product of two n-digit-numbers using some number of multiplications of nā3-digit numbers (plus some additions, subtractions and shifts).
What is the largest number of nā3-digit number multiplications that leads to an asymptotically faster algorithm than the nā2 divide-and-conquer algorithm discussed in the lecture?
Provide and solve the recurrence equations.
(Zoran 2020)
QUESTION 1
What is the running time of the following pseudo code expressed in the big O-notation?
1 for i ā 1 to n ā 1 do O(n-1) ā O(n)
2 j ā i + 1 O(1)
3 while j ā¤ n do O(n+1) ā O(n)
4 for k ā 2j to 2n do
5 print i
6 j ā j + 1
Put the running time in order together with: O(nlogn), O(n^3), O(e^n), O(n^n), O(n^logn).
(Zoran 2020)
QUESTION 2 or 3
Describe the differences in solution strategies between the Knapsack variant in which a fraction of an object is allowed to be packed in comparison to the variant in which only entire objects of integer weight are allowed to be packed. Be prepared to derive the computational complexity of the two algorithms.
(Zoran 2020)
QUESTION 4
GRAPHS
Provide an algorithm to find the longest path in a directed acyclic graph.
What is the computational complexity of the algorithm?
Explain the relation of this problem to the alignment of sequences (global and local).
(Zoran 2020)
Which grows faster:
O(n) or O(nlog(n))?
O(nlog(n))
List in order of best time complexity:
O(__)
- n
- nlogn
- 2n
- 1
- n!
- n2
- logn
- 1
- logn
- n
- nlogn
- n2
- 2n
- n!
QUESTION 1
Which is bound above by which? Prove.
- nlogn
- nn
(2022)
QUESTION 1
Which is bound above by which? Prove.
- nlogn
- n3
(2022)
QUESTION 1
Summation problem and analysis of time complexity.
For i from 1 to n^3:
for j from i+1 to n^3
k <- n-3
while k>n+4
print(i)
What is the complexity? What is the output for k?
QUESTION 1
For i <- i to n^2 do
For j <- i + 1 to n do
Print j
For k <- n to n+1 do
Print i
- What is the output?
- Write the summations and solve one/expand it.
QUESTION 2/3
Describe the differences in solution strategies between the Knapsack variant in which a fraction of an object is allowed to be packed in comparison to the variant in which only entire objects of integer weight are allowed to be packed.
Be prepared to derive the computational complexity of the two algorithms.
(2020 Zoran)
QUESTION 1