Lecture 1.2 Introduction and Complexity (Advance) Upload
Lecture 1.2 Introduction and Complexity (Advance) Upload
Week 1
AIUB
Course Objective
• Theoretical importance
• The core of computer science
•Practical importance
• A practitioner’s toolkit of known algorithms
• Framework for designing and analyzing algorithms for new
problems
ANALYSIS OF ALGORITHMS
•Sorting
•Searching
•Shortest paths in a graph
•Minimum spanning tree
•Traveling salesman problem
•Knapsack problem
•Chess
•Towers of Hanoi
•Program termination
ALGORITHM DESIGN STRATEGIES
•Brute force
•Divide and conquer
•Decrease and conquer
•Transform and conquer
•Greedy approach
•Dynamic programming
•Backtracking and Branch and bound
•Space and time tradeoffs
ANALYSIS OF ALGORITHMS
• For the above defined function max(), following is the function declaration:
• Parameter names are not important in function declaration only their type is required,
so following is also valid declaration:
• Function declaration is required when you define a function in one source file and you call
that function in another file. In such case you should declare the function at the top of the file
calling the function.
Calling a Function
Code Example: Calling a Function
RECURSIVELY DEFINED FUNCTIONS
o A function is said to be recursively defined if the function
refers to itself such that
1. There are certain arguments, called base values, for which
the function does not refer to itself.
2. Each time the function does refer to itself, the argument
of the function must be closer to a base value.
Recursion
• RECURSION
The process of defining an object in terms of smaller versions of
itself is called recursion.
• A recursive definition has two parts:
1. BASE:
An initial simple definition which cannot be expressed in terms of
smaller versions of itself.
2. RECURSION:
The part of definition which can be expressed in terms of smaller
versions of itself.
THE FACTORIAL OF A POSITIVE INTEGER: Example
f(2)
f(1) f(1) f(0)
f(1) f(0)
USE OF RECURSION
o At first recursion may seem hard or impossible, may be
magical at best. However, recursion often provides elegant,
short algorithmic solutions to many problems in computer
science and mathematics.
– Examples where recursion is often used
• math functions
• number sequences
• data structure definitions
• data structure manipulations
• language definitions
USE OF RECURSION
o For every recursive algorithm, there is an
equivalent iterative algorithm.
o However, iterative algorithms are usually more
efficient in their use of space and time.
GOOD ALGORITHMS?
Is it correct ?
FACTORS
Hardware
Operating System
Compiler
Size of input
Nature of Input
Algorithm
Depends upon
Input Size
Nature of Input
Introduction34
Analysis of Algorithms
• Efficiency:
• Running time
• Space used
• Efficiency as a function of the input size:
• Number of data elements (numbers, points).
• The number of bits of an input number .
Introduction35
The RAM Model
• RAM model represents a “generic” implementation of the
algorithm
• Each “simple” operation (+, -, =, if, call) takes exactly 1 step.
• Loops and subroutine calls are not simple operations, but
depend upon the size of the data and the contents of a
subroutine. We do not want “sort” to be a single step
operation.
• Each memory access takes exactly 1 step.
The RAM model (cntd..)
• It is important to choose the level of detail.
• The RAM model:
• Instructions (each taking constant time), we usually choose one type of
instruction as a characteristic operation that is counted:
• Arithmetic (add, subtract, multiply, etc.)
• Data movement (assign)
• Control flow (branch, subroutine call, return)
• Comparison (logical ops)
• Data types – integers, characters, and floats
Introduction37
Example
Algorithm arrayMax(A, n)
# operations
currentMax A[0] 2
for (i =1; i<n; i++) 2n
(i=1 once, i<n n times, i++ (n-1) times)
if A[i] currentMax then 2(n 1)
currentMax A[i] 2(n 1)
return currentMax 1
Total 6n 1
Introduction38
Example: N-by-N matrix, N-by-1 vector, multiply
(3 accesses, 1 add, 1 multiply)
N times
c4
N times
Y(i) = Y(i) + A(i,j)*x(j);
end End of loop, return/exit, c5
end End of loop, return/exit, c5
Total = c1N+c2N+N(c3+c2N+N(c4+c5)+c5)
= (c2+c4+c5)N2 + (c1+c2+c3+c5)N
= c6N2 + c7N
Introduction39
Time complexity familiar tasks
Task Growth rate
Matrix/vector multiply O(N2)
Getting a specific element from a list O(1)
Dividing a list in half, dividing one halve in half, etc O(log2N)
Binary Search O(log2N)
Scanning (brute force search) a list
O(N)
Nested for loops (k levels)
O(Nk)
MergeSort
O(N log2N)
BubbleSort
O(N2)
Generate all subsets of a set of data
O(2N)
Generate all permutations of a set of data
O(N!)
Introduction40
Best/ Worst/ Average Case
• Best case: works fast on some input.
• Worst case: (usually) maximum time of algorithm on any input of size.
• Average case: (sometimes) expected time of algorithm over all inputs of size. Need assumption
of statistical distribution of inputs.
Introduction41
…Best/ Worst/
• For inputs of all sizes:
Average Case
worst-case
6n average-case
Running time
5n
best-case
4n
3n
2n
1n
1 2 3 4 5 6 7 8 9 10 11 12 …..
Input instance size
Introduction42
…Best/ Worst/ Average Case
• Worst case is usually used:
• It is an upper-bound.
• In certain application domains (e.g., air traffic control, surgery) knowing the worst-case
time complexity is of crucial importance.
• For some algorithms worst case occurs fairly often
• The average case is often as bad as the worst case.
• Finding the average case can be very difficult.
Introduction43
Asymptotic Notation
• Asymptotic Notations are languages that allow us to analyze an algorithm's
running time by identifying its behavior as the input size for the algorithm
increases.
• This is also known as an algorithm's growth rate.(Wiki)
Introduction44
Asymptotic Notation
• The “big-Oh” O-Notation
• asymptotic upper bound
• f(n) = O(g(n)), if there exists constants c>0 and n0>0, s.t. f(n) £ c * g(n) for n ³ n0
• f(n) and g(n) are functions
over non- negative integers c g ( n )
f (n )
• Used for worst-case analysis
Running Time
• F(n)=2n^2+n+3
• G(n)=n^2 n0 Input Size
Introduction45
Asymptotic Notation
Introduction46
...Asymptotic Notation
• The “big-Omega” W-Notation
• asymptotic lower bound
• f(n) = W(g(n)) if there exists constants c>0 and n0>0, s.t. c g(n) £ f(n) for n ³ n0
f (n )
Running Time
c g ( n )
n0 Input Size
Introduction47
...Asymptotic Notation
• The “big-Theta” Q-Notation
• asymptoticly tight bound
• f(n) = Q(g(n)) if there exists constants c1>0, c2>0, and n0>0, s.t. for n ³ n0 c1 g(n) £ f(n) £ c2 g(n)
Running Time
c1 g (n )
n0 Input Size
Introduction48
Asymptotic Analysis
• Goal: to simplify the analysis of the running time by getting rid of
details, which are affected by specific implementation and hardware
• rounding of numbers: 1,000,001 » 1,000,000
• rounding of functions: 3n2 » n2
Introduction49
...Asymptotic Analysis
• Simple Rule: Drop lower order terms and constant factors.
• 50 n log n is O(n log n)
• 7n - 3 is O(n)
• 8n2 log n + 5n2 + n is O(n2 log n)
Introduction50
Correctness of Algorithms
• An algorithm is correct if for any legal input it terminates and produces the
desired output.
• Automatic proof of correctness is not possible (so far).
• There are practical techniques and rigorous formalisms that help to reason about
the correctness of (parts of) algorithms.
Introduction51
Pre/post-conditions
• Example:
• Write a pseudocode algorithm to find the two smallest numbers in a sequence of
numbers (given as an array).
• Termination: when a loop terminates the invariant gives a useful property to show the correctness of the
algorithm
Introduction53
SIMPLE EXAMPLE (1)
N = 10 => 53 steps
N = 100 => 503 steps
N = 1,000 => 5003 steps
N = 1,000,000 => 5,000,003 steps
Why?
t s( e mi T
)spe
26
wth Rates and Dominance Relations
Introduction60
Complexity Analysis
Time complexity
Time complexity
Time complexity
Time complexity
Time complexity
Time complexity
STANDARD ANALYSIS
TECHNIQUES
Analyzing Loops
Arithmetic operations:
x = 5 * y + 4 - z;
Array referencing:
A[j] = 5;
Array assignment:
∀ j, A[j] = 5;
int j,k;
for (j=0; j<N; j++)
for (k=N; k>0; k--)
sum += k+j;
int j,k;
for (j=0; j < N; j++)
for (k=0; k < j; k++)
sum += k+j;
Gauss figured out that the sum of the first n numbers is always:
ANALYZING NESTED LOOPS[3]
int K=0;
for(int i=0; i<N; i++)
{
cout <<”Hello”;
for(int j=0; j<K; j--)
Sum++;
}
SEQUENCE OF STATEMENTS
For a sequence of statements, compute their complexity
functions individually and add them up
37
CONDITIONAL STATEMENTS
What about conditional statements such as
if (condition)
statement1;
else
statement2;
However if N ≥ 2, then running time T(N) is the cost of each step taken plus time
required to compute power(x,n-1). (i.e. T(N) = 2+T(N-1) for N ≥ 2)
39