CS-IT341_Lecture_2
CS-IT341_Lecture_2
Information Technology
CS/IT 341
Lecture 2
Recap: Algorithm
• Problem Statement
• Relationship between input and output
• Algorithm
• Procedure to achieve the relationship
• Definition
• A sequence of steps that transform the input to output
• Instance
• The input needed to compute solution
• Correct Algorithm 2
4
Al-Khwarizmi’s Golden Principle (2)
5
Al-Khwarizmi’s Golden Principle (3)
6
Problem Solving Process
• Problem
• Algorithm
Input
Output
Steps
• Analysis of Algorithm
Correctness (Input-Output Correlation)
Complexity (Resources Consumption)
Efficiency (Resources Optimality)
• Implementation
• Verification
7
Complexity of Algorithms
• The complexity of an algorithm is the amount of work the algorithm
performs to complete its task.
8
But Computers Are So Fast These Days??
• Do we need to bother with algorithmics and complexity
any more?
• CPUs of more speeds, compared to even 10 years ago...
• Memories of more capacities, compared to even 10 years ago..
• Networks of more throughputs, compared to even 10 years ago..
15
Algorithm Analysis (1)
• Two essential approaches to measuring algorithm efficiency:
• Empirical analysis:
• Program the algorithm and measure its running time on example
instances
• Theoretical analysis
• Employ mathematical techniques to derive a function which relates
the running time to the size of instance
16
Algorithm Analysis (2)
• The following three cases are investigated in algorithm analysis:
21
Empirical Analysis (4)
22
Empirical Analysis (5)
23
Empirical Analysis (6)
24
Empirical Analysis (7)
25
Empirical Analysis (8)
26
Empirical Analysis (9)
27
Empirical Analysis (10)
28
Empirical Analysis (11)
29
Empirical Analysis (12)
30
Empirical Analysis (13)
31
Limitations of Empirical Analysis
• Implementation dependent
• Execution time differ for
different implementations of
same program
• Platform dependent
• Execution time differ on different
architectures
• Data dependent
• Execution time is sensitive to
amount and type of data
minipulated.
• Language dependent
• Execution time differ for same
code, coded in different 32
languages
• Platform independent
• Language independent
• Implementatiton independent
• not dependent on skill of programmer
• can save time of programming an inefficient solution
ArrayMax(A, n)
Input: Array A of n integers
Output: maximum element of A
1. currentMax A[0];
2. for i = 1 to n-1 do
3. if A[i] > currentMax then 34
4. currentMax A[i]
5. return currentMax;
Pseudocode (2)
• Indentation indicates block structure. e.g body of loop
• Looping Constructs while, for and the conditional if-then-else
• The symbol // indicates that the reminder of the line is a comment.
• Arithmetic & logical expressions: (+, -,*,/, ) (and, or and not)
• Assignment & swap statements: a b , ab c, a b
• Return/Exit/End: termination of an algorithm or block
ArrayMax(A, n)
Input: Array A of n integers
Output: maximum element of A
1. currentMax A[0];
2. for i = 1 to n-1 do
3. if A[i] > currentMax then 35
4. currentMax A[i]
5. return currentMax;
Pseudocode (3)
• Local variables mostly used unless global variable explicitly defined
• If A is a structure then |A| is size of structure. If A is an Array then
n =length[A], upper bound of array. All Array elements are
accessed by name followed by index in square brackets A[i].
• Parameters are passed to a procedure by values
• Semicolons used for multiple short statement written on one line
ArrayMax(A, n)
Input: Array A of n integers
Output: maximum element of A
1. currentMax A[0]
2. for i = 1 to n-1 do
3. if A[i] > currentMax then 36
4. currentMax A[i]
5. return currentMax
Elementary Operations
• An elementary operation is an operation which takes constant time
regardless of problem size.
• The running time of an algorithm on a particular input is
determined by the number of “Elementary Operations” executed.
• Theoretical analysis on paper from a description of an algorithm
38
Instruction
• A linear sequence of elementary operations is also performed in
constant time.
• More generally, given two program fragments P1 and P2 which run
sequentially in times t1 and t2
• use the maximum rule which states that the larger time dominates
• complexity will be max(t1,t2)
39
Selection
• If <test> then P1 else P2 structures are a little harder;
conditional loops.
• The maximum rule can be applied here too:
• max(t1, t2), assuming t1, t2 are times of P1, P2
• However, the maximum rule may prove too conservative
• if <test> is always true the time is t1
• if <test> is always false the time is t2
for i = 1 to n do
P(i);
T(n) = nt
41
• This approach is reasonable, provided n is positive
• If n is zero or negative the relationship T(n) = nt is not valid
Repetition (Loops) - 2
Analysing Nested Loops
for i = 0 to n do
for j = 0 to m do
P(j);
for i = 0 to n do
for j = 0 to i do
P(j);
• Assume that P(j) takes time t, where t is independent of i and j
• How do we analyze the running time of an algorithm that has
complex nested loop?
• The answer is we write out the loops as summations and then
solve the summations. To convert loops into summations, we
work from inside-out.
i 0 ri + t
n n
T(n) = n + r
i 0 i 43
= n + n(n+1)/2 + tn(n+1)/2
Analysis Example
Algorithm: Number of times executed
1. n = read input from user 1
2. sum = 0 1
3. i = 0 1
4. while i < n n
n or i 0 1
n 1
5. number = read input from user
n or i 0 1
n 1
6. sum = sum + number
7. i=i+1
8. mean = sum / n
n or n 11
1 i 0
The computing time for this algorithm in terms on input size n is:
T(n) = 1 + 1 + 1 + n + n + n + n + 1 44
T(n) = 4n + 4
Another Analysis Example
i=1 ...............................1
while (i < n)................n-1
a=2+g...............n-1
i=i+1 ................n-1
if (i<=n).......................1
a=2 ....................1
else
a=3.....................1
T(n) = 1 + 3(n-1) + 1 + 1
45
=3n
Another Analysis Example
i=1...............................? 1
while (i<=10)................? 10
i=i+1...................? 10
i=1 ...............................? 1
while (i<=n)..................? n
a=2+g .................? n
i=i+1 ...................? n
if (i<=n)........................ ? 1
a=2 .................... ? 1
else
a=3..................... ? 1
46
T ( n) = ? T ( n) = 3n +24
The End
Questions?
47