Professional Documents
Culture Documents
What is Programming?
2
What is Data Structure?
■ Definition
■ It is a way of collecting and organizing data in a computer.
■ An aggregation of atomic and composite data into a set with defined
relationships.
Data Structures
Primitive Non-Primitive
Arrays
Integer Linear Stacks
Lists
Float Non-Linear Queues
Trees
Character Files
Graphs
3
What is Algorithm?
■ Definition: a finite set of instructions that should satisfy the
following:
■ Input: zero or more inputs
■ Output: at least one output
■ Definiteness: clear and unambiguous instructions
■ Finiteness: terminating after a finite number of steps
■ Effectiveness (Machine-executable): enough to be carried out
4
Algorithm Specification
■ How to express algorithms
■ High-level description
■ Natural language
■ Graphic representation, e.g., flowcharts
■ Pseudocode: informal language-dependent description
■ Implementation description
■ C, C++, Java, and etc.
5
Natural Language vs. Graphic Chart
■ Example: Selection Sort
From those integers that are currently unsorted, find the smallest value.
Place it next in the sorted list.
list[0] 6 2 2 2 2 2
list[1] 5 5 3 3 3 3
list[2] 3 3 5 4 4 4
list[3] 4 4 4 5 5 5
list[4] 2 6 6 6 6 6
Step 1 Step 2 Step 3 Step 4 Step 5 Step 6
6
Pseudocode (C-like Language)
■ Example: Selection Sort
7
Implementation in C
■ Example: Selection Sort
8
Algorithm Analysis
■ How do we evaluate algorithms?
■ Does the algorithm use the storage efficiently?
■ Is the running time of the algorithm acceptable for the task?
void search(int arr[], int len, int target) {
for (int i = 0; i < len; i++) {
if (arr[i] == target)
return i;
}
return -1
}
■ Performance analysis
■ Estimating machine-independent time and space
10
11
12
Time Complexity
■ Definition
■ (machine-independent) time required by an algorithm
■ Time is not easy to estimate!
■ Alternative
■ Count the number of program steps instead of time.
13
Time Complexity
■ Program steps
■ Syntactically or semantically meaningful program segment whose execution
time is independent of the number of inputs
14
Time Complexity
■ Example
15
Time Complexity
■ What is better for time complexity?
16
Asymptotic Notation
■ Do we need to calculate exact numbers?
200000
C 2000 40,000 600,000 ???
0
10 100 1,000
17
Asymptotic Notation
■ What is better?
■ (10n+10) vs. (0.01n2+10)
Simple rule:
1. Ignore any constants.
2. Compare only the term of the highest order.
18
Asymptotic Notation
■ Three notations for complexity
■ Big-O notation : O(f(n))
■ The complexity is not increasing faster than f(n).
19
Big-O Notation
■ Definition
f(n) = Ο(g(n))
⬄ f(n) is not increasing faster than g(n)
⬄ For a large number no, c*g(n) will be larger than f(n)
⬄ There exist positive constants c and no such that
f(n) ≤ c*g(n) for all n > no
■ Example
■ 3n + log n + 2 = Ο(n), because 3n + log n + 2 ≤ 5n for n ≥ 2
■ 10n2 + 4n + 2 = Ο(n4), because 10n2 + 4n + 2 ≤ 10n4 for n ≥ 2
20
Example: Asymptotic Notation
■ Three asymptotic notations for time complexity
21
Discussion: Asymptotic Notation
■ Big-O notation is widely used.
■ Big-Θ notation is the most informative, but the exact value is hard to know.
■ Big-Ω notation is the least informative. Why?
■ Big-O notation is good for rough description.
22
Comparison: Asymptotic Notation
■ Which is more costly?
■ O(1), O(log n), O(n log n), O(n2), O(n3), O(2n), O(n!), etc..
O(1): constant
O(log2n): logarithmic
O(n): linear
O(n·log2n): log-linear
O(n2): quadratic
O(n3): cubic
O(2n): exponential
O(n!): factorial
23
Mathematical Analysis of Non-recursive Algorithms
24
Matrix Multiplication
25
Finding the number of binary digits in the
binary representation of a positive decimal integer.
26
Mathematical Analysis of Recursive Algorithms
Factorial of a Number
27
Mathematical Analysis of Recursive Algorithms
Factorial of a Number
28
Towers of Hanoi
29
Recursive algorithm - Counting digits in n’s binary representation
30
Master’s Theorem for Recurrence Relation
(Divide and Conquer)
31
Master’s Theorem for Recurrence Relation
(Decrease and Conquer)
Recurrence Relation:
T(n) = c, if n <= 0
Solution:
T(n) = O(n^(k+1)), if a = 1
32