Design and Analysis of Algorithms: Dr. Muhammad Safysn Spring 2019
Design and Analysis of Algorithms: Dr. Muhammad Safysn Spring 2019
Algorithms
Dr. Muhammad Safysn
Spring 2019
1
Introduction
The word Algorithm comes from the name of the Muslim
author Abu Ja’far Mohammad ibn Musa al-Khowarizmi.
He was born in the eighth century at Khwarizm (Kheva), a
town south of river Oxus in present Uzbekistan.
Al-Khwarizmi parents migrated to a place south of Baghdad
when he was a child.
It has been established from his contributions that he
flourished under Khalifah Al-Mamun at Baghdad during 813
to 833 C.E. Al-Khwarizmi died around 840 C.E.
2
Definition
An algorithm is a mathematical entity, which is
independent of a specific programming language,
machine, or compiler.
Design is all about the mathematical theory behind
the design of good programs.
Multi-facets to good program design
Aware of programming and machine issues as well
Two Issues
Micro-Issues
Macro-Issues
3
Definition
Macro-Issues
Computation Cost:
Few lines e.g 100 lines of code take most of the execution time.
Micro-issue
Deals with small critical section e.g. debugging
To Deal the Macro-section, written code must be dealt
with efficient manner.
Conventional approach: codify poor design problem
and attempt to fine-tune its performance by applying clever
coding tricks OR
by implementing it on the most expensive and fastest machines
around to boost performance as much as possible
4
Good Design
The problem is that if the underlying design is bad,
then often no amount of fine-tuning is going to make a
substantial difference.
Before you implement, first be sure you have a good design, This
course is about to good design and good data structure.
Both are integrated issues
Fastest algorithms are fast because they use fast data
structures, and vice versa.
Applications: compiler , Operating system, databases, Artificial
Intelligence, Semantic Web, computer vision, machine leaning
etc.
5
Analyzing Algorithms
What is criteria for Good Algorithm?
What is criteria of measuring Algorithm?
6
Computing resources:
e Machine
e Programminglanguage
e Programmer
RAM: Computational Model
However to estimate running time we must use some computational
model.
Computational model is an abstract machine having some properties.
For this purpose we will use RAM computational model(Random
Access Machine) having the following properties
3. The time to access data does not depend on their address (there
are no differences between processing the first element of an
array and processing the last element) All basic operations,
assignment,
arithmetic, logical, relational take unit time.
Loop-holes
two numbers may be of any length.
Serlization
.
13
Running Time Analysis
Concern about measuring the execution time.
Concerned about the space (memory) required by the
algorithm.
Ignore during Running Time
Speed of computer
Programming Language
optimization by the compiler
.Count
Different inputs of the same size may result in different running time
14
Running Time Analysis
Two criteria for measuring running time are worst-case
time and average-case time
Worst-case time
maximum running time over all (legal) inputs of size n. Let I denote an input instance, let |I|
denote its length, and let T(I) denote the running time of the algorithm on input I. Then
Average-case time is the average running time over all inputs of size n. Let p(I) denote the
probability of seeing this input. The average-case time is the weighted sum of running times
with weights being the probabilities:
16
Running Time Analysis
17
Example1: running time
../pucitl ogo.jp
Example3: running time
Example 3: Find the minimum in non-empty array x [1 . .
. n]
P: n ≥ 1 Q: m = min{x [j ]|j = 1, 2, . . . n}n input: x
[1..n]
line no. cost iterations
1: m ← x [1] 1 1 1
2: for i ← 2, n do 2 1 2n
3: if x [i ] < m 3 1 n−1
4
then 1 h(n)
4: m ← x[i]
5: end if T (n) = 1 + 2n + n − 1 + h(n)
6: end for
7: return m
Example3: running time
Example 3: Find the minimum in non-empty array x [1 . .
. n]
P: n ≥ 1 Q: m = min{x [j ]|j = 1, 2, . . . n}n input: x
[1..n]
line no. cost iterations
1: m ← x [1] 1 1 1
2: for i ← 2, n do 2 1 2n
3: if x [i ] < m 3 1 n−1
4
then 1 h(n)
4: m ← x[i]
5: end if T (n) = 1 + 2n + n − 1 + h(n)
6: end for T (n) = 3n + h(n)
7: return m
Example3: running time
Example 3: Find the minimum in non-empty array x [1 . .
. n]
P: n ≥ 1 Q: m = min{x [j ]|j = 1, 2, . . . n}n input: x
[1..n]
line no. cost iterations
1: m ← x [1] 1 1 1
2: for i ← 2, n do 2 1 2n
3: if x [i ] < m 3 1 n−1
4
then 1 h(n)
4: m ← x[i]
5: end if T (n) = 1 + 2n + n − 1 + h(n)
6: end for T (n) = 3n + h(n)
7: return m
The running time depends not only on n but also on the properties
of input data
../pucitl ogo.jp
Best-case analysis and worst-case analysis
Whenever analysis of an algorithm not only depends on the input size
but also on some property of input data then we have to perform
analysis in more details
e Worst-case analysis: gives the longest running time for any input of
size n.
e Best-case analysis: gives us the minimum running time for any input
of size n.
)worst-case running time of an algorithm gives us an upper bound on the
running time for any input.
)Knowing it provides a guarantee that the algorithm will never take any
longer.
5: found ←true
6: else
7: i← i+ 1
8: end if
9: end while
Example 4: sequential search
Preconditions: x [1..n], n >= 1, v a value
Postconditions: found = TRUE when v ∈ x
[1..n] Input: x [1..n], v
Algorithm 2 search
1: found ← true
line no cost
2: i ← 1
1 1
3: while (found = false) and (i ≤ n) 2 1
do 3 f (n) + 1
4: if x [i ] = v then 4 f(n)
5: found ←true 5 g (n)
6: else 7 h(n)
7: i← i+ 1
T(n)=
8: end if
3 + 2f (n) + g(n) + h(n)
9: end while
Example 4: sequential search
Preconditions: x [1..n], n >= 1, v a value
Postconditions: found = TRUE when v ∈ x
[1..n] Input: x [1..n], v
Algorithm 3 search
1: found ← true
line no cost
2: i ← 1
1 1
3: while (found = false) and (i ≤ n) 2 1
do 3 f (n) + 1
4: if x [i ] = v then 4 f(n)
5: found ←true 5 g (n)
6: else 7 h(n)
7: i← i+ 1
T(n)=
8: end if
3 + 2f (n) + g(n) + h(n)
9: end while
Today’s Agenda
Role of dominant
term
Order of growth
Asymptotic Analysis
Dominant term or Leading term
log n n nlogn n2 2n
3.3 10 33 100 1024
6.6 100 664 10000 1030
10 1000 9965 1000000 10301
13 10000 132877 100000000 103010
A comparison of the order of growth
log n n nlogn n2 2n
3.3 10 33 100 1024
6.6 100 664 10000 1030
10 1000 9965 1000000 10301
13 10000 132877 100000000 103010
Comparing order of growth
The order of growth of two running times T1(n) and T2(n) can be
compared by computing the limit of T1(n)/T2(n) when n goes to
infinity.
e If the limit is 0 then T1(n) has a smaller order of growth than
T2(n)
e If the limit is a finite constant c(c > 0) then T1(n) and T2(n)
have the same order of growth
e If the limit is infinity then T1(n) has a larger order of growth
than T2(n)
.l ogo.jp
Asymptotic Analysis
..l ogo.jp
Asymptotic Analysis
../pucitl ogo.jp
Big-O Notation
../pucitl ogo.jp
Big-O Notation
·104
1
f(n)
g (n)
0.8
0.6
T (n)
0.4
0.2
0 ../pucitl ogo.jp
0 20 40 60 80 100
n
Big-O: Example
../pucitl ogo.jp
Big-O: Example
. ogo.jp
Big-O: FormalExample
../pucitl ogo.jp
Big-Ω Notation
../pucitl ogo.jp
Example
5n2 = Ω(n)
∃c, no such that 0 ≤ cn ≤ 5n2 ⇒ cn ≤ 5n2 ⇒ c = 1 and
no = 1
100n + 5 ƒ= Ω(n2)
∃c, no such that 0 ≤ cn2 ≤ 100n + 5 100n + 5 ≤ 100n +
5n(∀n ≥ 1) = 105n cn2 ≤ n ⇒ n(cn − 105) ≤ 0
since n is positive ⇒ cn − 105 ≤ 0 ⇒ n ≤ 105/c ⇒
contradiction: n cannot be smaller than constant.
../pucitl ogo.jp
Example
56
Running Time Analysis
57
Review: Running Time
An Example: Insertion Sort
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
58
Review: Running Time
30 10 40 20 i = j = key =
A[j] = A[j+1] =
1 2 3 4
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
59
An Example: Insertion Sort
i = 2 j = 1 key = 10
30 10 40 20
A[j] = 30 A[j+1] = 10
1 2 3 4
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
David Luebke
60
10/31/2019
An Example: Insertion Sort
i = 2 j = 1 key = 10
30 30 40 20
A[j] = 30 A[j+1] = 30
1 2 3 4
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
David Luebke
61
10/31/2019
An Example: Insertion Sort
i = 2 j = 1 key = 10
30 30 40 20
A[j] = 30 A[j+1] = 30
1 2 3 4
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
David Luebke
62
10/31/2019
An Example: Insertion Sort
i = 2 j = 0 key = 10
30 30 40 20
A[j] = A[j+1] = 30
1 2 3 4
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
David Luebke
63
10/31/2019
An Example: Insertion Sort
i = 2 j = 0 key = 10
30 30 40 20
A[j] = A[j+1] = 30
1 2 3 4
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
David Luebke
64
10/31/2019
An Example: Insertion Sort
i = 2 j = 0 key = 10
10 30 40 20
A[j] = A[j+1] = 10
1 2 3 4
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
David Luebke
65
10/31/2019
An Example: Insertion Sort
i = 3 j = 0 key = 10
10 30 40 20
A[j] = A[j+1] = 10
1 2 3 4
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
David Luebke
66
10/31/2019
An Example: Insertion Sort
i = 3 j = 0 key = 40
10 30 40 20
A[j] = A[j+1] = 10
1 2 3 4
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
David Luebke
67
10/31/2019
An Example: Insertion Sort
i = 3 j = 0 key = 40
10 30 40 20
A[j] = A[j+1] = 10
1 2 3 4
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
David Luebke
68
10/31/2019
An Example: Insertion Sort
i = 3 j = 2 key = 40
10 30 40 20
A[j] = 30 A[j+1] = 40
1 2 3 4
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
David Luebke
69
10/31/2019
An Example: Insertion Sort
i = 3 j = 2 key = 40
10 30 40 20
A[j] = 30 A[j+1] = 40
1 2 3 4
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
David Luebke
70
10/31/2019
An Example: Insertion Sort
i = 3 j = 2 key = 40
10 30 40 20
A[j] = 30 A[j+1] = 40
1 2 3 4
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
David Luebke
71
10/31/2019
An Example: Insertion Sort
i = 4 j = 2 key = 40
10 30 40 20
A[j] = 30 A[j+1] = 40
1 2 3 4
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
David Luebke
72
10/31/2019
An Example: Insertion Sort
i = 4 j = 2 key = 20
10 30 40 20
A[j] = 30 A[j+1] = 40
1 2 3 4
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
David Luebke
73
10/31/2019
An Example: Insertion Sort
i = 4 j = 2 key = 20
10 30 40 20
A[j] = 30 A[j+1] = 40
1 2 3 4
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
David Luebke
74
10/31/2019
An Example: Insertion Sort
i = 4 j = 3 key = 20
10 30 40 20
A[j] = 40 A[j+1] = 20
1 2 3 4
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
David Luebke
75
10/31/2019
An Example: Insertion Sort
i = 4 j = 3 key = 20
10 30 40 20
A[j] = 40 A[j+1] = 20
1 2 3 4
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
David Luebke
76
10/31/2019
An Example: Insertion Sort
i = 4 j = 3 key = 20
10 30 40 40
A[j] = 40 A[j+1] = 40
1 2 3 4
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
David Luebke
77
10/31/2019
An Example: Insertion Sort
i = 4 j = 3 key = 20
10 30 40 40
A[j] = 40 A[j+1] = 40
1 2 3 4
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
David Luebke
78
10/31/2019
An Example: Insertion Sort
i = 4 j = 3 key = 20
10 30 40 40
A[j] = 40 A[j+1] = 40
1 2 3 4
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
David Luebke
79
10/31/2019
An Example: Insertion Sort
i = 4 j = 2 key = 20
10 30 40 40
A[j] = 30 A[j+1] = 40
1 2 3 4
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
David Luebke
80
10/31/2019
An Example: Insertion Sort
i = 4 j = 2 key = 20
10 30 40 40
A[j] = 30 A[j+1] = 40
1 2 3 4
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
David Luebke
81
10/31/2019
An Example: Insertion Sort
i = 4 j = 2 key = 20
10 30 30 40
A[j] = 30 A[j+1] = 30
1 2 3 4
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
David Luebke
82
10/31/2019
An Example: Insertion Sort
i = 4 j = 2 key = 20
10 30 30 40
A[j] = 30 A[j+1] = 30
1 2 3 4
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
David Luebke
83
10/31/2019
An Example: Insertion Sort
i = 4 j = 1 key = 20
10 30 30 40
A[j] = 10 A[j+1] = 30
1 2 3 4
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
David Luebke
84
10/31/2019
An Example: Insertion Sort
i = 4 j = 1 key = 20
10 30 30 40
A[j] = 10 A[j+1] = 30
1 2 3 4
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
David Luebke
85
10/31/2019
An Example: Insertion Sort
i = 4 j = 1 key = 20
10 20 30 40
A[j] = 10 A[j+1] = 20
1 2 3 4
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
David Luebke
86
10/31/2019
An Example: Insertion Sort
i = 4 j = 1 key = 20
10 20 30 40
A[j] = 10 A[j+1] = 20
1 2 3 4
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
David Luebke
} Done!
87
10/31/2019
Asymptotic Notation
What is an
algorithm?
a step-by-step
procedure to solve a
problem
every program is the
instantiation of some
algorithm
http://blog.kovyrin.net/wp-content/uploads/2006/05/algorithm_c.png
89