Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Binary Relation - Data Structures - Exam, Exams of Data Structures and Algorithms

Main points of this exam paper are: Binary Relation, Randomized, Table Operations, Lexicographically, Deterministischen, Maximum Flow, Original Graph

Typology: Exams

2012/2013

Uploaded on 04/07/2013

sethuraman_h34rt
sethuraman_h34rt 🇮🇳

4.3

(8)

159 documents

1 / 16

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Midterm Exam
Algorithms and Data Structures WS03/04
Name:
Matr.–Nr.:
Time: 120 min
Aufgabe 12345
Maximal-
punktzahl 8 8 8 18 18
erreichte
Punktzahl
Σ: von 60
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Binary Relation - Data Structures - Exam and more Exams Data Structures and Algorithms in PDF only on Docsity!

Midterm Exam

Algorithms and Data Structures WS03/

Name:

Matr.–Nr.:

Time: 120 min

Aufgabe (^1 2 3 4 )

Maximal- punktzahl 8 8 8 18 18

erreichte Punktzahl

Σ: von 60

Problem 1: (Symmetric binary relation, 8 points)

Assume you are given a set M of pairs of integers in the range 1,... , |M|. M defines a binary relation RM 1. Outline an algorithm that checks in expected time O(|M|) whether RM is symmetric^2. Hint: there are at least two basic approaches to a solution. One yields a deterministic algorithm the other is randomized but would also work without the restriction on the size of the numbers.

We give two solutions.

(a) We store all the pairs (a, b) ∈ M in the hash table H with (a, b) as a key. Then we go through all pairs again, checking for each (a, b) whether (b, a) is in the hash table. If the test is passed then relation RM is symmetric. Each hash table operation has expected cost O(1). Therefore the expected running time is O(|M|).

for each (a, b) ∈ M do H.insert((a, b)) // O(|M|) expected time for each (a, b) ∈ M do if H.f ind((b, a)) = ∅ then return false // O(|M|) exp. time return true

(b) The running time of the previous algorithm depends on the running times of the hash table operations, which are in fact expected running times. Therefore the algorithm is a randomized one. To achieve deterministic run time guarantees, one can use a different approach. First, insert the pairs (a, b) ∈ M into array A with a ≤ b, if a > b insert (b, a) instead. Sort array A lexicographically using LSD radix sort. If each pair occurs exactly twice (the copies must come together in the sorted array A) then the relation RM is symmetric. Sorting takes time O(|M|). Checking is just one scan through A.

for each (a, b) ∈ M do A.push back((min(a, b), max(a, b))) sort(A) for i := 0 to |M| / 2 − 1 if A[2i] 6 = A[2i + 1] then return false return true

(^1) Binary relation R on a set X is a subset of the Cartesian product X × X. (^2) A relation R is symmetric if and only if ∀(a, b) ∈ R : (b, a) ∈ R.

Problem 2: (Maximum flow with node capacities, 8 points)

Explain how to solve a maximum flow problem with node capacities, i.e., besides edge capacities there is also a bound cap(v) on the flow through a node v. Do not forget to give a (short) argument why your algorithm actually computes a feasible and optimal solution.

Suppose G = (V, E) is the original graph. We define a new graph G′^ = (V ′, E′). For each vertex v in V , there are two vertices vin and vout in V ′. There is an edge (vin, vout) ∈ E′ with capacity cap(v). Furthermore, an edge (u, v) ∈ E is translated into an edge (uout, vin) in E′. Note that there is a one-to-one correspondence between feasible flows from sin to tout in G′^ and s-t flows that respect node capacities in G′, i.e., flows in G that respect node capacities can be translated into feasible flows in G′^ and vice versa in the obvious way. Hence, it suffices to find an ordinary maximum flow f ′^ in G′^ and to translate it into a flow f that respects node capacities in G. This flow is optimal: Assume f were not optimal. Then must be a flow f ∗^ that respects node capacities with higher value in G which corresponds to a flow with higher value than f ′^ in G′. This contradicts the optimality of f ′^ in G.

(Maximale Fl¨usse mit Knotenkapazit¨aten, 8 Punkte)

Erkl¨aren Sie wie maximale Fl¨usse mit Knotenkapazit¨aten berechnet werden k¨onnen, d.h., neben Kantenkapazit¨aten gibt es auch eine Schranke cap(v) f¨ur den Fluss durch einen Knoten v. Vergessen Sie nicht, kurz zu begr¨unden, warum Ihr Algorithmus einen zul¨assigen und optimalen Fluss berechnet.

(K¨urzeste Wege in azyklischen Graphen, 8 Punkte)

Skizzieren Sie einen Algorithmus f¨ur “single source shortest paths” in ungerichteten kreisfreien Graphen mit nichtnegativen Kantengewichten. Der Algorithmus soll in Zeit O(m + n) arbeiten.

Problem 4: (Short questions, 6 × 3 points)

Give true/false/choice answers and short explanations. For each correct true/false/choice answer there is 1 point, for a correct explanation we give another 2 points.

(a) How fast can one sort n integers in the range 0... n^4 − 1 using algorithms we have seen in this course: O(n), O(n log log n), O(n log n). Give the best possible bound. Shortly explain what algorithm to use and how. O(n): use LSD radix sort with radix n.

(b) Suppose you want to simplify the data structure of suffix trees^5 by defining edge labels to be just single characters (i.e. an edge with label “c 1 c 2 · · · ck” is replaced with the chain of k edges with labels “c 1 ”,”c 2 ”,· · ·,”ck”). What would be the worst space consumption of this data structure for strings with the length n: Θ(n), Θ(n log n), Θ(n^2 ) or Θ(n^3 )? (You may assume that there is no restriction on the alphabet size.) Θ(n^2 ), e.g., 〈 1 , 2 ,... , n〉

(c) If graph G is acyclic then for all feasible flows the residual graph Gf is acyclic too. False. For example, consider the graph G = ({s, u, v, t} , {(s, u), (s, v), (u, t), (v, t)}), assume unit capacities, and consider a flow of value one along the path 〈s, u, t〉.

(d) If |E| ≥ |V | − 1 then the undirected graph G = (V, E) is connected. No. For example, consider a graph consisting of a three-cycle 〈s, u, v, s〉 and an isolated vertex x.

(e) Let G be a directed weighted graph, and let u, v be two vertices. Then a shortest path from u to v remains a shortest path when 1 is added to every edge weight. False. For example, consider E = {(s, v), (v, t), (s, t)} where (s, t) has weight two and the other edges have weight one. After adding one to every edge weight, the shortest path from s to t changes from 〈s, v, t〉 to 〈s, t〉.

(f) For every text length n and pattern length m, give an instance of the string matching problem where the naive algorithm needs time Ω (nm) although there is no match. (3 points for a correct answer) text: an, pattern: am−^1 b.

(^5) Recall, that in a suffix trees the edge labels on a path from the root to a leaf represent suffixes and that the labels of edges (v, u) and (v, w) should differ in their first character.

Problem 5: (vEB remove, 18 points)

A vEB data structure maintains a set M ⊆ 0... 2 A^ − 1 for some constant A. It supports the following operations in time O(log A):

  • insert(x): M:= M ∪ {x}
  • remove(x): M:= M{x}
  • locate(x): returns min{y ∈ M|y ≥ x}

Recursive description for A-bit vEB tree (A is assumed to be a power of two and special case treatments for the case A = 1 are ignored):

  • |M| = 1, store members:
    • e stores the single element, M = {e}
    • size = |M| = 1
  • |M| > 1, store members:
    • size = |M|
    • K = A/ 2
    • minM = minx∈M (x)
    • maxM = maxx∈M (x)
    • t stores {x div 2K^ | x ∈ M}
    • ri stores {x mod 2K^ | x ∈ M and x div 2K^ = i}

Example of a vEB tree operation:

Procedure insert(x) if size = 1 then // M = {e} minM := maxM := e create t with the single element e div 2 K create r[e div 2 K^ ] with the single element e mod 2 K if r[x div 2 K^ ] = ∅ then create r[x div 2 K^ ] with the single element x mod 2 K t.insert(x div 2 K^ ) else r[x div 2 K^ ].insert(x mod 2 K^ ) minM := min(minM, x) maxM := max(maxM, x) size := size + 1

Implement the operation remove(x) for the case that x ∈ M. Prove that your implementation runs in time O(log A). Hint: be careful about the cost of recursive calls. There is also no need to use locate.

Procedure remove(x) if size = 1 then size := 0 return if size = 2 then // back to the trivial case if x = minM then e := minM := maxM else e := maxM:= minM t := ∅ // deallocation r[minM div 2 K^ ] := ∅ // deallocation r[maxM div 2 K^ ] := ∅ // deallocation size := 1 return if r[x div 2 K^ ].size = 1 then r[x div 2 K^ ] := ∅ // deallocation t.remove(x div 2 K^ ) else r[x div 2 K^ ].remove(x mod 2 K^ ) minM := 2 K^ · t.minM + r[t.minM].minM maxM := 2 K^ · t.maxM + r[t.maxM].maxM size := size − 1 return

There is at most one recursive call to an A/2-bit tree. Therefore the running time is bounded as T (A) = O(1) + T (A/2), which solves to O(log A).

(vEB remove, 18 Punkte)

Eine vEB Datenstruktur verwaltet eine Menge M ⊆ 0... 2 A^ − 1. A ist eine Konstante. Die folgenden Operationen mit Laufzeit O(log A) werden zur Verf¨ugung gestellt:

  • insert(x): M:= M ∪ {x}
  • remove(x): M:= M{x}
  • locate(x): returns min{y ∈ M|y ≥ x}

Hier ist eine rekursive Beschreibung f¨ur einen A-bit vEB tree (A wird als Zweierpotenz angenommen und Sonderfallbehandlungen f¨ur den Fall A = 1 werden ignoriert.):

  • |M| = 1:
    • e speichert das einzige Element, M = {e}
    • size = |M| = 1
  • |M| > 1 speichere:
    • size = |M|
    • K = A/ 2
    • minM = minx∈M (x)
    • maxM = maxx∈M (x)
    • t speichert {x div 2K^ | x ∈ M}
    • ri speichert {x mod 2K^ | x ∈ M and x div 2K^ = i}

Beispiel f¨ur eine vEB-tree-Operation:

Procedure insert(x) if size = 1 then // M = {e} minM := maxM := e create t mit dem einzigen Element e div 2 K create r[e div 2 K^ ] mit dem einzigen Element e mod 2 K if r[x div 2 K^ ] = ∅ then create r[x div 2 K^ ] mit dem einzigen Element x mod 2 K t.insert(x div 2 K^ ) else r[x div 2 K^ ].insert(x mod 2 K^ ) minM := min(minM, x) maxM := max(maxM, x) size := size + 1

Realisieren Sie die Operation remove(x) f¨ur den Fall, dass x ∈ M. Zeigen Sie, dass Ihre Implementierung in Zeit O(log A) arbeitet. Hinweis: Seien Sie vorsichtig bei den Kosten rekursiver Aufrufe. Es besteht auch keine Notwendigkeit, locate zu benutzen.