Chapter 01
Chapter 01
Hanoi, 2012
Pham Quang Dung () Data structures and Algorithms Basic definitions and notations Hanoi, 2012 1 / 46
Outline
1 First example
3 Big-Oh notation
4 Pseudo code
5 Analysis of algorithms
Pham Quang Dung () Data structures and Algorithms Basic definitions and notations Hanoi, 2012 2 / 46
First example
Pham Quang Dung () Data structures and Algorithms Basic definitions and notations Hanoi, 2012 3 / 46
Direct algorithm
n n2 +n
Scan all possible subsequences 2 = 2
Compute and keep the largest weight subsequence
C++ code :
Pham Quang Dung () Data structures and Algorithms Basic definitions and notations Hanoi, 2012 4 / 46
Direct algorithm
Faster algorithm
Pj Pj−1
Observation : k=i a[k] = a[j] + k=i a[k]
C++ code :
n2 n
Complexity : 2 + 2
Pham Quang Dung () Data structures and Algorithms Basic definitions and notations Hanoi, 2012 5 / 46
Recursive algorithm
Divide the sequence into 2 subsequences at the middle s = s1 :: s2
The largest subsequence might
be in s1 or
be in s2 or
start at some position of s1 and end at some position of s2
C++ code :
Pham Quang Dung () Data structures and Algorithms Basic definitions and notations Hanoi, 2012 6 / 46
Recursive algorithm
Pham Quang Dung () Data structures and Algorithms Basic definitions and notations Hanoi, 2012 7 / 46
Dynamic programming
General principle
Division : divide the initial problem into smaller similar problems
(subproblems)
Storing solutions to subproblems : store the solution to subproblems
into memory
Aggregation : establish the solution to the initial problem by
aggregating solutions to subproblems stored in the memory
Pham Quang Dung () Data structures and Algorithms Basic definitions and notations Hanoi, 2012 8 / 46
Dynamic programming
Largest subsequence
Division :
Let si be the weight of the largest subsequence of a1 , . . . , ai ending at
ai
Aggregation :
s1 = a1
si = max{si−1 + ai , ai }, ∀i = 2, . . . , n
Solution to the original problem is max{s1 , . . . , sn }
Number of basic operations is n (best algorithm)
Pham Quang Dung () Data structures and Algorithms Basic definitions and notations Hanoi, 2012 9 / 46
Comparison between algorithms
Pham Quang Dung () Data structures and Algorithms Basic definitions and notations Hanoi, 2012 10 / 46
Outline
1 First example
3 Big-Oh notation
4 Pseudo code
5 Analysis of algorithms
Pham Quang Dung () Data structures and Algorithms Basic definitions and notations Hanoi, 2012 11 / 46
Algorithms and complexity
Definition
Informally, an algorithm is any well-defined computational procedure that
takes a set of values, as input, and produces a set of values, as output
Input
Output
Precision
Finiteness
Uniqueness
Generality
Pham Quang Dung () Data structures and Algorithms Basic definitions and notations Hanoi, 2012 12 / 46
Complexity
Definition
The size of input is defined to be the number of necessary bits for
representing it
Definition
The basic operations are the operations which can be performed in a
bounded time, and do not depend on the size of the input
Pham Quang Dung () Data structures and Algorithms Basic definitions and notations Hanoi, 2012 13 / 46
Complexity
Pham Quang Dung () Data structures and Algorithms Basic definitions and notations Hanoi, 2012 14 / 46
Outline
1 First example
3 Big-Oh notation
4 Pseudo code
5 Analysis of algorithms
Pham Quang Dung () Data structures and Algorithms Basic definitions and notations Hanoi, 2012 15 / 46
Big-Oh notation
Given a fucntion g (n), we denote :
Θ(g (n)) = {f (n) : ∃c1 , c2 , n0 s.t. 0 ≤ c1 g (n) ≤ f (n) ≤ c2 g (n), ∀n ≥
n0 }
Example :
10n2 − 3n = Θ(n2 )
Pham Quang Dung () Data structures and Algorithms Basic definitions and notations Hanoi, 2012 17 / 46
Big-Oh notation
Pham Quang Dung () Data structures and Algorithms Basic definitions and notations Hanoi, 2012 18 / 46
Big-Oh notation
When we say ”the time complexity is O(f (n))” : the time complexity
in the worst case is O(f (n))
When we say ”the time complexity is Ω(f (n))” : the time complexity
in the best case is Ω(f (n))
Pham Quang Dung () Data structures and Algorithms Basic definitions and notations Hanoi, 2012 19 / 46
Little-o notation
Pham Quang Dung () Data structures and Algorithms Basic definitions and notations Hanoi, 2012 20 / 46
Little-o notation
Pham Quang Dung () Data structures and Algorithms Basic definitions and notations Hanoi, 2012 21 / 46
Big-Oh notation
f (n)
lim < ∞ ⇒ f (n) = O(g (n))
n→∞ g (n)
f (n)
lim > 0 ⇒ f (n) = Ω(g (n))
n→∞ g (n)
f (n)
0 < lim < ∞ ⇒ f (n) = Θ(g (n))
n→∞ g (n)
f (n)
lim = 0 ⇒ f (n) = o(g (n))
n→∞ g (n)
f (n)
lim = ∞ ⇒ f (n) = ω(g (n))
n→∞ g (n)
Pham Quang Dung () Data structures and Algorithms Basic definitions and notations Hanoi, 2012 22 / 46
Big-Oh notation
O(lgn) = O(lnn)
lg a n = o(nb ) where a, b are constant
n! = o(nn )
n! = ω(2n )
logn! = Θ(nlogn)
Pham Quang Dung () Data structures and Algorithms Basic definitions and notations Hanoi, 2012 23 / 46
Big-Oh notation
Example
A = log3 (n2 ) vs. B = log2 (n3 )
A = 2log3 n = 2lnn/ln3 where we denote lnx = loge (x)
B = 3log2 n = 3lnn/ln2
A
B = constant ⇒ A = Θ(B)
Pham Quang Dung () Data structures and Algorithms Basic definitions and notations Hanoi, 2012 24 / 46
Big-Oh notation
Example
A = nlg 4 vs. B = 3lgn where we denote lgx = log2 x
B = 3lgn = nlg 3 (logb a = loga b)
A
B = nlg (4/3) → ∞
A = ω(b)
Pham Quang Dung () Data structures and Algorithms Basic definitions and notations Hanoi, 2012 25 / 46
Big-Oh notation
Example
A = lg 2 n vs. B = n1/2 where we denote lgx = log2 x
A lg 2 n
lim = lim 1/2 = 0
n→∞ B n→∞ n
⇒ A = o(B)
Pham Quang Dung () Data structures and Algorithms Basic definitions and notations Hanoi, 2012 26 / 46
Big-Oh notation
Properties
f (n) = Θ(g (n)) ∧ g (n) = Θ(h(n)) ⇒ f (n) = Θ(h(n))
f (n) = O(g (n)) ∧ g (n) = O(h(n)) ⇒ f (n) = O(h(n))
f (n) = Ω(g (n)) ∧ g (n) = Ω(h(n)) ⇒ f (n) = Ω(h(n))
f (n) = o(g (n)) ∧ g (n) = o(h(n)) ⇒ f (n) = o(h(n))
f (n) = ω(g (n)) ∧ g (n) = ω(h(n)) ⇒ f (n) = ω(h(n))
f (n) = Θ(g (n)) iff g (n) = Θ(f (n))
f (n) = O(g (n)) iff g (n) = Ω(f (n))
f (n) = o(g (n)) iff g (n) = ω(f (n))
Pham Quang Dung () Data structures and Algorithms Basic definitions and notations Hanoi, 2012 27 / 46
Outline
1 First example
3 Big-Oh notation
4 Pseudo code
5 Analysis of algorithms
Pham Quang Dung () Data structures and Algorithms Basic definitions and notations Hanoi, 2012 28 / 46
Pseudo code
Variables declaration :
integer x, y ;
real u,v ;
boolean a,b ;
char c,d ;
datatype x ;
Pham Quang Dung () Data structures and Algorithms Basic definitions and notations Hanoi, 2012 29 / 46
Pseudo code
Assignment instruction :
x = expression ;
x ← expression ;
x ← x + 3;
Condition instruction
if condition then
instructions ;
else
instructions ;
endif ;
Pham Quang Dung () Data structures and Algorithms Basic definitions and notations Hanoi, 2012 30 / 46
Pseudo code
Loop
while condition do
instructions ;
endwhile ;
repeat instructions ;
until conddition ;
for i = n1 to n2 [step d]
instructions ;
endfor
Pham Quang Dung () Data structures and Algorithms Basic definitions and notations Hanoi, 2012 31 / 46
Pseudo code
Case instruction
Case
condition 1 : statement 1 ;
condition 2 : statement 2 ;
...
condition n : statement n ;
endcase
Pham Quang Dung () Data structures and Algorithms Basic definitions and notations Hanoi, 2012 32 / 46
Pseudo code
Function name(parameters)
begin
instructions ;
return value ;
end
Procedure name(parameters)
begin
instructions ;
end
Pham Quang Dung () Data structures and Algorithms Basic definitions and notations Hanoi, 2012 33 / 46
Pseudo code
Pham Quang Dung () Data structures and Algorithms Basic definitions and notations Hanoi, 2012 34 / 46
Pseudo code
Algorithm 1: max(A)
n ← A.length;
x ← A[1];
foreach i ∈ 2..n do
if x < A[i] then
x ← A[i];
return x;
Pham Quang Dung () Data structures and Algorithms Basic definitions and notations Hanoi, 2012 35 / 46
Outline
1 First example
3 Big-Oh notation
4 Pseudo code
5 Analysis of algorithms
Pham Quang Dung () Data structures and Algorithms Basic definitions and notations Hanoi, 2012 36 / 46
Analysis of algorithms
Experiments studies
Write a program implementing the algorithm
Execute the program on a machine with different input sizes
Measure the actual execution times
Plot the results
Pham Quang Dung () Data structures and Algorithms Basic definitions and notations Hanoi, 2012 37 / 46
Analysis of algorithms
Pham Quang Dung () Data structures and Algorithms Basic definitions and notations Hanoi, 2012 38 / 46
Analysis of algorithms
Pham Quang Dung () Data structures and Algorithms Basic definitions and notations Hanoi, 2012 39 / 46
Analysis of algorithms
Pham Quang Dung () Data structures and Algorithms Basic definitions and notations Hanoi, 2012 40 / 46
Analysis of algorithms
Pham Quang Dung () Data structures and Algorithms Basic definitions and notations Hanoi, 2012 41 / 46
Analysis of algorithms
Pham Quang Dung () Data structures and Algorithms Basic definitions and notations Hanoi, 2012 42 / 46
Analysis of algorithms
Pham Quang Dung () Data structures and Algorithms Basic definitions and notations Hanoi, 2012 43 / 46
Analysis of algorithms
Primitive operations
Function Fib(n)
begin
i ← 0; j ← 1;
for k = 2 to n do
begin
j ←j +i;
i ←j −i;
end
return j ;
end
Pham Quang Dung () Data structures and Algorithms Basic definitions and notations Hanoi, 2012 44 / 46
Analysis of algorithms
Primitive operations (be careful ! !)
1, if k = q 2
U[k] =
0, otherwise
Pham Quang Dung () Data structures and Algorithms Basic definitions and notations Hanoi, 2012 46 / 46