# Divide-and-Conquer Algorithms Part One

Save this PDF as:

Size: px
Start display at page:

## Transcription

1 Divide-and-Conquer Algorithms Part One

2 Announcements Problem Set One completely due right now. Solutions distributed at the end of lecture. Programming section today in Gates B08 from from 3:45PM 5:00PM. Resumes at normal Thursday schedule (4:15PM 5:05PM) next week.

3 Where We've Been We have just finished discussing fundamental algorithms on graphs. These algorithms are indispensable and show up everywhere. You can now solve a large class of problems by recognizing that they reduce to a problem you already know how to solve.

4 Where We're Going We are about to explore the divide-and-conquer paradigm, which gives a useful framework for thinking about problems. We will explore several major techniques: Solving problems recursively. Intuitively understanding how the structure of recursive algorithms influences runtime. Recognizing when a problem can be solved by reducing it to a simpler case.

5 Outline for Today Recurrence Relations Representing an algorithm's runtime in terms of a simple recurrence. Solving Recurrences Determining the runtime of a recursive function from a recurrence relation. Sampler of Divide-and-Conquer A few illustrative problems.

6 Insertion Sort As we saw in Lecture 00, insertion sort can be used to sort an array in time Ω(n) and O(n 2 ). It's Θ(n2 ) in the average case. Can we do better?

7 A Better Sorting Algorithm: Mergesort

8 Thinking About O(n 2 ) T(n) T(½n) ¼T(n) T(½n) ¼T(n)

9 procedure merge(list A, A, list list B): B): let let result be be an an empty empty list. list. while while both both A and and B are are nonempty: if if head(a) < head(b): append head(a) to to result remove head(a) from from A else: else: append head(b) to to result remove head(b) from from B append append all all elements remaining in in A to to result result append all all elements remaining in in B to to result return result Complexity: Θ(m + n), where m and n are the lengths of the input lists.

10 Motivating Mergesort Splitting the input array in half, sorting each half, and merging them back together will take roughly half as long as soring the original array. So why not split the array into fourths? Or eighths? Question: What happens if we never stop splitting?

11 High-Level Idea A recursive sorting algorithm! Base Case: An empty or single-element list is already sorted. Recursive step: Break the list in half and recursively sort each part. Merge the sorted halves back together. This algorithm is called mergesort.

12 procedure mergesort(list A): A): if if length(a) 1: 1: return A let let left left be be the the first first half half of of the the elements of of A let let right right be be the the second half half of of the the elements of of A return merge(mergesort(left), mergesort(right)) T(0) = Θ(1) T(1) = Θ(1) T(n) = T( n / 2 ) + T( n / 2 ) + Θ(n)

13 Recurrence Relations A recurrence relation is a function or sequence whose values are defined in terms of earlier values. In our case, we get this recurrence for the runtime of mergesort: T(0) T(0) = Θ(1) T(1) T(1) = Θ(1) T(n) T(n) = T( n T( n // 2 ) 2 ) + T( n T( n // 2 ) 2 ) + Θ(n) Θ(n) We can solve a recurrence by finding an explicit expression for its terms, or by finding an asymptotic bound on its growth rate. How do we solve this recurrence?

14 Simplifying our Recurrence It is often difficult to solve recurrences involving floors and ceilings, as ours does. T(1) T(1) = Θ(1) T(n) T(n) = T(n T(n // 2) 2) + T(n T(n // 2) 2) + Θ(n) Note that if we only consider n = 1, 2, 4, 8, 16,, then the floors and ceilings are always equivalent to standard division. Simplifying Assumption 1: We will only consider the recurrence as applied to powers of two. We need to justify why this is safe, which we'll do later.

15 Simplifying our Recurrence Without knowing the actual functions hidden by the Θ notation, we cannot get an exact value for the terms in this recurrence. T(1) T(1) = c₁ c₁ T(n) T(n) = 2T(n // 2) 2) + c₂n c₂n If the Θ(1) just hides a constant and Θ(n) just hides a multiple of n, this would be a lot easier to manipulate! Simplifying Assumption 2: We will pretend that Θ(1) hides some constant and Θ(n) hides a multiple of n. We need to justify why this is safe, which we'll do later.

16 Simplifying our Recurrence Working with two constants c₁ and c₂ is most accurate, but it makes the math a lot harder. T(1) T(1) c T(n) T(n) 2T(n // 2) 2) + cn cn If all we care about is getting an asymptotic bound, these constants are unlikely to make a noticeable difference. Simplifying Assumption 3: Set c = max{c₁, c₂} and replace the equality with an upper bound. This is less exact, but is easier to manipulate.

17 The Final Recurrence Here is the final version of the recurrence we'll be working with: T(1) c T(n) 2T(n // 2) 2) + cn cn As before, we will justify why all of these simplifications are safe later on. The analysis we're about to do (without justifying the simplifications) is at the level we will expect for most of our discussion of divide-and-conquer algorithms.

18 Getting an Intuition Simple recurrence relations often give rise to surprising results. It is often useful to build up an intuition for what the recursion solves to before trying to formally prove it. We will explore two methods for doing this: The iteration method. The recursion-tree method.

19 T(n) 2T ( n 2 ) +c n 2 ( 2T ( n 4 ) +c n 2 ) +c n T(1) T(1) c T(n) T(n) 2T(n // 2) 2) + cn cn n // 2 k k = 1 n = 2 k k log log 2 n 2 = k = 4T ( n ) +c n+c n 4 = 4T ( n 4 ) +2c n 4( 2T ( n 8 ) +c n 4 ) +2c n = 8T ( n ) +c n+2 c n 8 = 8T ( n 8 ) +3 c n... 2 k T (n 2 k ) +k c n

20 T(1) T(1) c T(n) T(n) 2T(n // 2) 2) + cn cn T(n) 2 k T (n 2 k ) +k c n = 2 log 2 n T(1)+c n log 2 n = nt(1)+c nlog 2 n c n+c nlog 2 n = O(nlog n)

21 The Iteration Method What we just saw is an example of the iteration method. Keep plugging the recurrence into itself until you spot a pattern, then try to simplify. Doesn't always give an exact answer, but useful for building up an intuition.

22 T(1) T(1) c T(n) T(n) 2T(n // 2) 2) + cn cn cn cn cn / 2 cn / 2 cn cn / 4 cn / 4 cn / 4 cn / 4 cn c c c c c c c c cn cn log₂ n + cn

23 The Recursion Tree Method This diagram is called a recursion tree and accounts for how much total work each recursive call makes. Often useful to sum up the work across the layers of the tree.

24 A Formal Proof Both the iteration and recursion tree methods suggest that the runtime is at most cn log₂ n + cn Neither of these lines of reasoning are perfectly rigorous; how could we formalize this? Induction!

25 Theorem: If n is a power of 2, T(n) cn log₂ n + cn Proof: By induction. As a base case, if n = 2 0 = 1, then T(n) = T(1) c = cn log₂ n + cn. For the inductive step, assume the claim holds for all n' < n that are powers of two. Then T(n) 2T(n / 2) + cn = 2((cn / 2) log₂ (n / 2) + cn / 2) + cn = cn log₂ (n / 2) + cn + cn = cn (log₂ n 1) + cn + cn = cn log₂ n cn + cn + cn = cn log₂ n + cn

26 What This Means We have shown that as long as we only look at powers of two, the runtime for mergesort is bounded from above by cn log₂ n + cn. In most cases, it's perfectly safe to stop here and claim we have a working bound. Mergesort is indeed O(n log n). For completeness, let's take some time to see why it is safe to stop here. In the future, we won't go into this level of detail.

27 Replacing Θ Our original recurrence was T(0) T(0) = Θ(1) T(1) T(1) = Θ(1) T(n) T(n) T( n T( n // 2 ) 2 ) + T( n T( n // 2 ) 2 ) + Θ(n) We claimed it was safe to remove the Θ notation and rewrite it as T(0) T(0) c T(1) T(1) c T(n) T(n) T( n T( n // 2 ) 2 ) + T( n T( n // 2 ) 2 ) + cn cn Why can we do this?

28 Fat Base Cases When n n₀, we can replace Θ(n) by cn for some constant c. Our simplification in the previous step assumed that n₀ = 0. What if this isn't the case? Can always rewrite the recurrence to use a fat base case: T(n) T(n) T( n T( n // 2 )+T( n // 2 ) 2 ) + cn cn (if (if n n₀) n₀) T(n) T(n) c (otherwise) Makes the induction a lot harder to do, but the result would come out the same.

29 Non Powers of Two Consider this recurrence: T(0) T(0) c T(1) T(1) c T(n) T(n) T( n T( n // 2 ) 2 ) + T( n T( n // 2 ) 2 ) + cn cn We know that for powers of two, this is upper bounded by cn log₂ n + cn. Does that upper bound still hold for values other than powers of two? If not, is our bound even useful?

30 Non Powers of Two Can we claim that since T(n) cn log₂ n + cn when n is a power of two, that T(n) = O(n log n)? Without more work, no. Consider this function: f(n) = n log₂ n n! if n = 2 k otherwise Only looking at inputs that are powers of two, we might claim that f(n) = Θ(n log n), even though this isn't the case! We need to do extra work to show that T(n) is well-behaved enough to extrapolate.

31 b(n) a(n) b(n) = Θ(a(n))

32 Our Proof Strategy We will proceed as follows: Show that the values generated by the recurrence are nondecreasing. For each non power-of-two n, provide an upper bound T(n) using our upper bound on the next power of two greater than n. Show that the upper bound we find this way is asymptotically equivalent (in terms of Θ) to our original bound.

33 Making Things Easier We are given this recurrence: T(0) T(0) c T(1) T(1) c T(n) T(n) T( n T( n // 2 ) 2 ) + T( n T( n // 2 ) 2 ) + cn cn This only gives an upper bound on T(n); we don't know the exact values. Let's define a new function f(n) as follows: f(0) f(0) = c f(1) f(1) = c f(n) f(n) = f( n f( n // 2 ) 2 ) + f( n f( n // 2 ) 2 ) + cn cn Note that T(n) f(n) for all n N.

34 f(0) f(0) = c f(1) f(1) = c f(n) f(n) = f( n f( n // 2 ) 2 ) + f( n f( n // 2 ) 2 ) + cn cn Lemma: f(n + 1) f(n) for all n N. Proof: By induction on n. As a base case, note that f(1) = c c = f(0) For the inductive step, assume that for some n that the lemma holds for all n' < n. Then f(n + 1) = f( (n+1) / 2 ) + f( (n+1) / 2 ) + c(n+1) f( n / 2 ) + f( n / 2 ) + cn = f(n)

35 Theorem: T(n) = O(n log n) Proof: Consider any n N with n 1. Let k be such that 2 k n < 2 k+1. Thus 2 k+1 2n < 2 k+2. From our lemma, we know that T(n) f(n) f(2 k+1 ) Using our upper bound for powers of two: Therefore f(2 k+1 ) c(2 k+1 ) log₂ (2 k+1 ) + c(2 k+1 ) T(n) c(2 k+1 ) log₂ (2 k+1 ) + c(2 k+1 ) c(2n) log₂ (2n) + 2cn = 2cn (log₂ n + 1) + 2cn = 2cn log₂ n + 4cn So for any n 1, T(n) 2cn log₂ n + 4cn. Thus T(n) = O(n log n).

36 Summary We can safely extrapolate from the runtime bounds at powers of two for the following reasons: The runtime is nondecreasing, so we can use powers of two to provide upper bounds on other points. The runtime grows only polynomially, so this upper bounding strategy does not produce values that are too much bigger than the actual values. In the future, we will assume that this line of proof works and will not repeat it.

37 Perfectly Safe Assumptions For the purposes of this class, you can safely simplify recurrences by Only evaluating the recurrences at powers of some number to avoid ceilings and floors. Replace Θ(f(n)) or O(f(n)) terms in a recurrence with a constant multiple of f(n). Replace all constants with a single constant equal to the max of all of the constants.

38 A Different Problem: Maximum Single-Sell Profit

39 Maximum Single-Sell Profit procedure maxprofit(list prices): if if length(prices) 1: 1: return return 0 let let left left be be the the first first half half of of prices let let right right be be the the second half half of of prices return max(maxprofit(left), maxprofit(right), max(right) - min(left))

40 Analyzing the Algorithm procedure maxprofit(list prices): if if length(prices) 1: 1: return 0 let let left left be be the the first first half half of of prices let let right right be be the the second half half of of prices return max(maxprofit(left), maxprofit(right), max(right) - min(left)) T(0) T(0) = Θ(1) T(1) T(1) = Θ(1) T(n) T(n) T( n T( n // 2 ) 2 ) + T( n T( n // 2 ) 2 ) + Θ(n) T(n) = O(n log n)

41 The Divide-and-Conquer Framework The two algorithms we have just seen are examples of divide-and-conquer algorithms. These algorithms usually have two steps: (Divide) Split the input apart into multiple smaller pieces, recursively solving each piece. (Conquer) Combine the solutions to each smaller piece together into the overall solution. Typically, correctness is proven inductively and runtime is proven by solving a recurrence relation. In many cases, the runtime is determined without actually solving the recurrence; more on that later.

42 Another Algorithm: Binary Search

43 procedure binarysearch(list A, A, int int low, low, int int high, high, value value key): key): if if low low high: high: return false false let let mid mid = (high (high + low) low) / 2 if if A[mid] = key: key: return true true else else if if A[mid] > key: key: return binarysearch(a, low, low, mid) mid) else else (A[mid] < key): key): return binarysearch(a, mid mid + 1, 1, high) high) T(1) T(1) c T(n) T(n) T(n T(n // 2) 2) + c

44 The Iteration Method T(1) T(1) c T(n) T(n) T(n T(n // 2) 2) + c T(n) T ( n 2 ) +c ( T ( n 4 ) +c ) +c = T ( n 4 ) +2 c ( T ( n 8 ) +c ) +2c = T ( n 8 ) +3c... T (n 2 k ) +k c

45 The Iteration Method T(1) T(1) c T(n) T(n) T(n T(n // 2) 2) + c T(n) T ) (n 2 k +k c = T (1)+c log 2 n c+c log 2 n = O(log n)

46 The Recursion Tree Method T(1) T(1) c T(n) T(n) T(n T(n // 2) 2) + c c c c c log₂ n + c c

47 Formalizing Our Argument To formalize correctness, it's useful to use this invariant: If key = A[i] for some i, then low i < high You can prove this is true by induction on the number of calls made. We can also formalize the runtime bound by induction to prove the O(log n) upper bound, but it's not super exciting to do so.

### Divide-and-Conquer Algorithms Part Four

Divide-and-Conquer Algorithms Part Four Announcements Problem Set 2 due right now. Can submit by Monday at 2:15PM using one late period. Problem Set 3 out, due July 22. Play around with divide-and-conquer

### Intro. to the Divide-and-Conquer Strategy via Merge Sort CMPSC 465 CLRS Sections 2.3, Intro. to and various parts of Chapter 4

Intro. to the Divide-and-Conquer Strategy via Merge Sort CMPSC 465 CLRS Sections 2.3, Intro. to and various parts of Chapter 4 I. Algorithm Design and Divide-and-Conquer There are various strategies we

### Divide and Conquer. Textbook Reading Chapters 4, 7 & 33.4

Divide d Conquer Textook Reading Chapters 4, 7 & 33.4 Overview Design principle Divide d conquer Proof technique Induction, induction, induction Analysis technique Recurrence relations Prolems Sorting

### Today s Outline. Exercise. Binary Search Analysis. Linear Search Analysis. Asymptotic Analysis. Analyzing Code. Announcements. Asymptotic Analysis

Today s Outline Announcements Assignment #1 due Thurs, Oct 7 at 11:45pm Asymptotic Analysis Asymptotic Analysis CSE 7 Data Structures & Algorithms Ruth Anderson Autumn 2010 Exercise Analyzing Code bool

### CSC148 Lecture 8. Algorithm Analysis Binary Search Sorting

CSC148 Lecture 8 Algorithm Analysis Binary Search Sorting Algorithm Analysis Recall definition of Big Oh: We say a function f(n) is O(g(n)) if there exists positive constants c,b such that f(n)

### Binary Search. Search for x in a sorted array A.

Divide and Conquer A general paradigm for algorithm design; inspired by emperors and colonizers. Three-step process: 1. Divide the problem into smaller problems. 2. Conquer by solving these problems. 3.

### Many algorithms, particularly divide and conquer algorithms, have time complexities which are naturally

Recurrence Relations Many algorithms, particularly divide and conquer algorithms, have time complexities which are naturally modeled by recurrence relations. A recurrence relation is an equation which

### Divide And Conquer Algorithms

CSE341T/CSE549T 09/10/2014 Lecture 5 Divide And Conquer Algorithms Recall in last lecture, we looked at one way of parallelizing matrix multiplication. At the end of the lecture, we saw the reduce SUM

### CS 102: SOLUTIONS TO DIVIDE AND CONQUER ALGORITHMS (ASSGN 4)

CS 10: SOLUTIONS TO DIVIDE AND CONQUER ALGORITHMS (ASSGN 4) Problem 1. a. Consider the modified binary search algorithm so that it splits the input not into two sets of almost-equal sizes, but into three

### The Tower of Hanoi. Recursion Solution. Recursive Function. Time Complexity. Recursive Thinking. Why Recursion? n! = n* (n-1)!

The Tower of Hanoi Recursion Solution recursion recursion recursion Recursive Thinking: ignore everything but the bottom disk. 1 2 Recursive Function Time Complexity Hanoi (n, src, dest, temp): If (n >

### CMPS 102 Solutions to Homework 1

CMPS 0 Solutions to Homework Lindsay Brown, lbrown@soe.ucsc.edu September 9, 005 Problem..- p. 3 For inputs of size n insertion sort runs in 8n steps, while merge sort runs in 64n lg n steps. For which

### Data Structures and Algorithms Written Examination

Data Structures and Algorithms Written Examination 22 February 2013 FIRST NAME STUDENT NUMBER LAST NAME SIGNATURE Instructions for students: Write First Name, Last Name, Student Number and Signature where

### Merge Sort. 2004 Goodrich, Tamassia. Merge Sort 1

Merge Sort 7 2 9 4 2 4 7 9 7 2 2 7 9 4 4 9 7 7 2 2 9 9 4 4 Merge Sort 1 Divide-and-Conquer Divide-and conquer is a general algorithm design paradigm: Divide: divide the input data S in two disjoint subsets

### CS473 - Algorithms I

CS473 - Algorithms I Lecture 4 The Divide-and-Conquer Design Paradigm View in slide-show mode 1 Reminder: Merge Sort Input array A sort this half sort this half Divide Conquer merge two sorted halves Combine

### UNIT 5C Merge Sort. Course Announcements

UNIT 5C Merge Sort 1 Course Announcements Exam rooms for Lecture 1, 2:30 3:20 Sections A, B, C, D at Rashid Sections E, F, G at Baker A51 (Giant Eagle Auditorium) Exam rooms for Lecture 2, 3:30 4:20 Sections

### Quicksort is a divide-and-conquer sorting algorithm in which division is dynamically carried out (as opposed to static division in Mergesort).

Chapter 7: Quicksort Quicksort is a divide-and-conquer sorting algorithm in which division is dynamically carried out (as opposed to static division in Mergesort). The three steps of Quicksort are as follows:

### Class Overview. CSE 326: Data Structures. Goals. Goals. Data Structures. Goals. Introduction

Class Overview CSE 326: Data Structures Introduction Introduction to many of the basic data structures used in computer software Understand the data structures Analyze the algorithms that use them Know

### 8.1 Makespan Scheduling

600.469 / 600.669 Approximation Algorithms Lecturer: Michael Dinitz Topic: Dynamic Programing: Min-Makespan and Bin Packing Date: 2/19/15 Scribe: Gabriel Kaptchuk 8.1 Makespan Scheduling Consider an instance

### Mergesort and Quicksort

Mergesort Mergesort and Quicksort Basic plan: Divide array into two halves. Recursively sort each half. Merge two halves to make sorted whole. mergesort mergesort analysis quicksort quicksort analysis

### Lecture Notes on Linear Search

Lecture Notes on Linear Search 15-122: Principles of Imperative Computation Frank Pfenning Lecture 5 January 29, 2013 1 Introduction One of the fundamental and recurring problems in computer science is

### The Union-Find Problem Kruskal s algorithm for finding an MST presented us with a problem in data-structure design. As we looked at each edge,

The Union-Find Problem Kruskal s algorithm for finding an MST presented us with a problem in data-structure design. As we looked at each edge, cheapest first, we had to determine whether its two endpoints

### 3. Mathematical Induction

3. MATHEMATICAL INDUCTION 83 3. Mathematical Induction 3.1. First Principle of Mathematical Induction. Let P (n) be a predicate with domain of discourse (over) the natural numbers N = {0, 1,,...}. If (1)

### Closest Pair Problem

Closest Pair Problem Given n points in d-dimensions, find two whose mutual distance is smallest. Fundamental problem in many applications as well as a key step in many algorithms. p q A naive algorithm

### 6. Standard Algorithms

6. Standard Algorithms The algorithms we will examine perform Searching and Sorting. 6.1 Searching Algorithms Two algorithms will be studied. These are: 6.1.1. inear Search The inear Search The Binary

### Section IV.1: Recursive Algorithms and Recursion Trees

Section IV.1: Recursive Algorithms and Recursion Trees Definition IV.1.1: A recursive algorithm is an algorithm that solves a problem by (1) reducing it to an instance of the same problem with smaller

### Divide and Conquer: Counting Inversions

Divide and Conquer: Counting Inversions Rank Analysis Collaborative filtering matches your preference (books, music, movies, restaurants) with that of others finds people with similar tastes recommends

### Lecture 1: Course overview, circuits, and formulas

Lecture 1: Course overview, circuits, and formulas Topics in Complexity Theory and Pseudorandomness (Spring 2013) Rutgers University Swastik Kopparty Scribes: John Kim, Ben Lund 1 Course Information Swastik

### Analysis of Algorithms: The Non-recursive Case

Analysis of Algorithms: The Non-recursive Case Key topics: * Introduction * Generalizing Running Time * Doing a Timing Analysis * Big-Oh Notation * Big-Oh Operations * Analyzing Some Simple Programs -

### Introduction to Algorithms Review information for Prelim 1 CS 4820, Spring 2010 Distributed Wednesday, February 24

Introduction to Algorithms Review information for Prelim 1 CS 4820, Spring 2010 Distributed Wednesday, February 24 The final exam will cover seven topics. 1. greedy algorithms 2. divide-and-conquer algorithms

### Theory of Computation Prof. Kamala Krithivasan Department of Computer Science and Engineering Indian Institute of Technology, Madras

Theory of Computation Prof. Kamala Krithivasan Department of Computer Science and Engineering Indian Institute of Technology, Madras Lecture No. # 31 Recursive Sets, Recursively Innumerable Sets, Encoding

### 1 2-3 Trees: The Basics

CS10: Data Structures and Object-Oriented Design (Fall 2013) November 1, 2013: 2-3 Trees: Inserting and Deleting Scribes: CS 10 Teaching Team Lecture Summary In this class, we investigated 2-3 Trees in

### Introduction to Algorithms March 10, 2004 Massachusetts Institute of Technology Professors Erik Demaine and Shafi Goldwasser Quiz 1.

Introduction to Algorithms March 10, 2004 Massachusetts Institute of Technology 6.046J/18.410J Professors Erik Demaine and Shafi Goldwasser Quiz 1 Quiz 1 Do not open this quiz booklet until you are directed

### MapReduce and Distributed Data Analysis. Sergei Vassilvitskii Google Research

MapReduce and Distributed Data Analysis Google Research 1 Dealing With Massive Data 2 2 Dealing With Massive Data Polynomial Memory Sublinear RAM Sketches External Memory Property Testing 3 3 Dealing With

### Binary search algorithm

Binary search algorithm Definition Search a sorted array by repeatedly dividing the search interval in half. Begin with an interval covering the whole array. If the value of the search key is less than

### Format of Divide-and-Conquer algorithms:

CS 360: Data Structures and Algorithms Divide-and-Conquer (part 1) Format of Divide-and-Conquer algorithms: Divide: Split the array or list into smaller pieces Conquer: Solve the same problem recursively

### the recursion-tree method

the recursion- method recurrence into a 1 recurrence into a 2 MCS 360 Lecture 39 Introduction to Data Structures Jan Verschelde, 22 November 2010 recurrence into a The for consists of two steps: 1 Guess

### Efficiency of algorithms. Algorithms. Efficiency of algorithms. Binary search and linear search. Best, worst and average case.

Algorithms Efficiency of algorithms Computational resources: time and space Best, worst and average case performance How to compare algorithms: machine-independent measure of efficiency Growth rate Complexity

### Binary Search Trees CMPSC 122

Binary Search Trees CMPSC 122 Note: This notes packet has significant overlap with the first set of trees notes I do in CMPSC 360, but goes into much greater depth on turning BSTs into pseudocode than

### Near Optimal Solutions

Near Optimal Solutions Many important optimization problems are lacking efficient solutions. NP-Complete problems unlikely to have polynomial time solutions. Good heuristics important for such problems.

### Zabin Visram Room CS115 CS126 Searching. Binary Search

Zabin Visram Room CS115 CS126 Searching Binary Search Binary Search Sequential search is not efficient for large lists as it searches half the list, on average Another search algorithm Binary search Very

### CS473 - Algorithms I

CS473 - Algorithms I Lecture 9 Sorting in Linear Time View in slide-show mode 1 How Fast Can We Sort? The algorithms we have seen so far: Based on comparison of elements We only care about the relative

### Lecture Notes on Binary Search Trees

Lecture Notes on Binary Search Trees 15-122: Principles of Imperative Computation Frank Pfenning André Platzer Lecture 17 October 23, 2014 1 Introduction In this lecture, we will continue considering associative

Chapter 11 Theory of Computation 2007 Pearson Addison-Wesley. All rights reserved Chapter 11: Theory of Computation 11.1 Functions and Their Computation 11.2 Turing Machines 11.3 Universal Programming

### The Running Time of Programs

CHAPTER 3 The Running Time of Programs In Chapter 2, we saw two radically different algorithms for sorting: selection sort and merge sort. There are, in fact, scores of algorithms for sorting. This situation

### A Note on Maximum Independent Sets in Rectangle Intersection Graphs

A Note on Maximum Independent Sets in Rectangle Intersection Graphs Timothy M. Chan School of Computer Science University of Waterloo Waterloo, Ontario N2L 3G1, Canada tmchan@uwaterloo.ca September 12,

### CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis. Linda Shapiro Winter 2015

CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis Linda Shapiro Today Registration should be done. Homework 1 due 11:59 pm next Wednesday, January 14 Review math essential

### Computer Science 210: Data Structures. Searching

Computer Science 210: Data Structures Searching Searching Given a sequence of elements, and a target element, find whether the target occurs in the sequence Variations: find first occurence; find all occurences

### Algorithms. Margaret M. Fleck. 18 October 2010

Algorithms Margaret M. Fleck 18 October 2010 These notes cover how to analyze the running time of algorithms (sections 3.1, 3.3, 4.4, and 7.1 of Rosen). 1 Introduction The main reason for studying big-o

### Loop Invariants and Binary Search

Loop Invariants and Binary Search Chapter 4.3.3 and 9.3.1-1 - Outline Ø Iterative Algorithms, Assertions and Proofs of Correctness Ø Binary Search: A Case Study - 2 - Outline Ø Iterative Algorithms, Assertions

### Data Structures. Algorithm Performance and Big O Analysis

Data Structures Algorithm Performance and Big O Analysis What s an Algorithm? a clearly specified set of instructions to be followed to solve a problem. In essence: A computer program. In detail: Defined

### Lecture 4: Properties of and Rules for

Lecture 4: Properties of and Rules for Asymptotic Big-Oh, Big-Omega, and Big-Theta Notation Georgy Gimel farb COMPSCI 220 Algorithms and Data Structures 1 / 13 1 Time complexity 2 Big-Oh rules Scaling

### Quiz 1 Solutions. (a) T F The height of any binary search tree with n nodes is O(log n). Explain:

Introduction to Algorithms March 9, 2011 Massachusetts Institute of Technology 6.006 Spring 2011 Professors Erik Demaine, Piotr Indyk, and Manolis Kellis Quiz 1 Solutions Problem 1. Quiz 1 Solutions True

### Why? A central concept in Computer Science. Algorithms are ubiquitous.

Analysis of Algorithms: A Brief Introduction Why? A central concept in Computer Science. Algorithms are ubiquitous. Using the Internet (sending email, transferring files, use of search engines, online

### Induction. Margaret M. Fleck. 10 October These notes cover mathematical induction and recursive definition

Induction Margaret M. Fleck 10 October 011 These notes cover mathematical induction and recursive definition 1 Introduction to induction At the start of the term, we saw the following formula for computing

### Lectures 5-6: Taylor Series

Math 1d Instructor: Padraic Bartlett Lectures 5-: Taylor Series Weeks 5- Caltech 213 1 Taylor Polynomials and Series As we saw in week 4, power series are remarkably nice objects to work with. In particular,

### Lecture Notes on Binary Search Trees

Lecture Notes on Binary Search Trees 15-122: Principles of Imperative Computation Frank Pfenning Lecture 17 March 17, 2010 1 Introduction In the previous two lectures we have seen how to exploit the structure

### Factoring & Primality

Factoring & Primality Lecturer: Dimitris Papadopoulos In this lecture we will discuss the problem of integer factorization and primality testing, two problems that have been the focus of a great amount

### Sorting revisited. Build the binary search tree: O(n^2) Traverse the binary tree: O(n) Total: O(n^2) + O(n) = O(n^2)

Sorting revisited How did we use a binary search tree to sort an array of elements? Tree Sort Algorithm Given: An array of elements to sort 1. Build a binary search tree out of the elements 2. Traverse

### Recursive Algorithms. Recursion. Motivating Example Factorial Recall the factorial function. { 1 if n = 1 n! = n (n 1)! if n > 1

Recursion Slides by Christopher M Bourke Instructor: Berthe Y Choueiry Fall 007 Computer Science & Engineering 35 Introduction to Discrete Mathematics Sections 71-7 of Rosen cse35@cseunledu Recursive Algorithms

### 1.1 Logical Form and Logical Equivalence 1

Contents Chapter I The Logic of Compound Statements 1.1 Logical Form and Logical Equivalence 1 Identifying logical form; Statements; Logical connectives: not, and, and or; Translation to and from symbolic

### Analysis of Binary Search algorithm and Selection Sort algorithm

Analysis of Binary Search algorithm and Selection Sort algorithm In this section we shall take up two representative problems in computer science, work out the algorithms based on the best strategy to

### Mathematical Induction. Lecture 10-11

Mathematical Induction Lecture 10-11 Menu Mathematical Induction Strong Induction Recursive Definitions Structural Induction Climbing an Infinite Ladder Suppose we have an infinite ladder: 1. We can reach

### Algorithms, Integers

CHAPTER 3 Algorithms, Integers 3.1. Algorithms Consider the following list of instructions to find the maximum of three numbers a, b, c: 1. Assign variable x the value of a. 2. If b > x then assign x the

### From Last Time: Remove (Delete) Operation

CSE 32 Lecture : More on Search Trees Today s Topics: Lazy Operations Run Time Analysis of Binary Search Tree Operations Balanced Search Trees AVL Trees and Rotations Covered in Chapter of the text From

### Polynomials and the Fast Fourier Transform (FFT) Battle Plan

Polynomials and the Fast Fourier Transform (FFT) Algorithm Design and Analysis (Wee 7) 1 Polynomials Battle Plan Algorithms to add, multiply and evaluate polynomials Coefficient and point-value representation

### What Is Recursion? Recursion. Binary search example postponed to end of lecture

Recursion Binary search example postponed to end of lecture What Is Recursion? Recursive call A method call in which the method being called is the same as the one making the call Direct recursion Recursion

### Sequential Data Structures

Sequential Data Structures In this lecture we introduce the basic data structures for storing sequences of objects. These data structures are based on arrays and linked lists, which you met in first year

### CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team Lecture Summary In this lecture, we learned about the ADT Priority Queue. A

### Dynamic Programming. Lecture 11. 11.1 Overview. 11.2 Introduction

Lecture 11 Dynamic Programming 11.1 Overview Dynamic Programming is a powerful technique that allows one to solve many different types of problems in time O(n 2 ) or O(n 3 ) for which a naive approach

### Unit 1 Number Sense. In this unit, students will study repeating decimals, percents, fractions, decimals, and proportions.

Unit 1 Number Sense In this unit, students will study repeating decimals, percents, fractions, decimals, and proportions. BLM Three Types of Percent Problems (p L-34) is a summary BLM for the material

### Parallel Random Access Machine (PRAM) PRAM Algorithms. Shared Memory Access Conflicts. A Basic PRAM Algorithm. Time Optimality.

Parallel Random Access Machine (PRAM) PRAM Algorithms Arvind Krishnamurthy Fall 2 Collection of numbered processors Accessing shared memory cells Each processor could have local memory (registers) Each

### Recursive void Methods. Chapter 11. A recursive method is a method that includes a call to itself Recursion is based on the general problem.

Chapter 11 Recursion Recursive void Methods A recursive method is a method that includes a call to itself Recursion is based on the general problem solving technique of breaking down a task into subtasks

### CSE 326, Data Structures. Sample Final Exam. Problem Max Points Score 1 14 (2x7) 2 18 (3x6) 3 4 4 7 5 9 6 16 7 8 8 4 9 8 10 4 Total 92.

Name: Email ID: CSE 326, Data Structures Section: Sample Final Exam Instructions: The exam is closed book, closed notes. Unless otherwise stated, N denotes the number of elements in the data structure

### 2.3 Scheduling jobs on identical parallel machines

2.3 Scheduling jobs on identical parallel machines There are jobs to be processed, and there are identical machines (running in parallel) to which each job may be assigned Each job = 1,,, must be processed

### Outline BST Operations Worst case Average case Balancing AVL Red-black B-trees. Binary Search Trees. Lecturer: Georgy Gimel farb

Binary Search Trees Lecturer: Georgy Gimel farb COMPSCI 220 Algorithms and Data Structures 1 / 27 1 Properties of Binary Search Trees 2 Basic BST operations The worst-case time complexity of BST operations

### An example of a computable

An example of a computable absolutely normal number Verónica Becher Santiago Figueira Abstract The first example of an absolutely normal number was given by Sierpinski in 96, twenty years before the concept

### Iteration, Induction, and Recursion

CHAPTER 2 Iteration, Induction, and Recursion The power of computers comes from their ability to execute the same task, or different versions of the same task, repeatedly. In computing, the theme of iteration

### Converting a Number from Decimal to Binary

Converting a Number from Decimal to Binary Convert nonnegative integer in decimal format (base 10) into equivalent binary number (base 2) Rightmost bit of x Remainder of x after division by two Recursive

### Regular Languages and Finite Automata

Regular Languages and Finite Automata 1 Introduction Hing Leung Department of Computer Science New Mexico State University Sep 16, 2010 In 1943, McCulloch and Pitts [4] published a pioneering work on a

### Lecture 4: AC 0 lower bounds and pseudorandomness

Lecture 4: AC 0 lower bounds and pseudorandomness Topics in Complexity Theory and Pseudorandomness (Spring 2013) Rutgers University Swastik Kopparty Scribes: Jason Perry and Brian Garnett In this lecture,

### Randomized algorithms

Randomized algorithms March 10, 2005 1 What are randomized algorithms? Algorithms which use random numbers to make decisions during the executions of the algorithm. Why would we want to do this?? Deterministic

### CSE373: Data Structures & Algorithms Lecture 14: Hash Collisions. Linda Shapiro Spring 2016

CSE373: Data Structures & Algorithms Lecture 14: Hash Collisions Linda Shapiro Spring 2016 Announcements Friday: Review List and go over answers to Practice Problems 2 Hash Tables: Review Aim for constant-time

### Binary Multiplication

Binary Multiplication Q: How do we multiply two numbers? eg. 12, 345 6, 789 111105 987600 8641500 + 74070000 83, 810, 205 10111 10101 10111 00000 1011100 0000000 + 101110000 111100011 Pad, multiply and

### #1 Make sense of problems and persevere in solving them.

#1 Make sense of problems and persevere in solving them. 1. Make sense of problems and persevere in solving them. Interpret and make meaning of the problem looking for starting points. Analyze what is

### Functions Recursion. C++ functions. Declare/prototype. Define. Call. int myfunction (int ); int myfunction (int x){ int y = x*x; return y; }

Functions Recursion C++ functions Declare/prototype int myfunction (int ); Define int myfunction (int x){ int y = x*x; return y; Call int a; a = myfunction (7); function call flow types type of function

### Classification - Examples

Lecture 2 Scheduling 1 Classification - Examples 1 r j C max given: n jobs with processing times p 1,...,p n and release dates r 1,...,r n jobs have to be scheduled without preemption on one machine taking

### CSE373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks/Queues. Nicki Dell Spring 2014

CSE373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks/Queues Nicki Dell Spring 2014 Registration We have 140 students registered and 140+ on the wait list! If you re thinking of

### 28 Closest-Point Problems -------------------------------------------------------------------

28 Closest-Point Problems ------------------------------------------------------------------- Geometric problems involving points on the plane usually involve implicit or explicit treatment of distances

### Homework Exam 1, Geometric Algorithms, 2016

Homework Exam 1, Geometric Algorithms, 2016 1. (3 points) Let P be a convex polyhedron in 3-dimensional space. The boundary of P is represented as a DCEL, storing the incidence relationships between the

### Analysis of Algorithms, I

Analysis of Algorithms, I CSOR W4231.002 Eleni Drinea Computer Science Department Columbia University Thursday, February 26, 2015 Outline 1 Recap 2 Representing graphs 3 Breadth-first search (BFS) 4 Applications

### High degree graphs contain large-star factors

High degree graphs contain large-star factors Dedicated to László Lovász, for his 60th birthday Noga Alon Nicholas Wormald Abstract We show that any finite simple graph with minimum degree d contains a

### Connectivity and cuts

Math 104, Graph Theory February 19, 2013 Measure of connectivity How connected are each of these graphs? > increasing connectivity > I G 1 is a tree, so it is a connected graph w/minimum # of edges. Every

### Tree Properties (size vs height) Balanced Binary Trees

Tree Properties (size vs height) Balanced Binary Trees Due now: WA 7 (This is the end of the grace period). Note that a grace period takes the place of a late day. Now is the last time you can turn this

### csci 210: Data Structures Recursion

csci 210: Data Structures Recursion Summary Topics recursion overview simple examples Sierpinski gasket Hanoi towers Blob check READING: GT textbook chapter 3.5 Recursion In general, a method of defining

### CS2 Algorithms and Data Structures Note 11. Breadth-First Search and Shortest Paths

CS2 Algorithms and Data Structures Note 11 Breadth-First Search and Shortest Paths In this last lecture of the CS2 Algorithms and Data Structures thread we will consider the problem of computing distances

### 6.3 Conditional Probability and Independence

222 CHAPTER 6. PROBABILITY 6.3 Conditional Probability and Independence Conditional Probability Two cubical dice each have a triangle painted on one side, a circle painted on two sides and a square painted

### Full and Complete Binary Trees

Full and Complete Binary Trees Binary Tree Theorems 1 Here are two important types of binary trees. Note that the definitions, while similar, are logically independent. Definition: a binary tree T is full

### Data Structures Fibonacci Heaps, Amortized Analysis

Chapter 4 Data Structures Fibonacci Heaps, Amortized Analysis Algorithm Theory WS 2012/13 Fabian Kuhn Fibonacci Heaps Lacy merge variant of binomial heaps: Do not merge trees as long as possible Structure: