Lec06 - Analysis of Algorithm Part 2 - v3
Lec06 - Analysis of Algorithm Part 2 - v3
Computational Thinking
Expressing and
Analyzing
Algorithms:
Part 2 Komarudin
Computational Thinking
Outline
Part 1:
✔ Finding extreme values
✔ Linear search
✔ Algorithm complexity
Part 2:
✔ More on Algorithm
Complexity
✔ Binary search
✔ Brute force algorithm
✔ Greedy algorithm
Computational Thinking
Extensibility
Computational Thinking
Analysis of Algorithm
• The process of evaluating the
performance of an algorithm
• time complexity: the time required
to execute an algorithm
• space complexity: the total
amount of memory space used by
an algorithm/program, including
the space of input values for
execution
Computational Thinking
Analysis of Algorithm
• The process of evaluating the
performance of an algorithm
Our • time complexity: the time required
Focus to execute an algorithm
• space complexity: the total
amount of memory space used by
an algorithm/program, including
the space of input values for
execution
Computational Thinking
PUZZLE(x)
while x != 1
if x is even then
x = x / 2
else
x = 3x + 1
Sample run: 7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1
Computational Thinking
•
Time complexity
•
Computational Thinking
Growth rates
•
1 2 3 4 5 6 7 8
15 20 25 30 35 40 45 50
Algorithm A
Algorithm B
Computational Thinking
Asymptotic Notation
• Running time of an algorithm as a function of input size n for
large n.
• Expressed using only the highest-order term in the expression
for the exact running time.
12
Computational Thinking
Asymptotic Notation
• Threre are 5 asymptotic notations:
• Θ (theta) average case
• O (Big Oh) worst case
• Ω (Big Omega) best case
• o (little oh) loose upper bound bound
• ω (little omega) loose lower bound
• Always prepare for the worst! The worst case is often happened!
That’s why we will focus on Big Oh
Computational Thinking
•
Big Oh
Computational Thinking
Examples
Complexity Example & notes
Constant • Any integer/double arithmetic/logic operation
• Accessing a variable or an element in an array
Logarithmic
Linear
Quadratic complexity
Computational Thinking
max := a1
i := 2
no
max < ai?
yes
max := ai i: = i + 1
no
i = n?
yes
end
Computational Thinking
Matrix multiplication
Input:
Output:
Computational Thinking
Standard algorithm
for i ← 1 to n
do for j ← 1 to n
do cij ← 0
for k ← 1 to n
do cij ← cij + aik ⋅bkj
Examples:
• exploring all the paths to a nearby market
What is the probability of rolling two • Arranging the books in a rack using all the
dice to have a sum of 7? possibilities
Computational Thinking
Examples:
• Factorial calculation, Fibonacci number
• Tower of Hanoi algorithm
Computational Thinking
Examples:
• Iteratively go to the nearest neighboring city, when
finding shortest path from Depok to Banyuwangi
https://www.simplilearn.com/tutorials/data-structure-tutorial/greedy-algorithm
• Sequencing jobs with the smallest processing time (SPT)
Computational Thinking
Examples:
• Merge sort
• Strassen’s matrix multiplication
algorithm
Computational Thinking
Examples:
• The Floyd-Warshall algorithm for all-pair shortest path
• Partition a Set into Two Subsets of Equal Sum
Computational Thinking