Chapter 3 - Algorithms Part I
Chapter 3 - Algorithms Part I
PROGRAMMING TECHNIQUES
CHAPTER 3
Algorithms – Part I
Võ Duy Thành
Department of Automation Engineering
Control Technique and Innovation Lab. for Electric Vehicles
School of Electrical and Electronic Engineering
Hanoi University of Science and Technology
[email protected] | [email protected]
Outline
1. What is Algorithm
2. Examples
3. Algorithm Complexity
4. Fundamental Algorithms
5. Recursion
2
1. What is algorithm | 1.1. Definition
Mission
accomplished!
Problem is solved.
• Why algorithm?
• Efficiency: Algorithms can perform tasks quickly and accurately.
• Consistency: Algorithms are repeatable and produce consistent results every
time they are executed.
• Scalability: Algorithms can be scaled up to handle large datasets or complex
problems.
• Automation: Algorithms can automate repetitive tasks, reducing the need for
human intervention and freeing up time for other tasks.
• Standardization: Algorithms can be standardized and shared among different
teams or organizations.
• Properties of algorithms
Clear and
• Terminates at finite time Unambiguous
input case
• Each step must be effective, i.e. Algorithms
every step should do some works
Language
Finiteness
independent
Feasible
• Requirements:
Draw flowcharts/structure charts/pseudo code and write code for
the following algorithms
Step 1:
Start
Step 2:
Declare variables num1, num2 and sum.
Step 3:
Read values num1 and num2.
Step 4:
Add num1 and num2 and assign the result to sum.
sum←num1+num2
Step 5: Display sum
Step 6: Stop
6. Find the Fibonacci series till the term less than 1000
Step 1: Start
Step 2: Declare variables first_term,second_term and temp.
Step 3: Initialize variables first_term ← 0 second_term ← 1
Step 4: Display first_term and second_term
Step 5: Repeat the steps until second_term ≤ 1000
5.1: temp ← second_term
5.2: second_term ← second_term + first_term
5.3: first_term ← temp
5.4: Display second_term
Step 6: Stop
• Cases in complexity
• Worst case or upper bound: 𝑂(𝑛)
• Best case or lower bound: Ω 𝑛
• Average case: Θ 𝑛
where 𝑛 is the size of input
• Example:
• Searching in an unsorted list:
linear complexity 𝑂 𝑛
• Find duplicate in an unsorted list:
quadratic complexity 𝑂 𝑛2
• Search a word in a sorted list (dictionary):
logarithmic complexity 𝑂 log 𝑛 Source: https://devopedia.org/algorithmic-complexity
• Quicksort complexity: Ω 𝑛 log 𝑛 , Θ 𝑛 log 𝑛 , 𝑂 𝑛2
𝑂(1) < 𝑂(log 𝑛) < 𝑂( 𝑛) < 𝑂(𝑛) < 𝑂(𝑛 log 𝑛) < 𝑂(𝑛2 ) < 𝑂(𝑛3 ) < 𝑂(2𝑛 ) < 𝑂(10𝑛 ) < 𝑂(𝑛!)
• Complexity of
sorting algorithms
(Source: Big-O Cheat Sheet,
2016)
i = 1 -3 11 2 -8 4 -3 2 -8 4 11 -8 -3 2 4 11 -8 -3 2 4 11
i = 2 -3 2 11 -8 4 -3 -8 2 4 11 -3 -8 2 4 11
i = 3 -3 2 -8 11 4 -3 -8 2 4 11
-3 2 -8 4 11
begin BubbleSort(list)
for all elements of list
if list[i] > list[i+1]
swap(list[i], list[i+1])
end if
end for
return list
end BubbleSort
i = 1 -3 11 2 -8 4 -3 2 -8 4 11 -8 -3 2 4 11 -8 -3 2 4 11
i = 2 -3 2 11 -8 4 -3 -8 2 4 11 -3 -8 2 4 11
i = 3 -3 2 -8 11 4 -3 -8 2 4 11
-3 2 -8 4 11
4 3 2 1
No. of
comparison (n-1) (n-2) (n-3) (n-4) …
𝑛 𝑛−1
Number of comparisons = 𝑛 − 1 + 𝑛 + 2 + ⋯ + 1 = ⇒ Complexity: 𝑂 𝑛2
2
0 3 2
i = 1 -3 11 2 -8 4 -8 11 2 -3 4 -8 -3 2 11 4 -8 -3 2 4 11
3 3
i = 2 -3 11 2 -8 4 -8 11 2 -3 4 -8 -3 2 11 4
3
i = 3 -3 11 2 -8 4 -8 -3 2 11 4
-8 11 2 -3 4
Note: Red numbers are the indices of the minimum elements
selectionSort(array, size)
repeat (size - 1) times
set the first unsorted element as the minimum
for each of the unsorted elements
if element < currentMinimum
set element as new minimum
swap minimum with first unsorted position
end selectionSort
2 -3 -8 -1 11 -3 2 -8 -1 11 -8 -3 2 -1 11 -8 -3 -1 2 11
Key Key Key Key
-3 2 2 -8 -1 11 -8 -3 2 2 -1 11 -1 -8 -3 2 2 11 11
-3 -3 2 -1 11 -8 -3 -1 2 11 -8 -3 -1 2 11
-3 2 -8 -1 11
-8 -3 2 -1 11
• Practical example 1
A flat beam of light is bent
when going near a highly charged
Highly Electricity
sphere.
Charged Sphere
On the screen, we collected a
curve of light instead of a straight
one.
Requirement: Find the
trajectory of the line on the
screen.
Problem: Solve the equation Light Beam Source
below
2
𝑟−𝑅−∆𝑅 2 𝑟−𝑅−∆𝑅 2
− −
𝑛0 − 51.10−6 . 𝑇0 + ∆𝑇. 𝑒 𝑐 − 11.10−8 . 𝑇0 + ∆𝑇. 𝑒 𝑐 . 𝑟 − 𝑛0 . 𝑥0 = 0
• Practical example 2:
Studied object: dual-motor EV with efficiency
maps for each electric motor
Problem: find the optimal operation points of
motors for the best efficiency
Energetic index in acceleration:
𝑘𝑓𝑟 1−𝑘𝑓𝑟
𝐽𝑎𝑐𝑐 = +
𝜂𝐸𝐷𝑓 𝜂𝐸𝐷𝑟
∗
𝑘𝑓𝑟 = 𝑎𝑟𝑔𝑚𝑖𝑛 𝐽𝑎𝑐𝑐 𝑤𝑖𝑡ℎ 𝑘𝑓𝑟 ≔ 0 → 1
Energetic index in deceleration
𝐽𝑑𝑒𝑐 = 𝑘𝑓𝑟 𝜂𝐸𝐷𝐹 + 1 − 𝑘𝑓𝑟 𝜂𝐸𝐷𝑟
∗
𝑘𝑓𝑟 = 𝑎𝑟𝑔𝑚𝑖𝑛 𝐽𝑑𝑒𝑐 𝑤𝑖𝑡ℎ 𝑘𝑓𝑟 ≔ 0 → 1
• Practical example 3
Studied object: a HESS electric
vehicle
Problem: Optimal energy
management of the two sources
𝑡 2
𝐽 = 0 𝑓 𝑃𝑏𝑎𝑡 𝑑𝑡
Constraints:
𝑃𝑏𝑎𝑡−𝑚𝑖𝑛 ≤ 𝑃𝑏𝑎𝑡 ≤ 𝑃𝑏𝑎𝑡−𝑚𝑎𝑥
𝑈𝑆𝐶−𝑚𝑖𝑛 ≤ 𝑈𝑆𝐶 ≤ 𝑈𝑆𝐶−𝑚𝑎𝑥
𝑈𝑆𝐶−𝑓𝑖𝑛𝑎𝑙 = 𝑈𝑆𝐶−𝑖𝑛𝑖𝑡
𝐸𝑆𝐶
𝐸𝑆𝐶_𝑚𝑎𝑥
𝐸𝑆𝐶_𝑖𝑛𝑖𝑡
𝐸𝑆𝐶_𝑚𝑖𝑛
i = 0 2 -3 -8 5 11
𝑘≠2
i = 1 2 -3 -8 5 11
𝑘 ≠ −3
i = 2 2 -3 -8 5 11
𝑘 ≠ −8
Return i i = 3 2 -3 -8 5 11
𝑘=5
LinearSearch(array, key)
for each item in the array
if item == value
return its index
• Looping structures using while and for loops lead to the iterative
algorithm
• The value of computation of the program can be captured in a set of
state variables which are updated on each iteration through loops
• Example of “Multiplication” using iteration method
• The multiplication of a and b (axb) is equivalent to add a to itself b times
5.3.2. Factorial
𝑛! = 1 × 2 × 3 × ⋯ × (𝑛 − 1) × 𝑛
• The pattern/smaller version?
𝑛! = 𝑛 × 𝑛 − 1 !
or facto(n)=n*facto(n-1)
• The base case?
if n=1 → n! = 1
• The code? def facto(n) :
if n == 1 :
return 1
else :
return n*facto(n-1)
facto n n n n
some
4 3 2 1
code
A B C
5.3.4. Well-known the Tower of Hanoi – Exercise
• The puzzle invented by Édouard Lucas in 1883
• Objective: move all n disks from rod A to rod C
• Only one disk can be moved at a time
• The moving disk can be placed on other disks at another
rod or to an empty rod
• No disk is allowed to be put on top of a smaller disk
• So, find the pattern (smaller version), the base case
and then code
50