|
|
COT 4401
Top 10 Algorithms
Chris Lacher
Introduction to Algorithms
|
What is an Algorithm?
- Compare Definitions
-
COP4530 Notes
-
Wikipedia
- Section 1.1 of [Cormen]
- Section 1.1 of [Sedgewick]
- Example 1
template < typename N >
size_t Iterations ( N )
{
size_t i = 0;
N n = 1;
while (n != 0)
{
++i;
n = n/2;
}
return i;
}
- Is this an algorithm?
- Does the answer to (1) depend on "context" (assumptions or type)?
- Write a simple driver and run experiments with Iterations ( type ) in C++
What are "asserted outcomes" for computation? Do they provide significant
information about the "machine" on which it is run?
What is an Algorithm? - Official definition for this course
An algorithm is any clearly specified computational procedure that
operates on some set of values. (Note that a set may be empty.) An algorithm
should have the following attributes (which may be stated explicitly or understood implicitly):
- Assumptions: things that must be true before executing the algorithm
- Outcomes: things that are asserted to be true after executing the algorithm
- Proof: of the proposition "if the assumptions are true and the algorithm is
executed then the outcomes are true"
- Runtime: the time required to execute the algorithm, usually expressed as an asymptotic estimate as a function of input size
- Runspace: the space required to execute the algorithm, usually expressed as
an asymptotic estimate of the additional space required as a function of input size
Algorithm Analysis
- Compare Definitions
-
COP4530 Notes
-
Wikipedia
- Chapter 2 of [Weiss]
- Chapters 2, 3 of [Cormen]
- Chapter 2 of [Sedgewick]
- Example Sort Algorithms
template < typename T >
void SelectionSort (T * array, size_t size)
{
if (size < 2) return;
size_t i, j, k;
size_t comps = 0;
for (i = 0; i != size; ++i)
{
k = i;
for (j = i; j != size; ++j)
{
if (array[j] < array[k])
k = j;
++comps;
}
XC (array[i], array[k]);
}
// report comparisons data
std::cout << "SelectionSort\n"
<< " Size = " << size << '\n'
<< " Comps = " << comps << '\n';
// */
}
template < typename T >
void InsertionSort (T* array, size_t size)
{
if (size < 2) return;
size_t i, j, k;
size_t comps = 0;
T t;
for (i = 0; i != size; ++i)
{
t = array[i];
for (k = i, j = k--; j != 0 && t < array[k]; --j, --k)
{
array[j] = array[k];
++comps; // counts each execution of loop body
}
// ++comps; // counts the last invocation of the for loop header
array[j] = t;
}
// report comparisons data
std::cout << "InsertionSort\n"
<< " Size = " << size << '\n'
<< " Comps = " << comps << '\n';
// */
}
- Exercises.
- These two "sort" algorithms have been equipped with extra code designed to
measure the number of comparisons that are made during a run (code
highlighted in red).
- Explain why the number of comparisons is a measure of the runtime of
each of the algorithms.
- Do you agree with the placement of the measurement code capturing every
call to the type T less-than operator?
- Write a C++ driver program for running sorts on unsigned long data in
input files. Run the sort tests on files of unsigned integers of size 10, 100, and 1000.
- For random data, what is the number of comparisons, approximately?
- For data that is already sorted, what are the results?
- What would you expect the number of comparisons called by these two
algorithms to be, on (a) random data and (b) sorted data?
Generate random integers to a file
#include <xran.cpp> // /home/courses/cop4530p/LIB/cpp
int main(int argc, char* argv[])
{
if (argc != 5)
{
std::cout << " ** program requires 4 arguments\n"
<< " 1 = number of generated entries\n"
<< " 2 = min size\n"
<< " 3 = max size\n"
<< " 4 = output filename\n"
<< " ** try again\n";
exit(0);
}
std::cout << "Program generating file of integers\n"
<< " ...\n";
std::ofstream out1;
out1.open(argv[4]);
if (out1.fail())
{
std::cout << " ** Unable to open file " << argv[4] << '\n'
<< " ** program closing\n";
exit(0);
}
fsu::Random_int ranint;
size_t num = atoi(argv[1]), min = atoi(argv[2]), max = atoi(argv[3]) + 1;
for (size_t i = 0; i < num; ++i)
{
out1 << ranint(min,max) << '\t';
}
// close outfile
out1.close();
// terminate program
std::cout << "File of int constructed:\n"
<< " filename: " << argv[4] << '\n'
<< " number of ints: " << num << '\n'
<< " int bounds: [" << min << ',' << max - 1 << "]\n";
return 0;
}