EECS 311: Chapter 2 Notes Chris Riesbeck EECS Northwestern.

Slides:



Advertisements
Similar presentations
Chapter 20 Computational complexity. This chapter discusses n Algorithmic efficiency n A commonly used measure: computational complexity n The effects.
Advertisements

Analysis of Algorithms II
Copyright © 2003 Pearson Education, Inc. Slide 1.
Towers of Hanoi Move n (4) disks from pole A to pole C such that a disk is never put on a smaller disk A BC ABC.
Divide and Conquer (Merge Sort)
Algorithm Analysis.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
§3 Compare the Algorithms 〖 Example 〗 Given (possibly negative) integers A 1, A 2, …, A N, find the maximum value of Max sum is 0 if all the integers.
EECS 311: Chapter 8 Notes Chris Riesbeck EECS Northwestern.
EECS 311: Chapter 4 Notes Chris Riesbeck EECS Northwestern.
Chapter 2: Algorithm Analysis
Introduction to Analysis of Algorithms
Proof Techniques and Recursion. Proof Techniques Proof by induction –Step 1: Prove the base case –Step 2: Inductive hypothesis, assume theorem is true.
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.
CSC 2300 Data Structures & Algorithms January 30, 2007 Chapter 2. Algorithm Analysis.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 11 Sorting and Searching.
Cpt S 223 – Advanced Data Structures
Chapter 10 Sequences, Induction, and Probability Copyright © 2014, 2010, 2007 Pearson Education, Inc Mathematical Induction.
Analysis of Algorithms COMP171 Fall Analysis of Algorithms / Slide 2 Introduction * What is Algorithm? n a clearly specified set of simple instructions.
Algorithms: Selected Exercises Goals Introduce the concept & basic properties of an algorithm.
CS 201 Data Structures and Algorithms Chapter 2: Algorithm Analysis - II Text: Read Weiss, §2.4.3 – Izmir University of Economics.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 19: Recursion.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 19 Recursion.
Vishnu Kotrajaras, PhD.1 Data Structures. Vishnu Kotrajaras, PhD.2 Introduction Why study data structure?  Can understand more code.  Can choose a correct.
Chapter 2 - Mathematical Review Functions
Advance Data Structure and Algorithm COSC600 Dr. Yanggon Kim Chapter 1.
Recursion l Powerful Tool l Useful in simplifying a problem (hides details of a problem) l The ability of a function to call itself l A recursive call.
Analyzing Complexity of Lists OperationSorted Array Sorted Linked List Unsorted Array Unsorted Linked List Search( L, x ) O(logn) O( n ) O( n ) Insert(
Computer Science 101 Fast Searching and Sorting. Improving Efficiency We got a better best case by tweaking the selection sort and the bubble sort We.
1 Programming with Recursion. 2 Recursive Function Call A recursive call is a function call in which the called function is the same as the one making.
Data Structures & Algorithms CHAPTER 4 Searching Ms. Manal Al-Asmari.
SEARCHING UNIT II. Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 2: Recursion: The Mirrors Data Abstraction & Problem Solving.
Chapter 13 Recursion. Learning Objectives Recursive void Functions – Tracing recursive calls – Infinite recursion, overflows Recursive Functions that.
SEARCHING (Linear/Binary). Searching Algorithms  method of locating a specific item of information in a larger collection of data.  two popular search.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 11 Sorting and Searching.
Chapter 13 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Recursive Functions for Tasks(13.1) Recursive Functions.
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Chapter 14 Recursion.
EECS 311: Chapter 6 Notes Chris Riesbeck EECS Northwestern.
Mathematical Background and Linked Lists. 2 Iterative Algorithm for Sum Find the sum of the first n integers stored in an array v : sum (v[], n) temp_sum.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Recursive Solutions Recursion is an extremely powerful problem-solving.
Basic Mathematics Chapter 1 (1.2 and 1.3) Weiss. Recursion / Slide 2 Logarithms * Definition: if and only if * Theorem 1.1: n Proof: apply the definition.
Chapter 1: Introduction
DATA STRUCTURES AND ALGORITHMS Lecture Notes 2 Prepared by İnanç TAHRALI.
Computer Science 101 Fast Algorithms. What Is Really Fast? n O(log 2 n) O(n) O(n 2 )O(2 n )
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Searching When we maintain a collection of data,
Algorithm Analysis. What is an algorithm ? A clearly specifiable set of instructions –to solve a problem Given a problem –decide that the algorithm is.
2IS80 Fundamentals of Informatics Fall 2015 Lecture 6: Sorting and Searching.
Chapter 2: Algorithm Analysis Application of Big-Oh to program analysis Logarithms in Running Time Lydia Sinapova, Simpson College Mark Allen Weiss: Data.
Pei Zheng, Michigan State University 1 Chapter 8 Recursion.
Vishnu Kotrajaras, PhD.1 Data Structures
1 Chapter 8 Recursion. 2 Recursive Function Call a recursion function is a function that either directly or indirectly makes a call to itself. but we.
1 Chapter 2 Algorithm Analysis Reading: Chapter 2.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Recursion.
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
Recursion Powerful Tool
Chapter 13 Recursion Copyright © 2016 Pearson, Inc. All rights reserved.
Analysis of Algorithms CS 477/677
Algorithm Analysis CSE 2011 Winter September 2018.
Proving algorithms (programs) correct with induction
Algorithm design and Analysis
CE 221 Data Structures and Algorithms
CE 221 Data Structures and Algorithms
Chapter 13 Recursion Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

EECS 311: Chapter 2 Notes Chris Riesbeck EECS Northwestern

 Unless otherwise noted, all tables, graphs and code from Mark Allen Weiss' Data Structures and Algorithm Analysis in C++, 3 rd ed, copyright © 2006 by Pearson Education, Inc.

Maximal Subsequence Sum Problem  Given a sequence of N positive and negative integers A 1, A 2, A 3, …, A n  Find the subsequence A j, … Ak with the largest sum  Example: -2, 11, -4, 13, -5,

Triple-Loop Algorithm For every start point For every end point Add up the subsequence Save the biggest

Algorithm 1 Run-Times

Double-Loop Algorithm For every start point For every end point New sum is old sum + next item Save the biggest

Algorithm 2 Run-Times

Divide and Conquer Algorithm Sum for 1 element subsequence Sums for max subsequences in left and right halves Sum for max left subsequence ending on center Sum for max right subsequence starting right of center Return largest of the 3 sums

Analysis of Algorithm 3 To derive T(N), time to solve a problem with N items Base case T(1) = O(1) Recursive case T(N) = 2T(N/2) +… … O(N) + … … O(1)

Analyzing Divide and Conquer  T(1) = O(1)  T(N) = 2T(N/2) + N T(2) = 2T(1) + 2 = 4 = 2*2 T(4) = 2T(2) + 4 = 12 = 4*3 T(8) = 2T(4) + 8 = 32 = 8*4 T(16) = 2T(8) + 16 = 80 = 16*5  T(2 k ) = 2 k * (k+1)  For general N, O(N log N)

Algorithm 3 Run-Times

Single-Loop Algorithm For every start point New sum is old sum + next item If bigger, save; if negative, forget and start over

Algorithm 4 Run-Times

Algorithmic Analysis

Run-times for small N

Run-times for large N

Typical Growth Rates Don't confuse with log log N which is < log N

Binary Search Does this algorithm always stop? If it does, does it always return the right answer? If it does, how long does it take, in the worst case?

Does it halt?  Find a measure M such that you can prove it monotonically decrease on every iteration the algorithm halts when M passes some threshold, e.g., 0  Binary search example: M = high – low M decreases by at least 1 every iteration algorithm halts when M < 0

Does it give the right answer?  Proof by cases When it returns a value, is it correct (no false positives)? When it returns not found, is it correct (no false negatives)?

When it returns a value, is it correct ?  Proof by contradiction: Assume desired property is false. Prove contradiction results.  Binary search example: Assume k ≠ -1 is returned and a[k] ≠ x. k is returned on line 19. This means a[k] is neither > nor < than x. a[k] must be or =. Contradiction.

When it returns not found, is it correct ?  Proof by induction: Prove P is true for K, typically 1 or 2 Prove P is true for N+1 if it’s true for N Then P is true for all N ≥ K  Binary search example: Assume x not in a[]. Assume a[] has 1 element.  a[0] ≠ x and code correctly returns not found. Assume a[] has N + 1 elements.  Proof by cases: If a[mid] < x, search will look at a[mid] … a[high], which has less than N elements. By assumption, that search returns correct answer. Similarly if a[mid] > x. Ergo, binary search returns not found correctly for all N ≥ 1

Greatest Common Divisor Does this algorithm always stop? If it does, does it always return the right answer? If it does, how long does it take, in the worst case?

Exponentiation Does this algorithm always stop? If it does, does it always return the right answer? If it does, how long does it take, in the worst case?

Hailstone Numbers void printHailStones(int n) { cout << n << ":"; while (n > 1) { cout << " " << n; if ( n % 2 == 0 ) n /= 2; else n = 3 * n + 1; } cout << " " << n << endl; } Does this algorithm always stop? If it does, does it always return the right answer? If it does, how long does it take, in the worst case?