15–210: Parallel and Sequential Data Structures and Algorithms, Lecture notes of Algorithms and Programming

Practice Final. December 2016. • Verify: There are 18 pages in this examination, comprising 8 questions worth a total of 152 points.

Typology: Lecture notes

2022/2023

Uploaded on 05/11/2023

freddye
freddye 🇺🇸

4.3

(11)

235 documents

1 / 18

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Full Name:
Andrew ID: Section:
15–210: Parallel and Sequential Data Structures and Algorithms
Practice Final
December 2016
Verify: There are 18 pages in this examination, comprising 8 questions worth a total of 152
points. The last 2 pages are an appendix with costs of sequence, set and table operations.
Time: You have 180 minutes to complete this examination.
Goes without saying: Please answer all questions in the space provided with the question.
Clearly indicate your answers.
Beware: You may refer to your two¡ double-sided 81
2×11in sheet of paper with notes, but
to no other person or source, during the examination.
Primitives: In your algorithms you can use any of the primitives that we have covered in
the lecture. A reasonably comprehensive list is provided at the end.
Code: When writing your algorithms, you can use ML syntax but you don’t have to. You
can use the pseudocode notation used in the notes or in class. For example you can use the
syntax that you have learned in class. In fact, in the questions, we use the pseudo rather
than the ML notation.
Sections
A9:30am - 10:20am Andra/Charlie
B10:30am - 11:20am Andres/Emma
C12:30pm - 1:20pm Anatol/John
D12:30pm - 1:20pm Aashir/Ashwin
E1:30pm - 2:20pm Nathaniel/Sonya
F3:30pm - 4:20pm Teddy/Vivek
G4:30pm - 5:20pm Alex/Patrick
15–210 Practice Final 1 of 18 December 2016
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12

Partial preview of the text

Download 15–210: Parallel and Sequential Data Structures and Algorithms and more Lecture notes Algorithms and Programming in PDF only on Docsity!

Full Name:

Andrew ID: Section:

15–210: Parallel and Sequential Data Structures and Algorithms

Practice Final

December 2016

  • Verify: There are 18 pages in this examination, comprising 8 questions worth a total of 152 points. The last 2 pages are an appendix with costs of sequence, set and table operations.
  • Time: You have 180 minutes to complete this examination.
  • Goes without saying: Please answer all questions in the space provided with the question. Clearly indicate your answers.
  • Beware: You may refer to your two¡ double-sided 8 12 × 11in sheet of paper with notes, but to no other person or source, during the examination.
  • Primitives: In your algorithms you can use any of the primitives that we have covered in the lecture. A reasonably comprehensive list is provided at the end.
  • Code: When writing your algorithms, you can use ML syntax but you don’t have to. You can use the pseudocode notation used in the notes or in class. For example you can use the syntax that you have learned in class. In fact, in the questions, we use the pseudo rather than the ML notation.

Sections

A 9:30am - 10:20am Andra/Charlie B 10:30am - 11:20am Andres/Emma C 12:30pm - 1:20pm Anatol/John D 12:30pm - 1:20pm Aashir/Ashwin E 1:30pm - 2:20pm Nathaniel/Sonya F 3:30pm - 4:20pm Teddy/Vivek G 4:30pm - 5:20pm Alex/Patrick

Full Name: Andrew ID:

 - Binary Answers Question Points Score - Costs - Short Answers 
  • Slightly Longer Answers - Neighborhoods - Median ADT
    • Geometric Coverage
  • Swap with Compare-and-Swap - Total:

Question 2: Costs (12 points)

(a) (6 points) Give tight assymptotic bounds (Θ) for the following recurrence using the tree method. Show your work. W (n) = 2W (n/2) + n log n

(b) (6 points) Check the appropriate column for each row in the following table:

root dominated leaf dominated balanced W (n) = 2W (n/2) + n^1.^5 W (n) =

nW (

n) +

n W (n) = 8W (n/2) + n^2

Question 3: Short Answers (26 points)

Answer each of the following questions in the spaces provided. (a) (3 points) What simple formula defines the parallelism of an algorithm (in terms of work and span)?

(b) (3 points) Name two algorithms we covered in this course that use the greedy method.

(c) (3 points) Given a sequence of key-value pairs A, what does the following code do? Table.map Seq.length (Table.collect A)

(d) (5 points) Consider an undirected graph G with unique positive weights. Suppose it has a minimum spanning tree T. If we square all the edge weights and compute the MST again, do we still get the same tree structure? Explain briefly.

(e) (3 points) What asymptotically efficient parallel algorithm/technique can one use to count the number of trees in a forest (tree and forest have their graph-theoretical meaning)? (Hint: the ancient saying of “can’t see forest from the trees” may or may not be of help.) Give the work and span for your proposed algorithm.

(f) (3 points) What are the two ordering invariants of a Treap? (Describe them briefly.)

(g) (6 points) Is it the case that in a leftist heap the left subtree of a node is always larger than the right subtree. If so, argue why (briefly). If not, give an example.

Question 5: Neighborhoods (20 points)

Suppose that you are given a weighted, directed graph G representing the road network in a city. Your mission is to develop a “walking paths” algorithm that may not always return the shortest paths but will return a path between two points of interest that is enjoyable to walk. To this end, suppose that the graph G is labeled with its neighborhood. For example, a vertex representing the Gates building may have an “oakland” label. In G, a walking path from a source in a neighborhood to another vertex in the same neighbor- hood is defined as the shortest path that never leaves that neighborhood—all the vertices on the shortest path are in the neighborhood. Throughout assume that G contain no negative edges. Use n for the number of vertices in the graph and m for the number of edges. (a) (5 points) Describe how to modify Dijkstra’s algorithm so that it calculates in H walking paths from a source to all the other vertices in the same neighborhood.

(b) (5 points) What is the work and span of your algorithm? Give a tight bound. Define any extra variables that you may use, if any.

W ork =

Span =

(c) For this part, assume that you live in a city that is planned to be walkable. Specifically, the city consists of a single center vertex c with k outgoing edges/streets each of which connects with one of k neighborhoods with n 1... nk vertices and m 1... mk edges respec- tively. Furthermore, you can walk on a street in either direction, i.e., each edge has a corresponding reverse edge with the same weight. The graph below illustrates an example with k = 5, where G 1... Gk represents the neighborhoods.

G

G

c

G

G

1 G 3

2 5

4

Give a parallel algorithm for the SSSP (single-source shortest paths) problem that given a source s finds the shortest paths to all vertices in the graph. Your algorithm should take advantage of the special topology of your city. You are not allowed to use Bellman-Ford because it will likely perform too much work and it still has a relatively large span. i. (5 points) Describe your algorithm. Let Gs denote the neighborhood for the source s.

ii. (5 points) What is the work and span of your algorithm

W ork =

Span =

Question 7: Geometric Coverage (12 points)

For points p 1 , p 2 ∈ R^2 , we say that p 1 = (x 1 , y 1 ) covers p 2 = (x 2 , y 2 ) if x 1 ≥ x 2 and y 1 ≥ y 2. Given a set S ⊆ R^2 , the geometric cover number of a point q ∈ R^2 is the number of points in S that q covers. Notice that by definition, every point covers itself, so its cover number must be at least 1. In this problem, we’ll compute the geometric cover number for every point in a given sequence. More precisely:

Input: a sequence S = 〈s 1 ,... , sn〉, where each si ∈ R^2 is a 2-d point. Output: a sequence of pairs each consististing of a point and its cover number. Each point must appear exactly once, but the points can be in any order.

Assume that we use the ArraySequence implementation for sequences.

(a) (4 points) Develop a brute-force solution gcnBasic (in pseudocode or Standard ML). De- spite being a brute-force solution, your solution should not do more work than O(n^2 ).

(b) (4 points) In words, outline an algorithm gcnImproved that has O(n log n) work. You may assume an implementation of OrderedTable in which split, join, and insert have O(log n) cost (i.e., work and span), and size and empty have O(1) cost.

(c) (4 points) Show that the work bound cannot be further improved by giving a lower bound for the problem.

(b) (10 points) Does your algorithm suffer from the ABA problem? If so, explain how it does, and whether the problem affects the correctness of your algorithm. If so, then can you describe briefly a way to fix the problem (no pseudo-code needed)?

Appendix: Library Functions

signature SEQUENCE = sig type ’a t type ’a seq = ’a t type ’a ord = ’a * ’a -> order datatype ’a listview = NIL | CONS of ’a * ’a seq datatype ’a treeview = EMPTY | ONE of ’a | PAIR of ’a seq * ’a seq

exception Range exception Size

val nth : ’a seq -> int -> ’a val length : ’a seq -> int val toList : ’a seq -> ’a list val toString : (’a -> string) -> ’a seq -> string val equal : (’a * ’a -> bool) -> ’a seq * ’a seq -> bool

val empty : unit -> ’a seq val singleton : ’a -> ’a seq val tabulate : (int -> ’a) -> int -> ’a seq val fromList : ’a list -> ’a seq

val rev : ’a seq -> ’a seq val append : ’a seq * ’a seq -> ’a seq val flatten : ’a seq seq -> ’a seq

val filter : (’a -> bool) -> ’a seq -> ’a seq val map : (’a -> ’b) -> ’a seq -> ’b seq val zip : ’a seq * ’b seq -> (’a * ’b) seq val zipWith : (’a * ’b -> ’c) -> ’a seq * ’b seq -> ’c seq val enum : ’a seq -> (int * ’a) seq val filterIdx : (int * ’a -> bool) -> ’a seq -> ’a seq val mapIdx : (int * ’a -> ’b) -> ’a seq -> ’b seq val update : ’a seq * (int * ’a) -> ’a seq val inject : ’a seq * (int * ’a) seq -> ’a seq

val subseq : ’a seq -> int * int -> ’a seq val take : ’a seq -> int -> ’a seq val drop : ’a seq -> int -> ’a seq val splitHead : ’a seq -> ’a listview val splitMid : ’a seq -> ’a treeview

val iterate : (’b * ’a -> ’b) -> ’b -> ’a seq -> ’b val iteratePrefixes : (’b * ’a -> ’b) -> ’b -> ’a seq -> ’b seq * ’b val iteratePrefixesIncl : (’b * ’a -> ’b) -> ’b -> ’a seq -> ’b seq val reduce : (’a * ’a -> ’a) -> ’a -> ’a seq -> ’a val scan : (’a * ’a -> ’a) -> ’a -> ’a seq -> ’a seq * ’a val scanIncl : (’a * ’a -> ’a) -> ’a -> ’a seq -> ’a seq

val sort : ’a ord -> ’a seq -> ’a seq val merge : ’a ord -> ’a seq * ’a seq -> ’a seq val collect : ’a ord -> (’a * ’b) seq -> (’a * ’b seq) seq

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

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

type t type set = t

val size : set -> int val toString : set -> string val toSeq : set -> Key.t Seq.t

val empty : unit -> set val singleton : Key.t -> set val fromSeq : Key.t Seq.t -> set

val find : set -> Key.t -> bool val insert : set * Key.t -> set val delete : set * Key.t -> set

val filter : (Key.t -> bool) -> set -> set

val reduceKey : (Key.t * Key.t -> Key.t) -> Key.t -> set -> Key.t val iterateKey : (’a * Key.t -> ’a) -> ’a -> set -> ’a

val union : set * set -> set val intersection : set * set -> set val difference : set * set -> set

val $ : Key.t -> set end