L1-ts
L1-ts
and Complexity
Expectations
◦ Your attendance in the lectures and recitations (tutorials) is very important. I expect that
every student should be in class before the lecturer enters. Late coming causes distractions so
your punctuality will be highly appreciated.
Why algorithm?
20th century science was dominated by mathematics, physics and chemistry
21st century will focus majorly on the science of algorithm
Algorithm is everywhere
In our DNA
In ant colony
In swarm behaviour
In self-driving cars
In drones
The efficiency and effectiveness of hardware and software depend largely on the
underlying algorithms
Unfortunately, computers are limited in time (processing) and space (memory)
What do we do?
Learning Outcomes
At the end of this course, you should be able to:
Demonstrate a good understanding of the role of algorithms in
computing
Design appropriate algorithmic solutions for problem solving
using different paradigms
Prove that your algorithms are correct
Analyse algorithms to determine their efficiency
Perform asymptotic and empirical analysis of some popular
algorithms
Course Content
Roles of Algorithms in Computing
Some Representative Problems
Analysis of Algorithm
Design of Algorithms
Divide-and-Conquer
Dynamic Programming
Greedy Algorithms
Graph Algorithms
Computational Complexity
Course Schedule
WEEK TOPICS COURSE INSTRUCTORS
Chapter 1
◦ Jon Kleinberg & Éva Tardos (2005) Algorithm Design, Pearson-Adisson Wesley
Chapter 1
◦ Steven S S. Skiena (2011) The Algorithm Design Manual, 4th Edition, Pearson – Adisson Wesley
What is an Algorithm
A set of steps for performing a task
Example:
Algorithm to make a cup of tea
Algorithm to prepare a lecture note
Algorithm to move a car from point A to point B
Algorithm to prepare for an exam
Algorithm to sew a piece of cloth
What is Computer Algorithm?
“A set of steps to accomplish or complete a task that is described
precisely enough that a computer can run it”
Definition
A sequence of computational steps that transforms the input
into the output
A tool for solving well specified computational problem
Problem statement – specifies the desired input/output relationship
Algorithm – elicitation of a specific computational procedure for
achieving the input/output relationship
Definition contd.
Input: A sequence of n numbers ‹a1, a2, …, an›
2 𝑥 107 2 𝑖𝑛𝑠𝑡𝑟𝑢𝑐𝑡𝑖𝑜𝑛𝑠
A takes = 20,000 seconds
1010 𝑖𝑛𝑠𝑡𝑟𝑢𝑐𝑡𝑖𝑜𝑛𝑠/𝑠𝑒𝑐𝑜𝑛𝑑
√n 1012
n 106 6⋅107
n lgn
n2
n3
2n
n!
Lecture2: Analysis of
Algorithm
DR VICTOR ODUMUYIWA
Learning Outcome
At the end of this class, you should be able to:
Program Algorithm
Model of
computation
Programming
Pseudocode describes what your
language
computer can do in
Model of
Computer
computation constant time.
A posteriori analysis
Empirical analysis
Algorithm is implemented using programming language.
The implementation is executed on target computer machine.
Actual statistics like running time and space required, are collected.
A Priori Analysis
Focus here is on a priori algorithm analysis
𝒏 𝒏 𝒏
𝑇 𝑛 = 𝑐1𝑛 + 𝑐2 𝑛 − 1 + 𝑐4 𝑛 − 1 + 𝑐5 𝑛 − 1 + 𝑐8 𝑛 − 1
𝑇 𝑛 = 𝑐1 + 𝑐2 + 𝑐4 + 𝑐5 + 𝑐8 𝑛 − (𝑐2 + 𝑐4 + 𝑐5 + 𝑐8)
𝑇 𝑛 = 𝑎𝑛 + 𝑏
𝑇 𝑛 𝑖𝑠 𝑎 𝑙𝑖𝑛𝑒𝑎𝑟 𝑓𝑢𝑛𝑐𝑡𝑖𝑜𝑛 𝑜𝑓 𝑛
Worst-case Analysis of Insertion-Sort(A)
(1/4)
Occurs if the array is in reverse sorted order i.e. in decreasing order.
For each j = 2, 3, … ,n, we compare each element A[j] with each
element in the entire sorted subarray A[1 .. j-1].
𝑡𝑗 = j for j = 2, 3, … ,n
𝑛 𝑛 𝑛
𝑛 𝑛
𝑛 𝑛+1 𝑛 𝑛−1
𝑗 = −1 (𝑗 − 1) =
𝑗=2
2 𝑗=2
2
𝑛2 𝑛 𝑛2 𝑛 𝑛2 𝑛
𝑇 𝑛 = 𝑐1𝑛 + 𝑐2𝑛 − 𝑐2 + 𝑐4𝑛 − 𝑐4 + 𝑐5 + 𝑐5 − 𝑐5 + 𝑐6 − 𝑐7 + 𝑐7 − 𝑐6 + 𝑐8𝑛 − 𝑐8
2 2 2 2 2 2
𝑛2 𝑛2 𝑛2 𝑛 𝑛 𝑛
𝑇 𝑛 = 𝑐5 + 𝑐6 + 𝑐7 + 𝑐1𝑛 + 𝑐2𝑛 + 𝑐4𝑛 + 𝑐5 − 𝑐7 − 𝑐6 + 𝑐8𝑛 − 𝑐2 − 𝑐4 − 𝑐5 − 𝑐8
2 2 2 2 2 2
𝑐5 𝑐6 𝑐7 2 𝑐5 𝑐6 𝑐7
𝑇 𝑛 = + + 𝑛 + 𝑐1 + 𝑐2 + 𝑐4 + − − 𝑛 + (𝑐2 − 𝑐4 − 𝑐5 − 𝑐8)
2 2 2 2 2 2
Worst-case Analysis of Insertion-Sort(A)
(4/4)
𝑐5 𝑐6 𝑐7 2 𝑐5 𝑐6 𝑐7
𝑇 𝑛 = + + 𝑛 + 𝑐1 + 𝑐2 + 𝑐4 + − − 𝑛 + (𝑐2 − 𝑐4 − 𝑐5 − 𝑐8)
2 2 2 2 2 2
𝑇 𝑛 = 𝑎𝑛2 + 𝑏𝑛 + 𝑐
𝑇 𝑛 𝑖𝑠 𝑎 𝑞𝑢𝑎𝑑𝑟𝑎𝑡𝑖𝑐 𝑓𝑢𝑛𝑐𝑡𝑖𝑜𝑛 𝑜𝑓 𝑛