CS 24o Quiz/Exam Review Questions
In C++ implementations of Heap operations that percolate down the heap from top to bottom, which of the following best describes how the code knows that it should not try to move down any further, because it has reached the bottom of the heap? (Equivalently, how can the code check whether a heap node has a child?)
The calculated index of where the child would be in the array, will be greater than or equal to the number of elements in the heap
Which of the following best describes how the edges in some particular vertex's adjacency list are ordered, when a graph is stored using an adjacency list representation?
The edges are not stored in any particular order
Which of the following is the best reason (among the reasons provided) to use an adjacency list representation instead of an adjacency matrix representation to store a graph?
The graph is sparse and directed
Select the most accurate statement about graph representations (adjacency matrix vs. adjacency lists) and graph algorithms (DFS, BFS, Dijkstra, Prim, Kruskal).
The graph representation can influence the tight upper bound O() runtime complexity of some algorithms, especially for sparse graphs. That is, the same algorithm run on a graph that is stored with one representation could have a different tight upper bound O() than when the graph is stored using the other representation.
Which of the following best represents the definition of a balanced binary tree?
The height of the left subtree of any node x in a balanced tree is within 1 of the height of the right subtree of x
Hash Tables with open addressing can require two different placeholders that indicate that a slot in the hash table is empty. Which of the following best describes the "two different empties"?
One indicates that the slot is empty and has always been empty; the other indicates that the slot used to have a value stored in it, but no longer does.
Which of the following best describes the difference between preorder, inorder, and postorder traversal of a tree?
Preorder visits a node before its subtrees, inorder visits a node between visiting its subtrees, and postorder visits a node after visiting its subtrees.
Kruskal's Algorithm adds edges to a growing minimum spanning tree by considering them in what order?
From smallest edge weight to largest edge weight
Dijkstra's Algorithm augments each vertex of a graph to include information that is used to find shortest paths and/or store them once the algorithm finishes. If Dijkstra were implemented in C++, with a Vertex class, then this new information might be realized with new data members of Vertex. Or, the information per vertex could be stored in one or more arrays or vectors, each of which contains V entries. However it is implemented, which of the following does Dijkstra's algorithm include for each vertex v in the graph?
-A shortest path (estimate), storing the length of the shortest path (so far) to v. -A predecessor vertex, indicating the 2nd to last vertex in the shortest path (so far) to v.
Which of the following is/are always true of binary Min Heaps?
-All Min Heaps are complete trees -the height of all min heaps is O(log N)
Which of the following is/are true statements about Binary Search Trees (BSTs)? (assume no duplicate elements are stored in the BST)
-All typical container operations (insert, remove, and find) are usually implemented on a BST, and all can be made to run in tight upper bound O(logN) time by implementing a balanced BST -All of the values in any node x's left subtree are less than the value stored in x
Select all true statements about the Split() (also called Partition()) and Merge() operations in Quicksort and Merge Sort. (Neither Split() nor Merge() is O(1)).
-Merge Sort makes two recursive calls, then calls Merge -Quicksort calls Split(), checks the sizes of the two arrays, and calls Merge Sort if one of them has only one element.
Which of the following can be sorted in linear (O(N)) time (which is better than just O(N log N))?
-Negative integers in the range [-20000, -10000] -integers in the range [0, 10000]
Which of the following is/are (an) accurate description(s) of what might happen immediately after inserting a new node into a non-empty binary search tree (BST)? (By "immediately after", I mean before any other BST operations are performed.)
-The new node might have no children -The new node might cause the BST tree to become unbalanced
Assuming a Max Heap contains N elements, which of the following descriptions of operations on the heap accurately describes how many times the expression 2*i + 1 would be evaluated when completing that operation?
-When extracting the maximum element from the heap, 2*i+1 could be evaluated up to approximately logN-1 times. -When building a heap from an unordered array (which the zybook calls heapify and which the slides call Build-Max-Heap), the expression 2*i+1 would be evaluated at least approximately N/2 times.
Suppose we have a weighted, undirected graph G = {V, E}, with all edge weights set to 1. Which of the following algorithms can be used to find the shortest paths from some source vertex S and all other vertices in V? (Please interpret "can be used" to mean that the algorithm, if necessary, could be modified without impacting the O() asymptotic runtime complexity, to produce the appropriate output.)
-breadth first search -dijkstra's algorithm
Which of the following best describes a dense graph?
A dense graph has lots of edges
Which of the following best describes where the smallest value of a Binary Search Tree (BST) is stored?
Going down the left side of the tree, the smallest value is stored in the first node that does not have a left child
Which of the following best captures the fundamental difference between Depth First Search (DFS) and Breadth First Search (BFS)?
BFS and DFS visit vertices in (potentially) different orders from one another
Which of the following best describes how to find a node in a Binary Search Tree?
Compare the item being search to the value at the root of the tree. If they are equal, the item has been found. If the item being searched is less than the value at the root, search the left subtree; if the item being searched is greater than the value at the root, then search the right subtree. When searching subtrees, use this same procedure.
Suppose we pause Dijkstra's Algorithm right in the middle of it running on a very large graph. Not including the source vertex, which of the following best describes the vertices that have had their shortest paths already correctly computed (that is, it will not change again before the algorithm completes)?
Every vertex that is not currently in the Min-Heap has had its shortest path correctly computed.
Which of the following best describes the tight upper bound runtime complexity and extra space requirements of Heap Sort? (Remember, extra space means space in addition to the space required for the input array itself).
Heap Sort is O(N log N) and requires O(1) extra space
Which of the following best describes what the compiler does to compile a C++ templated class.... that is instantiated to be used with int, string, and Activity?
It essentially makes three copies of CloneMe, one for each type, replacing T with int, string, and Activity. It then compiles each copy.
Which of the following best describes linear probing in a hash table lookup() (i.e. search()) operation?
Linear probing happens in some hash tables with open addressing. When a key hashes to a spot s that is occupied by an element other than the one being searched for, the search continues at spot s+1, s+2, etc. (wrapping around to the top of the table if necessary)
Think about your growable dynamic array class that you used for CP2. (In that class, when the number of elements reaches the capacity, you double the array by allocating twice as much space and copying everything over into corresponding spots, leaving the upper half of the new array empty.) Does that same approach work for growing Hash Tables? Answer YES or NO and then explain why or why not.
No this does not work for hash tables. When allocating space for a new hash table, typically the next largest prime number that is greater than 2 times the size of the current array is used. (if array size was 7, 7*2=14 and the next prime number is 17 so new array size would be 17). Hash tables use a hash function that typically corresponds to mod % N where N is the size of the table. Growing a larger array requires using the new hash function to place elements into their spots in the array, so an element will not necessaryily keep its same location and cannot directly be copied into the new array without first using the new hash function to find an appropraite index. Once a new index is computed through the new hash function, the element can be copied into the new array into its new index.
In class, we found the runtime complexity of Dijkstra's Algorithm, when it uses a Min-Heap for the priority queue, to be O((V+E) log V). This is a reduction from an analysis that found three separate basic operation counts to be O(V), O(V log V) and O(E log V). Match those three components to the best descriptions of where they come from in Dijkstra's Algorithm.
O(V log V): The basic operations associated with extracting each node from the Min-Heap. O(V): The basic operations associated with initializing the Min-Heap priority queue to include all |V| vertices, with shortest path distances set to 0 for the source vertex, and infinity for all other verticies O(E log V): The basic operations associated with all of the calls to the Relax function, including its effect on the Min-Heap.
Which of the following best describes the data structure that is used by Prim's Algorithm for finding a minimum spanning tree.
a min-heap organized by edge weight
Associates a color with each node while the algorithm runs, to help make sure vertices are not visited more than once
both BFS and DFS
Runs in tight upper bound O(E + V) time
both BFS and DFS
Uses a first in first out (FIFO) queue of vertices
breadth first search
Uses a last in first out (LIFO) stack of vertices
depth first search
A minimum spanning tree for G could have a cycle in it, if that cycle includes every node in the graph
false
Every graph G has a unique (that is, one and only one) minimum spanning tree
false
Which of the following is most true about the runtime complexity of Merge Sort?
merge sort is tight upper bound O(N log N) because the collective runtime complexity of all the Merges at each level of recursion is O(N) and there are log N levels
Finds a minimum spanning tree (MST) of a weighted undirected graph
neither DFS nor BFS
A minimum spanning tree for G always contains exactly |V-1| edges
true
A minimum spanning tree for G always contains exactly |V| vertices
true