CS201 Final
A list of 10 elements is to be sorted using insertion sort algorithm. How many times will the outer loop be executed?
9 times
In quicksort, elements in the lower partition must be
<= pivot
In quicksort, elements in the higher partition must be
>= pivot
A heap is:
A BST satisfying two conditions: 1. order property 2. structure property
Graph definition
A hierarchical data structure in which elements are related to an arbitrary number of other elements
Topological sort looks like? What about the edge directions?
A horizontal line, edges go left to right
Heapsort outputs what?
An array from least to greatest
DFS total time complexity
O(V+E)
How much memory does adjacency list use?
O(V+E)
Topological Sort runtime
O(V+E)
How much memory does adjacency matrix use?
O(V^2)
Height of heap?
O(log n)
How much space does quicksort require?
O(log n)
How much extra space does merge sort require?
O(n) space
Which collision resolution technique places the item in another empty bucket?
Open addressing
Difference between Prim's and Kruskal's algorithms
Prims can only pick connected edges, kruskal's can pick an edge anywhere in the graph
What container does Prim's use? Why?
Priority queue through a heap, because values are changing dynamically.
Heaps can be used to implement:
Priority queues and garbage collection storage
If you are given an array of strings of fixed length, which of the algorithms would you choose to sort the array in the shortest amount of time?
Radix Sort
Sorting algorithms that have linear time complexity:
Radix, bucket, counting
Example of every pivot being bad in quicksort:
Select first element as pivot. The smallest element happens to be the first one on each call. "n subdivisions".
How does radix sort work?
Sort least significant digits first.
If a BST already contains the node you're trying to insert:
Take the right subtree path, and keep going as normal. Insert at null.
Each element in an adjacency list is?
The head of a linked list of all neighbors
What happens when a leaf node is removed?
The parent's left or right child node becomes null
During insertions in a hash table, if the bucket is occupied, iterating over i values to determine next empty bucket is called __________.
probing sequence
Which of the following inputs gives the best performance with insertion sort? a. Random array b. None of the options c. Sorted array d. Reverse sorted array
sorted array
Is counting sort stable or unstable?
stable
Formula for quicksort midpoint to get pivot:
start index + [(end index - start index) / 2]
Union worst case runtime, and scenario?
O(N^2) when appending a longer list to a shorter list.
The big O notation of the composite function 5 N^3 + O(N^2) is?
O(N^3)
Bellman-ford time complexity
O(V * E)
BFS total time complexity
O(V+E)
Steps for determining whether to use Dijkstra, Bellman-ford, or DAG:
1. Check if DAG, shortest time w/ linear complexity. 2. Negative weights? If no, use dijkstra ( O(E Log V), same complexity as MST) 3. If yes, use bellman-ford (O(V * E) complexity)
How can you change SSSP into SSLP for DAGs?
1. Replace infinity by negative infinity in line 2 of INITIALIZE-SINGLE-SOURCE 2. Replace > by < in line 1 of RELAX
Advantages and Disadvantages of Adjacency Matrix:
Advantage: O(1) to determine if two nodes are adjacent Disadvantage: O(V^2) size
Advantages and Disadvantages of Adjacency List:
Advantage: O(V+E) complexity Disadvantage: O(V) to determine if two nodes are adjacent (okay for sparse graphs)
Dynamic programming typically computes the value of an optimal solution in what fashion? When is this better?
Bottom-up, better if the degree of overlap is not significant
Which linear sort uses extra space?
Counting sort
Greedy algorithms vs. Dynamic programming:
Greedy: Solves problems by making a choice and then solving the subproblem. Dynamic: Solves problems by combining the solutions to subproblems.
Why use dynamic programming over recursive algorithms?
More efficient uses less memory bc not on stack
What causes Bellman-Ford to fail?
Negative edge weight cycle
Dijkstra's time complexity
O (V^2)
Kurskal's space complexity?
O(E+V)
cycle
Simple path in which the first and last nodes are connected
What algorithm is used for SSSP in DAGs?
Topological sort.
When should you use dynamic programming instead of divide and conquer?
When there is overlap in the subproblems.
What are the steps for inserting an item into a linked list?
1. Create a list node for the new item. 2. Assign a pointer of the new item to point to the next item. 3. Update the pointer of the previous node to point to the new node.
Binary tree requirements
1. Degree no greater than 2 2. Ordered trees: every node is expected as being either left/right child of parent 3. Finite set of nodes, possibly empty 4. Consists of a root and two disjoint BSTs
List all RBT Rules:
1. Every node is colored either red or black. 2. The root node is black. 3. A red node's children cannot be red. 4. A null child is considered to be a black leaf node. 5. All paths from a node to any null leaf descendant node must have the same number of black nodes.
Breadth first steps:
1. Mark and enqueue node N, visit, mark visited. 2. Enqueue N's neighbors, mark waiting.
Depth-first steps:
1. Visit node N then stack up its neighbors 2. Pop the top node, visit, stack its neighbors
Using the Big O runtime complexity O(N^2), how many times longer will sorting a list of 30 elements take compared to sorting a list of 20 elements?
30^2 / 20^2 = 2.25x
In-Place sort algorithm:
Insertion sort, quicksort, heapsort
Out-of-place sort algorithm:
Merge sort
Which of the sorts is stable?
Merge sort, counting sort
Which sorts are asymptotically optimal comparison sorts?
Mergesort and Quicksort
Structure Property
Nearly complete binary tree (complete except last level)
In order to use counting sort, what condition must be true?
Numbers to be sorted are integers in the range 0 to k.
The space complexity of the BST search algorithm is __________.
O (log N)
The best case runtime complexity of an algorithm that searches an array of size N is?
O(1)
Kruskal's runtime?
O(E log E)
Prim's runtime?
O(E log V)
Heap sort runtime complexity, average and worst case:
O(N log N)
If you have a sorted array and you pick the middle element as the pivot element, then the time complexity of quicksort is?
O(N logN)
What is the time complexity of the following algorithm? Assume S1 is a statement that will be executed by the program. for (i = 1; i < N; i = i*2) for (j = 0; j < N; j++) S1;
O(N logN)
Disjoint set operation runtimes for MAKE-SET are all:
O(N)
Union runtime?
O(N)
What is the worst-case time complexity for the search operation using a DLL?
O(N)
What is the worst-case time complexity of deleting a node from a SLL?
O(N)
Union time complexity worst case
O(N^2)
What is the Big O notation for a recursive function with a runtime complexiy of T(N) = 5 N + T (N^2 - 1)?
O(N^2)
What is the worst case runtime for quicksort?
O(N^2)
Doubly Linked List CIRCULAR runtimes for:insertFront, insertBack, removeFront, removeBack
insertFront: 1 insertBack: 1 removeFront: 1 removeBack: 1 same as DLL circular
Doubly Linked List WITH TAIL runtimes for:insertFront, insertBack, removeFront, removeBack
insertFront: 1 insertBack: 1 removeFront: 1 removeBack: 1 same as DLL circular
Singly Linked List runtimes for:insertFront, insertBack, removeFront, removeBack
insertFront: 1 insertBack: N removeFront: 1 removeBack: N
Array insertFront, insertBack, removeFront, removeBack runtimes
insertFront: N insertBack: 1 removeFront: N removeBack: 1
A perfect hash function _______________.
maps items to buckets with no collisions
Merge Sort: Avg/Best/Worst
n log n / n log n / n log n
Quick Sort: Avg/Best/Worst
n log n / n log n / n^2
How is an adjacency list represented?
n-element one-dimensional array (node list)
Insertion sort average/best/worst
n^2 / n / n^2
Quicksort conditions and O notation for worst case:
n^2, every pivot has to be bad.
How is an adjacency matrix represented?
nxn two-dimensional array of A boolean values
Given the list (27, 40, 15, 25, 10, 19, 30), what are the new partitioned lists if the pivot is 25? a. (19, 27, 30, 40) and (10, 15, 25) b. (10, 15, 19, 25) and (27, 30, 40) c. (27, 40, 15, 25) and (10, 19, 30) d. (25, 10, 19, 30) and (27, 40, 15)
(10, 15, 19, 25) and (27, 30, 40)
Given the list (62, 45, 29, 32, 25, 10, 75), what is the list after sorting by the unit digit?
(10, 62, 32, 45, 25, 75, 29)
Formula for mergesort mid (j):
(start index (i) + end index (k) ) / 2
Best/Worst/Average for BST
Best: O(1) Worst: O(n) (keys in ascending/descending order Average: O(log n)
Which collision resolution technique places the item in the same bucket in a list?
Closed addressing
Directed acyclic graph
Contains no cycles, edges are directed
Which sort is often used in radix sort? Why?
Counting sort, stability
What valuable information does DFS provide about the graph?
Cycle detection, strongly connected components
What is better for SSSP in an unweighted graph, DFS or BFS?
DFS
When asked if you should use DFS or BFS, which one is usually the best?
DFS, unless finding shortest path in unweighted graph.
Order property
Data value in a node is no greater than data values stored in descendants
Topological sort requirements and ordering
Directed acyclic graph required. In the proposed ordering, for each edge (u,v), u must come before v.
Disjoint sets can be used to?
Find cycles and unions of connected edges.
BFS is mainly only good for doing what?
Finding shortest path of unweighted graph
Cut property
Given any cut in an edge-weighted graph, the crossing edge of minimum weight is in the MST of the graph.
What does bucket sort assume?
Input is generated by random process, uniform distribution over [0, 1)
What is required of heap insertions?
Insertion must maintain order and structure properties
In the time complexity O(V+E) what do V and E represent?
The size of the set of V vertices and the size of the set of E edges
What kind of graph is best to use Dijkstra's for?
Weighted graph with NO negative weights.
What kind of graph is best to use Bellman-Ford algorithm for?
Weighted graph, may have negative weights.
Identify the new array created after the heapify operation to create a max-heap on this array: [47, 25, 36, 60, 54] a. [25, 36, 47, 54, 60] b. [25, 47, 36, 60, 54] c. [60, 54, 36, 25, 47] d. [60, 54, 47, 36, 25]
[60, 54, 36, 25, 47]
Which of the following inputs gives the best performance with merge sort? a. reverse sorted array b. all of the options c. sorted array d. random array
all of the options
Identify the constant time operation a. while x > y) x = x + 1 b. x = array[option] temp = array[option + 1] c. for (i = 0; i < length; i++) sum += i d. string str1 string str2 string str3 = concat(str1, str2)
b. x = array[option] temp = array[option + 1] it isn't d because strings are arrays lol oops
Which XXX would replace the missing statement in the given algorithm for traversing a circular linked list? CircularListTraverse(head) { if (head is not null) { current = head do { visit current current = current->next } while (XXX) } }
current != head because it's circular! can't be current != tail.
identify the non-constant time operation. a. if (x > y) return x b. num = array[i]; array[i+1] = num+1 c. for (i = 0; i < 10; i++) sum+= num2 d. i = 0; while (i < listSize) { sum = sum + i; i++)
d. i = 0; while (i < listSize) { sum = sum + i; i++)
Adjacency matrix is appropriate to use for _____________.
dense graphs
Application of disjoint sets
determining the connected components of an undirected graph when edges are added dynamically
Directed graph
edges have a direction
Path
finite sequence of edges from a source node to a destination node
Heap array is called?
heapform