Exam questions Flashcards

1
Q

Analysis of binary search algorithm

function binarySearch(š‘‡[1. . š‘›], š‘„)
    š‘– ā†1
    j ā†š‘›
    while š‘– < š‘— do
        š‘˜ ā† āŒŠ(š‘–+š‘—)/2āŒ‹
        case š‘„ < š‘‡[š‘˜]: š‘— ā† š‘˜ āˆ’ 1
                 š‘„ = š‘‡ [š‘˜]: š‘– ā† š‘˜; š‘— ā† š‘˜
                 š‘„ > š‘‡[š‘˜]: š‘– ā† š‘˜ + 1
    return š‘–
A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

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)

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

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)

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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)

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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)

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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)

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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)

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Which grows faster:

O(n) or O(nlog(n))?

A

O(nlog(n))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

List in order of best time complexity:

O(__)

  • n
  • nlogn
  • 2n
  • 1
  • n!
  • n2
  • logn
A
  • 1
  • logn
  • n
  • nlogn
  • n2
  • 2n
  • n!
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

QUESTION 1

Which is bound above by which? Prove.

  • nlogn
  • nn

(2022)

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

QUESTION 1

Which is bound above by which? Prove.

  • nlogn
  • n3

(2022)

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

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?

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

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.
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

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)

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

QUESTION 1

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q
A