CS 415, Section 001 | Sonoma State University | Fall, 2022 |
Algorithm Analysis
|
||
Instructor: Henry M. Walker
Lecturer, Sonoma State University |
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.
A hash function returns integers between 0 and 15 based on the first letter of a data item according to the following table.
A | B | C | D | E | F | G | H | I | J | K | L | M |
13 | 15 | 4 | 9 | 11 | 13 | 2 | 9 | 0 | 7 | 6 | 5 | 2 |
N | O | P | Q | R | S | T | U | V | W | X | Y | Z |
9 | 3 | 11 | 4 | 10 | 13 | 5 | 1 | 7 | 14 | 11 | 8 | 2 |
For example, according to this hash function, the string TM would hash to the value 5 (based on the initial letter T; the hash function does not look at the second letter M).
Consider the following sequence of 13 data items:
UQ GR GD WZ QS NW DT AX SL CY EM MR ZB
Suppose the above sequence is to construct each of the following data structures 16 locations (labeled 0 through 15), based on the hash function given above.
Show the resulting structure by filling in the following tables:
Closed, unbucketed hash table using linear probing |
Closed, unbucketed hash table using quadratic probing | Open, bucketed hash table (with chaining) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|
Program fibonacci.c defines two procedures to compute the nth Fibonacci number.
int arr [n+1] = {0};
if (fibArr[n] != 0)
return fibArr[n] = 1;
A widely-used problem in computer science involves determining the number of ways that k items can be chosen from a pool of n distinct objects, ignoring the ordering of elements selected. In writing and speech, this quantity is denoted several ways:
This "binomial coefficient" is discussed in CS 242 and on many Web sites, such as Combinations and Permutations by Maths is Fun.
Computationally, discussions of these combinations often specify a computational formula involving factorials (where k factorial or k! = k*(k-1)*(k-2)*...*3*2*`)
For example, the number of 5-card hands possible from a deck of 52 cards would be C(52, 5).
Although this formula works well on paper, a pragmatic problem is that the computation of factorials produces large numbers very quickly:
The program factorial.c computes successive factorials (1!, 2!, 3, ...) until integer overflow occurs—the integer is too large to be stored in an integer variable.
Since factorials are often too large for many simple computations, alternative approaches are needed for computation. One approach uses the recurrence relation:
As well as being explained in CS 242, many online sources provide a proof of this relation, including Proposition: Recursive Formula for Binomial Coefficients from the Book of Proofs.
With this recurrence relation, alternative ways to compute C(n, k) could follow either of two approaches:
These alternatives form the basis for the following programming task.
Write a program that reads values for n and k and computes C(n, k) in the two ways described above.
created December, 2021 revised December-January 2021 revised and expanded July 29, 2022 minor editing October 20, 2022 adjustments of problem 4 for C and C++ October 24, 2022 |
|
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu. |
Copyright © 2011-2022
by Henry M. Walker.
|