CS 23022 Discrete Mathematical Structures Mehdi Ghayoumi MSB rm 132 Ofc hr: Thur, 9:30-11:30a.

Slides:



Advertisements
Similar presentations
College of Information Technology & Design
Advertisements

MATH 224 – Discrete Mathematics
Recursion CS /02/05 L7: Files Slide 2 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Iteration.
Fall 2006CENG 7071 Algorithm Analysis. Fall 2006CENG 7072 Algorithmic Performance There are two aspects of algorithmic performance: Time Instructions.
Recursion. Idea: Some problems can be broken down into smaller versions of the same problem Example: n! 1*2*3*…*(n-1)*n n*factorial of (n-1)
Recursion Rosen 5 th ed., § Recursion Sometimes, defining an object explicitly might be difficult.Sometimes, defining an object explicitly might.
Chapter 10 Recursion Instructor: alkar/demirer. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.10-2 Recursive Function recursive functionThe.
Recursion CS 308 – Data Structures. What is recursion? smaller version Sometimes, the best way to solve a problem is by solving a smaller version of the.
Complexity Analysis (Part I)
Recursion. Idea: Some problems can be broken down into smaller versions of the same problem Example: n! 1*2*3*…*(n-1)*n n*factorial of (n-1)
CSE 326: Data Structures Lecture #3 Analysis of Recursive Algorithms Alon Halevy Fall Quarter 2000.
Complexity Analysis (Part I)
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (3) Recurrence Relation 11/11 ~ 11/14/2008 Yang Song.
Chapter 2: Fundamentals of the Analysis of Algorithm Efficiency
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.
Analysis of Algorithm.
Chapter 3: Recursive Algorithms
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
Analysis of Algorithms Spring 2015CS202 - Fundamentals of Computer Science II1.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
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.
1 Chapter 24 Developing Efficient Algorithms. 2 Executing Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
{ CS203 Lecture 7 John Hurley Cal State LA. 2 Execution Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
12-CRS-0106 REVISED 8 FEB 2013 KUG1C3 Dasar Algoritma dan Pemrograman.
Chapter 9: Recursion1 CHAPTER 9 RECURSION. Recursion  Concept of recursion  A recursive: Benefit and Cost  Comparison : Iterative and recursive functions.
Recursion CS 3358 – Data Structures. Big Picture Our objective is to write a recursive program and convince ourselves it is correct with the minimum amount.
Analysis of Algorithms
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 2: Recursion: The Mirrors Data Abstraction & Problem Solving.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
Chapter 10 A Algorithm Efficiency. © 2004 Pearson Addison-Wesley. All rights reserved 10 A-2 Determining the Efficiency of Algorithms Analysis of algorithms.
SEARCHING (Linear/Binary). Searching Algorithms  method of locating a specific item of information in a larger collection of data.  two popular search.
Searching. RHS – SOC 2 Searching A magic trick: –Let a person secretly choose a random number between 1 and 1000 –Announce that you can guess the number.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Recursive Solutions Recursion is an extremely powerful problem-solving.
Lecture 5 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
Data Structure Introduction.
Data Structure and Algorithms. Algorithms: efficiency and complexity Recursion Reading Algorithms.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part 1. Complexity Bounds.
CompSci 100E 15.1 What is a Binary Search?  Magic!  Has been used a the basis for “magical” tricks  Find telephone number ( without computer ) in seconds.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
1 Recursive algorithms Recursive solution: solve a smaller version of the problem and combine the smaller solutions. Example: to find the largest element.
Today’s Material Sorting: Definitions Basic Sorting Algorithms
BINARY SEARCH CS16: Introduction to Data Structures & Algorithms Thursday February 12,
Analysis of Algorithms Spring 2016CS202 - Fundamentals of Computer Science II1.
Lecture #3 Analysis of Recursive Algorithms
12-CRS-0106 REVISED 8 FEB 2013 KUG1C3 Dasar Algoritma dan Pemrograman.
chap10 Chapter 10 Recursion chap10 2 Recursive Function recursive function The recursive function is a kind of function that calls.
Recursion.
Algorithm Analysis 1.
Complexity Analysis (Part I)
Applied Discrete Mathematics Week 2: Functions and Sequences
Analysis of Algorithms
Chapter 10 Recursion Instructor: Yuksel / Demirer.
Analysis of Algorithms
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Recursion Data Structures.
Analysis of Algorithms
Searching, Sorting, and Asymptotic Complexity
And now for something completely different . . .
Divide-and-Conquer 7 2  9 4   2   4   7
Recurrences.
Main() { int fact; fact = Factorial(4); } main fact.
Recursion.
Recursion.
Discrete Mathematics CS 2610
Complexity Analysis (Part I)
Analysis of Algorithms
Complexity Analysis (Part I)
Presentation transcript:

CS Discrete Mathematical Structures Mehdi Ghayoumi MSB rm 132 Ofc hr: Thur, 9:30-11:30a

Announcements Homework 6 available. Due 07/10, 8a. Quiz time

Binary Search Example Looking for 42 FLM

Looking for 42 FLM Binary Search Example

found – in 3 comparisons F L M Binary Search Example

Looking for 89 FLM Binary Search Example

Looking for 89 FLM Binary Search Example

Looking for 89 FL M Binary Search Example

not found – 3 comparisons FL Binary Search Example

Algorithm Complexity How long does the “linear search algorithm” take? Suppose the data is (2, 8, 3, 9, 12, 1, 6, 4, 10, 7) and we’re looking for # 8124 The running time of the algorithm depends on the particular input to the problem. In this case we have two different complexity measures: Worst case complexity - running time on worst input Average case - average running time among all inputs Worst case for linear search is time n. Average case (1+2+…+n)/n = n(n+1)/2n = (n+1)/2. Both are O(n)

Algorithm Complexity How long does the “binary search algorithm” take? Binary search Input: a sorted array of numbers a 1, a 2, … a n and a number x Output: position i where a i = x, if x is in the list 1.i = 1, j = n; 2.while i < j 3. m =  (i + j)/2  ; /* midpt of range (i,j)*/ 4. if x > a m then i = m else j = m 6.if x = a i then output “x is in position a i ” 7.else output “x is not in list” The main point of showing you this code is to remind you that the range is cut in half at every iteration.

Algorithm Complexity How long does the “binary search algorithm” take? Binary search Input: a sorted array of numbers a 1, a 2, … a n and a number x Output: position i where a i = x, if x is in the list 1.i = 1, j = n; 2.while i < j 3. m =  (i + j)/2  ; /* midpt of range (i,j)*/ 4. if x > a m then i = m else j = m 6.if x = a i then output “x is in position a i ” 7.else output “x is not in list” If n is a power of 2, n = 2 k, then k = lg n iterations occur. If n is not a power of 2, let k be the number so that 2k < n < 2 k+1, and imagine that the array has 2 k+1 elements. Then k+1 < lg n + 1 = O(log n)

Recursive Function recursive function The recursive function is a kind of function that calls itself, or a function that is part of a cycle in the sequence of function calls. f1 f2fn …

What is recursion? smaller version Sometimes, the best way to solve a problem is by solving a smaller version of the exact same problem first smaller problem Recursion is a technique that solves a problem by solving a smaller problem of the same type

Recursive Function

How do I write a recursive function? Determine the size factor Determine the base case(s) (the one for which you know the answer) Determine the general case(s) (the one where the problem is expressed as a smaller version of itself) Verify the algorithm

10-17 An Example of Recursive Function We can implement the multiplication by addition. The simple case is “m*1=m.” The recursive step uses the following equation: “m*n = m+m*(n-1).”

Copyright ©2004 Pearson Addison- Wesley. All rights reserved Trace of Function multiply(6,3)

Problems defined recursively There are many problems whose solution can be defined recursively Example: n factorial 1if n = 0 n!= (recursive solution) (n-1)!*nif n > 0 1if n = 0 n!= (closed form solution) 1*2*3*…*(n-1)*nif n > 0

Coding the factorial function Recursive implementation int Factorial(int n) { if (n==0) return 1; else return n * Factorial(n-1); }

Coding the factorial function

Coding the factorial function (cont.) Iterative implementation int Factorial (int n) { int fact = 1; for(int count = 2; count <= n; count++) fact = fact * count; return fact; }

On to recursive algorithms… Factorial (n) Input: an integer n > 0. Output: n! 1.If n = 1 then output 1 2.else 3. output n x Factorial(n-1) Let T(n) denote the running time of the algorithm on input of size n. T(n) = C + T(n-1) T(1) = c T(n) = C + (C + T(n-2)) = C + (C + (C + T(n-3)))… = nC = O(n)

On to recursive algorithms… Now let’s analyze the running time. T(n) = 2 T(n-1) + C = 2 (2 T(n-2) + C) + C = 4 T(n-2) + 3C = 4 (2 T(n-3) + C) + 3C = 8 T(n-3) + 7C … = 2 k T(n-k) + (2 k -1)C T(1) = C = 2 n-1 T(1) + (2 n-1 -1)C = (2 n -1) C = O(2 n )

Recursion vs. iteration Iteration can be used in place of recursion An iterative algorithm uses a looping construct A recursive algorithm uses a branching structure Recursive solutions are often less efficient, in terms of both time and space, than iterative solutions Recursion can simplify the solution of a problem, often resulting in shorter, more easily understood source code