CS6045 Fall 2024 Midterm Exam Review
CS6045 Fall 2024 Midterm Exam Review
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 .