Академический Документы
Профессиональный Документы
Культура Документы
What is an Algorithm?
A well-defined computational procedure that takes some value (or set of values) as input and produces some value (or set of values) as output Provides a step by step method for solving a computational problem. Is not dependent on any particular programming language, machine or 2 compiler.
What is an Algorithm?
An organized sequence or list of clear steps or operations needed to solve a given programming problem 3 components: Inputs Steps or instructions Output Any well-defined computational procedure that takes some value(s) as input and produces some value(s) as output Sequence of computational steps that transform the input to output 3
Programmers
Different tools Different ways of specifying the procedure(s)
Chefs
Different Output Different Ingredients to create their final product
A Computational Problem is a well-defined specification of the relationship between the input and the output
7
Criteria
Input there are zero or more quantities which are externally supplied. Output at least one quantity is produced Definiteness each instruction must be clear and unambiguous Finiteness it terminates after a finite number of steps Effectiveness every instruction must be sufficiently basic that it can in principle be carried out by a person using only pencil 8 and paper.
Algorithm (Example)
Let A[i] be the ith number on the list (a1,a2,an)
1 2 3 4 5 6
Max, min = A[1] For i = 2 to n If A[i] > max then max = A[i] ElseIf A[i] < min then min = A[i] Next i Return max + min
10
11
Efficient Algorithms
We choose between different algorithms based on their efficiency Usual measure of efficiency: Speed how long an algorithm takes to produce its result
12
Technology improves things by a constant factor only Even a supercomputer cannot rescue a bad algorithm
a faster algorithm on a slower computer will always win for sufficiently large inputs
13
14
Merge-Sorting in Arrays
15
Merging: An Example
16
Merge-Sort
17
Analyzing Merge-Sort
18
19
20
21
22
23
24
25
26
Analyzing Algorithms
predicting the resources that the algorithm requires
memory communication bandwidth computer hardware computational time
27
Given two algorithms, A1 and A2 solving the same problem: which is better?
28
Assumption
We are using a generic processor, Random Access Memory (RAM) model of computation
Instructions are executed ONE AT A TIME, no concurrent operations
29
A priori analysis determines the efficiency of an algorithm based on or derived from mathematical or logical facts A posteriori analysis determines the efficiency of an algorithm based on actual experiments
30
100
10 sec
Algo 2
3 sec
15 sec
31
10
100
1000
30 sec
32
1 2 3 4 5 6
Max, min = A[1] For i = 2 to n If A[i] > max then max = A[i] ElseIf A[i] < min then min = A[i] Next i Return max + min
33
Rule1: Declarations with no initialization Declarations with no initialization have no operation count (i.e. 0).
36
Rule 2: Delimiters (such as { and }) Delimiters have no operation count (i.e. 0).
37
Rule 3: Function Heading Function heading such as main() has no operation count (i.e. 0).
38
Rule 4: Operators Each operator (whether arithmetic, logical, relational) has a running time of 1 (for simplicity only, though it may not hold in reality).
39
Rule 5: Expressions The operation count of an expression depends on the number of operators (arithmetic, logical, relational) are there.
40
Rule 6: Assignment Statement The operation count of an assignment statement is 1 plus the number of operators in the expression. Arithmetic operators (such as +=, -=, *=) have an operation count of 2.
41
Rule 7: Function call 1(function call) + operation count for the operators + operation count of the function
42
Rule 8: if/if-else Statement Given an if-else statement of the form: if (<condition>) <S1>; else <S2>; The overall operation count of the if-else statement is the operation count of the <condition> plus the maximum of the operation counts of <S1> and <S2>. The keyword else does not consume any operation 43 count.
Rule 9: for Statement Given a for statement of the form: for (<initialization>; <condition/test>; <increment>) { <S>; } The total operation count of the for statement is operation count of the <initialization> + operation count of the <condition/test> * (number of iterations + 1) + operation count of <increment> * number of iterations + operation count of <S> * number of 44 iterations
Rule 10: while Statement Given the while statement of the form while (<condition>) { <S>; } The operation count is the <condition> * (number of iterations) + operation count of <S> * number of iterations.
45
Rule 11: do-while Statement Given the do-while statement of the form do { <S>; } while (<condition>); The operation count is the <S> * number of iterations + operation count of the <condition> * number of iterations.
46
Rule 12: Nested Loops Analyze these inside out. The total operation count of a statement inside a group of nested loops is the operation count of the statement multiplied by the product of all the sizes of all the loops.
47
T(n) = 38
10
T(n) = 42
T(n) = 22
T(n) = 26
Lets Generalize
for (j = 1; j < 10; j++) printf (I love Ice Cream); 1 + 10 + 9(2) 9
1 + 6 + 5(2) 5
Lets Generalize
for (j = a; j < b; j++) printf (I love Ice Cream);
1 ba+1 ba ba
Lets Generalize
for (j = 1; j <= 10; j++) printf (I love Ice Cream); 1 + 11 + 10(2) 10
1 + 7 + 6(2) 6
Lets Generalize
for (j = a; j <= b; j++) printf (I love Ice Cream);
1 ba+2 ba+1 ba+1
Sample Problems
(a) for (j = 0; j < n; j++) printf (Sample Problem 1\n);
(b) for (j = 0; j < n; j++) { printf (Operation Count - ); printf (Sample Problem 2\n); }
(e)
59