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 two parts: Brute Force Algorithms and Merge Sort
This section on contains two exercises related to brute force algorithms.
Consider the Brute-Force String Matching Problem, in which a string or text T[0..n-1] is searched for the occurance of a substring or pattern P[0..m-1]. For details, see the discussion in Section 3.2 (pp. 105-106) of Levitin's textbook or in numerous Web sources.
For this problem, we consider the following text, slightly edited from the textbook and numerous Web sites:
for (int i = 0; i <= n-m; i++) { for (int j = 0; (j < m) && (P[j] == T[i+j]); j++) { } if (j == m) { return i; } } return -1;
Merge Sort is discussed in the "Divide and Conquer" section of the textbook. What part of the algorithm corresponds to a "divide" segment and what part corresponds to a "conquer" segment? Explain in at least 5 sentences.
Figure 5.2 in the textbook show a graph, giving the steps involved for sorting an 8-element array using a Merge Sort.
As discussed in the textbook and in class, a Merge Sort has Θ(n log n), and this provides a general sense of how this algorithm will scale as the array increases in size. This problem explores how much variation might be expected in time for sorting with different types of data. To begin, consider the program mergesort-data-sets.c which runs the traditional, recursive merge sort on ascending, random, and descending data for arrays of different size.
The program is designed to run with array sizes 800,000, 1,600,000, 3,200,000, . . . , 25600000. Compile and run the program and describe what happens?
With size constraints on the run-time stack, the stack overflows, since the merge procedure allocates local storage with each call. (On my machine, stack overflow occurs with an array size between 2,000,000 and 2,100,000—but your experience may be different.) To resolve this problem, replace the local declarations
int larr [lsize]; int rarr [rsize];with dynamic memory (e.g., declare variables
int *
larr
and initialize using malloc
, but be
sure to free that memory when the merge procedure
terminates).
To further explore how timing might be effected by the data set, expand the program to include two more data sets for each array (adding additional columns to the output).
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 with moderate editing Summer, 2023 additional editing November 30-December 7, 2024 |
|
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu. |
Copyright © 2011-2025
by Henry M. Walker.
|