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

Contain Cycles - Data Structures - Exam, Exams of Data Structures and Algorithms

Main points of this exam paper are: Contain Cycles, Maximum Size, Approximation, Algorithm Outputs, Contain Cycles, Optimal Solution, Gerichteter 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
Final Exam
Algorithms and Data Structures WS03/04
Name:
Matr.–Nr.:
Time: 120 min
Aufgabe 12345
Maximal-
punktzahl 9 12 10 8 21
erreichte
Punktzahl
Σ: von 60
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Contain Cycles - Data Structures - Exam and more Exams Data Structures and Algorithms in PDF only on Docsity!

Final Exam

Algorithms and Data Structures WS03/

Name:

Matr.–Nr.:

Time: 120 min

Aufgabe (^1 2 3 4 )

Maximal- punktzahl

erreichte Punktzahl

Σ: von 60

Problem 1: (Acyclic Subgraphs, 5+4 points)

In the Acyclic Subgraph Problem, we are given a directed graph G = (V, E) and we are asked to pick the maximum size subset E′^ of E such that G′^ = (V, E′) has no cycles. Consider the following algorithm: Without loss of generality assume that V = { 1 ,... , n}. Let S 1 = {(u, v) ∈ E : u < v} and S 2 = {(u, v) ∈ E : u > v}. The output is the larger of the two sets S 1 and S 2.

  1. Show that neither S 1 nor S 2 contain cycles.
  2. Show that the algorithm outputs a 0.5 approximation of the acyclic subgraph problem.

Edges in S 1 do not form cycles: to close a cycle that starts at node u we need to traverse an edge (w, u) such that w > u, this edge does not belong to S 1. Analogously one can show that S 2 does not have cycles. Among S 1 and S 2 we choose the set with larger cardinality. Let’s denote it S, and let Sopt be the set of edges in the optimal solution, then:

|S| = max(|S 1 |, |S 2 |) ≥

(|S 1 | + |S 2 |) =

|E|

|Sopt| 2 Therefore set S is a 0.5-factor approximation of the optimal solution.

Problem 2: (Maximum weighted matchings, 5 + 4 + 1 + 2 points)

You are given a path P = 〈v 1 , v 2 , v 3 ,... , vn〉. Edge ei = (vi, vi+1) has weight wi. The problem is to find a subset M of edges in P that forms a matching^1 and has maximum weight W =

ei∈M wi^ over all possible matchings. Provide a dynamic programming algorithm for computing the weight of such a matching. The algorithm must run in linear time.

(a) List the table(s) your algorithm will use, and explain the meaning of each entry.

(b) Specify the recurrences and the base case(s) used by your algorithm. (No correctness proof required)

(c) Give pseudocode that fills the tables.

(d) Give a short argument why your algorithm runs in linear time. (^1) A matching M in a graph G = (V, E) is a subset of the edge set E such that (V, M ) is a graph of maximum degree one.

(a) Here we also compute the matching, however it was not required for the exam. Table entry i of table W stores the weight of a maximum matching for the path 〈v 1 , v 2 ,... , vi〉. Entry j of table M stores a pair of pointers (sub, e), where sub is a pointer to another entry in M that describes a part of the solution for the path 〈v 1 , v 2 ,... , vi〉, and e is the last edge that completes the solution. The matching stored in M[j] can be obtained combining edges of sub and edge e. e might be nil if the solution for the path 〈v 1 , v 2 ,... , vi〉 is the same as the solution pointed by sub.

(b)

W (i) =

0 if i = 1 w 1 if i = 2 max(wi + W (i − 2), W (i − 1)) otherwise

M(i) =

(nil, nil) if i = 1 (nil, e 1 ) if i = 2 (M(i − 2), ei− 1 ) if (i > 2) ∧ (wi + W (i − 2) > W (i − 1)) (M(i − 1), nil) otherwise

(c)

W is array of numbers M is array of pairs (pointer,edge) M[0] := (0, nil) W [0] := 0 M[1] := (0, e 1 ) W [1] := w 1 for i := 3 to n if wi + W [i − 2] > W [i − 1] M[i] := (pointer to(M[i − 2]), ei− 1 ) W [i] := wi + W [i − 2] else M[i] := (pointer to(M[i − 1]), nil) W [i] := W [i − 1] print W [n] print edges in M[n]

(d) Each iteration of the loop performs a constant number of operations. Printing out edges of the matching takes O(n). Therefore the running time of the algorithm is O(n).

Problem 3: (Closest and farthest points, 4 + 6 points)

(a) Let P be a set of n points in the plane^3. Sketch an algorithm that finds the two closest input points in expected time O(n log n). Explain why your algorithm is correct and achieves the claimed run time.

(b) Consider the opposite problem: two points in P (call them pi, pj ) that are farthest apart. We are not interested in the points pi, pj but we want to estimate the value distance(pi, pj ) which is also called diameter(P ). Give an O(n) time algorithm (again, in the expected sense) that returns a value d which lies between diameter(P )/c and diameter(P ) for some constant c ≥ 1. Explain why your algorithm is correct and achieves the claimed run time.

(a) It is easy to show that the two closest points have to be adjacent in the Delaunay triangulation (or in the Euclidean MST). So, compute Del(P ) and take the minimum weighted edge. Del(P ) runs in O(n log n) expected time and since Del(P ) is a planar graph, the number of edges is O(n), so determining the minimum weighted edge is O(n) time more.

(b) There are many ways to determine a value d which lies between diameter(P )/c and diameter(P ) for some constant c ≥ 1. We could compute the smallest enclosing disk of P. The radius r of the smallest enclosing disk is a value which lies between diameter(P )/2 and diameter(P ). Let q be the center of the smallest enclosing disk D of P. Some point of P , call it p, has to lie on the boundary of this disk (otherwise, the disk can be made smaller). Take the disk D′^ with center as p and the same radius r. Some point of P has to lie outside D′, otherwise we have a smaller disk C that encloses all the points of P. (See the figure.)

p q

C

D’ D

Hence, the distance between p and its farthest point is at least r and since all points of P are enclosed in D, the maximum distance between any two points of P is at most 2r. Hence,

r ≤ diameter(P ) ≤ 2 r.

So, the radius r of the smallest enclosing disk lies between diameter(P )/2 and diameter(P ).

(^3) You can assume that no four points in P lie on the same circle.

(N¨achste und weiteste Punkte, 4 + 6 Punkte)

(a) Sei P eine Menge von n Punkten in der Ebene^4. Skizzieren Sie einen Algorithmus, der in erwarteter Zeit O(n log n) zwei Punkte findet, die n¨aher beieinander liegen als alle anderen Paare von Punkten in P. Begr¨unden Sie warum Ihr Algorithmus korrekt ist und die verlangte Laufzeit erreicht.

(b) Betrachten Sie das entgegengesetzte Problem: Zwei Punkte pi und pj in P , die m¨oglichst weit auseinander liegen. Wir interessieren uns nicht f¨ur die Punkte, sondern wir m¨ochten den Abstand distance(pi, pj ) absch¨atzen, der auch als diameter(P ) bezeichnet wird. Skizzieren Sie einen Algorithmus f¨ur dieses Problem, der in erwarteter Zeit O(n) l¨auft und einen Wert d liefert, der zwischen diameter(P )/c und diameter(P ) liegt f¨ur eine Konstante c ≥ 1. Begr¨unden Sie warum Ihr Algorithmus korrekt ist und die verlangte Laufzeit erreicht.

(^4) Sie k¨onnen annehmen, dass keine vier Punte in P auf einem gemeinsamen Kreis liegen.

(Konvexe H¨ulle, 8 Punkte)

Sei P eine Menge von n Punkten in der Ebene mit ganzzahligen Koordinaten im Bereich 1 bis n^2. Zeigen Sie, dass die konvexe H¨ulle von P in linearer Zeit bestimmt werden kann. (Mehrere Punkte k¨onnen die gleiche x- oder y-Koordinate haben.)

Problem 5: (Short questions, 7 × 3 points)

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

(a) (True/False) It can be decided in polynomial time whether a graph is 2-colorable, i.e., whether there is a mapping c : V → { 0 , 1 } such that ∀(u, v) ∈ E : c(u) 6 = c(v). True: Find any spanning forest of G. If G is two-colorable then (up to interchanging 0 and 1) there is a unique coloring for each tree. It only remains to check whether the remaining edges are compatible with this coloring. All of this can be done in linear time. (One can even devise a single pass DFS algorithm.)

(b) (True/False) The minimum traveling salesman tour changes when we add a constant K to the weight of each edge. False: Since all tours contain exactly n edges we change the weight of every tour by the same value of Kn.

(c) (True/False) The minimum traveling salesman tour changes when we multiply the weight of each edge by a positive constant K. False: The weight of every tour is simply multiplied by a factor of K.

(d) Let P be a set of n points in the plane^5. The furthest point Voronoi region of a point p ∈ P (call it Fp) consists of all points at least as far from p as from any other point in P. (True/False): Fp 6 = ∅ implies p has to be a vertex of the convex hull of P.

True. Any point p that is not a vertex of CH(P ) either lies in the interior of a segment ab or in the interior of a triangle abc, where a, b, c are points in P. It is easy to check that the distance from any arbitrary point q in the plane to p is strictly less than max{dist(q, a), dist(q, b)} or max{dist(q, a), dist(q, b), dist(q, c)}, as the case may be. So, p can never be the furthest point among points in P to any point in the plane.

(^5) You can assume that no 4 points in P lie on the same circle and that no 3 points in P lie on the

same line.

(Kurze Fragen, 7 × 3 Punkte)

F¨ur jede korrekte wahr/falsch Antwort gibt es einen Punkt. Es gibt zwei weitere Punkte f¨ur eine korrekte Begr¨undung.

(a) (wahr/falsch) Es l¨asst sich in polynomieller Zeit entscheiden ob ein Graph 2-f¨arbar ist, d.h ob es eine Abbildung c : V → { 0 , 1 } gibt, so dass ∀(u, v) ∈ E : c(u) 6 = c(v).

(b) (wahr/falsch) Die minimale Traveling-Salesman-Tour ¨andert sich, wenn eine Konstante K zu jedem Kantengewicht addiert wird.

(c) (wahr/falsch) Die minimale Traveling-Salesman-Tour ¨andert sich, wenn jedes Kantengewicht mit einer positiven Konstante K multipliziert wird.

(d) Sei P eine Menge von Punkten in der Ebene.^6 Das Weiteste-Punkt-Voronoi-Gebiet eines Punktes p ∈ P (genannt Fp) besteht aus allen Punkten, die mindestens so weit von p entfernt sind wie von irgend einem anderen Punkt in P. (wahr/falsch): Aus Fp 6 = ∅ folgt, dass p ein Knoten der konvexen H¨ulle von P sein muss.

(^6) Sie k¨onnen annehmen, dass keine 4 Punkte in P auf einem Kreis liegen und keine 3 Punkte in P auf

einer Geraden liegen.

(e) (wahr/falsch) Ein l¨angster Pfad in einem gerichteten azyklischen graph G = (V, E) mit nichtnegativen Kantengewichten l¨asst sich in Zeit O(|V | + |E|) finden.

(f) (wahr/falsch) Sei G ein gerichteter Graph mit einem Ausgangsknoten, einem Zielknoten und Kantenkapazit¨aten. Sei ferner k eine positive Zahl. Dann gibt es einen Fluss der Gr¨oße k durch G dann und nur dann, wenn die Summe der Kapazit¨aten von Kanten aus dem Quellknoten mindestens k betr¨agt und die Summe der Kapazit¨aten von Kanten in den Zielknoten mindestens k betr¨agt.

(g) (wahr/falsch) Gegeben sei ein zusammenh¨angender, ungerichteter Graph G mit verschiedenen Kantengewichten, dann geh¨ort die Kante mit dem zweitkleinsten Kantengewicht zum minimalen Spannbaum.