0% found this document useful (0 votes)
189 views7 pages

CS6045 Fall 2024 Midterm Exam Review

The document outlines the structure and content of the CS6045 Midterm 1 exam for Fall 2024, covering chapters 1 to 5. It includes various types of questions such as True/False statements, algorithm analysis, and practical applications of data structures and algorithms. Additionally, it features problems related to sorting, searching, and graph traversal techniques.

Uploaded by

saitsuer02
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
189 views7 pages

CS6045 Fall 2024 Midterm Exam Review

The document outlines the structure and content of the CS6045 Midterm 1 exam for Fall 2024, covering chapters 1 to 5. It includes various types of questions such as True/False statements, algorithm analysis, and practical applications of data structures and algorithms. Additionally, it features problems related to sorting, searching, and graph traversal techniques.

Uploaded by

saitsuer02
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Print Name: Fall 2024

CS6045 Midterm 1 (Chapter 1 ~ 5)


Time: 120 mins Sep. 25, 2024 (Wednesday)
1. [12 points] True or False

T F (a) An algorithm’s space efficiency is measured by counting the number of extra memory
units consumed by the algorithm.

T F (b) All polynomials of the same degree k (aknk + ak-1nk-1 + … + a0) belong to the same class
order of growth functions.

T F (c) log2n ∈ Ω(√𝑛).

T F (d) If f (n) ∈ O(g (n)) and g(n) ∈ O(h(n)) , then h(n) ∈ O(f(n)).

T F (e) ∑𝑖=2
𝑛−1 𝑙𝑜𝑔2𝑖2 ∈ 𝑂(𝑙𝑜𝑔2 𝑛2).

T F (f) The average case time complexity of Hanoi Tower problem is O(n!).

T F (g) Apply exhaustive search to solve knapsack problem, the time complexity is O(2n).

T F (h) A function f(n) is Big-O of g(n) if there exist positive constants c and n0 such that f(n) >=
cg(n) for all n >= n0.

T F (i) Suppose we are sorting an array of eight integers using quicksort, and we have just finished
the first partitioning with the array looking like this: 2 5 1 7 9 12 11 10 . The pivot used in the
partition could be the 7 but not the 9.

T F (j) The recurrence for worst case of MergeSort is T(n) = T(n-1)+O(n).

T F (k) Selection sort is a direct application of the brute force technique to the sorting problem.

T F (l) In terms of average case time complexity, insertion sort is better than selection sort.
2. [15 points] Examine the procedure DOES_SOMETHING below and answer the following
questions.
Procedure DOES_SOMETHING(A,l,r)
Input: Array A[l … r]
1. x = A[l];
2. i = l;
3. j = r + 1;
4. do
5. for i = i + 1 to r do
6. if A[i] > x then break;
7. for j = j - 1 to 1 do
8. if A[i] < x then break;
9. exchange A[i] and A[j];
10. while i < j
11. exchange A[i] and A[j];
12. exchange A[l] and A[j];

(a) What is the function of the procedure DOES_SOMETHING? (5 points)

(b) What is the worst-case running time of the procedure (justify your answer)? (5 points)

(c) If A = [24, 30, 9, 46, 15, 19, 29, 86, 78], what is the output? (5 points)
3. [10 points] Consider the following recursive algorithm:
function(n)
{
If n =1, then
return 0;
else
return function( 𝑛 ) + 1;
⌊ ⌋
2
}
where n is a positive integer.
(a) What does this algorithm compute? (5 points)

(b) What the efficiency class of this recursive algorithm (justify your answer by applying the
analysis framework)? (5 points)
4. [10 points] List the following functions according to their order of growth from the lowest
to the highest.
3 1
( )n, n2(log n)2, 10log (n+100),n2 + n3 + logn, ln2 n, n2 log(n2), 22n, 3n, 3√𝑛 , ( )n
2 8

5. [5 points] Apply the Russian peasant algorithm to compute 25 * 47 (justify your answer).
6. [10 points] Trace BFS and DFS on the following two graph. For each do the following:
Start a node S = 1 and when there is a choice follow edges from left to right. Number the
nodes 2,3,4 ….. in the order that they are visited. Darken the edges of DFS/BFS trees.

7. [5 points] Considering the following graph, find topological sort of the graph.
8. [8 points] Apply quickselect to find the median of the sequence {45, 29, 71, 90, 12, 31,
10}, the last swap is , the first swap is , the number of swaps is , the
number of comparisons is . Justify you answer.

9. [15 points] Binary Search Tree Operations.


(a) Construct a Binary Search Tree (BST) by inserting the following characters successively,
starting with the empty tree: K, E, T, A, J, H, M, W, P.
(b) Traverse the constructed BST in pre-order, in-order, and post-order.
(c) Remove the ROOT node from the constructed BST.
10. [10 points] What is the ouput of the second iteration in the merge phase to apply
MergeSort on the array {38, 27, 43, 3, 9, 82, 10}? Justify your answer.

Common questions

Powered by AI

The pre-order traversal of the constructed Binary Search Tree from the sequence K, E, T, A, J, H, M, W, P is K, E, A, J, H, T, M, W, P. In pre-order traversal, the nodes are visited in the order: root, left subtree, right subtree, reflecting the step-by-step insertion and path-following in the BST based on the given sequence .

In the array {2, 5, 1, 7, 9, 12, 11, 10}, 7 can be a valid pivot after the first partition, resulting in elements smaller than 7 on one side and those larger on the other. The number 9 cannot be a pivot initially as it is positioned in such a way that it disrupts the correct partitioning respecting the sorting boundaries .

The procedure DOES_SOMETHING is performing a variation of the partitioning step in the quicksort algorithm. The steps involve choosing a pivot and rearranging the elements such that all elements less than the pivot are on one side and all elements greater are on the other. The worst-case running time is O(n), occurring when the pivot is always the smallest or largest element, requiring a linear scan through the entire array with no significant partitioning .

Selection sort uses a brute force approach by blindly searching each pass through the entire remaining list to find the minimum and iteratively swapping it to the front. Its average time complexity is O(n^2), much like insertion sort, but insertion sort can perform better on partially sorted lists through adaptive property despite same asymptotic efficiency .

The recursive algorithm computes the number of divisions by 2 needed to reduce n to 1, essentially calculating the floor of the logarithm base 2 of n, or floor(log2(n)). The efficiency class of the algorithm is O(log n) because each recursive call reduces the problem size by a factor of 2, similar to the binary logarithmic progression .

A topological sort of a directed graph is determined by performing a depth-first search (DFS), ensuring all vertices maintain the directed constraint order. Visited nodes are finalized once all descendants are visited. Ensuring no back edges (which creates cycles) ensures the sort’s validity; nodes are then recorded in reverse order of their completion time in DFS .

The Russian Peasant algorithm multiplies two numbers by understanding multiplication as a process of successive doubling and halving. For 25 * 47, the algorithm involves repeatedly halving one number (ignoring remainders) and doubling the other, then summing all doubled numbers corresponding to odd halves. The process yields: 25 x 47 becomes (12, 94), (6, 188), (3, 376), (1, 752), so adding 47, 376, and 752 results in 1175 .

The order of growth from lowest to highest for the given functions is: 10log(n+100), √n, ln^2(n), n^2(log n)^2, n^2log(n^2), n^2 + n^3 + logn, 3^n, 2^n, 2^(2n). This is determined based on analyzing each function's growth rate, considering both polynomial, logarithmic, and exponential factors, where exponential grows faster than polynomial and logarithmic components .

In BFS, starting from a chosen node, exploration occurs at the breadth of a node’s neighbors first before proceeding to the next level. A queue is used to track the order: dequeueing the front for visitation proceeds to enqueue its unexplored neighbors, maintaining the order per layer and ensuring all nodes are visited .

During the second iteration of the merge phase of MergeSort, the array segments are partially merged into larger ordered sequences. The sequence appears as: {27, 38}, {3, 43}, {9, 82}, and {10}. This ensures each sub-segment is sorted within itself but not with respect to others yet, forming partially ordered groups preparing for the next iteration .

You might also like