King Fahd University of Petroleum and Minerals

advertisement
1
King Fahd University of Petroleum and Minerals
College of Computer Science and Engineering
Information and Computer Science Department
ICS 202: Data Structures
Spring 2008-2009 (082)
Final Exam, Sunday June 21, 2009.
Time: 120 minutes
Name:
ID#:
Circle Your Section
Question #
Maximum Mark
01
Hamdi Yahyaoui
SMW 9 – 9:50AM
I
45
II
55
III
25
IV
30
V
45
Total
200
02
Wasfi Al-Khatib
SMW 10 – 10:50AM
03
El-Sayed El-Alfy
SMW 11 – 11:50AM
Obtained Mark
Instructions:
1. Write your name and ID in the respective boxes above and circle your section.
2. This exam consists of 5 questions on 11 pages, including this cover page. Also a two
page reference sheet will be distributed which you can keep.
3. You have to answer all 5 questions.
4. The exam is closed book and closed notes. No calculators or any helping aides are
allowed. Make sure you turn off your mobile phone and keep it in your pocket if you
have one.
5. The questions are not equally weighed.
6. The maximum number of points for this exam is 200.
7. You have exactly 120 minutes to finish the exam.
8. Make sure that your answers are readable, and include intermediate steps.
9. If there is no space on the front of the page, feel free to use the back of the page.
2
QI (45 points): Complexity Analysis
1. (30 points) Consider the following piece of code, where n is a positive integer:
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
int Complex(int n) {
sum = 0;
2
for (k=n; k <= n ; k++) {
if (MyFunction(k) > 0) {
for (j=1; j <= k ; j*=2)
sum++;
}
}
return sum;
}
Also, assume that method MyFunction(k) costs O(1) for any integer input
value k.
(a) (5 points) Which statement would you choose to count if you are asked to
find the best case time complexity of method Complex.
(b) (7 points) Clearly compute the best case time complexity of the algorithm,
finding the maximum number of times the statement chosen in part (a) gets
executed. Then, express the result in terms of Big O() notation.
(c) (5 points) Which statement would you choose to count if you are asked to
find the worst case time complexity of method Complex.
(d) (13 points) Clearly compute the worst case time complexity of the
algorithm, finding the maximum number of times the statement chosen in
part (c) gets executed. Then, express the result in terms of Big O() notation.
3
2. (15 points) Consider the following method
void Interesting(int i) {
if (i > 1) {
Interesting(i-1);
System.out.println(“ i = “ + i);
Interesting(i-1);
}
}
Find the exact number of times the println statement gets executed when method
Interesting is called with positive integer n as its parameter. Then, express the result
in terms of Big O() notation.
4
QII (55 points): Graph Applications
1.
(27 points) Use the following graph to trace the execution of Dijkstra’s algorithm as
it solves the shortest path problem starting from vertex B. Provide the table and
draw the resulting vertex-weighted graph.
3
Start
A
B
C
2
5
6
4
1
2
D
E
F
3
2
G
A
Initial (B)
Process B
Predecessor
2
4
2
B
1
H
C
D
3
E
F
I
G
H
I
2.
5
(28 points) Given the following graph:
Start
A
(a)
B
C
D
E
F
G
H
I
(10 points) List the vertices in the order they will be visited using pre-order
depth-first traversal, starting from vertex B.
Pre-Order
(b)
(8 points) Classify the edges of the pre-order depth-first traversal of part (a)
into tree edges, back edges, forward edges and cross edges. Use the graph
below.
Start
A
B
C
D
E
F
G
H
I
6
(c)
(10 points) List the vertices in the order they will be visited using
Topological order traversal.
Note: In all the traversals, if at any point there is more than one possible vertex to
visit, visit them alphabetically.
Start
Topological
Order
A
B
C
D
E
F
G
H
I
7
QIII (25 points): Algorithms and Problem Solving
1.
(15 points) Suppose the BinaryTree class has the following method that calls a
private recursive method of the BinaryTree class:
public int minHeight(Object targetKey){
if(isEmpty())
throw new IllegalArgumentEXception("The tree is empty");
else
return minHeight(targetKey, getHeight());
}
Write the recursive method:
private int minHeight(Object targetKey, int height)
such that it returns the minimum height of all subtrees in the invoking tree that have
targetKey as the root key.
Note: Your recursive method may use other BinaryTree methods and BinaryTree
instance variables; but it MUST NOT USE iterators, loops, traversal (or equivalent)
methods, additional instance or static variables.
YOUR METHOD MUST ALSO BE GENERAL AND NOT SPECIFIC TO THE
EXAMPLE BELOW:
Example: Suppose targetKey is the Integer 6, then there are 4 subtrees in the tree
below that have 6 as the root key, namely the subtrees A, B, C, and D:
In this case your method will return the height of D; because it is the minimum height
of the heights of subtrees A, B, C and D
8
2.
(10 points) Write a method public void withdrawEdge(String from,
String to) of the GraphAsArrayLists class that deletes the edge (from, to)
from the invoking directed graph. Your method must throw an appropriate
exception if the graph is not directed or if (from, to) is not a valid edge.
9
QIV (30 points):.Hashing
1.
(10 points) What is meant by secondary clustering in hashing? Clearly explain one
method to avoid this problem.
2.
(20 points) Use double hashing with the first hash function
h(key) = key % 11
and the second hash function
hp(key) = 1 + key % 10
to insert the keys
31, 32, 29, 18, 7, 20, 26, 15, 23
into an initially empty hash table of size 11. Show all computations below.
Index
Key
0
1
2
3
4
5
6
7
8
9
10
10
QV (45 points): LZ-Based Compression and Garbage Collection
1.
(25points) Consider the following ASCII codes for the following letters, where each
letter is represented by 8 bits.
Character
ASCII Code
Character
ASCII Code
A
65
S
83
C
67
T
84
I
73
(a) (20 points) Use LZW and show clear steps to encode the following string:
SATATASACITASA
11
(b) (5 points) Compute the compression ratio for the previous string, assuming that
each codeword is stored using 12 bits.
2.
(10 points) Describe how the stop-and-copy garbage collector works and mention
one advantage and one disadvantage of it.
3.
(10 points) Mention two advantages and two disadvantages of the reference count
method of the garbage collector
1
ICS 202 – Data Structures
Quick Reference Sheet
public interface Iterator {
public class MyLinkedList {
boolean hasNext( );
public void purge( )
Object next( ) throws NoSuchElementException;
public Element getHead( )
}
public Element getTail( )
public Element find(Object obj)
public interface Visitor {
public boolean isEmpty( )
void visit (Object object);
public Object getFirst( )
boolean isDone( );
public Object getLast( )
}
public void prepend(Object obj)
public interface Container {
public void append(Object obj)
int getCount( );
public void assign(MyLinkedList list)
boolean isEmpty( );
public void extract(Object obj)
boolean isFull( );
public void extractFirst( )
void purge( );
public void extractLast( )
void accept (Visitor visitor);
public String toString( )
Iterator iterator( );
public Iterator iterator( )
}
public final class Element {
public interface SearchableContainer extends Container {
public Object getData( )
boolean isMember (Comparable object);
public Element getNext( )
void insert (Comparable object);
public void insertAfter(Object obj)
void withdraw (Comparable obj);
public void insertBefore(Object obj)
Comparable find (Comparable object);
public void extract( )
}
}
}
public class Association implements Comparable
public Association(Comparable key, Object val)
public Association(Comparable key)
public class AVLTree extends BinarySearchTree {
public Comparable getKey( )
public AVLTree( )
public Object getValue( )
public int getHeight( )
public void setKey(Comparable key)
public void insert(Comparable comparable)
public void setValue(Object value )
public void attachKey(Object obj)
public int compareTo(Object obj)
public Object detachKey( )
public boolean equals(Object obj)
}
public String toString( )
}
𝑛
𝑛
𝑛(𝑛 + 1)
𝑛(𝑛 + 1)(2𝑛 + 1)
∑𝑖 =
, ∑ 𝑖2 =
public interface Stack extends Container {
2
6
Object getTop( );
𝑖=1
𝑖=1
void push(Object obj);
Object pop( );
𝑛
𝑛
2
}
𝑛(𝑛 + 1)
𝑎𝑛+1 − 1
3
∑𝑖 = (
,𝑎 ≠ 1
) , ∑ 𝑎𝑖 =
2
𝑎−1
𝑖=1
𝑖=0
public interface Queue extends Container {
Object getHead( );
ln 𝑎
void enqueue(Object obj);
log𝑏 𝑎 =
, log 𝑎𝑏 = log 𝑎 + log 𝑏
ln 𝑏
Object dequeue( );
}
𝑎
log = log 𝑎 − log 𝑏 , 𝑎log𝑎 𝑏 = 𝑏
𝑏
𝑐
(𝑎𝑏 ) = (𝑎𝑐 )𝑏 = 𝑎𝑏𝑐
2
public class BinaryTree extends AbstractContainer
implements Comparable{ public BinaryTree(Object
obj, BinaryTree left, BinaryTree right)
public BinaryTree( )
public BinaryTree(Object obj)
public void purge( )
public boolean isLeaf( )
public boolean isEmpty( )
public Object getKey( )
public BinaryTree getLeft( )
public BinaryTree getRight( )
public void attachKey(Object obj)
public Object detachKey( )
public void preorderTraversal(Visitor v)
public void inorderTraversal(Visitor v)
public void postorderTraversal(Visitor v)
public void breadthFirstTraversal(Visitor visitor)
public void accept(Visitor visitor)
public boolean isMember(Object obj)
public int getHeight()
}
public class BinarySearchTree extends BinaryTree {
private BinarySearchTree getLeftBST( )
private BinarySearchTree getRightBST( )
public boolean isMember(Comparable c)
public Comparable find(Comparable c)
public Comparable findMin( )
public Comparable findMax( )
public void attachKey(Object obj)
public void insert(Comparable comparable)
public void withdraw(Comparable comparable)
}
// implemented by MinHeap
public interface PriorityQueue extends Container{
public abstract void enqueue(Comparable c);
public abstract Comparable findMin( );
public abstract Comparable dequeueMin( );
}
public abstract class AbstractGraph implements Graph {
public AbstractGraph(boolean directed)
}
public class GraphAsArrayLists extends AbstractGraph
{
public GraphAsArrayLists(int size, boolean
directed)
}
public interface Graph{
public int getNumberOfEdges( );
public int getNumberOfVertices( );
public Iterator getVertices( );
public Iterator getEdges( );
public void addVertex(String label);
public void addVertex(String label, Comparable weight);
public Vertex getVertex(String label);
public int getIndex(Vertex v);
public void addEdge(String from, String to);
public void addEdge(String from, String to, Comparable
weight);
public Edge getEdge(String from, String to);
public boolean isReachable(String from, String to);
public boolean isDirected( );
public boolean isWeighted( );
public boolean isConnected( );
public abstract boolean isStronglyConnected( );
public abstract boolean isWeaklyConnected( );
public boolean isCyclic( );
public void preorderDepthFirstTraversal(Visitor visitor,
Vertex start);
public void postorderDepthFirstTraversal(Visitor visitor,
Vertex start);
public void breadthFirstTraversal(Visitor visitor, Vertex
start);
public abstract int topologicalOrderTraversal(Visitor visitor);
}
public interface Edge extends Comparable{
public abstract Vertex getFromVertex( );
public abstract Vertex getToVertex( );
public abstract Comparable getWeight( );
public abstract boolean isDirected( );
public abstract Vertex getMate(Vertex vertex);
}
public interface Vertex extends Comparable{
public String getLabel( );
public Comparable getWeight( );
public Iterator getIncidentEdges( );
public Iterator getEmanatingEdges( );
public Iterator getPredecessors( );
public Iterator getSuccessors( );
}
Download