Professional Documents
Culture Documents
COMP2015 HKBU/CS/JF/2023 1
Why study Data Structures and
Algorithms?
To run computer programs efficiently!
n Computer program:
q Accepts input (data)
n Using a computer
q Solve computational problems?
q Want it to go faster?
COMP2015 HKBU/CS/JF/2023 2
Course Motivation
COMP2015 HKBU/CS/JF/2023 3
Data Structures
n Data structures
q Objects created to organize data used in computation
q Linked list
q Stack
q Queue
COMP2015 HKBU/CS/JF/2023 4
Algorithms
n Algorithm
q A problem-solving method suitable for implementation as
a computer program
n Given a well-spicified problem, an algorithm forms
computational steps to transform inputs to outputs, i.e.
Solution.
n Example: Sorting problem
Input Output
A sequence A permutation
of numbers Algorithm (reordering) <a1’,
(a1, a2, ..an) a2’, ..., an’> of the
input sequence,
a1’ ≤ a2’, ... ≤ an’
COMP2015 HKBU/CS/JF/2023 5
Data Structures and Algorithms
COMP2015 HKBU/CS/JF/2023 6
Example: Selection Problem
n Solution 1:
COMP2015 HKBU/CS/JF/2023 7
Example: Selection Problem
n Solution 2:
(1) read the first k elements into an array and sort them
in decreasing order,
COMP2015 HKBU/CS/JF/2023 8
Example: Selection Problem
n Two natural questions:
q Which solution is better ?
n By simulation
n By theoretical analysis
COMP2015 HKBU/CS/JF/2023 9
Conclusion from the Previous
Example
n An important concept
q In many problems, writing a working program is not
good enough (The program may be inefficient!).
q If the program is to be run on a large data set, then the
running time becomes an issue.
n We will discuss
q How to estimate the running time of a program for large
inputs
q How to compare the running time of two programs
q Techniques to improve the speed of a program, and to
determine the program bottlenecks
COMP2015 HKBU/CS/JF/2023 10
Attributes of Algorithms
q Correctness
q Elegance
q Efficiency
COMP2015 HKBU/CS/JF/2023 11
Attributes of Algorithms
n Correctness
COMP2015 HKBU/CS/JF/2023 12
Attributes of Algorithms
n Elegance
q How clever or sophisticated is the algorithm?
q Sometimes elegance and ease of understanding work
at cross-purposes.
n Efficiency
q How much time and/or space does the algorithm
require when executed?
q Perhaps the most important desirable attribute
COMP2015 HKBU/CS/JF/2023 13
Study of Algorithms
n Study of Algorithms
q Design of algorithms
n Efficient vs inefficient
q Running time
q Storage required
COMP2015 HKBU/CS/JF/2023 14
Analysis of Algorithm
COMP2015 HKBU/CS/JF/2023 15
Empirical Analysis
n Empirical analysis
COMP2015 HKBU/CS/JF/2023 16
Mathematical Analysis
n Mathematical analysis
q To predict the performance of an algorithm
(speed/running time)
n Characterizes running time as a function of the input
size, N
n Takes into account all possible inputs, often
analyzing the worst case
q Allows us to evaluate the speed of an algorithm
COMP2015 HKBU/CS/JF/2023 17
Analysis of Algorithm
n What to analyze?
q The most important resource to analyze is the running
time!
COMP2015 HKBU/CS/JF/2023 18
Analysis of Algorithm
COMP2015 HKBU/CS/JF/2023 19
Analysis of Algorithm
COMP2015 HKBU/CS/JF/2023 21
Analysis of Algorithm
COMP2015 HKBU/CS/JF/2023 22
Analysis of Algorithm
n We define:
COMP2015 HKBU/CS/JF/2023 23
A Simple Example
n A program segment to calculate
The declarations count for no time
COMP2015 HKBU/CS/JF/2023 24
Review: Mathematical Foundation
( + 1) (arithmetic series)
= 1 +2 + 3 + ⋯+ =
2
1−
= 1+ + + ⋯+ = (geometric series)
1−
• Sum of squares:
( + 1)(2 + 1)
= 1 + 2 + ⋯+ =
6
COMP2015 HKBU/CS/JF/2023 25
Review: Recursion
COMP2015 HKBU/CS/JF/2023 26
Example 1: Factorial Function
v Is this correct?
COMP2015 HKBU/CS/JF/2023 27
Example 2: Fibonacci Numbers
Ø Recall definition of Fibonacci numbers:
0, 1, 1, 2, 3, 5, 8, 13, 21, …
F(n)
F(n-1) + F(n-2)
COMP2015 HKBU/CS/JF/2023 29
Tutorial
(2)
4
COMP2015 HKBU/CS/JF/2023 30