CS210- Lecture 3 Jun 6, 2005 Announcements

Slides:



Advertisements
Similar presentations
© 2004 Goodrich, Tamassia Using Recursion1 Programming with Recursion.
Advertisements

Stacks, Queues, and Deques
Stacks. What is a stack? Last-in first-out data structure (LIFO) New objects are placed on top Removal restricted to top object Examples?
© 2004 Goodrich, Tamassia Using Recursion1. © 2004 Goodrich, Tamassia Using Recursion2 Recall the Recursion Pattern (§ 2.5) Recursion: when a method calls.
Stacks.
Part-B1 Stacks. Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations.
Part-B1 Stacks. Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations.
Stacks. What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
© 2004 Goodrich, Tamassia Using Recursion1. © 2004 Goodrich, Tamassia Using Recursion2 Recall the Recursion Pattern (§ 2.5) Recursion: when a method calls.
Programming with Recursion
Fall 2006CSC311: Data Structures1 Chapter 3 Arrays, Linked Lists, and Recursion Objectives –Using Arrays –Singly Linked Lists –Doubly Linked Lists –Circularly.
Recursion.
Topic 3 The Stack ADT.
Introduction to Recursion by Dr. Bun Yue Professor of Computer Science 2013
Using Recursion1 Recursion © 2010 Goodrich, Tamassia.
1 Object-Oriented Programming Using C++ CLASS 2. 2 Linear Recursion Summing the Elements of an Array Recursively Algorithm LinearSum(A, n): Input: An.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: Recursion Problem Solving, Abstraction, and Design using C++
Introduction to Recursion CSCI 3333 Data Structures.
Stack. Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations on the data.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
1 Chapter 3. Recursion Lecture 6. In functions and data structures.
30 May Stacks (5.1) CSE 2011 Winter Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 3.
Algorithm Analysis CS 400/600 – Data Structures. Algorithm Analysis2 Abstract Data Types Abstract Data Type (ADT): a definition for a data type solely.
Chapter 2 Computational Complexity. Computational Complexity Compares growth of two functions Independent of constant multipliers and lower-order effects.
23 February Recursion and Logarithms CSE 2011 Winter 2011.
1 Chapter 2 Algorithm Analysis All sections. 2 Complexity Analysis Measures efficiency (time and memory) of algorithms and programs –Can be used for the.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
1 Chapter 2 Algorithm Analysis Reading: Chapter 2.
Given a node v of a doubly linked list, we can easily insert a new node z immediately after v. Specifically, let w the be node following v. We execute.
Click to edit Master text styles Stacks Data Structure.
1 Stacks Abstract Data Types (ADTs) Stacks Application to the analysis of a time series Java implementation of a stack Interfaces and exceptions.
Data Structures I (CPCS-204) Week # 5: Recursion Dr. Omar Batarfi Dr. Yahya Dahab Dr. Imtiaz Khan.
CSC 143 P 1 CSC 143 Recursion [Chapter 5]. CSC 143 P 2 Recursion  A recursive definition is one which is defined in terms of itself  Example:  Compound.
Chapter 2 Algorithm Analysis
Chapter Topics Chapter 16 discusses the following main topics:
Stacks II David Lillis School of Computer Science and Informatics
Recursion 5/4/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H.
Chapter 15 Recursion.
Recursion 5/22/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M.
Week 4 - Friday CS221.
CSCI 3333 Data Structures Stacks.
Chapter 15 Recursion.
Recursion and Logarithms
Week 3 - Friday CS221.
Stacks.
Queues Queues Queues.
The Stack ADT. 3-2 Objectives Define a stack collection Use a stack to solve a problem Examine an array implementation of a stack.
Introduction to C++ Recursion
CS 3343: Analysis of Algorithms
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Stacks Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Algorithm design and Analysis
Data Structures and Algorithms
Stacks.
Recursion © 2013 Goodrich, Tamassia, Goldwasser Recursion Sequences
Recursion Data Structures.
CS200: Algorithms Analysis
Stacks Abstract Data Types (ADTs) Stacks
Cs212: Data Structures Computer Science Department Lecture 6: Stacks.
Programming with Recursion
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Programming with Recursion
Chapter 5 Stack (part 1).
CS210- Lecture 2 Jun 2, 2005 Announcements Questions
Sequences 6/1/2019 7:49 PM Using Recursion Using Recursion.
Recursion © 2013 Goodrich, Tamassia, Goldwasser Recursion Sequences
A type is a collection of values
Advanced Analysis of Algorithms
Presentation transcript:

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

Agenda Analysis of Algorithms Recursion Stacks 5/11/2019 CS210-Summer 2005, Lecture 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 3. 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

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 3. k++; n(n+1)/2 Total: O(n2) # operations for 2. # operations for 3. 5/11/2019 CS210-Summer 2005, Lecture 3

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

A Simple Array Based Implementation We initialize t to -1 (which means stack is empty initially). size: No of elements in stack: t + 1. (-1 + 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

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