Introduction to Algorithms 6.046J/18.401J

Similar documents
ECE250: Algorithms and Data Structures Analyzing and Designing Algorithms

Data Structures and Algorithms CSE 465

Algorithms Design & Analysis. Analysis of Algorithm

Analysis of Algorithms I: Asymptotic Notation, Induction, and MergeSort

Computational Complexity

Algorithms and Programming I. Lecture#1 Spring 2015

COMP Analysis of Algorithms & Data Structures

COMP Analysis of Algorithms & Data Structures

Data Structures and Algorithms CSE 465

Problem Set 1. MIT students: This problem set is due in lecture on Wednesday, September 19.

data structures and algorithms lecture 2

Foundations II: Data Structures and Algorithms

CIS 121. Analysis of Algorithms & Computational Complexity. Slides based on materials provided by Mary Wootters (Stanford University)

CS 4407 Algorithms Lecture 2: Iterative and Divide and Conquer Algorithms

CS473 - Algorithms I

Introduction to Algorithms 6.046J/18.401J/SMA5503

Growth of Functions (CLRS 2.3,3)

Problem Set 1 Solutions

P, NP, NP-Complete, and NPhard

CS 380 ALGORITHM DESIGN AND ANALYSIS

Lecture 3. Big-O notation, more recurrences!!

CS 161 Summer 2009 Homework #2 Sample Solutions

Lecture 1: Asymptotics, Recurrences, Elementary Sorting

Lecture 2: Asymptotic Notation CSCI Algorithms I

Grade 11/12 Math Circles Fall Nov. 5 Recurrences, Part 2

Inf 2B: Sorting, MergeSort and Divide-and-Conquer

Data Structures and Algorithms CMPSC 465

Data Structures and Algorithms Running time and growth functions January 18, 2018

CS 4407 Algorithms Lecture 2: Growth Functions

Ch01. Analysis of Algorithms

COE428 Notes Week 4 (Week of Jan 30, 2017)

Analysis of Algorithms

i=1 i B[i] B[i] + A[i, j]; c n for j n downto i + 1 do c n i=1 (n i) C[i] C[i] + A[i, j]; c n

The Time Complexity of an Algorithm

CMPS 2200 Fall Divide-and-Conquer. Carola Wenk. Slides courtesy of Charles Leiserson with changes and additions by Carola Wenk

Big-O Notation and Complexity Analysis

CS 4407 Algorithms Lecture 3: Iterative and Divide and Conquer Algorithms

CS 577 Introduction to Algorithms: Strassen s Algorithm and the Master Theorem

CS F-01 Algorithm Analysis 1

Analysis of Algorithms

Notes for Recitation 14

3.1 Asymptotic notation

Analysis of Algorithms - Using Asymptotic Bounds -

COMP 382: Reasoning about algorithms

Big O 2/14/13. Administrative. Does it terminate? David Kauchak cs302 Spring 2013

Ch 01. Analysis of Algorithms

Analysis of Multithreaded Algorithms

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 3. Θ Notation. Comparing Algorithms

Analysis of Algorithms [Reading: CLRS 2.2, 3] Laura Toma, csci2200, Bowdoin College

Design and Analysis of Algorithms Recurrence. Prof. Chuhua Xian School of Computer Science and Engineering

Lecture 4. Quicksort

The Time Complexity of an Algorithm

Written Homework #1: Analysis of Algorithms

CSED233: Data Structures (2017F) Lecture4: Analysis of Algorithms

Introduction to Algorithms

Problem Set 2 Solutions

More Asymptotic Analysis Spring 2018 Discussion 8: March 6, 2018

On my honor I affirm that I have neither given nor received inappropriate aid in the completion of this exercise.

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 3

CS161: Algorithm Design and Analysis Recitation Section 3 Stanford University Week of 29 January, Problem 3-1.

Computer Algorithms CISC4080 CIS, Fordham Univ. Outline. Last class. Instructor: X. Zhang Lecture 2

Computer Algorithms CISC4080 CIS, Fordham Univ. Instructor: X. Zhang Lecture 2

Analysis of Algorithm Efficiency. Dr. Yingwu Zhu

Introduction to Computer Science Lecture 5: Algorithms

Chapter 4. Recurrences

Algorithms, Design and Analysis. Order of growth. Table 2.1. Big-oh. Asymptotic growth rate. Types of formulas for basic operation count

CS 4349 Lecture August 30th, 2017

Menu. Lecture 2: Orders of Growth. Predicting Running Time. Order Notation. Predicting Program Properties

Data Structures and Algorithm. Xiaoqing Zheng

How many hours would you estimate that you spent on this assignment?

1.1 Administrative Stuff

Lecture 1: Asymptotic Complexity. 1 These slides include material originally prepared by Dr.Ron Cytron, Dr. Jeremy Buhler, and Dr. Steve Cole.

Module 1: Analyzing the Efficiency of Algorithms

Algorithm Design and Analysis

Course Structure. Computer Science 2300: Data Structures and Algorithms. What is this class about? Textbooks

Asymptotic Notation. such that t(n) cf(n) for all n n 0. for some positive real constant c and integer threshold n 0

COMP 9024, Class notes, 11s2, Class 1

Divide-and-conquer: Order Statistics. Curs: Fall 2017

Midterm Exam. CS 3110: Design and Analysis of Algorithms. June 20, Group 1 Group 2 Group 3

Advanced Algorithmics (6EAP)

CS 341: Algorithms. Douglas R. Stinson. David R. Cheriton School of Computer Science University of Waterloo. January 16, 2019

Design and Analysis of Algorithms March 12, 2015 Massachusetts Institute of Technology Profs. Erik Demaine, Srini Devadas, and Nancy Lynch Quiz 1

What we have learned What is algorithm Why study algorithm The time and space efficiency of algorithm The analysis framework of time efficiency Asympt

Cpt S 223. School of EECS, WSU

CSE 3101: Introduction to the Design and Analysis of Algorithms. Office hours (Las/CSEB 3043): Tue 6-7 pm, Wed 4-6 pm or by appointment.

Asymptotic Algorithm Analysis & Sorting

Lecture 3: Big-O and Big-Θ

Algorithms and Their Complexity

Comp 11 Lectures. Mike Shah. July 26, Tufts University. Mike Shah (Tufts University) Comp 11 Lectures July 26, / 45

Md Momin Al Aziz. Analysis of Algorithms. Asymptotic Notations 3 COMP Computer Science University of Manitoba

CSCE 750 Final Exam Answer Key Wednesday December 7, 2005

Outline. 1 Introduction. 3 Quicksort. 4 Analysis. 5 References. Idea. 1 Choose an element x and reorder the array as follows:

Module 1: Analyzing the Efficiency of Algorithms

3. Algorithms. What matters? How fast do we solve the problem? How much computer resource do we need?

Recommended readings: Description of Quicksort in my notes, Ch7 of your CLRS text.

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 2

Introduction to Algorithms March 10, 2010 Massachusetts Institute of Technology Spring 2010 Professors Piotr Indyk and David Karger Quiz 1

Problem Set 4 Solutions

Data Structures and Algorithms

CSE 548: (Design and) Analysis of Algorithms

Transcription:

Introduction to Algorithms 6.046J/8.40J Lecture Prof. Piotr Indyk Welcome to Introduction to Algorithms, Spring 08 Handouts. Course Information. Calendar 3. Signup sheet (PLEASE return at the end of this lecture!) Charles Leiserson and Piotr Indyk L. Course information Design and analysis of algorithms. Staff. Prerequisites 3. Lectures &Recitations 4. Problem sets 5. Describing algorithms 6. Grading policy. Collaboration policy 8. Textbook (CLRS). Course web site 0.Extra help (HKN) L.3 Theoretical study of how to solve computational problems efficiently Computational problems : e.g., sorting data, finding shortest path, etc. Solve : design an algorithm that does the job (correctly) Theoretical : use the language of (algorithmic) mathematics to understand the performance of the algorithms. In particular, it enables us to define what efficiently means Typical question: what is the fastest algorithm for a given problem? L.4 Performance vs. the rest of Course 6 The problem of sorting Also important: modularity maintainability functionality robustness user-friendliness programmer time simplicity extensibility reliability Performance is often one of the key aspects: Searching the Web Analyzing the genome(s) L.5 Input: sequence a, a,, a n of numbers. Output: permuted input a', a',, a' n such that a' a' a' n. Example: Input: 8 4 3 6 Output: 3 4 6 8 L.6

A: Insertion sort pseudocode INSERTION-SORT (A, n) A[.. n] for j to n do key A[ j] i j while i > 0 and A[i] > key do A[i+] A[i] i i A[i+] = key i j n sorted key 8 4 3 6 L. L.8 8 4 3 6 8 4 3 6 8 4 3 6 L. L.0 8 4 3 6 8 4 3 6 8 4 3 6 8 4 3 6 4 8 3 6 L. L.

8 4 3 6 8 4 3 6 4 8 3 6 8 4 3 6 8 4 3 6 4 8 3 6 4 8 3 6 L. L.4 8 4 3 6 8 4 3 6 4 8 3 6 4 8 3 6 8 4 3 6 8 4 3 6 4 8 3 6 4 8 3 6 3 4 8 6 L.5 L.6 8 4 3 6 8 4 3 6 4 8 3 6 4 8 3 6 3 4 8 6 8 4 3 6 8 4 3 6 4 8 3 6 4 8 3 6 3 4 8 6 3 4 6 8 done L. L.8 3

Running time of I-Sort? INSERTION-SORT (A, n) A[.. n] for j to n do key A[ j] i j while i > 0 and A[i] > key do A[i+] A[i] i i A[i+] = key Issues: the running time depends on The input: an already sorted sequence is easy to sort The processor: ZX80 vs Pentium How can we develop metrics that do not depend on these factors? L. Input-independence ANALYSES: Worst-case: (usually) T(n) = maximum time of algorithm on any input of size n. Average-case: (sometimes) T(n) = expected time of algorithm over all inputs of size n. Need assumption of statistical distribution of inputs. Best-case: (bogus) Cheat with a slow algorithm that works fast on some input. L. Machine-independence Θ-notation What is insertion sort s worst-case time? BIG IDEA: Ignore machine-dependent constants. Look at growth of T(n) as n. Asymptotic Analysis Math: Θ(g(n)) = { f (n) :there exist positive constants c, c, and n 0 such that 0 c g(n) f (n) c g(n) for all n n 0 } We write f (n) = Θ(g(n)) instead of f (n) Θ(g(n)) Engineering: Drop low-order terms; ignore leading constants. Example: 3n 3 + 0n 5n + 6046 = Θ(n 3 ) L. Also: O(), Ω(), o(), ω(), L. T(n) Asymptotic analysis When n gets large enough, a Θ(n) algorithm always beats a Θ(n ) algorithm. n n 0 We shouldn t ignore asymptotically slower algorithms, however. Real-world design situations often call for a careful balancing of engineering objectives. L.3 Insertion sort analysis Worst case: T ( n) = Θ( n j= j) = Θ ( n ) Is insertion sort a fast sorting algorithm? Moderately so, for small n. Not at all, for large n. L.4 4

Merge sort MERGE-SORT A[.. n]. If n =, done.. Recursively sort A[.. n/ ] and A[ n/ +.. n ]. 3. Merge the two sorted lists into one. Key subroutine: MERGE L.5 L.6 L. L.8 L. L.30 5

6 L.3 L.3 L.33 L.34 L.35 L.36

Time to merge a total of n elements? Θ(n) L.3 L.38 Analyzing merge sort T(n) Θ() T(n/) Θ(n) MERGE-SORT A[.. n]. If n =, done.. Recursively sort A[.. n/ ] and A[ n/ +.. n ]. 3. Merge the sorted lists Sloppiness: Should be T( n/ ) + T( n/ ), but it turns out not to matter asymptotically. Recurrence for merge sort T(n) = Θ() if n = ; T(n/) + Θ(n) if n >. How to solve it? One approach on the next slide More in CLRS and Lecture L.3 L.40 Solve T(n) = T(n/) +, where c > 0 is constant. Solve T(n) = T(n/) +, where c > 0 is constant. T(n) L.4 L.4

Solve T(n) = T(n/) +, where c > 0 is constant. Solve T(n) = T(n/) +, where c > 0 is constant. T(n/) T(n/) / / T(n/4) T(n/4) T(n/4) T(n/4) L.43 L.44 Solve T(n) = T(n/) +, where c > 0 is constant. Solve T(n) = T(n/) +, where c > 0 is constant. / / /4 /4 /4 /4 / / /4 /4 /4 /4 Θ() Θ() L.45 L.46 Solve T(n) = T(n/) +, where c > 0 is constant. Solve T(n) = T(n/) +, where c > 0 is constant. / / / / /4 /4 /4 /4 /4 /4 /4 /4 Θ() Θ() L.4 L.48 8

Solve T(n) = T(n/) +, where c > 0 is constant. Solve T(n) = T(n/) +, where c > 0 is constant. / / / / /4 /4 /4 /4 /4 /4 /4 /4 Θ() Θ() #leaves = n Θ(n) L.4 L.50 Solve T(n) = T(n/) +, where c > 0 is constant. / / /4 /4 /4 /4 Conclusions Θ(n lg n) grows more slowly than Θ(n ). Therefore, merge sort asymptotically beats insertion sort in the worst case. In practice, merge sort beats insertion sort for n > 30 or so. Go test it out for yourself! Θ() #leaves = n Θ(n) Total = Θ(n lg n) L.5 L.5