NAME: ......................................... This is not an example sheet, rather a

advertisement
1
NAME: .........................................
This is not an example sheet, rather a set of example questions required for AA final exam. Real exam
sheet will be larger as it will include questions for marks higher than 3.
Write your name on every page. Unless suggested differently more than one option can sometimes be
true. Entirely correct answer earns one point (except the last question). No half points. Assessing rules
may be stricter in case of the retake. Exam will be completed orally once this sheet is assessed. Good luck!
1) P = “barabimbaraboa”. What is the (ordinary/weak)
partial match table for KMP algorithm.
Answer:
Answer:
bool visited[G.size()];
bool isacyclic(G) {
b a r a I m b a r a b o a
0 0 0 0 0 0 1 2 3 4 1 0 0
2) Given the graph, draw its DFS forest if vertices and
edges are always iterated over in increasing order of
their ids.
for(int i=0; i < G.size(); i++)
visited[i] = false;
return dive(0,-1);
}
bool dive(int to, int from) {
for(int i=0; i < G[to].size(); i++) {
if (G[to][i] != from) {
if (visited[i])
return false;
visited[i] = true;
if (!dive(G[to][i],to))
return false;
}
}
return true;
}
4) What is 𝝅 table (predecessor) for the given graph
produced by (unweighted) shortest path algorithm
(BFS) with starting point 1 and adjacent vertices
being iterated over in increasing order of ids.
3) Devise an algorithm to check whether a given
connected undirected graph G is acyclic or not. Use
a C++ pseudocode. Graph G is represented by a
std::vector<vector<int>> i.e.:
G.size() returns number of vertices.
G[i], for i = 1, …, G.size()-1 are
std::vector<int>’s of adjacent vertices (aka
neighbours) of vertex i.
2
5) Given hash function 𝒉(𝒌) = (𝟑𝒌 + 𝟏)%𝟕, in which
of the seven slots (0,…,6) the key 12 is stored if we
are using linear probing and the following sequence
operations has been performed on initially empty
hash table:
insert 5, insert 8, insert 12
Answer: 3
6) Describe what is done in Graham scan algorithm to
find convex hull of the following set of points:
(0,1), (1,4), (3,3), (4,5), (4,2)
Answer:
Chose point with smallest y-coordinate -> x=(0,1)
Sort other points y wrt. angle between vector xy
and x axis, append point x to both ends:
(0,1), (4,2), (3,3), (4,5), (1,4), (0,1)
Put first three points on stack S. Take (4,5) as a
candidate. Since vector (3,3)(4,5) is turn right (1,2)(3,3)
we pop (3,3) off the stack. Now since (0,1)(4,2) to
(4,2)(4,5) is turn left, we push (4,5) onto the stack.
Take (1,4) as a candidate … is turn left thus we push
(1,4) onto S.
Take (0,1) as a candidate … is turn left thus we push
(0,1) onto the stack.
Stack S contains points of the convex hull in “pop off”
order (with point x doubled).
NAME: .........................................
Download