Analysis Algorithms.

Slides:



Advertisements
Similar presentations
College of Information Technology & Design
Advertisements

College of Information Technology & Design
CHAPTER 2 ALGORITHM ANALYSIS 【 Definition 】 An algorithm is a finite set of instructions that, if followed, accomplishes a particular task. In addition,
Chapter 1 – Basic Concepts
What is an Algorithm? (And how do we analyze one?)
CSC401 – Analysis of Algorithms Lecture Notes 1 Introduction
Analysis of Algorithms Algorithm Input Output. Analysis of Algorithms2 Outline and Reading Running time (§1.1) Pseudo-code (§1.1) Counting primitive operations.
CSE115/ENGR160 Discrete Mathematics 02/24/11 Ming-Hsuan Yang UC Merced 1.
Analysis of Algorithms1 Estimate the running time Estimate the memory space required. Time and space depend on the input size.
The Fundamentals: Algorithms, the Integers & Matrices.
February 17, 2015Applied Discrete Mathematics Week 3: Algorithms 1 Double Summations Table 2 in 4 th Edition: Section th Edition: Section th.
Chapter 2 The Fundamentals: Algorithms, the Integers, and Matrices
Time Complexity Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
Chapter 3: The Fundamentals: Algorithms, the Integers, and Matrices
Week 2 CS 361: Advanced Data Structures and Algorithms
Discrete Structures Lecture 11: Algorithms Miss, Yanyan,Ji United International College Thanks to Professor Michael Hvidsten.
DISCRETE MATHEMATICS I CHAPTER 11 Dr. Adam Anthony Spring 2011 Some material adapted from lecture notes provided by Dr. Chungsim Han and Dr. Sam Lomonaco.
Analysis of Algorithms
Fall 2002CMSC Discrete Structures1 Enough Mathematical Appetizers! Let us look at something more interesting: Algorithms.
MCA-2012Data Structure1 Algorithms Rizwan Rehman CCS, DU.
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
Data Structure Introduction.
Chapter Algorithms 3.2 The Growth of Functions 3.3 Complexity of Algorithms 3.4 The Integers and Division 3.5 Primes and Greatest Common Divisors.
1 Section 2.1 Algorithms. 2 Algorithm A finite set of precise instructions for performing a computation or for solving a problem.
ALGORITHMS.
ALGORITHMS AND FLOWCHARTS. Why Algorithm is needed? 2 Computer Program ? Set of instructions to perform some specific task Is Program itself a Software.
Copyright © 2014 Curt Hill Algorithms From the Mathematical Perspective.
BITS Pilani Pilani Campus Data Structure and Algorithms Design Dr. Maheswari Karthikeyan Lecture1.
Ch03-Algorithms 1. Algorithms What is an algorithm? An algorithm is a finite set of precise instructions for performing a computation or for solving a.
Chapter 3 Chapter Summary  Algorithms o Example Algorithms searching for an element in a list sorting a list so its elements are in some prescribed.
Discrete Mathematics Chapter 2 The Fundamentals : Algorithms, the Integers, and Matrices. 大葉大學 資訊工程系 黃鈴玲.
Algorithm Analysis 1.
Analysis of Algorithms
CSE15 Discrete Mathematics 03/06/17
Introduction to Algorithms
Growth of Functions & Algorithms
COMP9024: Data Structures and Algorithms
Applied Discrete Mathematics Week 2: Functions and Sequences
COMP9024: Data Structures and Algorithms
Analysis of Algorithms
COP 3503 FALL 2012 Shayan Javed Lecture 15
GC211Data Structure Lecture2 Sara Alhajjam.
ALGORITHMS AND FLOWCHARTS
Analysis of Algorithms
COMP9024: Data Structures and Algorithms
Analysis of Algorithms
DATA STRUCTURES Introduction: Basic Concepts and Notations
Teach A level Computing: Algorithms and Data Structures
CS 583 Fall 2006 Analysis of Algorithms
Enough Mathematical Appetizers!
Computation.
Algorithms Chapter 3 With Question/Answer Animations
Algorithm An algorithm is a finite set of steps required to solve a problem. An algorithm must have following properties: Input: An algorithm must have.
Objective of This Course
Algorithm Efficiency Chapter 10.
Analysis of Algorithms
Algorithm Discovery and Design
Analysis of Algorithms
Applied Discrete Mathematics Week 6: Computation
Analysis of Algorithms
Introduction to Algorithms
Enough Mathematical Appetizers!
Revision of C++.
Analysis of Algorithms
Enough Mathematical Appetizers!
Discrete Mathematics 7th edition, 2009
At the end of this session, learner will be able to:
Algorithms.
Discrete Mathematics CS 2610
Analysis of Algorithms
Presentation transcript:

Analysis Algorithms

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.

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.

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.

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.

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.

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.

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

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.

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

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

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

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

Pseudo code Sequential Looping Relational Operators Compound conditions Logical operator (&&, II, !) Selection If-else Switch Looping While for

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

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

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}

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

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.51041 steps.

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.

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.

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

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

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

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

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

Complexity Analysis

Seven most important function 31

Big-Oh Graph Representation 32

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.

More examples Algorithm analysis

Measuring the time n n * n n (n+1) / 2

Algorithm Analysis for (j=0 ; j <n ; j++ ) n a= a + 1 1.n O(n) = 2n

Algorithm Analysis for (i=0 ; i < n ; i++) n for (j=0 ; j <n ; j++ ) n2 a= a + 1 O(n2)= n+2n2

Algorithm Analysis int x , i=1; while ( i <= n ) n { x++; i++; } O(3n)= 3n

Algorithm Analysis int x , i=1; while ( i <= n ) 1+log2n { x++; i=i*2; } O(log2n) = 3(1+log2n)

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).

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).

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  0 1 for i  0 to n  1 do n {s  s + X[i] n A[i]  s / (i + 1) } n return A 1 Algorithm prefixAverages2 runs in O(n) time

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

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 : 9 12 11 12 23 17 reverse_search(s, n, key) i=n while (i >=1) if(si = = key) return 1 exit i=i - 1 return 0 end reverse_search

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.