{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

05-ch5-divconq

# 05-ch5-divconq - CS 4102 Algorithms More Divide and Conquer...

This preview shows pages 1–9. Sign up to view the full content.

CS 4102, Algorithms: More Divide and Conquer • Read: Algorithms text, Chapter 5 • Examples: • Mergesort • Trominos Closest Pair of Points Strassen’s Matrix Multiplication Algorithm

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
New Problem: Sorting a Sequence The problem: Given a sequence a 0 … a n reorder them into a permutation a’ 0 … a’ n such that a’ i <= a’ i+1 for all pairs Specifically, this is sorting in non-descending order… Basic operation Comparison of keys. Why? Controls execution, so total operations often proportional Important for definition of a solution Often an expensive operation (say, large strings are keys) However, swapping items is often expensive We can apply same techniques to count swapping in a separate analysis
Why Do We Study Sorting? An important problem, often needed Often users want items in some order Required to make many other algorithms work well. Example: For searching on sorted data by comparing keys, optimal solutions require θ (log n) comparisons using binary search And, for the study of algorithms… A history of solutions Illustrates various design strategies and data structures Illustrates analysis methods Can prove something about optimality

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Mergesort is Classic Divide & Conquer Mergesort Strategy
Algorithm: Mergesort • Specification: Input: Array list and indexes first, and Last, such that the elements list[i] are defined for first <= i <= last. Output: list[first], …, list[last] is sorted rearrangement of the same elements • Algorithm: def mergesort(list, first, last): if first < last: mid = (first+last)/2 mergesort(list, first, mid) mergesort(list, mid+1, last) merge(list, first, mid, last) return

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Exercise: Trace Mergesort Execution Can you trace MergeSort() on this list? A = {8, 3, 2, 9, 7, 1, 5, 4};
Efficiency of Mergesort Cost to divide in half? No comparisons Two subproblems: each size n/2 Combining results? What is the cost of merging two lists of size n/2 Soon we’ll see it’s n-1 in the worst-case Recurrence relation: W(1) = 0 W(n) = 2 W(n/2)+ Wmerge(n) = 2 W(n/2) + n-1 You can now show that this W(n) θ (n log n)

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Merging Sorted Sequences • Problem: Given two sequences A and B sorted in non-
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 26

05-ch5-divconq - CS 4102 Algorithms More Divide and Conquer...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online