Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Preface
This is a collection of typical problems recommended for the problem solving sessions (Lektionerna) of TDDC70/TDDC91 Data Structures and Algorithms and for self studies. The
collection consists of explicitly stated problems and of references to problems in the course
book: Michael T. Goodrich and Roberto Tamassia Data Structures & Algorithms in JAVA
Fifth Edition, John Wiley & Sons, Inc., 2010.
The collection developed gradually during the previous editions of this course. Many of the
problems were included in the previous exams. Most of the problems are of very basic nature,
reflecting the requirements to pass the course.
The problems are divided into 5 sections, corresponding to the five problem solving sessions
scheduled for TDDC70/TDDC91 in the academic year 2011/2012. Each section indicates
prioritized problems to be solved in the first place, preferably during or before the session.
Each section indicates also a number of selected problems in the course book.
8. Algorithms A and B have worst case time complexity, respectively, O(n2 ) and O(n log n).
On some data the running time of A is shorter than the running time of B. Give three
different possible reasons of this phenomenon. Is this phenomenon still possible if the
worst case time complexities are (n2 ) and (n log n).
9. Provide justified answers to the following questions:
(a) The worst case time complexity of an algorithm is (n). Is it possible that the
algorithm executes in time T (n) (n2 ) for some input data?
(b) The worst case time complexity of an algorithm is (n). Is it possible that the
algorithm executes in time T (n) (n2 ) for every input data?
(c) For the best case data the algorithm takes time (n). Is it possible that the
algorithm executes in time T (n) (n2 ) for some input data?
(d) For the best case data the algorithm takes time (n). Is it possible that the
algorithm executes in time T (n) (n2 ) for every input data?
10. Analyse the time complexity of the following algorithms:
(a)
procedure Mystery(integer n):
for i from 1 to n 1 do
for j from i + 1 to n do
for k from 1 to j do
an instruction that runs in time O(1)
(b)
(c)
(d)
length n.
function (integer x, table A[1..n]): boolean
f irst 1
last n
f ound false
while f irst last and not f ound do
index (f irst + last)/2
if i = A[index] then f ound true
elsif i < A[index] then last index 1
else f irst index + 1
return f ound
13. Write a recurrence equation characterising the time complexity of the following program:
function Factorial(integer n): integer
if n = 0 then return 1
else return n Factorial(n 1)
14. Analyse the time complexity and the space complexity of the following algorithm:
procedure PrintC(integer n):
array A[1..n]
for i from 1 to n do
A[i] 0
loop1:
i1
for j from 1 to n do
print A[j]
loop2:
A[i] A[i] + 1
if A[i] = 2 then
A[i] 0
i i+1
else
exit loop2
if i = n then
exit loop1
6. We use an array with indices 0 to 6 to implement a hash table of size 7. The keys of
the inserted elements are integers.
The hash value is calculated as the key value modulo the table length.
Show the contents of the initially empty array after performing each of the following
operations
Insert(15, c), Insert(8, a), Insert(14, b), Delete(15), Insert(32, d), Insert(4, e), Insert(7, f )
when the following technique is used:
(a) coalesced chaining,
(b) open addressing with linear probing,
(c) open addressing with double hashing, where h2 (k) = 5 k mod 5
The deletion is to be handled by the deleted bit technique.
7. Consider hashing based on open addressing with linear probing. What is the disadvantage of using the deleted bit marking technique instead of the delete technique based
on re-hashing of the probe sequence? Illustrate both techniques on an example.
8. The police plans to implement a register of stolen cars in form of a hash table using the
car identification numbers as keys. Each number consists of three letters followed be
three digits. Assume that all combinations appear with equal probability. The value
of the hash function is determined by two characters of the number. Which of the
following variants of the hash function give the best distribution in the hash table:
(a) The last two letters,
(b) The last two digits,
(c) The last letter and the last digit.
9. Consider the following binary tree.
a
b
c
b
c
Which of the four usual kinds of binary tree traversals visits the nodes in the alphabetic order of the labels?
10. Consider the following tree traversal algorithm, where the argument n refers to the root
of the tree.
procedure TraverseTree(n: treenode);
var S: ADT Stack
MakeEmptyStack(S)
Push(n)
while not IsEmptyStack(S) do
n Pop(S)
Print label of n
foreach child c of n in reverse order do
Push(c)
(a) Which of the traversal orders is implemented by this algorithm?
(b) Transform the algorithm, so that it uses ADT queue instead of ADT stack.
Which of the traversal orders is implemented by the transformed algorithm?
11. Show which of the following binary trees are
(a) full
(b) complete
(c) perfect
a
12. Show the array-list representation of each of the following binary trees.
0
a
2
1
3
c
d
f
e
j
h
15
6
21
10
18
25
...
...
.
...
...
.
...
.....
..
...
...
.
...
.....
..
...
...
...
...
4. Is it true that every AVL tree of height h has less nodes than an AVL tree of height
h + 1? Prove or disprove.
5. (a) Which of the following are AVL trees? Justify your answers.
28
21
32
21
T1
24
14
T2
31
7
24
14
18
18
24
24
T3
21
T4
21
14
31
14
7
32
18
7
18
17
40
48
50
75
62
Show step by step how the operations Insert(41), Insert(54), Delete(68) are
performed on the tree above (all operations are applied to the same tree). What
rotations are performed?
(c)
ii. Discuss advantages and disadvantages of splay trees with respect to AVL trees.
6. Following the definition of (2,4) tree in p. 465 and the definition of (a,b) tree in p. 679
of the course book define a notion of (2,3) tree. Give an example of a (2, 3) tree of
height 2, with all internal nodes being 3-nodes, and show how an example insertion
operation will be performed on it. Discuss how to represent (2, 3) trees in memory and
analyze the time complexity of the insertion operation on such trees.
1 2
2 3
2 1 1
4 5
2 2
6 7 8
2 2
9 10
11
1 1
12
13
8
7
10
6
11
12
Show how this tree is transformed by the following sequence of Find and Merge operations (in the course book called PathCompressFind and Union-by-Size):
Find(2); Merge(A,C); Find(4); Find(11)
10
Le 4 Sorting, Selection
Proritized problems: 1, 2, 3, 4, 5, 7, 9, R-11.9, R-11.10, 11, 12
Textbook problems: R-11.9-11, R-11.17-19, R-11.21, R-11.25
1. For the following sequence of integers:
[7, 4, 12, 2, 5]
illustrate the operation of:
(a) Insertion Sort.
(b) Heap Sort.
(c) In-place Quick Sort.
2. The following table is submitted as input data to a sorting algorithm.
8 7 6
5 4
3 2
5 4
3 7
(b) What requirement should be satisfied by Bucket Sort to use it in Radix Sort with
phases starting from the least significant fragments of the keys? Discuss, how to
implement Radix Sort with a variant of Bucket Sort which does not fulfill the
requirement.
12
Le 5 Graphs
Prioritized problems: 1, 2, 3, R-13.7, R-13.8, 4, 5, 7, 8
Textbook problems: R-13.3, R-13.6-9, R-13.12-14, R-13.16
1. In a search starting in vertex A of the following graph the vertices were visited in the
order indicated by the numbers.
2
5
E
B
.
1
3
A
D
4
1
5
3
8
f
9
c
7
(a) Show the order of visiting the nodes by depth-first search from vertex c: the
neighbours are selected in the increasing weights of the connecting edges.
(b) Show the order of visiting the nodes by breadth-first search from vertex c: the
neighbours are selected in the increasing weights of the connecting edges.
(c) Show a Topological Sort of the graph.
3. Consider the following operations of ADT Directed Graph:
Insertedge(v,u,G): insert the edge hv, ui in G
DeleteEdge(v,u,G): delete the edge hv, ui in G
First(v,G) = index of vs first neighbour (empty if v has no neighbours)
Next(i,v,G) = index of the next neighbour of v, counted from i (empty if there is no
such neighbour).
IsIndex(i,G) = true iff i is an index different from the empty index
Vertex(i,G) = vertex determined by index i
ExistEdge(v,u,G) = true iff the edge hv, ui is in G
13
Find lower and upper bounds of the asymptotic time complexity of these operations
for graphs represented as adjacency matrices and as adjacency lists. To characterize
argument graphs use the following parameters:
v number of vertices,
e number of edges,
k maximal number of edges outgoing from a vertex
Analyze the time complexity of the abstract instruction
foreach neighbour u of v do
foo
which now can be implemented as:
i F irst(v)
while IsIndex(i) do
foo
i N ext(i, v)
4. Design an algorithm that checks existence of cycles in undirected graphs. The algorithm
should work in time O(n + e) where n is the number of vertices and e is the number of
edges of the graph.
(a) Sketch the idea.
(b) Justify that the algorithm has the required complexity.
(c) Explain how to extend your algorithm so that it can check whether the input
graph is a tree.
5. A study curriculum includes (among others) the following courses: Discrete Math (DM),
Compiler Construction (CC), Data Structures (DS), Programming 1 (P1), Programming 2 (P2), Theory of Programming (TP), Analysis of Algorithms (AA), Formal Languages (FL) and Optimisation Techniques(OT). Each of the courses has some (possibly
none) other courses as prerequisites for admission. They are listed below:
DM prerequisites : none.
CC prerequisites : FL, P2.
DS: DM, P1.
P1: none.
P2: P1, TP.
TP: DM, P1.
AA: DS, P1.
FL: DM, DS.
OT: DS, P2.
(a) Represent the information as a graph G where courses correspond to vertices and
prerequisites correspond to edges. What kind of graph is it?
(b) Johan is a part-time student. He can only take one course at a time. Use a wellknown graph algorithm to determine a study plan for Johan. Show the consecutive
steps of the computation performed on the graph G.
(c) Each of the courses is taught during a whole term. Several courses may be taught
in the same term provided that all prerequisites were offered in previous terms.
What is the minimum number of terms needed to schedule all courses in the
14
example curriculum? Justify your answer. Use the notion of path length to give
a general answer to this question for an arbitrary graph of courses.
6. Modify the DFS algorithm so that it can be used for computing a topological sort of a
directed acyclic graph. Analyze the complexity of your solution and show by example
how it works.
7. Alice is building a holiday house. She has broken the project into the following tasks:
preparing the ground, building exterior walls, building interior walls, building the chimney, building the roof, painting the interior walls, laying floor, setting doors, setting
windows, covering the roof, insulating the exterior walls, moving in.
(a) There are some restrictions on the ordering of these tasks:
The ground must be prepared before building the exterior walls or the chimney.
To build the roof Alice must first build the external walls and the chimney.
The interior walls are built after the exterior walls.
The windows are set after building the exterior walls.
The doors are set after building the interior walls.
The floor can only be layed when the roof is covered and the interior walls
are built.
To cover the roof Alice must first build it.
The insulation of exterior walls can only be done when the windows and the
doors are already set.
Painting of the interior walls can only be done when the floor is ready.
Alice will not move in until the interior walls are painted and the exterior
walls are insulated.
Represent the above restrictions as a directed graph with vertices representing
tasks and edges representing their ordering. The graph must not introduce more
direct restrictions on task ordering than those stated above.
(b) Alice wants to perform each of the tasks during consecutive weekends. Which of
the graph algorithms discussed in the course can be used to schedule the tasks in
the way that all restrictions are observed. Explain how this algorithm works for
your graph by discussing the intermediate stages of its operation and show the
resulting schedule.
8. Illustrate the execution of Dijkstras algorithm on the following graph, with s as starting
vertex:
s
5
10
3
2
7
4
6
15