Sonoma State University | ||
Algorithm Analysis
|
||
Instructor: Henry M. Walker
Lecturer, Sonoma State University |
Although CS 415 has been well developed for several years, last year the CS faculty made a significant, long-term curricular change regarding SSU's Upper Division GE Area B Requirement.
Historically, CS Majors could satisfy this requirement by taking CS 454, Theory of Computation, and CS 454 will continue in this role for the next several semesters.
At some time in the future (but not Spring 2025), CS 415, Algorithm Analysis, will allow students to satisfy SSU's Upper Division GE Area B Requirement.
During an anticipated time of transition:
For future semesters, students should check with the CS faculty regarding which course(s) (CS 415 and/or CS 454) will satisfy SSU's Upper Division GE Area B Requirement.
This assignment is in three parts: Heaps, Heap Sort and Horner's Rule.
This section is divided into
A max-heap is a binary tree with the property that the value at each node is larger (according to some ordering) than the values stored in either child. Similarly, a min-heap is a binary tree with the property that the value at each node is smaller (according to some ordering) than the values stored in either child.
Further, we restrict our attention to trees which are completely balanced. All nodes have 2 children, except at the bottom of the tree. The last row of the tree may not be full, but any items on the last level are moved left as much as possible. For example, the following tree illustrates the shape of a tree and the ordering of values within the tree for a min-heap.
Since nodes in a heap fit a straight forward identification system, there is a natural mapping between the logical nodes in a heap and the indexed elements of an array. To identify the nodes in a tree, the simplest approach is to number the nodes level-by-level from the root downward. Within a level, nodes are numbered left to right. Also, for simplicity, many textbooks assign the index 1 to the root node.
In examining the label nodes, a pattern emerges: for each node labeled i, the left child has label 2*i and the right node has label 2*i+1.
Note: As previously mentioned, some textbooks, such as Data Structures and Problem Solving Using Java, Fourth Edition by Mark Allen Weiss, use this numbering scheme in writing code, with the top node numbered 1. If one considers an array element 0, Weiss suggests filling that position with -∞ for a min-heap or ∞ for a max-heap.
In the context of 0-based labeling, identify a pattern for moving from a node with label j to its left child and its right child. What labels would be found on the left and right nodes of a node with label j?
In class we considered how to insert an item into a heap and maintain the structure. In each case, we first place the item at the end of a tree (as far left as possible in the last row). The item then is worked up from its position, until the data in the tree are appropriately ordered.
The following min-heap repeats the structure given at the beginning of this lab:
Using this min-heap as a start and following the standard procedure to add an element to a heap, insert the values 10, 14, 6, and 12. Show the structure of the tree and the values in each node after each insertion.
In class, we also considered how to remove the top-priority item from a heap: remove the root as the item to be returned, move the last item from the end of the heap to the root, and work that item down in the heap until the data are properly ordered.
The Heap Sort draws upon the heap data structure, which is discussed in Section 6.4 of the textbook.
The key procedure percDown
for the Heap Sort has
the following specification
/** ******************************************************************************* * percDown function * * @param array the array to be made into a heap, starting at hold * * @param hole: the node index (or base) of subtree for start of processing * * @param size the size of the array * * @pre all nodes in left and right subtrees of the hole node are heaps * * @post all nodes in the tree from the hole node downward form a heap * *********************************************************************************/ void percDown(int array [ ], int hole, int size)
Implement this procedure, paying attention to the following notes.
Using the percDown
procedure, implement a heapSort
procedure, with the following specification. (Details
may be found in Levitin's text, Section 6.4.)
/** ****************************************************************************** * heap sort, main function * * @param a the array to be sorted * * @param n the size of the array * * @post the first n elements of a are sorted in non-descending order * *********************************************************************************/
Include the heapSort procedure within a complete program that
For the Heap Sort part of the assignment, turn in both a listing of the complete program (including all of Step 4) and the output produced by running the code on several arrays.
A polynomial function has the form
p(x) = anxn + an-1xn-1 + ... + a2x2 + a1x + a0
Write a function compute_poly that takes three parameters:
and returns the value of the polynomial p(x).
Notes:
int a = {2, 0, 0, 5};then the polynomial being evaluated is 5x3 + 2
For this part of the assignment, turn in both a listing of the complete program and the output produced by running the code for several different arrays with varying sizes, including the example given above. Further, for each of your tests, write a short paragraph or present a table that indicates the polynomial being tested (e.g., 2x3 + 5) and indicates the correct value (e.g., result=137) when a specified value (e.g., x=3) is used in the polynomial evaluation. Effectively, this commentary demonstrates that you have looked at the output and can certify that the program works properly. (An answer without this testing/paragraph will be subject to a 10-point penalty. Also, claiming a test is correct when an output value matches an incorrectly computed "correct" value is subject to a 5 point penalty per test case.)
created December 1, 2018 revised December 2, 2018 revised December 27-30, 2021 revised February 4, 2022 reformatted and heap material added July 28, 2022 reorganized with brute force/merge sort added October 3-6, 2022 revised December 30, 2022 reorganized and revised Summer, 2023 reorganized and revised November 30, 2024 |
|
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu. |
Copyright © 2011-2025
by Henry M. Walker.
|