Eidgen¨ossische
Technische Hochschule Z¨urich
Ecole polytechnique f´ed´erale de Zurich Politecnico federale di Zurigo
Federal Institute of Technology at Zurich
Departement of Computer Science 22. October 2018
Markus P¨uschel, David Steurer
Algorithms & Data Structures Homework 5 HS 18 Exercise Class
(Room & TA):
Submitted by:
Peer Feedback by:
Points:
Exercise 5.1 Big-ΘNotation(No bonus points).
1. For the following code fragment, count the number of times the functionfis invoked and express this inΘ-notation as concisely as possible. The functionfdoes not invoke itself.
for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) {
for(int k = j; k < n; k++) { f();
} } }
2. For the following code fragment, determine inΘ-notation the number of times the functionf is invoked. The functionfdoes not invoke itself.
for(int i = 1; i <= 2*n; i+=5) { for(int j = 1; j*j <= n; j++) {
f();
}
for(int k = 1; k*k < 1000; k = k+1) { f();
} }
Exercise 5.2 Relations between big-O,ΩandΘ(No bonus points).
1. Prove or disprove:Ω(n2)∩ O(n3) = Θ(n2)∪Θ(n3)
2. Is there a functionf :N→ R+that is neither inO(n2)nor inΩ(n2)? If no, prove, if yes, give an example.
3. Prove or disprove: Letf, g∈Θ(h), then|f−g| ∈ O(1).
4. Is there a non-constant functionf :N→R+withf ∈Θ(1)? If no, prove, if yes, give an example.
5. Prove or disprove: Letf ≤ O(g),g≤ O(h), andh≤ O(f). ThenΘ(f) = Θ(g) = Θ(h).
Exercise 5.3 Recurrence Relations(1 Point for Part 2).
For this exercise, assume thatnis a power of two, and that a and c are positive constants.
1. Letf : N → R+ be defined asf(1) = c, andf(n) = 2f(n2) +a·n, forn ≥ 2. Assume that n= 2kis a power of2,k∈N∪ {0}. Show that:
f(n) =c·n+a·n·log2n.
2. Letf :N → R+be defined asf(1) = a, andf(n) = 2f n2
+an2. Assume thatn= 2k is a power of2,k ∈N∪ {0}. Use telescoping to guess a closed form solution forf (and show how you did it), and then prove it by mathematical induction.
Exercise 5.4 Binary Relations(No bonus points).
In discrete mathematics, you learned that arelationρfrom a setAto a setBis a subset of the Cartesian productA×B. If(a, b)∈ρ, thenais related tob. IfA=B, thenρis said to be a relation onA. LetA={f :N→R+}. Recall that0∈/ R+. Then we can define relations onA, associated withO,Ω, andΘin the following way:
ρO ={(f, g) :f ≤ O(g)}
ρΩ={(f, g) :f ≥Ω(g)}
ρΘ={(f, g) :f = Θ(g)}
1. For each of the relationsρO,ρΩ,ρΘ, determine (a) If it is an equivalence relation. Prove or disprove.
(b) If it is a partial order. Prove or disprove.
2. We can define a binary relation from a graph: LetG= (V, E)be any directed graph, allowing also loops, i.e. edges(u, u), whereu∈V. ThenρG =Eis a relation onV. (Recall thatE ⊂V ×V).
Similarly, we can define a graph from a binary relation: LetρG be a relation on V. ThenG = (V, ρG)is a directed graph, possibly with loops.
For each of the following graphsG, what are the properties (reflexivity, symmetricity, antisym- metricity, transitivity) of its corresponding relation, ρG? Arrows represent directed edges. If a line has two arrows, it represents two directed edges.
• Graph A
A B
C D
E
G F
2
• Graph B
A B
C D
E
G F
• Graph C
A B
C D
F E
3. Draw a directed graphG= (V, E), possibly with loops, with7vertices and17edges that corre- sponds to an equivalence relation. There can be no duplicate edges inE. If both(u, v)and(v, u) are inE, it counts as two edges.
Exercise 5.5 Max Submatrix Sum(2 Points).
Provide anO(n3)time algorithm which given ann×n-matrixMoutputs its maximal submatrix sum S. That is, ifM has some nonnegative entries,
S = max
0≤a≤b<n 0≤c≤d<n
b
X
i=a d
X
j=c
M[i][j],
and if all entries ofM are negative,S= 0.
Justify your answer, i.e. prove that the asymptotic runtime of your algorithm isO(n3).
Submission:On Monday, 29.10.2018, hand in your solution to your TAbeforethe exercise class starts.
3