Académique Documents
Professionnel Documents
Culture Documents
Preliminaries
COMP 523: Advanced Algorithmic Techniques
Lecturer: Dariusz Kowalski
Lecture 1: Preliminaries
Announcements
Textbook: J. Kleinberg, E. Tardos. Algorithm Design.
Addison-Wesley, 2005
Lectures: Wednesday 9-11, Friday 15-16 in 3.10
Tutorials: Thursday 11-12 in 1.01 Ashton Bldg
Eleni Akrida (akridel@hotmail com)
Lecture 1: Preliminaries
Lecture 1: Preliminaries
Lecture 1: Preliminaries
Types of algorithms
Constructive vs. Non-constructive
Discrete vs. Numerical
Deterministic vs. non-deterministic (e.g.,
Randomized, Quantum)
Sequential vs. Concurrent vs. DNA vs.
Exact vs. Approximate
and many others
Lecture 1: Preliminaries
Example
Problem: Find if a given word occurs in a given
text
Input: Text and word represented as lists
Output: Word found or not
o
Lecture 1: Preliminaries
Example - algorithm
Nave solution (exhaustive search):
Intuition:
Check letter after letter, starting from the beginning of the lists, if the
corresponding letters are equal
If some corresponding letters are not equal, re-start comparing from the second
letter of the text (and the first letter of the word)
Etc. from the 3rd letter, 4th letter, until the end of text or successful comparison
Implementation:
Initiate three pointers:
blue_pointer and black_pointer at the beginning of blue_list
yellow_pointer at the beginning of yellow_list
stop := false
Move blue_pointer to the next element
Set black_pointer to blue_pointer
Move yellow_pointer to the first element of yellow_list
Repeat
Move black_pointer and yellow_pointer to the next elements in
corresponding lists
If values pointed by black_pointer and yellow_pointer are different
then stop := true
Repeat
stop := false,
move blue_pointer to the next element, set black_pointer to blue_pointer,
move yellow_pointer to the first element of yellow_list
Repeat
Move black_pointer and yellow_pointer to the next elements in corresponding lists
If values pointed by black_pointer and yellow_pointer are different then stop := true
Methods of measuring
Worst-case
Average-case
Smoothed (a subset of possible inputs, specific distribution of inputs, etc.)
Competitive (comparing to the best solution for particular input)
Expected
Lecture 1: Preliminaries
10
Example - correctness
Successful is set to true iff after some
execution of the internal loop the
yellow_pointer is at the end of the
yellow_list
yellow_pointer is at the end of the
yellow_list iff it came through the
whole yellow_list without coming back
to the beginning
it happens if the values pointed by
black_pointer and yellow_pointer
have been the same during all checks of
internal loop in the current run (thus a
copy of the word exists in the text)
Repeat
stop := false,
move blue_pointer to the next element,
set black_pointer to blue_pointer,
move yellow_pointer to the first element
of yellow_list
Repeat
Move black_pointer and yellow_pointer
to the next elements in corresponding
lists
If values pointed by black_pointer and
yellow_pointer are different then stop :=
true
11
Example - termination
External Loop Invariant:
Repeat
stop := false,
At the beginning of the external loop,
move blue_pointer to the next element,
blue_pointer advances, and it has a finite
set black_pointer to blue_pointer,
number of advances to take before
move yellow_pointer to the first element
reaching the end of the text
of yellow_list
Repeat
Internal Loop Invariant:
Move black_pointer and yellow_pointer
to the next elements in corresponding
Each run of the internal loop finishes
lists
eventually;
If values pointed by black_pointer and
yellow_pointer are different then stop :=
It happens because yellow_pointer keeps
true
until yellow_pointer or black_pointer
advancing every iteration (first line of the
is at the end of its list or stop
loop), unless stop condition becomes true
If yellow_pointer is at the end of
or it reaches the end of the word (or
yellow_list and not stop then
successful := true
black_ pointer reaches the end of the text)
until successful or blue_pointer is at the
end of blue_list
Lecture 1: Preliminaries
12
Example - efficiency
Complexity measures: time, size of additional memory
Size of additional memory:
3 single pointers and 2 binary variables
Time complexity:
Worst-case: if the sequence is not included in the text then
time is (almost) proportional to the multiplication of sizes of
blue and yellow lists, e.g.,
text: aab repeated n times
word: aaa
1, 2 or 3 internal loop runs, 3n times, gives about 6n runs
Lecture 1: Preliminaries
13
Lecture 1: Preliminaries
14
15
O((n/2)(n/2)) = O(n2)
If size of text is m and size of sequence is k :
O((m-k)k)
Lecture 1: Preliminaries
16
Examples
5n3+100 = O(n3) , 5n3+100 O(n2)
5n3+100 = (n3) , 5n3+100 (n4)
log n = o(na)
for any positive constant a
na = o(cn)
for any positive constants a,c
log (4n) = log n + log 4 = O(log n)
log (n4)= 4 log n = O(log n)
(4n)3 = 64n3 = O(n3)
(n4)3 = n12 = (n4)
(3n)4 = 81n = (3n)
Logarithms are to the base 2
Lecture 1: Preliminaries
17
18
Lecture 1: Preliminaries
19
Example:
3n2 + n +7 = (3n2) = (n2)
If the range of the summation index is not
constant:
in i = n(n+1)/2 = (n2)
(max{1, , n}) = (n)
Lecture 1: Preliminaries
20
Lecture 1: Preliminaries
21
Lecture 1: Preliminaries
22
Lecture 1: Preliminaries
23
Lecture 1: Preliminaries
24
Lecture 1: Preliminaries
25
Lecture 1: Preliminaries
26
Lecture 1: Preliminaries
27
Graphs
Set of nodes |V| = n
Set of edges |E| = m
Undirected edges/graph: pairs of nodes {v,w}
Directed edges/graph: pairs of nodes (v,w)
28
Cycle
Tree
Clique
Lecture 1: Preliminaries
29
Conclusions
Algorithm: list/structure of instructions
Algorithmic methods for problems arising
from computer and communication
applications
Guarantee correctness, termination and
efficiency
Model is important!
30
31