CS 415, Section 001 Sonoma State University Fall, 2022
 
Algorithm Analysis
Instructor: Henry M. Walker

Lecturer, Sonoma State University
Professor Emeritus of Computer Science and Mathematics, Grinnell College

Although much of this course is well developed, some details can be expected to evolve as the semester progresses.
Any changes in course details will be announced promptly during class.

Worksheet on Brute Force Algorithms, Merge Sort and Heaps

This worksheet/lab is in three parts: brute force algorithms, Merge Sort and heaps.

Brute Forch Algorithms

This section on contains two exercises related to brute force algorithms.

  1. As discussed in class, a permutation sort uses the following basic algorithm, given an array A of size n:

    Various sources indicate that the permutation sort has O((n-1)!), O(n!), and/or O((n!)*n), where ! is the factorial operator (so 4! = 4*3*2*1)
    Which, if any, of these conclusions is correct? Briefly justify your answer.
    Note: This problem asks for a logical argument and conclusion, NOT code to perform the permutation sort.

  2. In class, we discussed program list-processing.c, which included a recursive printReverseRecursive procedure that uses recursion to print a linked list in reverse order. For example, given the list

            "Node A"  "Node B"  "Node C"  "Node D"  "Node E"
          
    the procedure prints
            "Node E"  "Node D"  "Node C"  "Node B"  "Node A"
          
    However, the list is not formatted, with parentheses around the entire list and the list nodes separated by commas, such as
            ("Node E", "Node D", "Node C", "Node B", "Node A")
          
    Program list-processing.c contains a stub for a new procedure printReverseRecursiveFormatted. Complete this procedure, so that the resulting code recursively prints a formatted result of the nodes in reverse order, as described above.
    Notes:

Merge Sort

This section on Merge Sort asks you to compare two implementations of a Merge Sort, following the approaches to timing you followed in the Worksheet/Lab on the Quicksort [and other topics].

  1. Using the quicksort-comparisons.c as a guide, create a program that will contain two versions of a Merge Sort.

    As with the quicksort-comparisons.c program, your program should check that the sorted merge sort procedures produce properly sorted arrays and report the time required for sorting for various sized data sets.

    For this exercise, submit a full listing of your program (Doxygen output output not required, although the code must follow the course's C++ Style Guide).

  2. Run the program on data sets that have different sizes. For each size, run the procedures on data that have ascending, random, and descending order.

    1. Write several sentences comparing the efficiency of the two implementations.
    2. To what extent do the timings vary for data sets in ascending, random, and descending order?
    3. Are the timings of the two versions about the same, or is one version clearly better? Explain briefly.

Heaps

This section is divided into

Discussion

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 max-heap.

typical heap

A Simple Labeling of Nodes

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.

standard labeling

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.

detail for standard labeling

Note: As previously noted, some textbooks, such as Data Structures and Problem Solving Using Java, Fourth Edition by Mark Allen Weiss. uses 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.

0-based Labeling of Nodes

If we start labeling at 0 rather than 1, the labeling of nodes becomes:

0-based labeling
  1. 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?

detail for 0-based labeling

Connecting a Heap with an Array

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.

Insertion into a Heap

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.

  1. The following heap repeats the structure given at the beginning of this lab:

    typical heap

    Using this structure as a start, insert the values 30, 25, 55, 81, and 95. Show the structure of the tree and the values in each node after each insertion.

Deletion from a Heap

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.

  1. Starting with the original heap from the previous problem, remove four items in sequence. Show the structure of the tree and the values in each node after each deletion.

Constructing a Heap within an Array

In class, we discussed starting with an array of data and working from the bottom toward the top to rearrange the data to yield a heap. In the following problem, consider working with a min-heap.

  1. In the following questions, consider a heap implemented with 0-based indexing.

    1. Suppose an array of twelve elements, a[12], is initialized with a[i] = 20-i for each i. What rearrangements, if any, need to be done in order to make the corresponding tree structure into a heap? Show the data in the array once a heap is achieved.

    2. Suppose the array of twelve elements, a[12], is initialized with a[i] = i for each i. What rearrangements, if any, need to be done in order to make the corresponding tree structure into a heap? Show the data in the array once a heap is achieved.

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
Valid HTML 4.01! Valid CSS!
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu.
ccbyncsa.png

Copyright © 2011-2022 by Henry M. Walker.
Selected materials copyright by Marge Coahran, Samuel A. Rebelsky, John David Stone, and Henry Walker and used by permission.
This page and other materials developed for this course are under development.
This and all laboratory exercises for this course are licensed under a Creative Commons Attribution-NonCommercial-Share Alike 4.0 International License.