CS171 Introduction to Computer Science II Recursion (cont.) + - - PowerPoint PPT Presentation

cs171 introduction to computer science ii recursion cont
SMART_READER_LITE
LIVE PREVIEW

CS171 Introduction to Computer Science II Recursion (cont.) + - - PowerPoint PPT Presentation

CS171 Introduction to Computer Science II Recursion (cont.) + MergeSort Recursion (cont.) + MergeSort Li Xiong 3/8/2012 1 Reminders Hw3 due yesterday (use late credit if needed) Hw4 due Friday 3/8/2012 2 Today Recursion (cont.)


slide-1
SLIDE 1

CS171 Introduction to Computer Science II Recursion (cont.) + MergeSort

3/8/2012 1

Recursion (cont.) + MergeSort

Li Xiong

slide-2
SLIDE 2

Reminders

Hw3 due yesterday (use late credit if needed) Hw4 due Friday

3/8/2012 2

slide-3
SLIDE 3

Today

Recursion (cont.)

Concept and examples Analyzing cost of recursive algorithms Divide and conquer Divide and conquer Dynamic programming

MergeSort

3/8/2012 3

slide-4
SLIDE 4

Fibonacci Numbers

Recursive formula:

= −+ − = =

0, 1, 1, 2, 3, 5, 8, 13, …..

slide-5
SLIDE 5

Fibonacci Numbers

  • !
slide-6
SLIDE 6

Runtime of Recursive Fibonacci

  • 3/8/2012

6

slide-7
SLIDE 7

Dynamic programming

Dynamically solve a smaller problem

Solve each small problem only once

Applicable when

Overlapping subproblems are slightly smaller (vs. Overlapping subproblems are slightly smaller (vs. divide and conquer) Optimal substructure: the solution to a given

  • ptimization problem can be obtained by the

combination of optimal solutions to its subproblems.

slide-8
SLIDE 8

Memoization

A technique for dynamic programming

A memoized function "remembers" the results corresponding to some set of specific inputs. Subsequent calls with remembered inputs return the remembered result, rather than recomputing it remembered result, rather than recomputing it

General structure

!" ! ! # $%#! &

slide-9
SLIDE 9

Fibonacci with Dynamic Programming

" '()**+

  • ,,("

,

  • Example: F(5)
  • &

,," ,

  • &

" ,)-)

  • &

&

slide-10
SLIDE 10

Today

Recursion (cont.)

Concept and examples Analyzing cost of recursive algorithms Divide and conquer Divide and conquer Dynamic programming

MergeSort

3/8/2012 10

slide-11
SLIDE 11

Advanced Sorting

We’ve learned some simple sorting methods, which all have quadratic costs.

Easy to implement but slow.

Much faster advanced sorting methods:

Merge Sort Quick Sort Radix Sort

slide-12
SLIDE 12

MergeSort

Basic idea

Divide array in half Sort each half (how?) Merge the two sorted halves Merge the two sorted halves

slide-13
SLIDE 13

Merge Sort

This is a divide and conquer approach:

Partition the original problem into two sub- problems; Use recursion to solve each sub-problem; Sub-problem eventually reduces to base case; The results are then combined to solve the original problem.

slide-14
SLIDE 14

Merge Two Sorted Arrays

A key step in mergesort Assume arrays A and B are already sorted. Merge them to array C (the original array), such as C contains all

elements from A and B, and remains sorted elements from A and B, and remains sorted

Use an auxiliary array aux[] Example on board and demo

slide-15
SLIDE 15

Merging Two Sorted Arrays

1. Start from the first elements of A and B; 2. Compare and copy the smaller element to C; 3. Increment indices, and continue; 3. Increment indices, and continue; 4. If reaching the end of either A or B, quit loop; 5. If either A (or B) contains remaining elements, append them to C.

slide-16
SLIDE 16
slide-17
SLIDE 17

Merging Two Sorted Arrays: Analysis

How many comparisons is required? How many copies?

slide-18
SLIDE 18

Merging Two Sorted Arrays (Sol.)

How many comparisons is required? at most (A.length + B.length) How many copies? A.length + B.length

slide-19
SLIDE 19
slide-20
SLIDE 20

Divide

slide-21
SLIDE 21

Divide

slide-22
SLIDE 22

Divide

slide-23
SLIDE 23

Conquer

slide-24
SLIDE 24

Conquer

slide-25
SLIDE 25

Conquer

slide-26
SLIDE 26
slide-27
SLIDE 27

First base case encountered

slide-28
SLIDE 28

Return, and continue

slide-29
SLIDE 29

Merge

slide-30
SLIDE 30

Merge

slide-31
SLIDE 31

Merge

slide-32
SLIDE 32
slide-33
SLIDE 33
slide-34
SLIDE 34

Merge Sort Analysis

Cost Analysis What’s the cost of mergesort? Recurrence relation: T(N) = 2*T(N/2) + N O(N*logN) This is called log-linear cost.

slide-35
SLIDE 35
slide-36
SLIDE 36

Merge Sort

Is this a lot better than simple sorting?

# of elements 10 100 N^2 100 10,000 N logN 10 200 100 1,000 10,000 … 10,000 1,000,000 100,000,000 … 200 3,000 40,000 …

slide-37
SLIDE 37
slide-38
SLIDE 38
slide-39
SLIDE 39
slide-40
SLIDE 40

Divide and Conquer

slide-41
SLIDE 41

1. 2. Every element itself is trivially sorted; Start by merging every two adjacent elements;

Bottom-up MergeSort

2. 3. 4. 5. 6. Start by merging every two adjacent elements; Then merge every four; Then merge every eight; … Done.

slide-42
SLIDE 42
slide-43
SLIDE 43
slide-44
SLIDE 44
slide-45
SLIDE 45

Summary

Merging two sorted array is a key step in merge sort. Merge sort uses a divide and conquer approach. It repeatedly splits an input array to two sub-arrays, sort each sub-array, and merge the two. It requires O(N*logN) time. It requires O(N*logN) time. On the downside, it requires additional memory space (the workspace array).