Docsity

Practice Exam II for Parallel and Sequential Data Structures and Algorithms, Exams of Data Structures and Algorithms

Toggle sidebar
bg1
Full Name:
Andrew ID: Section:
15–210: Parallel and Sequential Data Structures and Algorithms
Practice Exam II (Solutions)
April 2016
•There are 16 pages in this examination, comprising 6 questions worth a total of 118 points.
The last few pages are an appendix with costs of sequence, set and table operations.
•You have 80 minutes to complete this examination.
•Please answer all questions in the space provided with the question. Clearly indicate your
answers.
•You may refer to your one double-sided 81
2×11in sheet of paper with notes, but to no other
person or source, during the examination.
Circle the section YOU ATTEND
Sections
A9:30am - 10:20am Edward/Angie
B10:30am - 11:20am Jake/Narain
C12:30pm - 1:20pm Sonya/Anisha
D12:30pm - 1:20pm Nick/Yongshan
E1:30pm - 2:20pm William/Bryan
F1:30pm - 2:20pm Sam S./Yutong
G3:30pm - 4:20pm Howard/Yongshan
15–210 Practice Exam II 1 of 16 April 2016
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Practice Exam II for Parallel and Sequential Data Structures and Algorithms and more Exams Data Structures and Algorithms in PDF only on Docsity!

Full Name:

Andrew ID: Section:

15–210: Parallel and Sequential Data Structures and Algorithms

Practice Exam II (Solutions)

April 2016

  • There are 16 pages in this examination, comprising 6 questions worth a total of 118 points. The last few pages are an appendix with costs of sequence, set and table operations.
  • You have 80 minutes to complete this examination.
  • Please answer all questions in the space provided with the question. Clearly indicate your answers.
  • You may refer to your one double-sided 8 12 × 11in sheet of paper with notes, but to no other person or source, during the examination.

Circle the section YOU ATTEND

Sections

A 9:30am - 10:20am Edward/Angie B 10:30am - 11:20am Jake/Narain C 12:30pm - 1:20pm Sonya/Anisha D 12:30pm - 1:20pm Nick/Yongshan E 1:30pm - 2:20pm William/Bryan F 1:30pm - 2:20pm Sam S./Yutong G 3:30pm - 4:20pm Howard/Yongshan

Full Name: Andrew ID:

 - Short Answers Question Points Score - Dijkstra and A∗ - (Shortest Paths) Wormholes 
  • Strongly Connected Components
  • (Dynamic Programming) Pipe Cutting - MST and Tree Contraction - Total:

(d) (5 points) Given a graph with integer edge weights between 1 and 5 (inclusive), you want to find the shortest weighted path between a pair of vertices. How would you reduce this problem to the shortest unweighted path problem, which can be solved using BFS?

Solution: Replace each edge with weight i with a simple path of i edges each with weight 1. Then solve with BFS.

(e) (5 points) Recall the implementation of DFS shown in class using the discover and finish functions. Circle the correct answer for each of the following statements, assuming DFS starts at A: A B

C D

E

discover D could be called before discover E: True False discover E could be called before discover D: True False discover D could be called before discover C: True False finish A could be called before finish B: True False finish D could be called before discover B: True False

Solution: True, True, True, False, True

(f) (6 points) Circle every type of graph listed below for which star contraction will reduce the number of edges by a constant factor in expectation in every round until fully reduced (and hence imply O(|E|) total work). You can assume redundant edges between vertices are removed.

(a) a graph in which all vertices have degree at most 2 (b) a graph in which all vertices have degree at most 3 (c) a graph in which all vertices have degree

|V |

(d) a graph containing a single cycle (i.e. a forest with one additional edge) (e) the complete graph (i.e. an edge between every pair of vertices) (f) any graph (still circle others if relevant)

Solution: a, d, e

Question 2: Dijkstra and A∗^ (15 points)

(a) (6 points) Consider the graph shown below, where the edge weights appear next to the edges and the heuristic distances to vertex G are in parenthesis next to the vertices.

B" 4"

C"

D"

E"

F"

G"

A"

1"

3"

2"

2"

3"

2"

10"

3"

i. Show the order in which vertices are visited by Dijkstra when the source vertex is A.

Solution: A C B E F D G

ii. Show an order in which vertices are visited by A∗^ when the source vertex is A and the destination vertex is G.

Solution: A C F G

(b) (4 points) What is the key reason you would choose to use A∗^ instead of Dijkstra’s algo- rithm?

Solution: You can use A∗^ if you want the shortest path to only a single goal vertex, and not all shortest paths. A∗^ can be much more efficient, as it tries to move toward the goal more directly, skipping many more vertices.

(c) (5 points) Show a 3-vertex example of a graph on which Dijkstra’s algorithm always fails. Please clearly identify which vertex is the source.

Solution: A /
x=4 / \ y=-2 x+y < z < x guarantees failure / \ x+y < z <= x may fail depending on the input order S ------- B z=

Question 4: Strongly Connected Components (20 points)

In this question, you will write 2 functions on directed graphs. We assume that graphs are represented as:

type graph = vertexSet vertexTable

with key comparisons taking O(1) work. (a) (10 points) Given a directed graph G = (V, E), its transpose GT^ is another directed graph on the same vertices, with every edge flipped. More formally, GT^ = (V, E′), where

E′^ = {(b, a) | (a, b) ∈ E}.

Here is a skeleton of an SML definition for transpose that computes the transpose of a graph. Fill in the blanks to complete the implementation. Your implementation must have O(|E| log |V |) work and O(log^2 |V |) span.

fun transpose (G : graph) : graph = let val S = vertexTable.toSeq(G) (* returns (vertex*vertexSet) seq *)

fun flip(u,nbrs) = Seq.map (fn v => (v,u)) (vertexSet.toSeq nbrs) val ET = Seq.flatten(Seq.map flip S)

val T = vertexTable. collect ET in vertexTable.map vertexSet.fromSeq T end

(b) (10 points) A strongly connected component of a directed graph G = (V, E) is a subset S of V such that every vertex u ∈ S can reach every other vertex v ∈ S (i.e., there is a directed path from u to v), and such that no other vertex in V can be added to S without violating this condition. Every vertex belongs to exactly one strongly connected component in a graph. Implement the function: val scc : graph * vertex -> vertexSet such that scc(G,v) returns the strongly connected component containing v. You may assume the existence of a function: val reachable : graph * vertex -> vertexSet such that reachable(G,v) returns all the vertices reachable from v in G. Not including the cost of reachable, your algorithm must have O(|E| log |V |) work and O(log^2 |V |) span. You might find transpose useful and can assume the given time bounds.

fun scc (G : graph, v : vertex) : vertexSet =

vertexSet.intersection(reachable(G,v),

reachable(transpose G,v))

Question 6: MST and Tree Contraction (25 points)

In SegmentLab, you implemented Bor˚uvka’s algorithm that interleaved star contractions and finding minimum weight edges. In this question you will analyze Bor˚uvka’s algorithm more carefully. We’ll assume throughout this problem that the edges are undirected, and each edge is labeled with a unique identifier (`). The weights of the edges do not need to be unique, and m = |E| and n = |V |.

1 % returns the set of edges in the minimum spanning tree of G 2 function MST (G = (V, E)) = 3 if |E| = 0 then {} 4 else let 5 val F = {min weight edge incident on v : v ∈ V } 6 val (V ′, P ) =contract each tree in the forest (V, F ) to a single vertex 7 V ′^ = remaining vertices 8 P = mapping from each v ∈ V to its representative in V ′ 9 val E′^ = {(Pu, Pv, ) : (u, v,) ∈ E | Pu 6 = Pv} 10 in 11 MST (G′^ = (V ′, E′)) ∪ {: (u, v,) ∈ F } 12 end

(a) (4 points) Show an example graph with 4 vertices in which F will not include all the edges of the MST.

Solution: 3 o --- o 1 | | 2 o o

(b) (4 points) Prove that the set of edges F must be a forest (i.e. F has no cycle).

Solution: Answer 1: The MST does not have a cycle (it is a tree) and F is a subset of F so it can’t have a cycle. Answer 2: AFSOC that there is a cycle. Consider the maximum weight edge on the cycle. Neither of its endpoints will choose it since they both have lighter edges. Contradiction.

(c) (4 points) Suggest a technique to efficiently contract the forest in parallel. What is a tight asymptotic bound for the work and span of your contract, in terms of n? Explain briefly. Are these bounds worst case or expected case?

Solution: Use star contraction as described in class. Since in contraction a tree will always stay a tree, the number of edges must go down with the number of vertices. Therefore total work will be O(n) and span will be O(log^2 n) in expectation.

(d) (4 points) Argue that each recursive call to MST removes, in the worst case, at least half of the vertices; that is, |V ′| ≤ |V 2 |.

Solution: Every vertex will join at least one other vertex. Since edges have two directions, at least n/2 of them must be selected, which will remove at least n/ 2 vertices (n = |V |).

(e) (4 points) What is the maximum number of edges that could remain after one step (i.e. what is |E′|)? Explain briefly.

Solution: m − n/2 since at least n/2 edges are removed, as described in previous answer.

(f) (5 points) What is the expected work and span of the overall algorithm in terms of m and n? Explain briefly. You can assume that calculating F takes O(m) work and O(log n) span.

Solution: Since vertices go down by at least a factor of 1/2 on each round, there will be at most log n rounds. The cost of each round is dominated by calculating F , O(m) work and O(log n) span and the contraction of forests O(n) work and O(log^2 n) span. Multiplying the max of each of these by log n gives O(m log n) work and O(log^3 n) span.

val collate : ’a ord -> ’a seq ord val argmax : ’a ord -> ’a seq -> int

val $ : ’a -> ’a seq val % : ’a list -> ’a seq end

ArraySequence Work Span empty ()

O(1) O(1)

singleton a length s nth s i subseq s (i, len) tabulate f n if f (i) has Wi work and Si span O

(n− 1 ∑ i=

Wi

O

n− 1 max i= Si

map f s if f (s[i]) has Wi work and Si span, and |s| = n zipWith f (s, t) if f (s[i], t[i]) has Wi work and Si span, and min(|s|, |t|) = n reduce f b s if f does constant work and |s| = n scan f b s^ O(n)^ O(lg^ n) if f does constant work and |s| = n filter p s if p does constant work and |s| = n

flatten s O

(n− 1 ∑ i=

1 + |s[i]|

O(lg |s|)

sort cmp s if cmp does constant work and |s| = n

O(n lg n) O(lg^2 n)

merge cmp (s, t) if cmp does constant work, |s| = n, and |t| = m

O(m + n) O(lg(m + n))

append (s,t) if |s| = n, and |t| = m

O(m + n) O(1)

signature TABLE = sig structure Key : EQKEY structure Seq : SEQUENCE

type ’a t type ’a table = ’a t

structure Set : SET where Key = Key and Seq = Seq

val size : ’a table -> int val domain : ’a table -> Set.t val range : ’a table -> ’a Seq.t val toString : (’a -> string) -> ’a table -> string val toSeq : ’a table -> (Key.t * ’a) Seq.t

val find : ’a table -> Key.t -> ’a option val insert : ’a table * (Key.t * ’a) -> ’a table val insertWith : (’a * ’a -> ’a) -> ’a table * (Key.t * ’a) -> ’a table val delete : ’a table * Key.t -> ’a table

val empty : unit -> ’a table val singleton : Key.t * ’a -> ’a table val tabulate : (Key.t -> ’a) -> Set.t -> ’a table val collect : (Key.t * ’a) Seq.t -> ’a Seq.t table val fromSeq : (Key.t * ’a) Seq.t -> ’a table

val map : (’a -> ’b) -> ’a table -> ’b table val mapKey : (Key.t * ’a -> ’b) -> ’a table -> ’b table val filter : (’a -> bool) -> ’a table -> ’a table val filterKey : (Key.t * ’a -> bool) -> ’a table -> ’a table

val reduce : (’a * ’a -> ’a) -> ’a -> ’a table -> ’a val iterate : (’b * ’a -> ’b) -> ’b -> ’a table -> ’b val iteratePrefixes : (’b * ’a -> ’b) -> ’b -> ’a table -> (’b table * ’b)

val union : (’a * ’a -> ’a) -> (’a table * ’a table) -> ’a table val intersection : (’a * ’b -> ’c) -> (’a table * ’b table) -> ’c table val difference : ’a table * ’b table -> ’a table

val restrict : ’a table * Set.t -> ’a table val subtract : ’a table * Set.t -> ’a table

val $ : (Key.t * ’a) -> ’a table end

MkTreapTable Work Span size T O(1) O(1) filter f T ∑ (k 7 →v)∈T

W (f (v)) (^) lg |T | + max (k 7 →v)∈T

S(f (v)) map f T

tabulate f X

k∈X

W (f (k)) max k∈X S(f (k))

reduce f b T if f does constant work

O(|T |) O(lg |T |)

insertWith f (T ,(k,v)) if f does constant work (^) O(lg |T |) O(lg |T |) find T k delete (T ,k) domain T range T O(|T |) O(lg |T |) toSeq T collect S (^) O(|S| lg |S|) O(lg (^2) |S|) fromSeq S

For each argument pair (A, B) below, let n = max(|A|, |B|) and m = min(|A|, |B|).

MkTreapTable Work Span union f (X,Y )

O

m lg( n+mm )

O

lg(n + m)

intersection f (X,Y ) difference (X,Y ) restrict (T ,X) subtract (T ,X)