CIS 403503 Accelerated DataFile Structures - PowerPoint PPT Presentation

1 / 23
About This Presentation
Title:

CIS 403503 Accelerated DataFile Structures

Description:

CIS 403/503. Accelerated Data-File Structures. Lecture 23 ... 0-fact. 0!=1. Return 0!=1. Example: factorial. Iterative factorial: factorial (n) fac=1; ... – PowerPoint PPT presentation

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

less

Transcript and Presenter's Notes

Title: CIS 403503 Accelerated DataFile Structures


1
CIS 403/503Accelerated Data-File Structures
  • Lecture 23
  • Recursive Algorithms

2
Objectives
  • In this lecture, we will learn
  • Recursion
  • Divide and conquer algorithms
  • Mergesort
  • Quicksort

3
Recursion
4
Definition
  • An algorithm solves a problem by partitioning it
    into subproblems that are solved by using the
    same algorithm
  • Stopping condition(s)
  • Can be directly evaluated for some input
  • Recursive steps
  • The current value of a function can be computed
    by repeatedly calling of the function with input
    that will eventually arrive at a stop condition
  • Alternative to an iterative process

5
Example factorial
  • n! n (n-1) (n-2) 2 1
  • The recursive definition
  • factorial(n)
  • 1 n 0
  • nfactorial(n-1) ngt0

6
Example factorial
  • factorial (n)
  • if n is 0
  • return 1
  • else
  • return nfactorial(n-1)
  • end

7
Example factorial
8
Example factorial
  • Iterative factorial
  • factorial (n)
  • fac1
  • for i from 1 to n
  • fac i
  • end

9
Exercise
  • What is the recursive definition for the power an?

10
Discussion
  • Recursion often provides an elegant solution to a
    problem at the cost of efficiency
  • Sometimes recursion is not appropriate and its
    iterative version is more efficient
  • Use recursion when
  • Natural stopping condition and recursive steps
  • Iterative approach is more difficult to devise

11
Divide Conquer
12
Definition
  • Divide-and-conquer A problem solving technique
    using recursion
  • Divide a problem into a disjoint set of smaller
    subproblems that eventually become a stop
    condition
  • The solutions to the subproblems combine to
    conquer the original problem

13
Mergesort
  • Running time O(nlogn)
  • Taking a sequence of elements indexed first,
    last) the intermediate index is mid
  • Divide the list into two sorted sublist
  • Merge the two sorted sublist

14
Mergesort
first
last
mid
first
last
mid
first
last
mid
first
last
first1gtlast
15
Mergesort
  • mergeSort(v, first, last)
  • if first 1 is greater than last
  • int mid (lastfirst)/2
  • mergeSort(v, first, mid)
  • mergeSort(v, mid, last)
  • merge(v, first, mid, last)
  • end

16
Merge
16
9
8
34
25
43
17
12
6
16
9
8
34
25
17
12
43
6
9
16
8
25
34
12
17
43
6
17
Running Time
  • The number of steps to partition the entire list
    is O(logn)
  • Merge each of the O(logn) partition takes O(n)
  • O(nlogn)

18
Quicksort
  • In-place, O(nlogn) average case
  • Partition the list into smaller ones about a
    pivot value
  • All items less than the pivot is place to the
    left of the pivot all items larger are placed to
    the right of the pivot

19
Quicksort
first
last
Pivot
scanup
scandown
Pivot
scanup
scandown
20
Quicksort
  • quickSort(v, first, last)
  • int pivot
  • if there is only one element in v
  • return
  • else if there are two elements
  • compare and swap if needed
  • else
  • update pivot, scanup, scandown
  • quickSort(v, first, pivot)
  • quickSort(v, pivot1, last)
  • end

21
Quicksort
  • Running time
  • If the two sublists is roughly the size of n/2,
    running time is O(nlogn)
  • On average, the running time is O(nlogn)
  • Whats the running time if one sublist is
    consistantly of size 1?

22
Summary
  • Recursive approach provides elegant solutions,
    sometimes at the cost of efficiency
  • Use recursion to solve a problem when there are
    natural stopping conditions and recursive steps
    and there is no efficient iterative approaches
  • Mergesot is not in-place its running time is
    O(nlogn) in worst, average, best cases
  • Quicksort is in-place its average running time
    is O(nlogn) the worst case running time is O(n2)

23
Possible Quiz Questions
  • Understand how recursion works?
  • Mergesort how does it work? Running time?
    In-place?
  • Quicksort how does it work? Running time?
    In-place?
Write a Comment
User Comments (0)
About PowerShow.com