Discrete Mathematics Algorithms. Introduction  An algorithm is a finite set of instructions with the following characteristics:  Precision: steps are.

Slides:



Advertisements
Similar presentations
College of Information Technology & Design
Advertisements

College of Information Technology & Design
MATH 224 – Discrete Mathematics
Analysis of Algorithms
HST 952 Computing for Biomedical Scientists Lecture 10.
Fundamentals of Python: From First Programs Through Data Structures
CompSci 102 Discrete Math for Computer Science
Algorithmic Complexity Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Fall 2006CENG 7071 Algorithm Analysis. Fall 2006CENG 7072 Algorithmic Performance There are two aspects of algorithmic performance: Time Instructions.
Complexity Analysis (Part I)
Analysis of Algorithms. Time and space To analyze an algorithm means: –developing a formula for predicting how fast an algorithm is, based on the size.
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.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Analysis of Algorithms 7/2/2015CS202 - Fundamentals of Computer Science II1.
1 Section 2.3 Complexity of Algorithms. 2 Computational Complexity Measure of algorithm efficiency in terms of: –Time: how long it takes computer to solve.
Chapter 1 Introduction Definition of Algorithm An algorithm is a finite sequence of precise instructions for performing a computation or for solving.
Analysis of Algorithms Spring 2015CS202 - Fundamentals of Computer Science II1.
February 17, 2015Applied Discrete Mathematics Week 3: Algorithms 1 Double Summations Table 2 in 4 th Edition: Section th Edition: Section th.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Time Complexity Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
Introduction to complexity Prof. Sin-Min Lee Department of Computer Science San Jose State University.
Chapter Complexity of Algorithms –Time Complexity –Understanding the complexity of Algorithms 1.
Analysis of Algorithm Lecture 3 Recurrence, control structure and few examples (Part 1) Huma Ayub (Assistant Professor) Department of Software Engineering.
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.
Week 2 CS 361: Advanced Data Structures and Algorithms
1 R. Johnsonbaugh, Discrete Mathematics Chapter 4 Algorithms.
1 Chapter 24 Developing Efficient Algorithms. 2 Executing Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
Discrete Structures Lecture 11: Algorithms Miss, Yanyan,Ji United International College Thanks to Professor Michael Hvidsten.
1 Recursion Algorithm Analysis Standard Algorithms Chapter 7.
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.
1 Computer Algorithms Lecture 3 Asymptotic Notation Some of these slides are courtesy of D. Plaisted, UNC and M. Nicolescu, UNR.
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.
Design and Analysis of Algorithms - Chapter 21 Analysis of Algorithms b Issues: CorrectnessCorrectness Time efficiencyTime efficiency Space efficiencySpace.
Analysis of Algorithms
Chapter 3 Sec 3.3 With Question/Answer Animations 1.
Analysis of Algorithms These slides are a modified version of the slides used by Prof. Eltabakh in his offering of CS2223 in D term 2013.
Fall 2002CMSC Discrete Structures1 Enough Mathematical Appetizers! Let us look at something more interesting: Algorithms.
MCA-2012Data Structure1 Algorithms Rizwan Rehman CCS, DU.
Data Structure Introduction.
3.3 Complexity of Algorithms
Lecture 10 – Algorithm Analysis.  Next number is sum of previous two numbers  1, 1, 2, 3, 5, 8, 13, 21 …  Mathematical definition 2COMPSCI Computer.
Algorithm Analysis Part of slides are borrowed from UST.
ALGORITHMS.
1/6/20161 CS 3343: Analysis of Algorithms Lecture 2: Asymptotic Notations.
27-Jan-16 Analysis of Algorithms. 2 Time and space To analyze an algorithm means: developing a formula for predicting how fast an algorithm is, based.
تصميم وتحليل الخوارزميات عال311 Chapter 3 Growth of Functions
R. Johnsonbaugh, Discrete Mathematics 5 th edition, 2001 Chapter 3 Algorithms.
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.
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.
GC 211:Data Structures Week 2: Algorithm Analysis Tools Slides are borrowed from Mr. Mohammad Alqahtani.
1 Algorithms Searching and Sorting Algorithm Efficiency.
Discrete Mathematics Chapter 2 The Fundamentals : Algorithms, the Integers, and Matrices. 大葉大學 資訊工程系 黃鈴玲.
Algorithm Analysis 1.
Applied Discrete Mathematics Week 2: Functions and Sequences
Analysis of Algorithms
Algorithms and Complexity
Introduction to Algorithms
Algorithms and Complexity
Enough Mathematical Appetizers!
Computation.
CS 2210 Discrete Structures Algorithms and Complexity
CS 2210 Discrete Structures Algorithms and Complexity
Applied Discrete Mathematics Week 6: Computation
Enough Mathematical Appetizers!
Enough Mathematical Appetizers!
Discrete Mathematics 7th edition, 2009
CS 2210 Discrete Structures Algorithms and Complexity
Presentation transcript:

Discrete Mathematics Algorithms

Introduction  An algorithm is a finite set of instructions with the following characteristics:  Precision: steps are precisely stated  Uniqueness: Results of each step of execution are uniquely defined. They depend only on inputs and results of preceding steps  Finiteness: the algorithm stops after a finite (maybe large) number of steps for any input in the set.

More characteristics of algorithms  Input: the algorithm receives input from a specified set  Output: The algorithm produces output. From each set of input, a set of output is produced. Outputs are solution to problem.  Generality: the algorithm applies to various sets of inputs  Effectiveness : It must be possible to perform each step of an algo exactly and in a finite amount of time.

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

Notation for algorithms PSEUDOCODE: Instructions given in a generic language similar to a computer language such as C++ or Pascal. (no fuss over syntax) Procedure If-then, action If-then-else begin Else Return While loop For loop End

Problem Solving  Searching  Sorting

Tracing Output A=1 B=1 Repeat until B  10 B=2A-2 A=A+3

Example Function F(X) F(3)? If X > 0 then R=X Else If X < 3 R=2X+6 Else R=X+7 Return (R)

Example A=1 B=0 While (A  10) X=X+1 A=A+1

Example (Sorting) Let assume there are two integer numbers: 3,1 Aim: Sort these numbers based on ascending order Location[1] =3, Location[2]=1 If Location[1] is greater than Location[2] then Begin temp=Location[1] Location[1]=Location[2] Location[2]=temp End

 Linear Search Begin i = 1 While ((i A[i])) i = i + 1 if i <= n then location = i else location = - 1; (where x is the item we are searching for; A is the array of where the item should be searched and location is a variable that will hold the position of where the item is, if it is found and -1 is the item is not found) Example (Searching)

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

factorial(n:positive integer) if n=1 then factorial(n)=1 else factorial(n)=n*factorial(n-1)

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,…

Complexity of algorithms  Analysis : refers to the process of deriving estimates for the time and space needed to execute the algorithm.  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.

Types of complexity Time needed to execute an algorithm is a function of the input. It is difficult to obtain an explicit formula for this function but we can try to determine :  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

Big-O Notation  Big-O has been used in mathematics for century, and in CS it is used in the analysis of algorithms  Popularized in CS by Donald Knuth.

Notational Issues Big-O notation is a way of comparing functions. Notation unconventional: EG: 3x 3 + 5x 2 – 9 = O (x 3 ) Doesn’t mean “3x 3 + 5x 2 – 9 equals the function O (x 3 )” Which actually means “3x 3 +5x 2 –9 is dominated by x 3 ” Read as: “3x 3 +5x 2 –9 is big-Oh of x 3 ”

Intuitive Notion of Big-O Asymptotic notation captures behavior of functions for large values of x. EG: Dominant term of 3x 3 +5x 2 –9 is x 3. As x becomes larger and larger, other terms become insignificant and only x 3 remains in the picture:

Intuitive Notion of Big-O domain – [0,2] y = 3x 3 +5x 2 –9 y = x 3 y = x y = x 2

Intuitive Notion of Big-O domain – [0,5] y = 3x 3 +5x 2 –9 y = x 3 y = x y = x 2

Intuitive Notion of Big-O domain – [0,10] y = 3x 3 +5x 2 –9 y = x 3 y = x y = x 2

Big-O. Formal Definition f (x ) is asymptotically dominated by g (x ) if there’s a constant multiple of g (x ) bigger than f (x ) as x goes to infinity: DEF: Let f, g be functions with the set of R 0 or N to set of R. If there are constants C and k such  x > k, |f (x )|  C  |g (x )| then we write: f (x ) = O ( g (x ) )  (Big- O is actually an estimate of average running time of an algorithm.)

Intuitive Notion of Big-O domain – [0,100] y = 3x 3 +5x 2 –9 y = x 3 y = x y = x 2

Common Terminology of Complexity ComplexityTerminology O(1)Constant O(log n)Logarithmic O(n)Linear O(n log n)n log n O(n b )Polynomial O(b n ), where b > 1Exponential O(n!)Factorial

Example: To find BigO of Linear search algo  Describe the average-case performance of linear search algo, assuming that the element x is in the list. Solution : There are n types of possible inputs when x is known to be in the list. If x is the first term of the list, 3 comparison is needed (one to determine whether the end of the list has been reached, one to compare x and the first term and one outside the loop). If x is the second term then 2 more comparisons is needed, which total up to 5 comparisons and so on, where if x is at the i th term a total of 2i + 1 comparisons are needed.

 Hence the average number of camparisons used : …+ (2n + 1) = 2( n)+ n n n …+ n = n(n+1) 2 therefore : 2[n(n+1) /2] + 1 = n + 2 n which is O(n).