CS 251 Fall 2018 Final Exam

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 15

NAME:_______________________________

SIGNATURE:_______________________________

NetID:_______________________________

CS251, FALL 2018


Final Exam .
120 Minutes

120 Points

Please wait until everyone has their exam before opening!


Question 1 (15 points): Below and on the next two pages are three C++
functions operating on an arrays of length n. To the right of each
function are two partial statements regarding the function’s runtime.

Select and circle all options (if any) that result in a true statement
about the C function’s runtime.
Circle ALL options that result in
a true statement.

int weird(int a[], int n) { “The worst-case runtime of weird


int i, j; is…
int x=0; (A) ...𝑂(𝑂2 )”

for(i=0; i<n; i++) { (B) ...𝛩(𝑂2 )”


j = 1; (C) ...𝑂(𝑂)”
while(j < n) {
x += a[j]; (D) ...𝛩(𝑂 𝑂𝑂𝑂 𝑂)”
j = j*2; (E) ...𝛺(𝑂)”
}

} Circle ALL options that result in


return x; a true statement.
}
“The best-case runtime of weird
is…

(A) ...𝑂(1)”
(B) ...𝑂(𝑂)”
(C) ...𝛩(𝑂𝑂𝑂 𝑂)”
(D) ...𝛩(𝑂 𝑂𝑂𝑂 𝑂)”
(E) ...𝑂(𝑂2 )”

Circle ALL options that result


in a true statement.

void wild(int a[], int n, int z) { “The worst-case runtime of wild


int i, j, tmp; is…

i=0;
(A) ...𝑂(𝑂3 )”
j=n-1;
(B) ...𝛩(𝑂2 )”
while(i<j) {
while(i<j && a[i] <= z) (C) ...𝑂(𝑂 𝑂𝑂𝑂 𝑂)”
i++; (D) ...𝛩(𝑂)”
while(j>i && a[j] > z)
j--; (E) ...𝑂(𝑂𝑂𝑂 𝑂)”
if(j>i){
tmp = a[i];
a[i] = a[j]; Circle ALL options that result
a[j] = tmp; in a true statement.
}
} “The best-case runtime of wild
} is…

(A) ...𝑂(1)”
(B) ...𝑂(𝑂𝑂𝑂 𝑂)”
(C) ...𝛩(𝑂)”
(D) ...𝛩(𝑂 𝑂𝑂𝑂 𝑂)”
(E) ...𝑂(𝑂2 )”
Circle ALL options that result in
a true statement.
void stuff(int a[], int n) {
int i, j; “The worst-case runtime of stuff
int beans=10*n; is…

// all children start with


// zero beans. (A) ...𝛩(𝑂 )”
for(i=0; i<n; i++) (B) ...𝛩(𝑂2 )”
a[i]=0;
(C) ...𝑂(𝑂)”
// hand out beans one by one
// to a random child (D) ...𝑂(𝑂2 )”
while(beans > 0){ (E) ...𝛩(𝑂!)”
i = rand() % n;
a[i]++;
beans--; Circle ALL options that result in
} a true statement.
// print one line of beans
// for each child
for(i=0; i<n; i++) { “The best-case runtime of stuff
for(j=0; j<a[i]; j++) is…
printf(“bean!”);
printf(“\n”);
(A) ...𝑂(1)”
}
} (B) ...𝛩(𝑂)”
(C) ...𝛩(𝑂𝑂𝑂 𝑂)”
(D) ...𝑂(𝑂2 )”
(E) ...𝛩(𝑂!)”
Question 2 (8 points): What is the runtime of an in-order traversal of a
balanced binary search tree?

How about if it is imbalanced (as imbalanced as you like)?

Question 3 (8 points):

(A) Give a recurrence relation describing the worst-case runtime of


QuickSort on n elements.

(B) Give a recurrence-relation describing the best-case runtime of


QuickSort on n elements.

Question 4 (12 points):


Suppose you want to find the k-th smallest element from a collection of
n given elements for a given k. (If k=1, then we want the smallest; if
k=n; we want the largest, etc.). The elements are given in arbitrary
order.

Consider these two algorithms:

// Algorithm 1
Build a min-heap on all n elements.
Perform k delete-mins and return the result of the last delete min.

// Algorithm 2 (idea: keep the k-smallest elements seen so far in


// a heap; when done return max of them.

Build a max-heap on the first k elements in the given sequence


For each of the remaining n-k elements x{
if x is smaller than the largest element in the heap {
do a delete-max
insert x into the heap
}
// else heap unchanged

}
return the max element in the heap after this process.

Give a tight runtime analysis of each algorithm in terms of both n and k.


Algorithm 1:

Algorithm 2:

Question 5 (6 points): Circle T or F accordingly for each of the following


statements. If the statement is true, briefly state why; if it is false,
correct it and explain. Your explanations should be brief, but complete.
An unexplained answer receives 0 points.

(a) T F QuickSort runs in 𝑂(𝑂 𝑂𝑂𝑂 𝑂) time in the worst case.

𝑂
(b) T F If 𝑂(𝑂) = 𝑂(2) + 𝑂 then 𝑂(𝑂) = 𝑂(𝑂 𝑂𝑂𝑂 𝑂). Assume 𝑂(1) is
constant.

2𝑂
(c) T F If 𝑂(𝑂) = 𝑂( 3 ) + 1 (and 𝑂(1) is a constant) then
𝑂(𝑂) = 𝑂(𝑂𝑂𝑂 𝑂)
Question 6 (20 points): The incomplete C++ function below which takes two
parameters:
● A binary search tree (containing doubles) as a bst_node pointer to its
root and
● a double x.

The function is supposed to return the value in the tree that is closest to
x.

Details:

● If the tree is empty, there is no answer and INFINITY is returned; this


case has already been handled for you.
● "Closeness" is measured by the absolute difference between values
(absolute value of difference).
● It is possible that x is an entry in the tree -- in this case, the
function should return x (the distance is zero!).
● If there is a tie (two tree entries that are equally close to x), your
function can return either of them.
● RUNTIME: O(h) where h is the height of the tree.

Feel free to write helper functions if you like. Your solution must be
completely self-contained -- exception: you may use the library function
fabs() for computing absolute values (or do it yourself if necessary). More
space on the next page.

struct bst_node {
double val;
bst_node *left;
bst_ node *right;
};

double closest(bst_node *t, double x) {

// empty tree: return INFINITY


if(t==nullptr)
return INFINITY;
Question 7 (13 points): This problem uses the same bst_node struct as the
previous one.
Your job: write a function which takes a BST as a bst_node pointer and
constructs a complete clone of the tree; the clone tree is returned as a
pointer to its root.

struct bst_node node {


double val;
struct node *left;
struct node *right;
};

static bst_node * clone(bst_node *t) {

}
Question 8 (8 points): The following statement is FALSE:

“If a DAG has a single vertex with indegree zero, invoking breadth-
first search from that vertex will label/process the vertices in a
valid topological order.”

YOUR JOB: give and explain a counter-example.


Question 9 (30 points): Breadth-First Search has been run on a graph and a
'report' vector has been created as in program-4 except:

The vertex labels (entries in the report vector) do NOT have a


predecessor field. (Notice that it has been commented out on the next
page).

The distance field however, is correctly set.

Your Job: write a correct extract_path function with this handicap!

Details:

● remember that if a distance value is set to -1, it indicates the the


vertex was not reachable from the source. In this case, the path
vector should simply be empty.
● Runtime: 𝑂(|𝑂| × 𝑂𝑂𝑂𝑂 ) where |𝑂| is the path length (in # edges) and 𝑂𝑂𝑂𝑂
is the average indegree of the vertices on the extracted path.
● The graph data members are on the next page for reference.
● Write your solution on the page after that.
● As usual, you may write helper functions, but your solution must be
self-contained.
class graph {

private:

struct edge {
int vertex_id;
double weight;
edge ( int vtx_id=0, double _weight=1.0)
: vertex_id { vtx_id}, weight { _weight}
{ }
};

struct vertex {
int id;
vector<edge> outgoing;
vector<edge> incoming;
string name;

vertex ( int _id=0, string _name="")


: id { _id }, name { _name }
{ }
};

unordered_map<string, int> _name2id;

vector<vertex> vertices;

unordered_set<string> edges;

public:

struct vertex_label {
double dist;
// int pred; <<<<<<====== oops -- no predecessor!
char state;
int npaths;

vertex_label( double _dist=0.0, int _pred=-1, char _state='?',


int _npaths=0)
: dist { _dist }, pred { _pred }, state { _state},
npaths { 0 }
{ }

/* ...... */
bool extract_path(const vector<vertex_label> & rpt,
int dest, vector<int> & path) {
path.clear();
if(rpt.size() != num_nodes())
return false;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy