Algorithms and Data Structures

Algorithms and Data
Structures
Lecture XV
Simonas Šaltenis
Aalborg University
[email protected]
December 4, 2003
1
December 4, 2003
DT
's
8.
B
ina
ry
S
7.
Ha
sh
ing
ea
rc h
9.
Tre
Re
es
d-B
10
.D
lac
yn
kT
am
ree
ic
s
pro
gra
mm
ing
11
.
12
BF
.M
S,
in.
DF
Sp
S
an
nin
gT
ree
s
6.
A

1.
Bru
2.
teCo
for
rre
ce
ctn
es
s,
3.
B ig
Div
ide
-O
-an
d-C
on
qu
er
4.
Re
cu
rre
nc
es
5.
So
rtin
g
Results of the questionnaire
Problematic lectures:
60
50
40
30
20
10
0
2
This Lecture

What have we learned?


Summary-Pensum
Solving exercises




Recurrences
Graph exercises
ADT exercises
Dvide and conquer algorithms
December 4, 2003
3
The Course – Pensum

Toolbox of algorithmic techniques

Divide and Conquer


Dynamic programming


Matrix chain multiplication, Longest Common Subsequence –
not in exam pensum, except basic understanding of LCS
Greedy algorithms


Merge sort, Quicksort, Binary search
Prim’s, Kruskal’s, Dijkstra’s
Analysis of algorithms:

Correctness of algorithms



Loop invariants – not in exam pensum
Asymptotic notations
Recurrences
December 4, 2003
4
The Course – Pensum (2)


Building algorithms - concept of ADT
Toolbox of data structures:

Simple data structures and ADTs


array, all sorts of linked lists, stacks, queues, trees,
heaps
Dictionaries



hash tables
binary search trees (unbalanced)
red-black trees
December 4, 2003
5
The Course – Pensum (3)

Toolbox of algorithms:

Sorting





insertion sort, selection sort
merge sort
quick sort
heap sort (priority queues)
Graphs


memory representation
graph traversal




breadth-first search
depth-first search (topological sort)
minimum spanning trees (Prim, Kruskal)
shortest path (Dijkstra, Bellman-Ford)
December 4, 2003
6
The Exam


The exam will be written, you will have
three hours.
Two parts:


Multiple-choice, “quiz”-style part – tests basic
knowledge, understanding of the core
concepts
“Creative exercises” – come up with
algorithms, analyze them, argue for your
solutions.
December 4, 2003
7
Preparation for the Exam

To prepare for the exam:


Concentrate on solving the exercises, not studying the
textbook (although you have to know the concepts!)
Solve exercises from last year exams


On Jan 16, 13:00 come to B2-104 (here) and have
your questions answered.


When solving write your solutions down!
Or pass by my office E1-215b!
Good news: solutions to hand-in exercises will be
posted on the web before Christmas!
December 4, 2003
8
Solving Recurrences

Repeated substitution procedure:





Substitute, expand, substitute, expand…
Observe a pattern and write how your expression looks
after the i-th substitution
Find out what the value of i (e.g., lgn or n-1) should
be to get the base case of the recurrence (e.g., T(1))
Insert the value of T(1) and the expression of i into
your expression
Compute your recurrence for small values and check if
your general solution gives the same results!
December 4, 2003
9
Types of Recurrences

Number of substitutions i :

If T(n) = …T(n - 1)… and base case T(k) = …


then i = n – k
If T(n) = …T(n/b)… and base case T(k) = …

then i = logb(n/k)
December 4, 2003
10
Types of recurrences II


T(n) = aT(n – b) + c
T(n) = aT(n/b) + cn + d


T(n)=T(n – b) + cn + d


Geometric series
Arithmetic series
Prove it yourself!
December 4, 2003
11
Graph Exercises

Types of graph exercises:


Decide what are the vertices and what are the
edges
Un-weighted graphs

“Traverse and check(do) something” => use DFS or
BFS


Find shortest paths => use BFS
Schedule or order dependent activities or processes
=> use Topological sorting
December 4, 2003
12
Graph Exercises

Types of graph exercises:

Weighted graphs



Decide what are the weights
Shortest paths, shortest times => use Dijkstra’s
Minimum (maximum) spanning tree => use Prim’s
or Kruskal’s

Finding a critical path in a schedule (longest path in
a DAG) => use Topological sorting on a DAG of
activities
December 4, 2003
13
Do not forget:


Write preconditions (INPUT) and
postconditions (OUTPUT) of your
algorithms
You can only use the ADT operations
provided in the exercise !!!
December 4, 2003
14