CS 251 Fall 2018 Final Exam
CS 251 Fall 2018 Final Exam
CS 251 Fall 2018 Final Exam
SIGNATURE:_______________________________
NetID:_______________________________
120 Points
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.
(A) ...𝑂(1)”
(B) ...𝑂(𝑂)”
(C) ...𝛩(𝑂𝑂𝑂 𝑂)”
(D) ...𝛩(𝑂 𝑂𝑂𝑂 𝑂)”
(E) ...𝑂(𝑂2 )”
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…
Question 3 (8 points):
// Algorithm 1
Build a min-heap on all n elements.
Perform k delete-mins and return the result of the last delete min.
}
return the max element in the heap after this process.
Algorithm 2:
𝑂
(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:
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;
};
}
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.”
Details:
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;
vector<vertex> vertices;
unordered_set<string> edges;
public:
struct vertex_label {
double dist;
// int pred; <<<<<<====== oops -- no predecessor!
char state;
int npaths;
/* ...... */
bool extract_path(const vector<vertex_label> & rpt,
int dest, vector<int> & path) {
path.clear();
if(rpt.size() != num_nodes())
return false;