Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS210- Lecture 3 Jun 6, 2005 Announcements

Similar presentations


Presentation on theme: "CS210- Lecture 3 Jun 6, 2005 Announcements"— Presentation transcript:

1 CS210- Lecture 3 Jun 6, 2005 Announcements
Assignment 1 has been posted Due on 06/13 Submit hardcopy in class. 5/11/2019 CS210-Summer 2005, Lecture 3

2 Agenda Analysis of Algorithms Recursion Stacks 5/11/2019
CS210-Summer 2005, Lecture 3

3 More nested loops #operations 1. for (i=0; i<n; i++) n+1
2. for (j=0; j<i; j++) n(n+1)/2 sum++; n(n-1)/2 Total: n2 + n + 1 = O(n2) # operations for 2. # operations for 3. 5/11/2019 CS210-Summer 2005, Lecture 3

4 More nested loops # operations 1. for (i=0; i<n; i++) n+1
2. for (j=i; j<n; j++) ((n+1)(n+2)/2) -1 k++; n(n+1)/2 Total: O(n2) # operations for 2. # operations for 3. 5/11/2019 CS210-Summer 2005, Lecture 3

5 Big-Omega Notation What is Big-Omega for f(n) = 3nlogn + 2n?
Let g(n) = nlogn, c = 3 and n0 = 2. Then f(n) = Ω(g(n)) = Ω(nlogn) because f(n) ≥ 3 g(n) if n ≥ 2. i.e., 3nlogn + 2n ≥ 3nlogn if n ≥ 2 5/11/2019 CS210-Summer 2005, Lecture 3

6 Big Theta Notation What is Big-Theta for f(n) = 3nlogn + 4n + 5logn?
Let g(n) = nlogn, c1 = 3, c2= 12 and n0 = 2. Then f(n) = Θ(g(n)) = Θ(nlogn) because 3 g(n) ≤ f(n) ≤ 12 g(n) if n ≥ 2. i.e., 3nlogn ≤ 3nlogn+ 4n+ 5logn ≤ 12nlogn if n ≥ 2 5/11/2019 CS210-Summer 2005, Lecture 3

7 Summary: algorithms vs. Problems
Running time analysis establishes bounds for individual algorithms. Upper bound O(g(n)) for a problem: there is some O(g(n)) algorithms to solve the problem. Lower bound Ω(g(n)) for a problem: every algorithm to solve the problem is Ω(g(n)). 5/11/2019 CS210-Summer 2005, Lecture 3

8 Recursion You must have seen examples of methods calling other methods. Most modern programming languages allow a method to call itself. Many useful algorithms are recursive in structure i.e to solve a given problem, they call themselves recursively one or more times to deal with closely related sub problems. 5/11/2019 CS210-Summer 2005, Lecture 3

9 The Factorial Function
n! = if n= 0 = n . (n-1), (n-2)……1 if n ≥ 1 5! = = 120 = 5 . 4! factorial(n) = 1 if n = 0 = n . factorial(n-1) if n ≥ 1 5/11/2019 CS210-Summer 2005, Lecture 3

10 Recursion The important thing to remember when creating a recursive function is to give an 'end-condition'. We don't want the function to keep calling itself forever. Somehow, it should know when to stop. This is called base case 5/11/2019 CS210-Summer 2005, Lecture 3

11 Factorial public int factorial(int n) { if (n == 0) return 1; else
return n * factorial(n-1); } 5/11/2019 CS210-Summer 2005, Lecture 3

12 Factorial return 4 * 6 = 24 Final Result call factorial(4)
5/11/2019 CS210-Summer 2005, Lecture 3

13 Recursion We also consider a method M to be recursive if it calls another method that ultimately leads to a call back to M. The main benefit of this approach to algorithm design is that it allows us to take advantage of the repetitive structure present in many problems. 5/11/2019 CS210-Summer 2005, Lecture 3

14 Linear Recursion The simplest form of recursion is Linear Recursion, where a method is defined so that it makes at most one recursive call each time it is invoked. Summing elements of Array Recursively Algorithm LinearSum(A,n) if n = 1 then return A[0] else return LinearSum(A,n-1) + A[n-1] 5/11/2019 CS210-Summer 2005, Lecture 3

15 Recursive Trace return 15 + A[4] = 15 + 5 = 20 Final Result call
LinearSuml(A,5) return A[3] = = 15 call LinearSuml(A,4) return 7 + A[2] = 7+ 6 = 13 call LinearSuml(A,3) return 4 + A[1] = = 7 call LinearSuml(A,2) return A[0] = 4 call LinearSuml(A,1) 5/11/2019 CS210-Summer 2005, Lecture 3

16 Reversing an Array by Recursion
Algorithm ReverseArray(A, i, j) Input: An array A and nonnegative integer indices I and j. Output: The reversal of the elements in A starting at index i and ending at j. if i < j then Swap A[i] and A[j] ReverseArray(A, i+1, j-1) return 5/11/2019 CS210-Summer 2005, Lecture 3

17 Recursion Use memory locations in our computer to keep track of the state of each active recursive call. When computer memory is at a premium, then it is useful in some cases to be able to derive nonrecursive algorithms from recursive ones. We can do this very easily for algorithms that use tail recursion. 5/11/2019 CS210-Summer 2005, Lecture 3

18 Tail Recursion An algorithm uses tail recursion if it uses linear recursion and the algorithm makes a recursive call as its very last operation. For example: ReverseArray Does LinearSum use tail recursion? When an algorithm uses tail recursion, we can convert recursive algorithm into a non recursive one, by iterating through the recursive calls rather than calculating them explicitly. 5/11/2019 CS210-Summer 2005, Lecture 3

19 Iterative Reverse Array
Algorithm IterativeReverseArray(A, i, j) Input: An array A and nonnegative integer indices I and j. Output: The reversal of the elements in A starting at index i and ending at j. while i < j Swap A[i] and A[j] i <- i+1 j <- j-1 return 5/11/2019 CS210-Summer 2005, Lecture 3

20 Binary Recursion When an algorithm makes two recursive calls we say that it uses binary recursion. Fibonacci numbers are recursively defined as follows: F0 = 0 F1 = 1; Fi = Fi-1 + Fi-2 for i > 1 5/11/2019 CS210-Summer 2005, Lecture 3

21 Fibonacci Numbers Algorithm Fib(k) Input: Nonnegative integer k.
Output: The kth Fibonacci number Fk. if k ≤ 1 then return k else return Fib(k – 1) + Fib(k -2) 5/11/2019 CS210-Summer 2005, Lecture 3

22 Recursive Trace 5 5 3 4 3 2 3 2 2 1 2 1 1 2 1 1 1 1 1 5/11/2019 CS210-Summer 2005, Lecture 3

23 Stacks A Stack is a container of objects that are inserted and removed according to the last-in-first-out (LIFO) principle. Objects can be inserted (push) into a stack at any time, but only the most recently inserted objects can be removed (pop) at any time. 5/11/2019 CS210-Summer 2005, Lecture 3

24 Stacks 1. Push(2) Push(10) 2 Pop() 10 2. 2 3. 2 4. 5/11/2019
CS210-Summer 2005, Lecture 3

25 ADT (Abstract Data Type)
An Abstract data type is a type in the same sense as int and double, that is there can be variables of the type, and there are operations available for the type that the user (client) don’t see implemented, just trusts to do the job. An ADT has an API that defines the operations allowed on the type, and the implementation of the operations keeps all of its data encapsulated from the client. 5/11/2019 CS210-Summer 2005, Lecture 3

26 Stack ADT Stack ADT supports following methods:
push(o): Insert object o at the top of stack Input: Object o Output: None pop(): Remove and return the top object on the stack. Error occurs if the stack is empty. Input: None Output: Top object 5/11/2019 CS210-Summer 2005, Lecture 3

27 Stack ADT size(): Return the number of objects in the stack
Input: none Output: Integer (total number of objects) isEmpty(): Return a boolean indicating if the stack is empty. Input: None Output: boolean (true if stack is empty, false otherwise) top(): Return the top object on the stack, without removing it. Error occurs if the stack is empty. Input: None Output: Top object 5/11/2019 CS210-Summer 2005, Lecture 3

28 Stack ADT in Java Stack data structure is included as a “built-in” class in the java.util package of java. Methods: push (Object item) pop() peek() //equivalent of top empty() //equivalent of isEmpty size() 5/11/2019 CS210-Summer 2005, Lecture 3

29 A Simple Array Based Implementation
We can implement a stack by storing its elements in an array. The stack in this implementation consists of an N element array S plus an integer variable t that gives the index of the top element in array S. S 1 2 t N -1 ……………………………… 5/11/2019 CS210-Summer 2005, Lecture 3

30 A Simple Array Based Implementation
We initialize t to -1 (which means stack is empty initially). size: No of elements in stack: t + 1. ( = 0 elements initially) isEmpty: if t < 0 then true otherwise false. To push object: If size is N (full stack) throw Exception Otherwise increment t and store new object at S[t] 5/11/2019 CS210-Summer 2005, Lecture 3

31 A Simple Array Based Implementation
To pop: If isEmpty() is true then throw Exception Otherwise store S[t] in a local variable, assign null to S[t], decrement top and return local variable having the previous top. 5/11/2019 CS210-Summer 2005, Lecture 3


Download ppt "CS210- Lecture 3 Jun 6, 2005 Announcements"

Similar presentations


Ads by Google