Exercise Sheets
Exercise Sheets
The solutions for this sheet do not have to be submitted. The sheet will be solved in the first exercise
session on 26.09.2022.
Exercises that are marked by ∗ are challenge exercises.
n(n + 1)
1 + 2 + ··· + n = .
2
b) (This subtask is from August 2019 exam). Let T : N → R be a function that satisfies the
following two conditions:
T (n) ≥ 6n2 − 2n
Asymptotic Growth
When we estimate the number of elementary operations executed by algorithms, it is often useful to
ignore smaller order terms, and instead focus on the asymptotic growth defined below. We denote by
R+ the set of all (strictly) positive real numbers and by R+
0 the set of nonnegative real numbers.
Definition 1. Let f, g : N → R+ be two functions. We say that f grows asymptotically faster than g if
lim fg(n)
(n) = 0.
n→∞
g(n)
This definition is also valid for functions defined on R+ instead of N. In general, lim is the same
n→∞ f (n)
g(x)
as lim if the second limit exists.
x→∞ f (x)
e)∗ f (n) := log2 n grows asymptotically faster than g(n) := log2 log2 n.
√
f)∗ f (n) := 2 log2 n grows asymptotically faster than g(n) := log100
2 n.
√
g)∗ f (n) := n0.01 grows asymptotically faster than g(n) := 2 log2 n .
It is guaranteed that all functions in this exercise take values in R+ (you don’t have to prove it).
a) f (n) := 5n3 + 40n2 + 100
1
b) f (n) := 5n + ln n + 2n3 + n
c) f (n) := n ln n − 2n + 3n2
√
d) f (n) := 23n + 4n log5 n6 + 78 n − 9
2
√ p
e) f (n) := log2 n5 + log2 n5
3
Eidgenössische Ecole polytechnique fédérale de Zurich
Technische Hochschule Politecnico federale di Zurigo
Zürich Federal Institute of Technology at Zurich
The solutions for this sheet are submitted at the beginning of the exercise class on 3 October 2022.
Exercises that are marked by ∗ are “challenge exercises”. They do not count towards bonus points.
You can use results from previous parts without solving those parts.
Hint: Write out the first few terms. How fast does the sequence grow?
n(n + 1)(2n + 1)
12 + 22 + · · · + n2 = .
6
Hint: Consider the second half of the sum, i.e., ni=d n e ik . How many terms are there in this sum?
P
2
How small can they be?
Together, these two inequalities show that C1 · nk+1 ≤ ni=1 ik ≤ C2 · nk+1 , where C1 = 2k+1 1
and
P
Pn k
C2 = 1 are two constants independent of n. Hence, when n is large, i=1 i behaves “almost like
nk+1 ” up to a constant factor.
(b)* Replace 3n + 1 by 3n on the right side, and try to prove the new inequality by induction. This
inequality is even weaker, hence it must be true. However, the induction proof fails. Try to explain
to yourself how is this possible?
2
Eidgenössische Ecole polytechnique fédérale de Zurich
Technische Hochschule Politecnico federale di Zurigo
Zürich Federal Institute of Technology at Zurich
The solutions for this sheet are submitted at the beginning of the exercise class on 10 October 2022.
Exercises that are marked by ∗ are “challenge exercises”. They do not count towards bonus points.
You can use results from previous parts without solving those parts.
2n > n2 .
(b) Let x be a real number. Prove via mathematical induction that for every positive integer n, we have
n
X n i
(1 + x)n = x ,
i
i=0
where
n n!
= .
i i!(n − i)!
We use a standard convention 0! = 1, so n0 = nn = 1 for every positive integer n.
Hint: Y ou can use the following fact without justification: for every 1 ≤ i ≤ n,
n n n+1
+ = .
i i−1 i
Asymptotic Notation
When we estimate the number of elementary operations executed by algorithms, it is often useful
to ignore constant factors and instead use the following kind of asymptotic notation, also called O-
Notation. We denote by R+ the set of all (strictly) positive real numbers and by N the set of all (strictly)
positive integers.
The following theorem can also be helpful when working with O-notation.
Theorem 2. Let f, g, h : N → R+ . If f ≤ O(h) and g ≤ O(h), then
1. For every constant c ≥ 0, c · f ≤ O(h).
2. f + g ≤ O(h).
Notice that for all real numbers a, b > 1, loga n = loga b · logb n (where loga b is a positive constant).
Hence loga n ≤ O(logb n). So you don’t have to write bases of logarithms in asymptotic notation, that
is, you can just write O(log n).
(b) Find f and g as in Theorem 1 such that f = O(g), but the limit limn→∞ fg(n)
(n)
does not exist. This
proves that the first point of Theorem 1 provides a necessary, but not a sufficient condition for
f = O(g).
2
Exercise 2.4 Asymptotic growth of ln(n!).
Recall that the factorial of a positive integer n is defined as n! = 1 × 2 × · · · × (n − 1) × n.
a) Show that ln(n!) ≤ O(n ln n).
Hint: You can use the fact that n! ≤ nn for n ≥ 1 without proof.
b) Show that n ln n ≤ O(ln(n!)).
n
n
Hint: You can use the fact that 2
2
≤ n! for n ≥ 1 without proof.
3
Eidgenössische Ecole polytechnique fédérale de Zurich
Technische Hochschule Politecnico federale di Zurigo
Zürich Federal Institute of Technology at Zurich
The solutions for this sheet are submitted at the beginning of the exercise class on 17 October 2022.
Exercises/questions marked by ∗ are “challenge exercises”. They do not count towards bonus points.
You can use results from previous parts without solving those parts.
Algorithm 1
(a) f ()
i←0
while i ≤ n do
f ()
i←i+1
Algorithm 2
(b) i←0
while i2 ≤ n do
f ()
f ()
for j ← 1, . . . , n do
f ()
i←i+1
2
*(c) Given an integer k ≥ 2, consider the following algorithm:
Algorithm 3
while k > 0 do
find the largest n such that fn ≤ k
k ← k − fn
3
Eidgenössische Ecole polytechnique fédérale de Zurich
Technische Hochschule Politecnico federale di Zurigo
Zürich Federal Institute of Technology at Zurich
The solutions for this sheet are submitted at the beginning of the exercise class on 24 October 2022.
Exercises that are marked by ∗ are “challenge exercises”. They do not count towards bonus points.
You can use results from previous parts without solving those parts.
Master Theorem. The following theorem is very useful for running-time analysis of divide-and-
conquer algorithms.
Theorem 1 (Master theorem). Let a, C > 0 and b ≥ 0 be constants and T : N → R+ a function such
that for all even n ∈ N,
T (n) ≤ aT (n/2) + Cnb . (1)
Then for all n = 2k , k ∈ N,
• If b > log2 a, T (n) ≤ O(nb ).
• If b = log2 a, T (n) ≤ O(nlog2 a · log n).
• If b < log2 a, T (n) ≤ O(nlog2 a ).
If the function T is increasing, then the condition n = 2k can be dropped. If (1) holds with “=”, then we
may replace O with Θ in the conclusion.
This generalizes some results that you have already seen in this course. For example, the (worst-case)
running time of Karatsuba algorithm satisfies T (n) ≤ 3T (n/2) + 100n, so a = 3 and b = 1 <
log2 3, hence T (n) ≤ O(nlog2 3 ). Another example is binary search: its running time satisfies T (n) ≤
T (n/2) + 100, so a = 1 and b = 0 = log2 1, hence T (n) ≤ O(log n).
n √
log n ≤ O( n)
log(n!) ≥ Ω(n2 )
log3 n4 = Θ(log7 n8 )
c) (This subtask is from August 2019 exam). For each of the following claims, state whether it is
true or false. You don’t need to justify your answers.
n
log n ≥ Ω(n1/2 )
√
log7 (n8 ) = Θ(log3 (n n ))
3n4 + n2 + n ≥ Ω(n2 )
(∗) n! ≤ O(nn/2 )
Note that the last claim is challenge. It was one of the hardest tasks of the exam. If you want a 6
grade, you should be able to solve such exercises.
2
Consider the following pseudocode whose goal is to sort an array A containing n integers.
(a) Show the steps of the algorithm on the input A = [10, 20, 30, 40, 50, 25] until termination. Specif-
ically, give the contents of the array A and the value of i after each iteration of the while loop.
(b) Explain why the algorithm correctly sorts any input array. Formulate a reasonable loop invariant,
prove it (e.g., using induction), and then conclude using invariant that the algorithm correctly sorts
the array.
Hint: Use the invariant “at the moment when the variable i gets incremented to a new value i = k for
the first time, the first k elements of the array are sorted in increasing order”.
(c) Give a reasonable running-time upper bound, expressed in O-notation.
3
Algorithm 2
(a) i←0
while 2i < n do
j←i
while j < n do
f ()
j ←j+1
i←i+1
Algorithm 3
(b) i ← n
while i > 0 do
j←0
f ()
while j < n do
f ()
k←j
while k < n do
f ()
k ←k+1
j ←j+1
i ← b 2i c
4
Eidgenössische Ecole polytechnique fédérale de Zurich
Technische Hochschule Politecnico federale di Zurigo
Zürich Federal Institute of Technology at Zurich
The solutions for this sheet are submitted at the beginning of the exercise class on 31 October 2022.
Exercises that are marked by ∗ are “challenge exercises”. They do not count towards bonus points.
You can use results from previous parts without solving those parts.
3 6 5 1 2 4 8 7 3 6 5 1 2 4 8 7
3 6 5 1 2 4 8 7 3 5 1 2 4 6 7 8
3 5 6 1 2 4 8 7 3 1 2 4 5 6 7 8
3 6 5 1 2 4 8 7 3 6 5 1 2 4 8 7
3 6 1 5 2 4 7 8 1 6 5 3 2 4 8 7
1 3 5 6 2 4 7 8 1 2 5 3 6 4 8 7
Algorithm 2
(b) function h(n)
i←1
while i < n do
f ()
i←i+1
k(n)
k(n)
function k(n)
i←2
while i < n do
f ()
i ← i2
h(n/2)
Algorithm 3 BubbleSort(A)
for 1 ≤ i ≤ n do
for 1 ≤ j ≤ n − i do
if A[j] > A[j + 1] then
t ← A[j]
A[j] ← A[j + 1]
A[j + 1] ← t
return A
(a) Formulate an invariant INV(i) that holds at the end of the i-th iteration of the outer for-loop.
(b) Using the invariant from part (a), prove the correctness of the algorithm. Specifically, prove the
following three assertions:
(1) INV(1) holds.
(2) If INV(i) holds, then INV(i + 1) holds (for all 1 ≤ i < n).
(3) INV(n) implies that BubbleSort(A) correctly sorts the array A.
2
Exercise 5.5 Guessing a pair of numbers (1 point).
Alice and Bob play the following game:
• Alice selects two integers 1 ≤ a, b ≤ 1000, which she keeps secret
• Then, Alice and Bob repeat the following:
– Bob chooses two integers (a0 , b0 )
– If a = a0 and b = b0 , Bob wins
– If a > a0 and b > b0 , Alice tells Bob ‘high!’
– If a < a0 and b < b0 , Alice tells Bob ‘low!’
– Otherwise, Alice does not give any clue to Bob
Bob claims that he has a strategy to win this game in 12 attempts at most.
Prove that such a strategy cannot exist.
Hint: Represent Bob’s strategy as a decision tree. Each edge of the decision tree corresponds to one of Alice’s
answers, while each leaf corresponds to a win for Bob.
Hint: After defining the decision tree, you can consider the sequence k0 = 1, kn+1 = 3kn + 1, and prove
n+1
that kn = 3 2 −1 . The number of leaves in the decision tree of level n should be related kn .
3
Eidgenössische Ecole polytechnique fédérale de Zurich
Technische Hochschule Politecnico federale di Zurigo
Zürich Federal Institute of Technology at Zurich
The solutions for this sheet are submitted at the beginning of the exercise class on 07 November 2022.
Exercises that are marked by ∗ are “challenge exercises”. They do not count towards bonus points.
You can use results from previous parts without solving those parts.
[19, 3, 7, 1, 4, 15, 18, 16, 14, 6, 5, 10, 12, 19, 13, 17, 20, 8, 14, 11]
Use the dynamic programming algorithm from section 3.2. of the script to find the length of a longest
ascending subsequence and the subsequence itself. Provide the intermediate steps, i.e., DP-table up-
dates, of your computation.
Algorithm 1 does not always produce the correct output. Show an example where the above algo-
rithms fails, i.e., when the output does not match OPT. Specify what are the values of n, k, b, what
is OPT and what does Algorithm 1 report.
(b) Consider the pseudocode below. Provide an upper bound in O notation that bounds the time it
takes a compute f [n] (it should be given in terms of n and k). Give a short high-level explanation
of your answer. For full points your upper bound should be tight (but you do not have to prove its
tightness).
Algorithm 2
1: Input: integers n, k. Array b = [1 = b1 , b2 , b3 , . . . , bk ].
2:
3: Let f [1 . . . n] be an array of integers.
4: f [0] ← 0 ▷ Terminating condition.
5: for N ← 1 . . . n do
6: f [N ] ← ∞ ▷ At first, we need ∞ coins. We try to improve upon that.
7: for i ← 1 . . . k do
8: if b[i] ≤ N then
9: val ← 1 + f [N − b[i]] ▷ Use coin b[i], it remains to optimally pay N − b[i].
10: f [N ] ← min(f [N ], val)
11: Print(f [n])
(c) Let OPT(N ) be the answer (min. number of coins needed) when n = N . Algorithm 2 (correctly)
computes a function f [N ] that is equal to OPT(N ). Formally prove why this is the case, i.e., why
f [N ] = OP T (N ).
Hint: Use induction to prove the invariant f [n] = OP T (n). Assume the claim holds for all values of
n ∈ {1, 2, . . . , N − 1}. Then show the same holds for n = N .
(d) Rewrite Algorithm 2 to be recursive and use memoization. The running time and correctness should
not be affected.
2
Given are the two arrays:
A = [7, 6, 3, 2, 8, 4, 5, 1]
and
B = [3, 9, 10, 8, 7, 1, 2, 6, 4, 5],
Use the dynamic programming algorithm from Section 3.3 of the script to find the length of a longest
common subsequence and the subsequence itself. Show all necessary tables and information you used
to obtain the solution.
5 . C . . C .
Remark: Be careful not to peek into an element of the table that is out-of-bounds (i.e., not within [1, m] ×
[1, n]), as this can cause undefined behavior on a real computer.
(a) Write the pseudocode of a recursive function f (x, y) which takes as argument a position of the
character (x, y), and outputs the maximum number of coins that the character can collect if it
started at (x, y) (ignoring all coins it might have previously collected). For example, in the grid
above, f (1, 1) = 5, f (2, 1) = 4, f (5, 5) = 1, f (5, 6) = 0. The function does not need to be
memoized for this subtask.
(b) Prove that your algorithm terminates in finite time (even if possibly exponential in the size of the
input). Prove that the algorithm is correct.
Hint: (This hint is assuming you implemented part (a) in the most natural recursive way.) To prove the
algorithm completes in finite time, observe that x + y only increases and is bounded, hence no infinite
execution paths exist.
Hint: To prove the algorithm is correct, we simply need to prove the invariant which describes f (x, y)
(i.e., the first sentence of part (a)). Assume, by induction, the invariant holds for recursive calls f (x, y)
with strictly larger values of x + y, i.e., for those f (x′ , y ′ ) such that x′ + y ′ > x + y. Argue that
then it also holds for f (x, y) — we do this by considering the optimal path P ∗ that starts at (x, y) and
3
consider three cases: if P ∗ ends immediately, if P ∗ initially goes to the right, or it goes down. Using
the inductive hypothesis, argue that in each of those cases f (x, y) becomes a value at least as large
as the number of coins collected on P ∗ . Similarly, by considering the three cases, argue that the final
value cannot be larger than that of P ∗ since otherwise we could find a better P ∗ . This, by induction,
establishes that f (x, y) is always equal to the number of coins on P ∗ .
(c) Rewrite the pseudocode of the subtask (a), but apply memoization to the above f . Prove that calling
f (1, 1) will, in the worst-case, complete in O(m · n) time.
(d) Write the pseudocode for an algorithm that computes the solution in O(m · n) time, but does not
use any recursion. Address the following aspects of your solution:
(a) Definition of the DP table: What are the dimensions of the table DP ? What is the meaning
of each entry?
(b) Computation of an entry: How can an entry be computed from the values of other entries?
(c) Specify the base cases, i.e., the entries that do not depend on others.
(d) Calculation order: In which order can entries be computed so that values needed for each
entry have been determined in previous steps?
(e) Extracting the solution: How can the final solution be extracted once the table has been filled?
(f) Running time: What is the running time of your solution?
(g) Explicitly write out the pseudocode.
4
Eidgenössische Ecole polytechnique fédérale de Zurich
Technische Hochschule Politecnico federale di Zurigo
Zürich Federal Institute of Technology at Zurich
The solutions for this sheet are submitted at the beginning of the exercise class on 14 November 2022.
Exercises that are marked by ∗ are “challenge exercises”. They do not count towards bonus points.
You can use results from previous parts without solving those parts.
6
0Z0Z0Z
5
Z0Z0Z0 5
o0Z0Z
4
0Z0o0Z 4
pZp 4
0Z0opZ 4
0Z0o0
3
o0o0o0 3
ZpZ 3
Z0o0Zp 3
Z0o0o
2
0o0Z0o 2
0Z0 2
0o0Z0Z 2
0o0Z0
1
Z0Z0Z0 1
Z0Z 1
o0Z0Z0 1
Z0Z0Z
a b c d e f a b c a b c d e f a b c d e
Describe a DP algorithm that, given N, M > 0, counts the number of safe pawn lines on an N × M
chessboard. In your solution, address the same six aspects as in Exercise 7.1. Your solution should have
complexity at most O(N M ).
2
Exercise 7.5 Longest Snake.
You are given a game-board consisting of hexagonal fields F1 , . . . , Fn . The fields contain natural num-
bers v1 , . . . , vn ∈ N. Two fields are neighbors if they share a border. We call a sequence of fields
(Fi1 , . . . , Fik ) a snake of length k if, for j ∈ {1, . . . , k − 1}, Fij and Fij+1 are neighbors and their
values satisfy vij+1 = vij + 1. Figure ?? illustrates an example game board in which we highlighted the
longest snake.
For simplicity you can assume that Fi are represented by their indices. Also you may assume that you
know the neighbors of each field. That is, to obtain the neighbors of a field Fi you may call N (Fi ),
which will return the set of the neighbors of Fi . Each call of N takes unit time.
(a) Provide a dynamic programming algorithm that, given a game-board F1 , . . . , Fn , computes the
length of the longest snake.
11 12 3 21
10 5 3 2 20
9 4 11 1 2
1 6 5 10 9
12 13 6 7 8
Hint: Your algorithm should solve this problem using O(n log n) time, where n is the number of
hexagonal fields.
Address the same six aspects as in Exercise 7.1 in your solution.
(b) Provide an algorithm that takes as input F1 , . . . Fn and a DP table from part a) and outputs the
longest snake. If there are more than one longest snake, your algorithm can output any of them.
State the running time of your algorithm in Θ-notation in terms of n.
*(c) Find a linear time algorithm that finds the longest snake. That is, provide an O(n) time algorithm
that, given a game-board F1 , . . . , Fn , outputs the longest snake (if there are more than one longest
snake, your algorithm can output any of them).
3
Eidgenössische Ecole polytechnique fédérale de Zurich
Technische Hochschule Politecnico federale di Zurigo
Zürich Federal Institute of Technology at Zurich
The solutions for this sheet are submitted at the beginning of the exercise class on 21 November 2022.
Exercises that are marked by ∗ are “challenge exercises”. They do not count towards bonus points.
You can use results from previous parts without solving those parts.
a0 = 1,
a1 = 1,
a2 = 2,
ai = ai−1 + 2ai−2 + ai−3 ∀i ≥ 3.
The goal of this exercise is to find exponential lower and upper bounds for an .
(a) Find a constant C > 1 such that an ≤ O(C n ) and prove your statement.
(b) Find a constant c > 1 such that an ≥ Ω(cn ) and prove your statement.
Remark. One can actually show that an = Θ(φn ), where φ ≈ 2.148 is the unique positive solution
of the equation x3 = x2 + 2x + 1.
7 12
size=4 size=2
3 8 15
size=1 size=2 size=1
9
size=1
b) Describe in pseudo-code an algorithm VerifySizes(root) that returns true if all the sizes in the
tree are correct, and returns false otherwise. For example, it should return true given the tree in
Fig. 1, but false given the tree in Fig. 2.
What is the running time of your algorithm? Justify your answer.
10
size=7
7 12
size=4 size=5
3 8 15
size=1 size=2 size=1
9
size=1
Figure 2: Augmented binary search tree with buggy size: incorrect size for node with data “12”
c) Suppose we have an augmented AVL tree (i.e., as above, each node has a size member variable).
Describe in pseudo-code an algorithm Select(root, k) which, given an augmented AVL tree and
an integer k, returns the k-th smallest element in the tree in O(log n) time.
2
Example: Given the tree in Fig. 1, for k = 3, Select returns 8; for k = 5, it returns 10; for k = 1, it
returns 3; etc.
d)* To maintain the correct sizes for each node, we have to modify the AVL tree operations, insert
and remove. For this problem, we will consider only the modifications to the AVL-insert method
(i.e., you are not responsible for AVL-remove). Recall that AVL-insert first uses regular insert for
binary search trees, and then balances the tree if necessary via rotations.
• How should we update insert to maintain correct sizes for nodes?
During the balancing phase, AVL-insert performs rotations. Describe what updates need to be
made to the sizes of the nodes. (It is sufficient to describe the updates for left rotations, as right
rotations can be treated analogously.)
3
7
Figure 3: A stack S0 containing the numbers 4, 3, 2, and 7 (7 is the top of the stack)
In this exercise, we focus on the second phase, and consider the following three basic operations used
to compute with stacks:
pop: If there is at least one element in the stack, remove the top element of the stack. Otherwise, do
nothing.
add: If there are at least two elements in the stack, remove the top two elements, compute their sum,
and push this sum back into the stack. If there is less than two elements in the stack, do nothing.
mul: If there are at least two elements in the stack, remove the top two elements, compute their prod-
uct, and push this product back into the stack. If there is less than two elements in the stack, do
nothing.
Below are examples of applications of pop, add, and mul.
7 7 7
2 → 2 2 → 9 2 → 14
3 3 3 3 3 3
4 4 4 4 4 4
We say that an integer i can be computed from a stack S if and only if there exists a sequence of pop, add,
and mul operations on S that ends with i on top of the stack. For example, the value (3 · 2) + 4 = 10
can be computed from the stack S0 above through the following sequence of operations:
4
7
3 3 6
4 4 4 10
Given a stack S containing n integers S1 , . . . , Sn ∈ {1, . . . , k} (with S1 being the top of the stack) and
an integer c, you are tasked to design a DP algorithm which determines if c can be computed from S.
To obtain full points, your algorithm should have complexity at most O(c · n), but partial points will
be awarded for any solution running in time O(k n · n).
In your solution, address the following aspects:
1. Dimensions of the DP table: What are the dimensions of the DP table?
2. Definition of the DP table: What is the meaning of each entry?
3. Computation of an entry: How can an entry be computed from the values of other entries? Specify
the base cases, i.e., the entries that do not depend on others.
4. Calculation order: In which order can entries be computed so that values needed for each entry have
been determined in previous steps?
5. Extracting the solution: How can the solution be extracted once the table has been filled?
6. Running time: What is the running time of your solution?
(*) Challenge question: Extend your algorithm to support the following additional operation:
neg: If there is at least one element in the stack, remove the top element x of the stack, and push −x
back into the stack. Otherwise, do nothing.
5
Eidgenössische Ecole polytechnique fédérale de Zurich
Technische Hochschule Politecnico federale di Zurigo
Zürich Federal Institute of Technology at Zurich
The solutions for this sheet are submitted at the beginning of the exercise class on 28 November 2022.
Exercises that are marked by ∗ are “challenge exercises”. They do not count towards bonus points.
You can use results from previous parts without solving those parts.
are numbers from {1, 2, 3, 4, 5, 6}. The tiles are symmetric, so [x|y] and [y|x] is the same tile and
appears only once.
Show that it is impossible to form a line of all 21 tiles such that the adjacent numbers of any
consecutive tiles coincide.
(b) What happens if we replace 6 by an arbitrary n ≥ 2? For which n is it possible to line up all n
2 +n
different tiles along a line?
2
Hint: One direction is immediate by part (c). For the other direction (every connected graph with n − 1
edges is a tree), use induction on n. First, prove there always exists a leaf by considering the average
degree. Then, disconnect the leaf from the graph and argue the remaining graph is still connected and
has exactly one less edge. Apply the inductive hypothesis and conclude.
(d) Write the pseudocode of an algorithm that is given a graph G as input and checks whether G is a
tree.
As input, you can assume that the algorithm has access to the number of vertices n, the number
of edges m, and to the edges {a1 , b1 }, {a2 , b2 }, . . . , {am , bm } (i.e., the algorithm has access to 2m
integers a1 , . . . , am , b1 , . . . , bm , where each edge of G is given by its endpoints ai and bi ). You can
assume that the graph is valid (specifically, 1 ≤ ai , bi ≤ n and ai 6= bi ). The algorithm outputs
“YES” or “NO”, corresponding to whether G is a tree or not. Your algorithm must always complete
in time polynomial in n (e.g., even O(n10 m10 ) suffices).
Hint: Use part (c). There exists a (relatively) simple O(n + m) solution. However, the official solution
is O(n · m) for brevity and uses recursion to check if G is connected.
Example 2: n = 5 Output: NO
m=4 4 5
a1 , b1 = 1, 3
a2 , b2 = 4, 5
2
a3 , b3 = 5, 2
a4 , b4 = 2, 4
3 1
3
Eidgenössische Ecole polytechnique fédérale de Zurich
Technische Hochschule Politecnico federale di Zurigo
Zürich Federal Institute of Technology at Zurich
The solutions for this sheet are submitted at the beginning of the exercise class on 5 December 2022.
Exercises that are marked by ∗ are “challenge exercises”. They do not count towards bonus points.
You can use results from previous parts without solving those parts.
T
B A
F
T
B T
D E F
T
T
C
C G H
T
(a) Mark the edges that belong to the depth-first tree (Tiefensuchbaum) with a “T” (for tree edge).
(b) For each vertex in the depth-first tree, give its pre- and post-number.
(c) Give the vertex ordering that results from sorting the vertices by pre-number. Give the vertex
ordering that results from sorting the vertices by post-number.
(d) Mark every forward edge (Vorwärtskante) with an “F”, every backward edge (Rückwärtskante) with
an “B”, and every cross edge (Querkante) with a “C”.
(e) Does the above graph have a topological ordering? How can we use the above execution of depth-
first search to find a directed cycle?
(f) Draw a scale from 1 to 16, and mark for every vertex v the interval Iv from pre-number to post-
number of v. What does it mean if Iu ⊂ Iv for two different vertices u and v?
(g) Consider the graph above where the edge from E to D is removed and an edge from A to H is
added. How does the execution of depth-first search change? Which topological sorting does the
depth-first search give? If you sort the vertices by pre-number, does this give a topological sorting?
Example: n = 5 1 2 Output: 3
(the path is highlighted in red.)
3
2
Explain in a few sentences why your algorithm achieves the desired runtime.
Hint: If needed, you can use the fact that “in G, there is a unique path from the root to each vertex” without
proof.
6 4
c) Adjacency lists, and additionally we store the degree of each node, and there are pointers between
the two occurences of each edge. (An edge appears in the adjacency list of each endpoint).
1 deg: 4 2 3 4 5
2 deg: 3 4 1 6
3 deg: 1
1
4 deg: 3
5 2 1
5 deg: 2 4 1
6 deg: 1
2
For each of the above data structures, what is the required memory (in Θ-Notation)?
Which runtime (worst case, in Θ-Notation) do we have for the following queries? Give your answer
depending on n, m, and/or deg(u) and deg(v) (if applicable).
(a) Input: A vertex v ∈ V . Find deg(v).
(b) Input: A vertex v ∈ V . Find a neighbour of v (if a neighbour exists).
(c) Input: Two vertices u, v ∈ V . Decide whether u and v are adjacent.
(d) Input: Two adjacent vertices u, v ∈ V . Delete the edge e = {u, v} from the graph.
3
(e) Input: A vertex u ∈ V . Find a neighbor v ∈ V of u and delete the edge {u, v} from the graph.
(f) Input: Two vertices u, v ∈ V with u 6= v. Insert an edge {u, v} into the graph if it does not exist
yet. Otherwise do nothing.
(g) Input: A vertex v ∈ V . Delete v and all incident edges from the graph.
For the last two queries, describe your algorithm.
Figure 1: An example of 7 × 7 maze in which purple fields are blocked, white fields can be traversed
(are unblocked). The start field is marked with ’S’ and the target field with a ’T’.
(a) Model the problem as a graph problem. Describe the set of vertices V and the set of edges E in
words. Reformulate the problem description as a graph problem on the resulting graph.
(b) Choose a data structure to represent your maze-graphs and use an algorithm discussed in the lecture
to solve the problem.
Hint: If there are multiple solutions of the same quality, return any one of them.
(c) Determine the running time and memory requirements of your algorithm in terms of n in Θ nota-
tion.
4
Eidgenössische Ecole polytechnique fédérale de Zurich
Technische Hochschule Politecnico federale di Zurigo
Zürich Federal Institute of Technology at Zurich
The solutions for this sheet are submitted at the beginning of the exercise class on 12 December 2022.
Exercises that are marked by ∗ are “challenge exercises”. They do not count towards bonus points.
You can use results from previous parts without solving those parts.
Algorithm 1
Input: a weighted graph, represeted via c(·, ·). Specifically, for two vertices u, v the value c(u, v)
represents the cost of an edge from u to v (or ∞ if no such edge exists).
function Dijkstra(G, s)
d[s] ← 0 . upper bounds on distances from s
d[v] ← ∞ for all v 6= s
S←∅ . set of vertices with known distances
while S 6= V do
choose v ∗ ∈ V \ S with minimum upper bound d[v ∗ ]
add v ∗ to S
update upper bounds for all v ∈ V \ S:
d[v] ← minpredecessor u∈S of v d[u] + c(u, v)
(if v has no predecessors in S, this minimum is ∞)
We remark that this version of Dijkstra’s algorithm focuses on illustrating how the algorithm explores
the graph and why it correctly computes all distances from s. You can use this version of Dijkstra’s
algorithm to solve this exercise.
In order to achieve the best possible running time, it is important to use an appropriate data structure
for efficiently maintaining the upper bounds d[v] with v ∈ V \S, as you saw in the lecture on December
1. In the other exercises/sheets and in the exam you should use the running time of the efficient version
of the algorithm (and not the running time of the pseudocode described above).
Consider the following weighted directed graph:
s
3
5
a 10 b
1
5
8 c 1
3
9
d e
2
a) Execute the Dijkstra’s algorithm described above by hand to find a shortest path from s to each
vertex in the graph. After each step (i.e. after each choice of v ∗ ), write down:
1) the upper bounds d[u], for u ∈ V , between s and each vertex u computed so far,
2) the set M of all vertices for which the minimal distance has been correctly computed so far,
3) and the predecessor p(u) for each vertex in M .
b) Change the weight of the edge (a, c) from 1 to −1 and execute Dijkstra’s algorithm on the new
graph. Does the algorithm work correctly (are all distances computed correctly) ? In case it breaks,
where does it break?
c) Now, additionally change the weight of the edge (e, b) from 1 to −6 (so edges (a, c) and (e, b) now
have negative weights). Show that in this case the algorithm doesn’t work correctly, i.e. there exists
some u ∈ V such that d[u] is not equal to a minimal distance from s to u after the execution of the
algorithm.
2
Algorithm 2
Input: graph G, given as adj and n ≥ 1.
Global variable: marked[1 . . . n], initialized to [F alse, F alse, . . . , F alse].
Global variable: T , initialized to T ← 1.
Global variable: pre[1 . . . n]. . Pre-order number.
Global variable: post[1 . . . n]. . Post-order number.
function DF S(v)
marked[v] ← T rue
pre[v] ← T
T ←T +1
for each neighbor w ∈ adj[v] do
if not marked[w] then
DF S(w)
post[v] ← T
T ←T +1
for v ∈ {1, . . . , n} do
if not marked[v] then
DF S(v)
(a) Consider the graphical representation of the DFS order where a vertex v is represented as an interval
[pre(v), post(v)]. Give a short argument why in directed or undirected graphs no two such intervals
can intersect without one being fully contained in the other. Specifically, argue why the situation
depicted in the figure below cannot happen.
pre(b) post(b)
pre(a) post(a)
(b) Give a short argument why undirected graphs cannot have any cross edges.
(c) Prove that a directed graph is acyclic (i.e., a DAG) if and only if it has no back edges. This was
proven in the lecture, but the goal here is to explicitly write out the entire argument.
Hint: You need to prove both directions of the equivalence.
Hint: For the ( =⇒ ) direction, assume the opposite (there is a back edge), then simply find a cycle
containing that back edge. If needed, you can use without proof the property that if the interval of a is
contained within interval b, then there exists a simple path from b to a.
Hint: For the ( ⇐= ) direction, we need to prove the graph is a DAG (i.e., acyclic). It is sufficient to
find a topological ordering such that all directed edges originate at vertices that are before their tail
(according to the ordering). One specific order that works is the reverse post-order.
3
You are given a graph G = (V, E) representing the towns of Switzerland. Each vertex V corresponds
to a town, and there is an (undirected) edge {v1 , v2 } ∈ E if and only if there exists a direct road going
from town v1 to town v2 . Additionally, there is a function w : E → N such that w(e) corresponds to
the number of hours needed to hike over road e, and a function ` : V → {G, F, I, R} that maps each
town to the language that is spoken there1 . For simplicity, we assume that only one language is spoken
in each town.
Alice asks you to find an algorithm that returns the walking duration (in hours) of the shortest hike
that goes through at least one town speaking each of the four languages.
For example, consider the following graph, where languages appear on vertices:
G
30 6 25
12
F F G G
4
12
12 25 30 14 8 12
7 15 14 9
F G G R G
10 8
25
I
9
I
The shortest path satisfying the condition is marked in red. It goes through one R vertex, one I vertex,
two G vertices and one F vertex. Your algorithm should return the cost of this path, i.e., 40.
(a) Suppose we know the order of languages encountered in the shortest hike. It first goes from an
R vertex to an I vertex, then immediately to a G vertex, and reaches an F vertex in the end, af-
ter going through zero, one or more additional G vertices. In other terms, the form of the path
is RIGF or RIG…GF. In this case, describe an algorithm which finds the shortest path satisfying
the condition, and explain its runtime complexity. Your algorithm must have complexity at most
O((|V | + |E|) log |V |).
Hint: Consider the new vertex set V 0 = V × {1, 2, 3, 4} ∪ {vs , vd }, where vs is a ‘super source’ and
vd a ‘super destination’ vertex.
(b) Now we don’t make the assumption in (a). Describe an algorithm which finds the shortest path
satisfying the condition. Briefly explain your approach and the resulting runtime complexity. To
obtain full points, your algorithm must have complexity at most O((|V | + |E|) log |V |).
Hint: Consider the new vertex set V 0 = V × {0, 1}4 ∪ {vs , vd }, where vs is a ‘super source’ and vd a
‘super destination’ vertex.
1
G, F, I and R stand for German, French, Italian, and Romansh respectively.
4
Eidgenössische Ecole polytechnique fédérale de Zurich
Technische Hochschule Politecnico federale di Zurigo
Zürich Federal Institute of Technology at Zurich
The solutions for this sheet are submitted at the beginning of the exercise class on 19 December 2022.
Exercises that are marked by ∗ are “challenge exercises”. They do not count towards bonus points.
You can use results from previous parts without solving those parts.
a
6
8
b 7 c
10
1
4 d 3
2
12
e f
5
a) Compute the minimum spanning tree (MST) using Boruvka’s algorithm. For each step, provide the
set of edges that are added to the MST.
b) Provide the order in which Kruskal’s algorithm adds the edges to the MST.
c) Provide the order in which Prim’s algorithm (starting at vertex d) adds the edges to the MST.
1 5 3 10 4 Output: Explanation:
Max cargo to 1 is ∞ The best path from the headquar-
10 8 Max cargo to 2 is 10 ters to 4 is 1 → 2 → 3 → 4, and
Max cargo to 3 is 8 the maximum cargo the truck
2 Max cargo to 4 is 8 can carry is min(10, 8, 10) = 8.
(c) Prove that for every t ∈ V , the optimal route is to take the unique path in the maximum spanning
tree of G.
Hint: Suppose that the largest amount of cargo we can carry from 1 to t in G (i.e., the correct result)
is OP T and let ALG be the largest amount of cargo from 1 to t in the maximum spanning tree. We
need to prove two directions: OP T ≤ ALG and OP T ≥ ALG.
Hint: One direction holds trivially as any spanning tree is a subgraph. For the other direction, use part
(b).
(d) Write the pseudocode of the algorithm that computes the output for all t ∈ V and runs in O((|V | +
|E|) log |V |). You can assume that you have access to a function that computes the maximum
spanning tree from G and outputs it in any standard format. Briefly explain why the runtime
bound holds.
Exercise 12.3 Counting Minimum Spanning Trees With Identical Edge Weights (1 point).
Let G = (V, E) be an undirected, weighted graph with weight function w.
It can be proven that, if G is connected and all its edge weights are pairwise distinct1 , then its Minimum
Spanning Tree is unique. You can use this fact without proof in the rest of this exercise.
For k ≥ 0, we say that G is k-redundant if k of G’s edge weights are non-unique, e.g.
In particular, if G’s edge weights are all distinct, then G is 0-redundant, and if its edge weights are all
identical, it is |E|-redundant.
(a) Given a weighted graph G = (V, E) with weight function c and e = {v, w} ∈ E, we say that we
contract e when we perform the following operations:
(i) Replace v and w by a single vertex vw in V , i.e., V 0 ← V − {v, w} ∪ {vw}.
1
I.e., for all e 6= e0 ∈ E, w(e) 6= w(e0 ).
2
(ii) Replace any edge {v, x} or {w, x} by an edge {vw, x} in E, i.e.,
(iii) Set the weight of the new edges to the weight of the original edges, taking the minimum of
the two weights if two edges are merged, i.e.
For all G = (V, E) and e ∈ E, we denote by Ge the graph obtained by contracting e in G. Explain
why if T is an MST of G and e ∈ T , then Te must be an MST of Ge .
(b) Let k > 0. Show that for all k-redundant G = (V, E) and e 6= e0 ∈ E with w(e) = w(e0 ), then Ge
is k 0 -redundant for some k 0 ≤ k − 1.
(c) Show that if G is connected and k-redundant, it has at most 2k distinct MSTs.
Hint: By induction over k, using (a) and (b).
(d) Show that for all large enough n, there exists a graph G such that G is n-redundant and has at least
n
2 2 distinct MSTs.
Hint: First assume that n = 3k for some k. Consider graphs of the following form, where all unmarked
edges have weight 0. When n = 3k + 1 or n = 3k + 2, you can add one or two edges with cost 0 at
either end.
• • • •
• •
1
• •
2
• •
3 . . . k − 1• •
3
Eidgenössische Ecole polytechnique fédérale de Zurich
Technische Hochschule Politecnico federale di Zurigo
Zürich Federal Institute of Technology at Zurich
Submission: This exercise sheet is not to be turned in. The solutions will be published at the end of
the week, before Christmas.
Exercise 13.1 Shortest path with negative edge weights (part I).
Let G = (V, E, w) be a graph with edge weights w : E → Z \ {0} and wmin = mine∈E w(e).
Since Dijkstra’s algorithm must not be used whenever some edge weights are negative (i.e., wmin < 0),
one could come up with the idea of applying a transformation to the edge weight of every edge e ∈ E,
namely w0 (e) = w(e) − wmin + 1, such that all weights become positive, and then find a shortest path
P in G by running Dijkstra with these new edge weights w0 .
Show that this is not a good idea by providing an example graph G with a weight function w, such
that the above approach finds a path P that is not a shortest path in G (this path P can start from the
vertex of your choice). The example graph should have exactly 5 nodes and not all weights should be
negative.
Exercise 13.2 Shortest path with negative edge weights (part II).
We consider the following graph:
6
1 2
4
2 1 5
3 1
1 4 -4 2 5
3 1 4
Exercise 13.3 Invariant and correctness of algorithm (This exercise is from the January 2020 ex-
am).
Given is a weighted directed acyclic graph G = (V, E, w), where V = {1, . . . , n}. The goal is to find
the length of the longest path in G.
Let’s fix some topological ordering of G and consider the array top[1, . . . , n] such that top[i] is a vertex
that is on the i-th position in the topological ordering.
Consider the following pseudocode
State the running time of the algorithm described above in Θ-notation in terms of |V | and |E|. Justify
your answer.
Exercise 13.4 Cheap flights (This exercise is from the January 2020 exam).
2
Suppose that there are n airports in the country Examistan. Between some of them there are direct
flights. For each airport there exists at least one direct flight from this airport to some other airport.
Totally there are m different direct flights between the airports of Examistan.
For each direct flight you know its cost. The cost of each flight is a strictly positive integer.
You can assume that each airport is represented by its number, i.e. the set of airports is {1, . . . , n}.
a) Model these airports, direct flights and their costs as a directed graph: give a precise description of
the vertices, the edges and the weights of the edges of the graph G = (V, E, w) involved (if possible,
in words and not formal).
In points b) and c) you can assume that the directed graph is represented by a data structure that allows
you to traverse the direct predecessors and direct successors of a vertex u in time O(deg− (u)) and
O(deg+ (u)) respectively, where deg− (u) is the in-degree of vertex u and deg+ (u) is the out-degree of
vertex u.
b) Suppose that you are at the airport S and you want to fill the array d of minimal traveling costs to
each airport. That is, for each airport A, d[A] is a minimal cost that you must pay to travel from S
to A.
Name the most efficient algorithm that was discussed in lectures which solves the corresponding
graph problem. If several such algorithms were described in lectures (with the same running time),
it is enough to name one of them. State the running time of this algorithm in Θ-notation in terms
of n and m.
c) Now you want to know how many optimal routes there are to airport T . In other words, if cmin is
the minimal cost from S to T then you want to compute the number of routes from S to T of cost
cmin .
Assume that the array d from b) is already filled. Provide an as efficient as possible dynamic pro-
gramming algorithm that takes as input the graph G from task a), the array d from point b) and the
airports S and T , and outputs the number of routes from S to T of minimal cost.
Address the following aspects in your solution and state the running time of your algorithm:
1) Definition of the DP table: What are the dimensions of the table DP [. . .] ? What is the meaning
of each entry ?
2) Computation of an entry: How can an entry be computed from the values of other entries ?
Specify the base cases, i.e., the entries that do not depend on others.
3) Calculation order: In which order can entries be computed so that values needed for each entry
have been determined in previous steps ?
4) Extracting the solution: How can the final solution be extracted once the table has been filled ?
5) Running time: What is the running time of your algorithm ? Provide it in Θ-notation in terms
of n and m, and justify your answer.
3
Exercise 13.5 Elevator (This exercise is from the January 2022 exam).
Consider the following definitions for a directed graph G = (V, E):
1. The out-degree of a vertex v ∈ V , denoted with degout (v), is the number of edges of E that start
at v, i.e., degout (v) = |{(v, w) ∈ E | w ∈ V }|.
2. The in-degree of a vertex v ∈ V , denoted with degin (v), is the number of edges that end at v, i.e.,
degout (v) = |{(u, v) ∈ E | u ∈ V }|.
3. A Eulerian walk is a sequence v1 , . . . , vk ∈ V such that k = |E| + 1 and {(vi , vi+1 ) | 1 ≤ i <
k} = E. Note that this definition implies (vi , vi+1 ) being different edges for 1 ≤ i < k.
In this exercise, you can use without proof the following result from the lecture:
Lemma 1. A directed graph G = (V, E) admits a Eulerian walk if, and only if, all of the following
conditions holds:
1. At most one vertex v ∈ V is such that degout (v) = degin (v) + 1;
2. At most one vertex v ∈ V is such that degin (v) = degin (v) + 1;
3. Every vertex that satisfies neither (i) nor (ii) is such that degout (v) = degin (v);
4. The undirected graph G0 obtained by ignoring the direction of edges in G is connected.
a) Write down the pseudocode of an O(|V | + |E|) time algorithm that takes as input a directed graph
G, and returns true if G has a Eulerian walk, and false otherwise. Justify its correctness and
complexity.
b) Alice is launching iFahrstuhl™, a start-up developing the next generation of elevators.
Assume a building with n floors indexed from 1 to n and an elevator which has room for a single
person. The elevator receives requests in the form of pairs (i, j) ∈ {1, . . . , n}2 of distinct floors
between which a single person is willing to travel.
Consider the scenario where m people want to use the elevator. For 1 ≤ t ≤ m, the t-th people want
to go from floor it to floor jt . These requests are given as a finite set S = {(i1 , j1 ), . . . , (im , jm )}.
A finite set S = {(i1 , j1 ), . . . , (im , jm )} of requests is called optimal if the pairs can be ordered such
that all requests can be processed and the elevator is never empty when moving between two floors
(except maybe on its way to fetching the first person).
For example, for n = 5, the set S1 = {(2, 3), (4, 1), (3, 4)} is optimal, since it can ordered as
{(2, 3), (3, 4), (4, 1)}, which means that the elevator can start on floor 2 to fetch person 1, go to
floor 3, drop person 1 and fetch person 3, go to floor 4, drop person 3 and fetch person 2, go to floor
1, drop person 2, and terminate there. However, the set S2 = {(2, 3), (4, 1)} is not optimal, since
there is no way a single elevator can satisfy both requests without moving empty from floor 3 to
floor 4 or floor 1 to floor 2.
Given a set of requests S, Alice’s elevators should be able to decide whether it’s optimal. Model
the problem of detecting optimal sets of requests as a graph problem and provide an algorithm to
solve it. Describe the vertex and edge set, edge weights (if needed), the graph problem you solve,
the algorithm you use, and its complexity. To obtain full points, your algorithm should run in time
O(n + |S|).
c) Alice’s startup has installed k single-person elevators in your n-floor building. Unfortunately, not
all elevators can reach all floors. Hence, for each elevator j ∈ {1, . . . , k}, you are given a set Fj ⊆
4
{1, . . . , n} of floors it can reach. When you arrive in front of an elevator j, say on floor f ∈ Fj ,
you can immediately call it, after which you have to wait until it reaches your floor from its current
position, moving at the constant speed of 1 time unit per floor. When the elevator arrives, you choose
the destination floor f 0 ∈ Fj , and the elevator brings you to this floor at the constant speed of 0.5
time units per floor (for security reasons, the elevator is slower when it is not empty). The time spent
moving between elevators on the same floor, calling the elevator or choosing the destination floor
is negligible, since you are very fast at interacting with elevators.
You are alone in the building at floor 1, with each elevator j being initally located on floor fj . You
would like to go to floor n. What is the minimal amount of time that you have to travel using Alice’s
elevators? If you cannot reach floor n, then output ∞.
Model the problem as a graph problem and provide an algorithm to solve it. Describe the vertex
and edge set, edge weights (if needed), the graph problem you solve, the algorithm you use, and
its complexity. To obtain full points, your algorithm should run in time O((n + K) log n), where
K = pj=1 |Fj |2 .
P
d) Continue the setting of (c). Elevator doors in your building need maintenance, but the people in
your building also need elevators. In your building, there is exactly one elevator door per elevator
and floor, which needs to be functional in order for the elevator to be used from or to this floor. Even
if a door is not functional, the elevator can still be used between all other floors where a functional
door is present. Alice wants to select as many elevator doors as possible to be maintained during
the next working day such that all floors can be reached from each other using the elevators and
the remaining functional doors (those not in maintenance).
Model the problem as a graph problem and provide an algorithm to solve it. Describe the vertex
and edge set, edge weights (if needed), the graph problem you solve, the algorithm you use, and
its complexity.
PTo obtain full points, your algorithm should run in time O((n + K 0 ) log(n + K 0 )),
p
where K = j=1 |Fj |.
0
v i1 1 wi1
1 0
v i2 wi2
0
elevatorj
... ...
0
viq 1 w iq
where Fj = {i1 , . . . , iq }.