1 R. Johnsonbaugh, Discrete Mathematics Chapter 4 Algorithms.

Slides:



Advertisements
Similar presentations
Chapter 20 Computational complexity. This chapter discusses n Algorithmic efficiency n A commonly used measure: computational complexity n The effects.
Advertisements

Discrete Structures CISC 2315
Analysys & Complexity of Algorithms Big Oh Notation.
Chapter 1 – Basic Concepts
the fourth iteration of this loop is shown here
CSC401 – Analysis of Algorithms Lecture Notes 1 Introduction
Analysis of Algorithms1 Estimate the running time Estimate the memory space required. Time and space depend on the input size.
1 Data Structures A program solves a problem. A program solves a problem. A solution consists of: A solution consists of:  a way to organize the data.
Algorithm Analysis CS 201 Fundamental Structures of Computer Science.
CS2420: Lecture 4 Vladimir Kulyukin Computer Science Department Utah State University.
Analysis of Algorithms 7/2/2015CS202 - Fundamentals of Computer Science II1.
Analysis of Algorithms COMP171 Fall Analysis of Algorithms / Slide 2 Introduction * What is Algorithm? n a clearly specified set of simple instructions.
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Analysis of Algorithms Spring 2015CS202 - Fundamentals of Computer Science II1.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 1 Prepared by İnanç TAHRALI.
February 17, 2015Applied Discrete Mathematics Week 3: Algorithms 1 Double Summations Table 2 in 4 th Edition: Section th Edition: Section th.
Algorithm analysis and design Introduction to Algorithms week1
Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure.
1. Reference  2  Algorithm :- Outline the essence of a computational procedure, step by step instructions.  Program :- an.
CSC 201 Analysis and Design of Algorithms Lecture 03: Introduction to a CSC 201 Analysis and Design of Algorithms Lecture 03: Introduction to a lgorithms.
C. – C. Yao Data Structure. C. – C. Yao Chap 1 Basic Concepts.
Discrete Mathematics Algorithms. Introduction  An algorithm is a finite set of instructions with the following characteristics:  Precision: steps are.
Lecture 8. How to Form Recursive relations 1. Recap Asymptotic analysis helps to highlight the order of growth of functions to compare algorithms Common.
For Wednesday Read Weiss chapter 3, sections 1-5. This should be largely review. If you’re struggling with the C++ aspects, you may refer to Savitch, chapter.
Discrete Structures Lecture 11: Algorithms Miss, Yanyan,Ji United International College Thanks to Professor Michael Hvidsten.
Chapter 1 1. Overview: System life cycle Requirements Analysis Bottom-up Top-down Design Data objects (abstract data type) and operations (algorithm)
2.3 Functions A function is an assignment of each element of one set to a specific element of some other set. Synonymous terms: function, assignment, map.
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.
Algorithm Analysis An algorithm is a clearly specified set of simple instructions to be followed to solve a problem. Three questions for algorithm analysis.
Analysis of Algorithms
CS 221 Analysis of Algorithms Instructor: Don McLaughlin.
Analyzing algorithms & Asymptotic Notation BIO/CS 471 – Algorithms for Bioinformatics.
Fall 2002CMSC Discrete Structures1 Enough Mathematical Appetizers! Let us look at something more interesting: Algorithms.
Asymptotic Notation (O, Ω, )
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.
Algorithm Analysis CS 400/600 – Data Structures. Algorithm Analysis2 Abstract Data Types Abstract Data Type (ADT): a definition for a data type solely.
Chapter 5 Algorithms (2) Introduction to CS 1 st Semester, 2015 Sanghyun Park.
Algorithm Analysis Part of slides are borrowed from UST.
Chapter 2 Computational Complexity. Computational Complexity Compares growth of two functions Independent of constant multipliers and lower-order effects.
ALGORITHMS.
R. Johnsonbaugh, Discrete Mathematics 5 th edition, 2001 Chapter 3 Algorithms.
BITS Pilani Pilani Campus Data Structure and Algorithms Design Dr. Maheswari Karthikeyan Lecture1.
1 Chapter 2 Algorithm Analysis Reading: Chapter 2.
Copyright © 2014 Curt Hill Algorithm Analysis How Do We Determine the Complexity of Algorithms.
1 Recursion Recursive function: a function that calls itself (directly or indirectly). Recursion is often a good alternative to iteration (loops). Its.
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.
Analysis of Algorithms Spring 2016CS202 - Fundamentals of Computer Science II1.
GC 211:Data Structures Week 2: Algorithm Analysis Tools Slides are borrowed from Mr. Mohammad Alqahtani.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
CSE 3358 NOTE SET 2 Data Structures and Algorithms 1.
Discrete Mathematics Chapter 2 The Fundamentals : Algorithms, the Integers, and Matrices. 大葉大學 資訊工程系 黃鈴玲.
Algorithm Analysis 1.
Introduction to the Design and Analysis of Algorithms
Introduction to Algorithms
What is an Algorithm? Algorithm Specification.
DATA STRUCTURES Introduction: Basic Concepts and Notations
Enough Mathematical Appetizers!
Computation.
Algorithm Analysis Neil Tang 01/22/2008
Algorithm design and Analysis
CS 201 Fundamental Structures of Computer Science
Analysys & Complexity of Algorithms
Applied Discrete Mathematics Week 6: Computation
Asst. Dr.Surasak Mungsing
Enough Mathematical Appetizers!
Introduction to Algorithm and its Complexity Lecture 1: 18 slides
Enough Mathematical Appetizers!
Discrete Mathematics 7th edition, 2009
CS210- Lecture 2 Jun 2, 2005 Announcements Questions
Presentation transcript:

1 R. Johnsonbaugh, Discrete Mathematics Chapter 4 Algorithms

2 4.1 Introduction  An algorithm is a finite set of instructions with the following characteristics:  Precision: steps are precisely stated  Deterministic: Results of each step of execution are uniquely defined. They depend only on inputs and results of preceding steps  Finiteness: the algorithm stops after finitely many steps.  Correctness: The output produced is correct.

3 More characteristics of algorithms  Input: the algorithm receives input  Output: the algorithm produces output  Generality: the algorithm applies to various sets of inputs

4 Example: a simple algorithm Algorithm to find the largest of three numbers a, b, c: Assignment operator s := k means “copy the value of k into s” 1. x:= a 2. If b > x then x:= b 3. If c > x then x:= c A trace is a check of the algorithm for specific values of a, b and c

5 4.2 Examples of algorithms Pseudocode: Instructions given in a generic language similar to a computer language such as C++ or Pascal. procedure if-then, action if-then-else begin return while loop for loop end

6 Variable declaration integer x, y; real x; or x : integer; boolean a; char c, d; datatype x; Note: We shall prefer not to give a complete declaration when the context of variables is obvious.

7 Assignment statements x := expression; orx = expression; or x  expression; eg. x  *2 y := a * y + 2;

8 Control structures  if if then [else ] endif

9 Control structures (cont.)  While while do ; endwhile

10 Control structures (cont.)  loop-until loop ; until Note: In comparison to while statement, the loop-until guarantees that the will be executed at least once.

11 Control structures (cont.)  for for i = to [step d] endfor

12 Control structures (cont.)  Case case : : ; : : ; [default : ] endcase

13 I/O statements read( ); print( );

14 Exit statement Example while condition1 do while condition2 do while condition3 do if…then exit (exit from the outmost loop) endwhile

15 Functions and procedures function name(parameter list) begin declarations statements; return(value); endname

16 Functions and procedures procedure name(parameter list) begin declarations statements; endname Note: Procedures are the similar to functions but they have no return statement.

17 Examples Procedure swap( x, y) /* in this case x, y are inout*/ begin temp  x x  y y  temp endswap

18 Examples: Find the maximum value of 3 numbers Input: a, b, c Output: large (the largest of a, b, and c) procedure max(a,b,c) { large = a if (b > large) large = b if (c > large) large = c return large }

19 Examples: Find the maximum value in a sequence S 1, S 2, S 3,…, S n Input: S, n Output: large (the largest value in the sequence S) procedure max(S,n) { large = S 1 for i = 2 to n if (S i > large) large = S i return large }

20 Examples: Print the even numbers 2, 4, 6,…, 10 Input: None Output: even numbers 2, 4, 6,…, 10 procedure printEven() { for i = 2 to 10 print i; }

21 Examples: Read 10 integer values, sort them in ascending order and print the even numbers Input: None Output: Sorted 10 integer values in ascending order procedure simpleSort() { integer numbers[10] for i = 1 to 10 read numbers[i];

22 Examples: Read 10 integer values, sort them in ascending order and print the even numbers for i = 1 to 10 { for j = i+1 to 10 { if (numbers[i] > numbers[j] ) then { temp = numbers[i] numbers[i] = numbers[j] numbers[j] = temp }

23 Examples: Read 10 integer values, sort them in ascending order and print the even numbers for i = 1 to 10 print numbers[i]; }

Analysis of algorithms  Big O notation Definition: f(n) = O(g(n)) iff there exist two positive constants c and n 0 such that |f(n)| = n 0. Theorem If A(n) = a m n m +…+ a 1 n + a 0 is a polynomial of degree m then A(n) = O(n m ).

25 Analysis of algorithms Ex. f(n) = 3 n 2 f(n) = O(n 2 ) Ex. f(n) = 3 n 2 + 5n + 4 f(n) = O(n 2 )

Recursive algorithms  A recursive procedure is a procedure that invokes itself  Example: given a positive integer n, factorial of n is defined as the product of n by all numbers less than n and greater than 0. Notation: n! = n(n-1)(n-2)…3.2.1  Observe that n! = n(n-1)! = n(n-1)(n-2)!, etc.  A recursive algorithm is an algorithm that contains a recursive procedure

27 Fibonacci sequence  Leonardo Fibonacci (Pisa, Italy, ca )  Fibonacci sequence f 1, f 2,… defined recursively as follows: f 1 = 1 f 2 = 2 f n = f n-1 + f n-2 for n > 3  First terms of the sequence are: 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597,…

28 How to write a recursion algorithm Function a(input) begin basis steps; /* for minimum size input */ call a(smaller input); /* could be a number of recursive calls*/ combine the sub-solution of the recursive call; end a

29 Recursion (cont.) function max( A[i : j]) begin if i = j then return( A[i]); else m1 = max ( A[ i : (i+j)/2] ); m2 = max ( A[ (i+j)/2 + 1 : j] ); if m1 > m2 then return( m1) else return(m2) endif endmax

30 Recursion: 4 Basic Rules  Base cases can be solved without recursion.  Making progress toward a base case for cases that are to be solved recursively.

31 Recursion: 4 Basic Rules  Design rule. Assume that all recursive calls work.  Compound interest rule. Never duplicate work by solving the same instance of a problem in separate recursive calls.

32 Recursion: A poor use of recursion fib (int : n) begin if ( n <= 1) then return 1; else return fib(n-1) + fib(n-2) /*redundant work*/ endif end fib

Complexity of algorithms  Complexity: the amount of time and/or space needed to execute the algorithm.  Complexity depends on many factors: data representation type, kind of computer, computer language used, etc.

34 Types of complexity Best-case time = minimum time needed to execute the algorithm for inputs of size n Worst-case time = maximum time needed to execute the algorithm for inputs of size n Average-case time = average time needed

35 Order of an algorithm Z Let f and g be functions with domain Z + = {1, 2, 3,…}  f(n) = O(g(n)): f(n) is of order at most g(n)  if there exists a positive constant C 1 such that |f(n)| < C 1 |g(n)| for all but finitely many n  f(n) =  (g(n)): f(n) is of order at least g(n)  if there exists a positive constant C 2 such that |f(n)| > C 2 |g(n)| for all but finitely many n  f(n) =  (g(n)): f(n) is or order g(n) if it is O(g(n)) and  (g(n)).

36 Review of Big O Example: 3n + 2 = O(n) as 3n + 2 = 2. 3n + 3 = O(n) as 3n + 3 = n + 6 = O(n) as 100n + 6 <= 101n for all n >= n 2 + 4n + 2 = O(n 2 ) as 10n 2 + 4n + 2 = 5. 3n + 2 is not O(1) as 3n + 2 is not less than or equal to c or any constant c for all n.

37 Review of Big O  O-notation is a means for describing an algorithm’s performance.  O-notation is used to express an upper bound on the value of f(n).  O-notation doesn’t say how good the bound is.

38 Review of Big O  Notice that n = O(n 2 ), n = O(n 3 ), n = O(2 n ) etc. In order for the statement f(n) = O(n) to be informative, g(n) should be the as small a function of n as one can come up. We shall never say 3n + n = O(n 2 ) even though it’s correct.

39 Review of Big O  f(n) = O(g(n)) is not the same as O(g(n)) = f(n).

40 Review of  3n + 2 = (n) as 3n + 2 >= 3n for all n >= 1. 3n + 3 =  (n) as 3n + 3 >= 3n for all n >= n + 6 = (n) as100n + 6 >= 100n for all n >= 1. 10n 2 + 4n + 2 =  (n 2 ) as 10n 2 + 4n + 2 >= n 2 for n>= 1. 3n + 2 is not O(1) as 3n + 2 is not less than or equal to c or any constant c for all n.

41 Review of  3n + 2 = (n) as 3n + 2 >= 3n for all n >= 1 and 3n + 2 = 2 so c 1 = 3 and c 2 = 4 and n 0 = 2 Note: The theta is more precise than both the big O and omega notations.

42 Review of notations Notice that the coefficients in all the g(n) used have been 1. This is accordance with practice. We shall never find ourselves saying that 3n + 3 = O(4n), or that 10 = O(100) even though each of these statements is true.