You are on page 1of 33

1

EEA216 資料結構
Data Structure
Lecture 5
Yu-Hui Huang, Data Structure

Yu-Hui Huang
Sep. 28, 2022
Python – OOP Class Car:
def __init__(self, fuel, maxspd):
self.fuel = fuel
self.maxspeed = maxsp
def refuel():
pass

applecar = Car(3, 200)

Fig. from https://commons.wikimedia.org/wiki/File:CPT-OOP-objects_and_classes_-_attmeth.svg


https://www.google.com/url?sa=i&url=https%3A%2F%2Fwww.milleworld.com%2Fapple-car-what-we-know-so-
far%2F&psig=AOvVaw3ZFS9F94R8So9Zubb2NDhd&ust=1664375276589000&source=images&cd=vfe&ved=0CAwQjRxq
FwoTCOC34qeXtfoCFQAAAAAdAAAAABAD Yu-Hui Huang, Data Structure 2
OOP Exercise
Create a child class Bus which inherits
all the variables and methods from
Vehicle class.

class Vehicle:
def __init__(self, name, max_speed, mileage):
self.name = name
self.max_speed = max_speed
self.mileage = mileage
class Bus(Vehicle):
pass

Yu-Hui Huang, Data Structure 3


Inheritance (updated)
To keep the original definition of __init__() from the parent
class:

class Parent: a = Child(‘y’,3,5)


def __init__(self, pname): print(a.num1) #3
self.pname = pname print(a.pname) #y
b = Parent(‘ab')
class Child(Parent): print(b.pname) #ab
def __init__(self, pname, cnum1, cnum2):
Parent.__init__(self, pname)
#super().__init__(pname)
self.num1 = cnum1
self.num2 = cnum2

Yu-Hui Huang, Data Structure 4


Inheritance
To keep the original definition of __init__() from the parent
class:

class Parent: a = Child(3,5)


def __init__(self, pname=‘abc’): print(a.num1) #3
self.pname = pname print(a.pname) #abc
b = Parent()
class Child(Parent): print(b.pname) #abc
def __init__(self, cnum1, cnum2):
Parent.__init__(self)
self.num1 = cnum1
self.num2 = cnum2

Yu-Hui Huang, Data Structure 5


Inheritance

Reference: https://www.brilliantcode.net/761/python-3-6-class/
Yu-Hui Huang, Data Structure 6
L4: Exercise 3
Write a program with the following class definition
and test it with the following expressions.
>>>cir = Circle(6)
Polygon
>>>print(cir.area())
(num_sides)
113.097335529
area
>>>print(cir.perimeter())
perimeter
37.6991118430
inherit inherit
>>>rec = rectangle(6,6)
>>>print(rec.area())
Circle Rectangle
36
(num_sides, radius) (num_sides, length,
>>>print(rec.perimeter())
area breadth)
24
perimeter area
perimeter

Yu-Hui Huang, Data Structure 7


What is Algorithm?
Algorithm is a finite sequence of rigorous instructions,
typically used to solve a class of specific problems or to
perform a computation. --Wikipedia

Algorithm can be described in the following formats:


1. Plain texts
2. Pseudo code
3. Flow chart

Yu-Hui Huang, Data Structure 8


Pseudocode
Algorithm LargestNumber
Input: A list of numbers L.
Output: The largest number in the list L.
if L.size = 0 return null pseudocode
largest ← L[0] A plain language description
of steps in an algorithm.
for each item in L, do
if item > largest, then
largest ← item
return largest

Reference: https://en.wikipedia.org/wiki/Algorithm#Algorithmic_analysis
Yu-Hui Huang, Data Structure 9
Flow chart

Figure from: https://en.wikipedia.org/wiki/Algorithm#Algorithmic_analysis


Yu-Hui Huang, Data Structure 10
Algorithm Analysis
Given a task: calculate the factorial of a number entered by
the user.
Which of the following is faster?

def fact1(num): def fact2(num):


product = 1 if num == 0:
for i in range(num): return 1
product = product * (i+1) else:
return product return num * fact2(num-1)

print(fact1(10)) print(fact2(10))

Yu-Hui Huang, Data Structure 11


Algorithm Analysis
def fact1(num): def fact2(num):
product = 1 if num == 0:
for i in range(num): return 1
product = product * (i+1) else:
return product return num * fact2(num-1)

print(fact1(100)) print(fact2(100))

timeit() or time function to measure the computation time:

à1.9e-05 sec à5.9e-05 sec

Yu-Hui Huang, Data Structure 12


Algorithm Analysis
• Why do we need to analyze algorithms?
• To have an estimation on how much of resources
(time /storage) are required for a given algorithm.
• To know which of the algorithms is better without
implementing it.
• Three types of algorithm analysis
• Best case : Define the input for which algorithm takes less time
or minimum time
• Worst case : Define the input for which algorithm takes a long
time or maximum time.
• Average case : In the average case take all random inputs and
calculate the computation time for all inputs.
And then we divide it by the total number of inputs
Reference: https://www.geeksforgeeks.org/what-is-algorithm-and-why-analysis-of-it-is-important/
Yu-Hui Huang, Data Structure 13
Big-O
• With the speed of computers today,
- we are not concerned with an exact measurement of an
algorithm’s efficiency
- as much as we are with its general order of magnitude

• Big-O notation signifies the relationship between the


input to the algorithm and the steps required to execute
the algorithm.

Slide Credit: I-Fen Chao

Yu-Hui Huang, Data Structure 14


Big-O
• f(n) = O(g(n)):
Let f(n) and g(n) be functions mapping positive integers to
positive real numbers.
We say that f(n) is O(g(n)) if there is a real constant c>0 and an
integer constant n0≥ 1 such that
f n ≤ 𝑐𝑔 𝑛 , 𝑓𝑜𝑟 𝑛 ≥ 𝑛!

Example:
3n+2 = O(n) as 3n+2 ≤4n for all n ≥2
3n+3 = O(n) as 3n+3 ≤4n for all n ≥3
100n+6 = O(n) as 100n+6 ≤ 101n for n ≥10

Figure from Introduction to Algorithms


Yu-Hui Huang, Data Structure 15
Big-O 𝑓 𝑛 ≤ 𝑐𝑔(𝑛) for all 𝑛, 𝑛 ≥ 𝑛!

• 1000𝑛! + 100𝑛 − 6 = Ο 𝑛! ?
• Yes, since 1000𝑛! + 100𝑛 − 6 ≤ 1001𝑛! for all 𝑛 ≥
100.
• 6 ∗ 2" + 𝑛! = Ο(2" ) ?
• Yes, since 6 ∗ 2" + 𝑛! ≤ 7 ∗ 2# for all 𝑛 ≥ 4.
• 3𝑛 + 3 = Ο(𝑛! ) ?
• Yes, since 3𝑛 + 3 ≤ 3𝑛! for all 𝑛 ≥ 2.
• 10𝑛! + 4𝑛 + 2 = Ο(𝑛$ ) ?
• Yes, since 10𝑛! + 4𝑛 + 2 ≤ 10𝑛$ for all 𝑛 ≥ 2.
• 3𝑛 + 2 = Ο 1 ?
• No. Cannot find c and n% . 3n < c − 2 is never true.
16
Big-O
• Theorem 1.
If 𝑓 𝑛 = 𝑎& 𝑛& + ⋯ + 𝑎' 𝑛+ 𝑎% , 𝑡ℎ𝑒𝑛 𝑓 𝑛 = 𝑂 𝑛& .

Example:
5n4+3n3+n2+5 is O(n4)
2n+2 is O(2n)

Yu-Hui Huang, Data Structure 17


Big-Omega
• We say that f(n) is Ω(g(n)) if there is a real constant c>0
and an integer constant n0≥ 1 such that
f n ≥ 𝑐𝑔 𝑛 , 𝑓𝑜𝑟 𝑛 ≥ 𝑛!

Example:
3n+2 = Ω(n) as 3n+2 ≥ 3n for n ≥ 1
3n+3 = Ω(n) as 3n+3 ≥ 3n for n ≥ 1

Yu-Hui Huang, Data Structure 18


Big-Omega
• Theorem 2.
If 𝑓 𝑛 = 𝑎& 𝑛& + ⋯ + 𝑎' 𝑛+ 𝑎% , 𝑎𝑛𝑑 𝑎& > 0,
𝑡ℎ𝑒𝑛 𝑓 𝑛 = Ω(𝑛& ).

Example:
3nlogn-2n is Ω(nlogn)

Yu-Hui Huang, Data Structure 19


Big-Theta
• f(n) = Θ(g(n)):
We say that f(n) is Θ(g(n)) if there exist positive constants
𝑐', 𝑐! and n0 such that
𝑐"g n ≤ 𝑓 𝑛 ≤ 𝑐#g n , 𝑓𝑜𝑟 𝑎𝑙𝑙 𝑛 ≥ 𝑛!

Example:
3n+2 = Θ(n) as 3n+2≥3n for all n ≥2 and
3n+2 ≤ 4n for all n ≥2
--> 𝑐"=3, 𝑐#=4 and 𝑛!=2

Yu-Hui Huang, Data Structure 20


Big-Theta
• Theorem 3.
If 𝑓 𝑛 = 𝑎& 𝑛& + ⋯ + 𝑎' 𝑛+ 𝑎% 𝑎𝑛𝑑 𝑎& > 0,
𝑡ℎ𝑒𝑛 𝑓 𝑛 = Θ(𝑛& ).

Example:
3nlogn+4n+5logn is Θ(n)

Yu-Hui Huang, Data Structure 21


Big O, Big Omega, Big Theta
• Big O gives upper asymptotic bound
• Big Omega gives a lower bound
• Big Theta gives both

Yu-Hui Huang, Data Structure 22


Exercise

Yu-Hui Huang, Data Structure 23


Comparative Analysis
If an algorithm A has a running time of O(n) and an algorithm
B has a running time of O(n2).
-->Which one is better?

algorithm A is asymptotically better than algorithm B

Yu-Hui Huang, Data Structure 24


Comparative Analysis
The order of functions by asymptotic growth rate:
1, logn, n, nlogn, n2, n3, 2n

Table from https://www.cpp.edu/~ftang/courses/CS240/lectures/analysis.htm


Yu-Hui Huang, Data Structure 25
Comparative Analysis
OKish

The order of functions by asymptotic growth rate:


1, logn, n, nlogn, n2, n3, 2n

Good!

Superb!

Fig. from https://stackabuse.com/big-o-notation-and-algorithm-analysis-with-python-examples/


Yu-Hui Huang, Data Structure 26
Constant Complexity
• O(1), O(c): Constant algorithm does not depend on the
input size.

• Typical Operations:
- Arithmetic calculation
- Comparison
- Assignment statement
- Invoking a method or function.

Yu-Hui Huang, Data Structure 27


Linear Time Complexity
• O(n): An algorithm with the execution time increasing
linearly with the increase in the size of the input.
• The linear function represents the best running time
we hope to achieve for any algorithm that processes a
collection of n inputs.
• For example, comparing a number x to each element
of an array of size n will require n comparisons.
def sum_list_elements(inList):
sum = 0
for num in inList:
sum += num Yu-Hui Huang, Data Structure 28
Quadratic Time Complexity
O(n2): An algorithm where for each of its input, another O(n)
complexity code is to be executed àquadratic time complexity

def add_2D_list(inList):
sum=0
for row in inList:
for element in row:
sum += element

add_2D_list([[1,2],[3,4]])

Table from https://www.cpp.edu/~ftang/courses/CS240/lectures/analysis.htm


Yu-Hui Huang, Data Structure 29
Logarithmic Time Complexity
O(log n) : An algorithm where with every iteration, the size of
input keeps on reducing àlogarithmic time complexity

Example: Bineary search


1st iteration: search 1 to 8
2nd iteration: search 1 to 4
3rd iteration: search 1 to 2

Reference: https://www.linkedin.com/pulse/time-complexity-algorithms-python-examples-hiral/

Yu-Hui Huang, Data Structure 30


def binary_search(arr, target): Example: Bineary search
l_idx = 0 1st iteration: search 1 to 8
r_idx = len(arr)-1 2nd iteration: search 1 to 4
while l_idx <= r_idx:
3rd iteration: search 1 to 2
mid_idx = (l_idx+r_idx)//2
if arr[mid_idx] == target:
return mid_idx
input_arr = [1,2,3,4,5,6,7,8] #sorted
elif arr[l_idx] == target:
target_val = 2
return l_idx
print(binary_search(input_arr,
elif arr[r_idx] == target: target_val))
return r_idx
elif arr[mid_idx] <target:
l_idx = mid_idx+1
else:
r_idx = mid_idx-1
Yu-Hui Huang, Data Structure 31
Quasilinear Time Complexity
O(n log n) : An algorithm where every iteration has a
logarithmic complexity

Example: we run binary search for an array of array.

Yu-Hui Huang, Data Structure 32


Homework
[pythonds]
• ch3.2 (What is Algorithm Analysis?),
• ch3.3(Big-O Notation),
• ch3.4 (An Anagram Detection Example)

https://runestone.academy/ns/books/
published/pythonds/index.html)

Yu-Hui Huang, Data Structure 33

You might also like