Algorithms and Data Structures Final Exam
100 Points
Fill in the Blank (1 point each)
1. After many insertions and deletions in a hash table, it is possible that every unused node is marked as
having been deleted. The process used to fix the problem is te
Chapter 6:
Priority Queues, AKA
Heaps
1
Not Quite Queues
Consider applications
ordering CPU jobs
searching for the exit in a maze (or looking for
moves in the 8 puzzle game)
emergency room admission processing
Goals
short jobs should go first
most promisi
Biconnected Components
An undirected graph is biconnected if there are no vertices whose removal disconnects
the rest of the graph.
This is important for computer networks (being able to route messages to all machines)
and traffic routes (able to get ever
Finding Strong Components
1. First do a depth-first search, numbering nodes in postorder. When tracing on paper, I
find it easiest to highlight the edges so I can keep the traversal straight. We may have to
restart several times. In this case, we start at
Spanning Tree
For the graph below, find a spanning tree rooted at A using a depth first ordering. Shade the edges
which are part of your spanning tree. Number the nodes as you encounter them. When you have
multiple choices for successors, consider them in
class EdgeNode
cfw_ public:
int target; / Node id of target of edge
int source; / Node id of source of edge
EdgeNode *next; / next edge in linked list for adjacency list representation
EdgeNode( int F = -1, int T=-1, int d=0, EdgeNode *N = NULL):
source(F
Leftist Heaps
null path length of a binary tree is the shortest path from the node to a node without two children.
[We could have done with pointer reference but used return value to see a different style.]
Node * Merge (Node * t1, Node * t2) / t1 and t2
Chapter 7
Sorting
Terminology
Internal sort done totally in main memory.
External uses auxiliary storage (disk).
Stable retains original order if keys are the same. In asking if a sort is stable, we are
asking can it be reasonably coded to be stable.
Adap
Single Source Shortest Path Algorithm (unweighted graph)
For the unweighted graph below, find the shortest path (in terms of number of
segments) between 7 and every other node.
What is the algorithm you are using?
If edges have lengths, how does your algo
Chapter 6
Priority Queues
Introduction
Note the story. We have a need (delete based on priority) and look for various ways of
accomplishing this. Many of the options were created to address a new twist on the basic desired
functionality. Notice that effic
Chapter 8
The Disjoint Set Class
Equivalence Relations
A relation R is defined on a set S if for every pair of elements (a, b), , a R b is either true
or false. If a R b is true, we say that a is related to b.
An equivalence relation is a relation R that
Graph Storage: Adjacency List
For a sparse graph, a better solution (as far as representation) is an adjacency list.
Each node points to a linked list of successors
Note there are two types of structures (or C+ classes) nodes and edges.
For the graph, the
B+-Trees
Motivation
An AVL tree with N nodes is an excellent data
structure for searching, indexing, etc.
The Big-Oh analysis shows that insert/delete/find
operations finish within O(log N) time
However, trees require that all data fit into the main
memor
Chapter 4
Trees
Binary Search Trees
Definition
o All keys are distinct why?
o Key in the root of the left subtree is less than the root.
o Key in the root of the right subtree is greater than the root.
o Left and right subtrees are binary search trees.
Fi
Chapter 9
Graphs
Introduction
What are some characteristics of a binary tree?
o They have a rigid structure of each node having a single node that points to it (or
none, in the case of the root). Sometimes life isnt so structured.
For example: I need to f
PassingReferences
Let's talk about passing references. I think you already understand most of this, but a review never hurts. In C+
+, parameters are passed by value (normally). This means that a copy of the data is made.
For example, in the following pro
When does complexity
matter?
Study the table below. Note that even for small n
(1000), time is measured in years for 2n.
log n
n
n3
n2
n log n
2
n
0
1
0
1
1
2
1
2
2
4
8
4
2
4
8
16
64
16
3
8
24
64
512
256
4
16
64
256
4096
65,536
5
32
160
1024
32,788
4,294,
Chapter 2
Algorithm Analysis
Problem: Given an array of positive integers, find a pair of integers in the array that adds up to
10 (or indicate none exists). Your code should be as efficient as possible.
Im not as interested in syntax as I am in the algor
Complexity
O Notation (Big Oh)
We want to give an upper bound on the
amount of time it takes to solve a problem.
Definition: constant c and such that
t(n) <= cf(n) whenever n>= n0.
The growth rate of the time to execute, t(n), is
less than or equal to tha
B+-Trees
Motivation
An AVL tree with N nodes is an excellent data
structure for searching, indexing, etc.
The Big-Oh analysis shows that insert/delete/find
operations finish within O(log N) time
However, trees require that all data fit into the main
memor
Chapter 5
Hash Tables
Dictionary
All elements have a unique key.
Operations:
o Insert element with a specified key.
o Search for element by key.
o Delete element by key.
Random vs. sequential access.
If we have duplicate keys, need rule to resolve ambigui
Complexity of Loops
Getting Started
1.
Generate the code.
The code is automatically updated as you make changes to the number of loops as well as the
maximum loop index values.
2. See the complexity of current code in pictoral format by clicking Play. You
CS 2420 Exam 2
Fill in the blank (1 point each blank):
1._probingissimpletoimplement,buttypicallyresultsinprimary_,
whichcausesperformancetodegraderapidly.
2._probingstillcausessecondaryclustering.
3.Recorddeletionisstraightforwardwhen_isusedtoresolvecoll
CS 2420 Spring 2013
Practice Exam
1. What is the complexity of the following piece of code? Draw a picture.
int increment=0;
for(int i=0; i<n; j+)cfw_
for(int j=i; j<n; j+)cfw_
increment+;
2. What is the complexity of the following piece of code? Draw a
CS 2420 Exam 2
Fill in the blank (1 point each blank):
1._ probing is simple to implement, but typically results in primary
_, which causes performance to degrade rapidly.
2._ probing still causes secondary clustering.
3. Record deletion is straightforwar