CIS 403503 Accelerated DataFile Structures - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

CIS 403503 Accelerated DataFile Structures

Description:

An algorithm contains a recursive call to itself ... Each item is accessed throught top() and pop() Possible Quiz Questions. Solve recurrences. ... – PowerPoint PPT presentation

Number of Views:52
Avg rating:3.0/5.0
Slides: 22
Provided by: yanz
Category:

less

Transcript and Presenter's Notes

Title: CIS 403503 Accelerated DataFile Structures


1
CIS 403/503Accelerated Data-File Structures
  • Lecture 12
  • Recurrence Stack

2
Objectives
  • In this lecture, we will learn Space
  • Solving recurrences
  • Master method
  • Stack STL
  • Runtime stack

3
Recurrence
  • An algorithm contains a recursive call to itself
  • A function in terms of its value on smaller input
  • T(n)
  • ?(1) if n 1
  • 2T(n/2) ?(n)

4
Recurrence
  • In general, T(n) aT(n/b) f(n)
  • a - of subproblems
  • n/b - size of a subproblem
  • f(n) - the cost of dividing the problem and
    combining the result of subproblems

5
Solve Recurrences
  • Method 1 substitution
  • Guess a solution
  • Verify by induction
  • Solve for constants
  • e.g., T(n) 4T(n/2) n

6
Solve Recurrences
  • Method 2 iteration or unrolling
  • T(n)
  • n 4T(n/2)
  • n4(n/24T(n/4))

7
Solve Recurrences
  • Method 3 recursion tree

8
Solve Recurrences
  • Method 4 Master Method
  • T(n) a T(n/b) f(n) assuming a ? 1 and b ? 1
  • Using recursion tree analysis, the total will be
  • ?(nlogba) ?i0,,logbn -1aif(n/bi)

9
Master Method
  • Case 1 if f(n)/nlogba O(1/n?) for some ? gt 0,
    then T(n) is ?(nlogba)
  • Case 2 if f(n)/nlogba ?(logkn) for some k ? 0,
    then T(n) ?(nlogbalogk1n)
  • Case 3 if f(n)/nlogba ?(n?) for some ? gt 0 and
    af(n/b) ? cf(n) for c lt 1, then T(n) ?(f(n))

10
Examples
  • T(n) 2T(n/2)n
  • T(n)4T(n/2)n
  • T(n)4T(n/2)n3
  • T(n)4T(n/2)n2/logn

11
Stacks
12
Overview
  • Stack
  • A data structure in which data are added and
    removed from only one end
  • LIFO

13
STL Stack Operations
  • Void push(const T item)
  • Insert item at the top
  • Postcondition new item at the top
  • void pop()
  • Remove the item from the top
  • Precondition stack is not empty
  • Postcondition stack has one less item
  • bool empty() const
  • Check if stack is empty
  • int size() const
  • Number of items on the stack
  • T top()
  • A reference to the top of the stack
  • Precondition stack is not empty
  • const T top() const
  • Constant reference

14
Example
  • includeltstackgt
  • includeltiostreamgt
  • using namespace std
  • int main()
  • stackltintgt s
  • int i
  • for (i1 ilt5 i)
  • s.push(i)
  • cout ltlt "Stack size " ltlt s.size() ltlt
    endl
  • cout ltlt "Popping the stack" ltlt endl
  • while(!s.empty())
  • cout ltlt s.top() ltlt " "
  • s.pop()
  • cout ltlt endl

15
Example
  • includeltstackgt
  • includeltiostreamgt
  • using namespace std
  • int main()
  • stackltintgt s
  • s.push(2)
  • s.push(3)
  • s.push(-1)
  • cout ltlt s.top() ltlt endl
  • //change top to 4
  • s.top() 5
  • while(!s.empty())
  • cout ltlt s.top() ltlt " "
  • s.pop()

16
Uncoupling Stack Elements
17
Uncouple
  • //remove the 1st occurrence of target
  • //true if target was removed, false otherwise
  • templateltclass Tgt
  • bool uncouple(stackltTgt s, const T target)
  • stackltTgt tmpStk
  • //assume target is on the stack
  • bool foundTarget true
  • //pop elements from s and push them to
    tmpStack
  • //till target is found or empty stack
  • while(!s.empty() s.top() ! target)
  • tmpStk.push(s.top())
  • s.pop()
  • //locates item if s is not empty
  • if(!s.empty())
  • s.pop()
  • else

18
Runtime Stack
  • Set up activation record
  • List of runtime arguments
  • Local variables and objects
  • Return address

19
Recursive Call
  • includeltiostreamgt
  • using namespace std
  • int fact(int)
  • int main()
  • int factValue
  • factValue fact(4)
  • cout ltlt "Value fact(4) " ltlt factValue
    ltlt endl
  • return 0
  • int fact(int n)
  • if ( n 0)
  • return 1
  • else
  • return n fact(n-1)

20
Summary
  • There four methods to solve recurrences
  • Substitution
  • Unrollng or iteration
  • Recursion trees
  • Master method
  • Stack is a LIFO data structure
  • In C STL, a stack is infinite
  • Each item is accessed throught top() and pop()

21
Possible Quiz Questions
  • Solve recurrences.
  • Stack oprations and application
Write a Comment
User Comments (0)
About PowerShow.com