Algorithm Design Techniques An Example The Problem Algorithm 1: Cubic Time Algorithm 2: Quadratic Time Algorithm 3: O(n log n) Time Algorithm 4: Linear.

Slides:



Advertisements
Similar presentations
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Advertisements

C++ Programming:. Program Design Including
College of Information Technology & Design
College of Information Technology & Design
Solve a System Algebraically
CS 206 Introduction to Computer Science II 02 / 27 / 2009 Instructor: Michael Eckmann.
Dynamic Programming.
Factoring Polynomials
More on Divide and Conquer. The divide-and-conquer design paradigm 1. Divide the problem (instance) into subproblems. 2. Conquer the subproblems by solving.
CS 206 Introduction to Computer Science II 03 / 02 / 2009 Instructor: Michael Eckmann.
Divide-and-Conquer Recursive in structure –Divide the problem into several smaller sub-problems that are similar to the original but smaller in size –Conquer.
Lecture 30 CSE 331 Nov 8, HW 7 due today Place Q1, Q2 and Q3 in separate piles I will not accept HWs after 1:15pm DO NOT FORGET TO WRITE DOWN YOUR.
CSC 331: Algorithm Analysis Divide-and-Conquer Algorithms.
CS223 Advanced Data Structures and Algorithms 1 Divide and Conquer Neil Tang 4/15/2010.
Divide and Conquer. Recall Complexity Analysis – Comparison of algorithm – Big O Simplification From source code – Recursive.
April 9, 2015Applied Discrete Mathematics Week 9: Relations 1 Solving Recurrence Relations Another Example: Give an explicit formula for the Fibonacci.
Greedy Algorithms Basic idea Connection to dynamic programming Proof Techniques.
Nattee Niparnan. Recall  Complexity Analysis  Comparison of Two Algos  Big O  Simplification  From source code  Recursive.
Design of Algorithms by Induction Part 2 Bibliography: [Manber]- Chap 5.
Quadratic Equations From Their Solutions From solutions to factors to final equations (10.3)
CSC2100B Quick Sort and Merge Sort Xin 1. Quick Sort Efficient sorting algorithm Example of Divide and Conquer algorithm Two phases ◦ Partition phase.
CS 206 Introduction to Computer Science II 10 / 13 / 2008 Instructor: Michael Eckmann.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (3) Recurrence Relation 11/11 ~ 11/14/2008 Yang Song.
CSC 2300 Data Structures & Algorithms January 26, 2007 Chapter 2. Algorithm Analysis.
Data Structure Algorithm Analysis TA: Abbas Sarraf
Patterns and Recursion
Learning Objectives for Section 2.1 Functions
February 17, 2015Applied Discrete Mathematics Week 3: Algorithms 1 Double Summations Table 2 in 4 th Edition: Section th Edition: Section th.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Maximum-sum contiguous subarray Prudence Wong.
HOW TO SOLVE IT? Algorithms. An Algorithm An algorithm is any well-defined (computational) procedure that takes some value, or set of values, as input.
Lecture 2 We have given O(n 3 ), O(n 2 ), O(nlogn) algorithms for the max sub-range problem. This time, a linear time algorithm! The idea is as follows:
Time Complexity Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
Nattee Niparnan. Recall  Complexity Analysis  Comparison of Two Algos  Big O  Simplification  From source code  Recursive.
Recursion and Dynamic Programming. Recursive thinking… Recursion is a method where the solution to a problem depends on solutions to smaller instances.
Recurrence Relation. Outline  What is a recurrence relation ?  Solving linear recurrence relations  Divide-and-conquer algorithms and recurrence relations.
CSC 172 DATA STRUCTURES. THEORETICAL BOUND  Many good sorting algorithms run in O(nlogn) time.  Can we do better?  Can we reason about algorithms not.
Getting Started Introduction to Algorithms Jeff Chastine.
Week 6 - Monday.  What did we talk about last time?  Exam 1!  Before that:  Recursion.
Main Index Contents 11 Main Index Contents Building a Ruler: drawRuler() Building a Ruler: drawRuler() Merge Algorithm Example (4 slides) Merge Algorithm.
Advanced Counting Techniques CSC-2259 Discrete Structures Konstantin Busch - LSU1.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 2 Prepared by İnanç TAHRALI.
ALGORITHMS.
Sorting Algorithm Analysis. Sorting  Sorting is important!  Things that would be much more difficult without sorting: –finding a phone number in the.
Solving Quadratic Equations by Factoring. Martin-Gay, Developmental Mathematics 2 Zero Factor Theorem Quadratic Equations Can be written in the form ax.
2.1 – Linear and Quadratic Equations Linear Equations.
Sorting. Sorting Sorting is important! Things that would be much more difficult without sorting: –finding a telephone number –looking up a word in the.
CS 206 Introduction to Computer Science II 10 / 10 / 2008 Instructor: Michael Eckmann.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
 Design and Analysis of Algorithms تصميم وتحليل الخوارزميات (311 عال) Chapter 2 Sorting (insertion Sort, Merge Sort)
Lecture # 6 1 Advance Analysis of Algorithms. Divide-and-Conquer Divide the problem into a number of subproblems Similar sub-problems of smaller size.
Review. Problem 1 What is the output of the following piece of code void increment(int x) { x++; } int main() { int y = 10; increment(y); cout
Dynamic Programming. What is Dynamic Programming  A method for solving complex problems by breaking them down into simpler sub problems. It is applicable.
Exercise 1: Maximum element for the following code a- what is the basic operation? b- what is C(n)? d-what is the running time?
CSC317 1 Recap: Oh, Omega, Theta Oh (like ≤) Omega (like ≥) Theta (like =) O(n) is asymptotic upper bound 0 ≤ f(n) ≤ cg(n) Ω(n) is asymptotic lower bound.
(Complexity) Analysis of Algorithms Algorithm Input Output 1Analysis of Algorithms.
Introduction to Algorithms: Divide-n-Conquer Algorithms
Modeling with Recurrence Relations
CSC 172 DATA STRUCTURES.
Warm Up /31/17 1. Evaluate x2 + 5x for x = 4 and x = –3. __; ___
Solve Linear Systems by Graphing
CS 3343: Analysis of Algorithms
A quadratic equation is written in the Standard Form,
Medians and Order Statistics
Objective Graph and solve systems of linear inequalities in two variables.
Computer Programming.
Solving Recurrence Relations
Lesson Extension: Patterns and Recursion
Objectives Identify solutions of linear equations in two variables.
15th Scandinavian Workshop on Algorithm Theory
Chapter 8 Systems of Equations
Presentation transcript:

Algorithm Design Techniques An Example The Problem Algorithm 1: Cubic Time Algorithm 2: Quadratic Time Algorithm 3: O(n log n) Time Algorithm 4: Linear Time Comparison of Algorithms Principles

The Problem Definition. Given the real vector x [n], compute the maximum sum found in any contiguous subvector. An Example. If the input vector is then the program returns the sum of x[2..6], or 187.

A Cubic Algorithm Idea. For all pairs of integers i and j satisfying 0 <= i<= j <n, check whether the sum of x [i.. j ] is greater than the maximum sum so far. Code. maxsofar = 0 for i = [0, n) for j = [i, n) sum = 0 for k = [i, j] sum += x[k] /* sum is sum of x[i..j] */ maxsofar = max(maxsofar, sum) Run Time. O(n 3 ).

A Quadratic Algorithm Idea. The sum of x [i.. j ] is close to the previous sum, x [i.. j - 1 ]. Code. maxsofar = 0 for i = [0, n) sum = 0 for j = [i, n) sum += x[j] /* sum is sum of x[i..j] */ maxsofar = max(maxsofar, sum) Run Time. O(n 2 ).

An O(n log n) Algorithm The Divide-and-Conquer Schema. To solve a problem of size n, recursively solve two sub-problems of size n / 2 and combine their solutions. The Idea. Divide into two sub-problems. ________________________ | a | b | Recursively find maximum in subvectors. _____________________________ | | ma | | mb | | Find maximum crossing sub-vector mc _____________________________ | | | mc | | | Return max of ma, mb and mc. Run Time. O(n log n).

Code for the O(N log N) Algorithm float maxsum3(l, u) if (l > u) /* zero elements */ return 0 if (l == u) /* one element */ return max(0, x[l]) m = (l + u) / 2 /* find max crossing to left */ lmax = sum = 0 for (i = m; i >= l; i--) sum += x[i] lmax = max(lmax, sum) /* find max crossing to right */ rmax = sum = 0 for i = (m, u] sum += x[i] rmax = max(rmax, sum) return max(lmax+rmax, maxsum3(l, m), maxsum3(m+1, u))

A Linear Algorithm Idea. How can we extend a solution for x [ 0..i - 1 ] into a solution for x [ 0..i]? Key variables: Code. maxsofar = 0 maxhere = 0 for i = [0, n) /* invariant: maxhere and maxsofar are accurate for x[0..i-1] */ maxhere = max(maxhere + x[i], 0) maxsofar = max(maxsofar, maxhere) Run Time. O(n). maxsofar maxhere