Download presentation
Presentation is loading. Please wait.
1
Analysis Algorithms
2
Algorithms a finite set of precise instructions for performing a computation or for solving problem. an unambiguous sequence of steps which completes a task in a finite time. a step-by-step method of solving some problems.
3
Characteristics of an algorithm
There are several properties that algorithms generally share. These properties are: Input: An algorithm has input value from a specified set. Output: From each set of input values an algorithm produces output values from a specified set. The output values are the solution to the problem. Definiteness: The steps of an algorithm must be defined precisely.
4
Characteristics of an algorithm
Correctness: An algorithm should produce the correct output values for each set of input values. Finiteness : An algorithm should produce the desired output after a finite number of steps for any input in the set. Effectiveness: It must be possible to perform each step of an algorithm exactly an in finite amount of time. Generality: The procedure should be applicable for all problems of the desired form, not just a particular set of input values.
5
Describe an algorithm for finding the largest value in a finite set of integers.
Solution: We perform the following steps: 1. Set the temporary maximum equal to the first integer in the sequence. 2. Compare the next integer in the sequence to the temporary maximum, and if it is larger than the temporary maximum, set the temporary maximum to this integer. 3. Repeat the previous step if there are more integers in the sequence. 4.Stop when there are no integers left in the sequence. The temporary maximum at this point is the largest integer in the sequence.
6
An algorithm is a procedure for solving a problem in terms of the actions to be executed and the order in which those actions are to be executed. An algorithm is merely the sequence of steps taken to solve a problem. The steps are normally "sequence," "selec Pseudocode is an artificial and informal language that helps programmers develop algorithms. Pseudocode is a "text-based" detail (algorithmic) design tool.
7
Pseudocode An algorithm is a made up of one or more pseudocode.
Many versions of pseudocode is a language similar to a programming language used to represent algorithms. It resembles the actual code of computer languages, such as C and Java. Unlike actual computer languages, which must be concerned about semicolons, uppercase and lowercase letters, special words, and so on, any version of pseudo-code is acceptable as long as it is instructions and unambiguous.
8
Pseudo-code it is intended to be just read by humans, not actually executed by a machine. It is not required to follow strict syntax rules. Usually a pseudocode will look like this: ProcedureName(Input) Instructions... end ProcedureName
9
Example : The rules of Pseudocode are reasonably straightforward.
All statements showing "dependency" are to be indented. These include while, do, for, if, switch.. If student's grade is greater than or equal to 60 Print "passed" else Print "failed“ Set total to zero Set grade counter to one While grade counter is less than or equal to ten Input the next grade Add the grade into the total Set the class average to the total divided by ten Print the class average.
10
Example : Finding the largest value in a finite set of integers: (an algorithm written in a form of pseudocode.) 1: procedure max(a1,a2,…,an: integers) 2: large:= a1 3: for i = 2 to n 4: if (ai > large) 5: large :=ai 6: return large 7: end max max(a1,a2,…,an:integers) large:= a1 for i = 2 to n if (ai > large) large :=ai return large end max
11
Example : Write an algorithm that returns the sum of the sequence s1, s2, s3,….. sn seq_sum(s, n) sum=0 for i=1 to n sum=sum + si return sum end seq_sum
12
Example : Write an algorithm that reverse the sequence s1, s2, s3,….. Sn reverse(s, n) i=1 j=n while (i < j) swap(si, sj) i=i + 1 j=j -1 return s end reverse
13
Exercise: Create a program to add 2 numbers together then display the result. Compute the Area of a rectangle Compute the Perimeter of a rectangle
14
Pseudo code Sequential Looping Relational Operators
Compound conditions Logical operator (&&, II, !) Selection If-else Switch Looping While for
15
Good Practice “bad pseudocode” need the programmer to
put a lot of thought into each line in order to convert it, which defeats the original purpose of writing it The “good pseudocode” is detailed enough that in order to convert it into code
16
Binary Search: Example
a c d f g h j l m o p r s u v x z binary search for the letter ‘j’ center element search interval
17
Algorithm Examples procedure binary_search(x: integer; a1, a2, …, an: integers)i := 1 {i is left endpoint of search interval} j := n {j is right endpoint of search interval} while (i < j) begin m := (i + j)/2 if x > am then i := m + 1 else j := m end if x = ai then location := i else location := 0 {location is the subscript of the term that equals x, or is zero if x is not found}
20
Computer Organization
Complexity Analysis Computer Organization INPUT PROCESS OUTPUT MEMORY Did my program execute fast ? how much memory do I need for the process ? it is essential to analyze the resources needed for the algorithm. 20
21
Complexity For example, let us assume two algorithms A and B that solve the same class of problems. The time complexity of A is 5,000n, the one for B is 1.1n for an input with n elements. For n = 10, A requires 50,000 steps, but B only 3, so B seems to be superior to A. For n = 1000, however, A requires 5,000,000 steps, while B requires 2.51041 steps.
22
Complexity This means that algorithm B cannot be used for large inputs, while algorithm A is still feasible. So what is important is the growth of the complexity functions. The growth of time and space complexity with increasing input size n is a suitable measure for the comparison of algorithms.
23
Time and Space **Important to compare algorithms
Time required by an algorithm is the number of steps to termination. Space required by an algorithm is the amount of storage required by the input, local variables, etc.
24
Analysis of Algorithms
Measuring the efficiency of algorithms How are the algorithms coded ? What computer should you use ? What data should the programs use ? Algorithm Growth rate Time complexity Big-Oh notation
25
Big Oh Notation Greek letter Omicron (Ο) is used to denote the limit of asymptotic growth of an algorithm If algorithm processing time grows linearly with the input set n, then we say the algorithm is Order n, or O(n). This notation isolates an algorithm’s run-time from other factors: Size of the problem set Initialization time Processor speed and instruction set
26
Big-Oh notation Let b(x) be the bubble sort algorithm
We say b(x) is O(n2) This is read as “b(x) is big-oh n2” This means that the input size increases, the running time of the bubble sort will increase proportional to the square of the input size In other words, by some constant times n2 Let l(x) be the linear (or sequential) search algorithm We say l(x) is O(n) Meaning the running time of the linear search increases directly proportional to the input size
27
Big-Oh notation Consider: b(x) is O(n2) Consider: l(x) is O(n)
That means that b(x)’s running time is less than (or equal to) some constant times n2 Consider: l(x) is O(n) That means that l(x)’s running time is less than (or equal to) some constant times n
28
Big-Oh Rules If f(n) is a polynomial of degree d, then f(n) is O(nd), i.e., Drop lower-order terms Drop constant factors Use the smallest possible class of functions Say “2n is O(n)” instead of “2n is O(n2)” Use the simplest expression of the class Say “3n + 5 is O(n)” instead of “3n + 5 is O(3n)”
29
29
30
Complexity Analysis
31
Seven most important function
31
32
Big-Oh Graph Representation
32
33
Since the time that takes to execute an algorithm usually depends on the input, its complexity must be expressed as a function of the input, or more generally as a function of the size of the input. 1. Best-case time: minimum time needed to execute the algorithm among all inputs of a given size n. 2. Wost-case time: maximum time needed to execute the algorithm among all inputs of a given size n. 3. Average-case time: average time needed to execute the algorithm among all inputs of a given size n.
34
More examples Algorithm analysis
35
Measuring the time n n * n n (n+1) / 2
36
Algorithm Analysis for (j=0 ; j <n ; j++ ) n a= a + 1 1.n O(n) = 2n
37
Algorithm Analysis for (i=0 ; i < n ; i++) n
for (j=0 ; j <n ; j++ ) n2 a= a + 1 O(n2)= n+2n2
38
Algorithm Analysis int x , i=1; while ( i <= n ) n { x++; i++; }
O(3n)= 3n
39
Algorithm Analysis int x , i=1; while ( i <= n ) 1+log2n { x++;
i=i*2; } O(log2n) = 3(1+log2n)
40
Complexity Examples What does the following algorithm compute?
procedure who_knows(a1, a2, …, an: integers) m := 0 for i := 1 to n-1 for j := i + 1 to n if |ai – aj| > m then m := |ai – aj| {m is the maximum difference between any two numbers in the input sequence} Time complexity is O(n2).
41
Complexity Examples Another algorithm solving the same problem:
procedure max_diff(a1, a2, …, an: integers) min := a1 max := a1 for i := 2 to n if ai < min then min := ai else if ai > max then max := ai m := max – min Time complexity is O(n).
42
Algorithm prefixAverages2(X, n)
Input array X of n integers Output array A of prefix averages of X #operations A new array of n integers n s for i 0 to n 1 do n {s s + X[i] n A[i] s / (i + 1) } n return A Algorithm prefixAverages2 runs in O(n) time
43
Searching Algorithms Examples: Looking for a solution to a puzzle
Looking for an optimal move in a game Using a search engine on the web Looking for a specified text in a document when running a word processor (text searching) finding medical records in a hospital
44
Example: reverse_search(s, n, key) i=n while (i >=1) if(si = = key)
Write an algorithm that returns the index of the last occurrence of the value key in the sequence s1, s2, s3,….. sn. If key is not in the sequence, the algorithm returns the value 0. Example the sequence is : reverse_search(s, n, key) i=n while (i >=1) if(si = = key) return 1 exit i=i - 1 return 0 end reverse_search
45
Sorting Algorithms Sorting a sequence - sorting a sequence s means to rearrange the data so that s is in order ( increasing or decreasing) example: the entries in a book’s index are stored in increasing order, thus making it easy to quickly locate an entry in the index.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.