CSCI 2270
Computer Science 2:
Data Structures
Spring 2003
Karl Winklmann
Friday, April 25, 2003
The final covers sorting algorithms: selection sort, merge sort, quicksort, heapsort (all covered in Chapter 13), counting sort, radix sort, bucket sort (see sorting.html); hash tables (see Section 12.2); recursion (as used in Assignment 4); 2-D arrays (as used in Assignment 5); breadth-first search, including shortest path algorithms (Chapter 15) . Contrary to an earlier handout, parsing won't be on the final.
The exam is open textbook (but no other books), open notes. No use of computers. There are 5 problems, each worth 20 points.
void Graph::bfs ()
{
if (n == 0)
return;
queue <int> Q;
int i;
int j;
int node;
// to keep track of which nodes have been seen
bool* visited = new bool [n];
// initializing arrays
for (i = 0; i < n; i++)
visited [i] = false;
// initialize array that kleeps track
// of which edges have been added to the
// spanning tree
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
treeEdges [i][j] = false;
// seed queue
Q.push (0);
visited [0] = true;
// loop that does the search
while (!Q.empty ())
{
// removed node from queue
node = Q.front ();
Q.pop ();
// add its not yet visited neighbors to the queue
for (i = 0; i < n; i++)
{
if (edges [node][i] && !visited [i])
{
Q.push (i);
visited [i] = true;
// and put into the tree the edges that connect
// the newly added nodes to the just deleted one
treeEdges [node][i] = true;
treeEdges [i][node] = true;
}
}
}
// show the result
drawTree ();
// no memory leak here
delete [] visited;
}
bool * * edges;
void Graph::closure ()
{
// This function adds an edge from node i to node j
// whenever there is an edge from i to k and an
// edge from k to j, for some node k. (I.e., if one can get
// from i to j in two steps, this function puts in a direct
// connection.) It keeps doing that until no more edges
// get added.
int i;
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
}
Most (all?) of the following proposed hash functions have a serious flaw. Very briefly describe those flaws. (Assume that whatever number i gets computed, the subscript used is i%1024.)
___________________________________________________
___________________________________________________
___________________________________________________
___________________________________________________
[ ( x - 1 ) * ( [ 2 * x ] - 3 ) ]