Introduction to Algorithms 6.046J/18.401J

Slides:



Advertisements
Similar presentations
Numbers Treasure Hunt Following each question, click on the answer. If correct, the next page will load with a graphic first – these can be used to check.
Advertisements

Scenario: EOT/EOT-R/COT Resident admitted March 10th Admitted for PT and OT following knee replacement for patient with CHF, COPD, shortness of breath.
한양대학교 정보보호 및 알고리즘 연구실 이재준 담당교수님 : 박희진 교수님
2. Getting Started Heejin Park College of Information and Communications Hanyang University.
Introduction to Algorithms
October 31, 2005Copyright © by Erik D. Demaine and Charles E. LeisersonL13.1 Introduction to Algorithms LECTURE 11 Amortized Analysis Dynamic tables.
Angstrom Care 培苗社 Quadratic Equation II
AP STUDY SESSION 2.
Analysis of Computer Algorithms
1
Copyright © 2003 Pearson Education, Inc. Slide 1 Computer Systems Organization & Architecture Chapters 8-12 John D. Carpinelli.
Processes and Operating Systems
Copyright © 2011, Elsevier Inc. All rights reserved. Chapter 6 Author: Julia Richards and R. Scott Hawley.
David Burdett May 11, 2004 Package Binding for WS CDL.
October 17, 2005 Copyright© Erik D. Demaine and Charles E. Leiserson L2.1 Introduction to Algorithms 6.046J/18.401J LECTURE9 Randomly built binary.
Introduction to Algorithms 6.046J/18.401J
Introduction to Algorithms 6.046J/18.401J
© 2001 by Charles E. Leiserson Introduction to AlgorithmsDay 17 L9.1 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 9 Prof. Charles E. Leiserson.
©2001 by Charles E. Leiserson Introduction to AlgorithmsDay 9 L6.1 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 6 Prof. Erik Demaine.
Local Customization Chapter 2. Local Customization 2-2 Objectives Customization Considerations Types of Data Elements Location for Locally Defined Data.
Process a Customer Chapter 2. Process a Customer 2-2 Objectives Understand what defines a Customer Learn how to check for an existing Customer Learn how.
Custom Statutory Programs Chapter 3. Customary Statutory Programs and Titles 3-2 Objectives Add Local Statutory Programs Create Customer Application For.
Custom Services and Training Provider Details Chapter 4.
CALENDAR.
1 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt BlendsDigraphsShort.
1 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt RhymesMapsMathInsects.
1 Click here to End Presentation Software: Installation and Updates Internet Download CD release NACIS Updates.
The 5S numbers game..
Break Time Remaining 10:00.
Turing Machines.
Table 12.1: Cash Flows to a Cash and Carry Trading Strategy.
PP Test Review Sections 6-1 to 6-6
EIS Bridge Tool and Staging Tables September 1, 2009 Instructor: Way Poteat Slide: 1.
Bellwork Do the following problem on a ½ sheet of paper and turn in.
CS 6143 COMPUTER ARCHITECTURE II SPRING 2014 ACM Principles and Practice of Parallel Programming, PPoPP, 2006 Panel Presentations Parallel Processing is.
Exarte Bezoek aan de Mediacampus Bachelor in de grafische en digitale media April 2014.
Copyright © 2012, Elsevier Inc. All rights Reserved. 1 Chapter 7 Modeling Structure with Blocks.
1 RA III - Regional Training Seminar on CLIMAT&CLIMAT TEMP Reporting Buenos Aires, Argentina, 25 – 27 October 2006 Status of observing programmes in RA.
Basel-ICU-Journal Challenge18/20/ Basel-ICU-Journal Challenge8/20/2014.
1..
Adding Up In Chunks.
MaK_Full ahead loaded 1 Alarm Page Directory (F11)
1 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt Synthetic.
Artificial Intelligence
: 3 00.
5 minutes.
1 hi at no doifpi me be go we of at be do go hi if me no of pi we Inorder Traversal Inorder traversal. n Visit the left subtree. n Visit the node. n Visit.
1 Let’s Recapitulate. 2 Regular Languages DFAs NFAs Regular Expressions Regular Grammars.
Speak Up for Safety Dr. Susan Strauss Harassment & Bullying Consultant November 9, 2012.
Essential Cell Biology
Converting a Fraction to %
Clock will move after 1 minute
PSSA Preparation.
Immunobiology: The Immune System in Health & Disease Sixth Edition
Physics for Scientists & Engineers, 3rd Edition
Energy Generation in Mitochondria and Chlorplasts
Select a time to count down from the clock above
1 Decidability continued…. 2 Theorem: For a recursively enumerable language it is undecidable to determine whether is finite Proof: We will reduce the.
Introduction to Algorithms 6.046J Lecture 1 Prof. Shafi Goldwasser Prof. Erik Demaine.
CS Main Questions Given that the computer is the Great Symbol Manipulator, there are three main questions in the field of computer science: What kinds.
Algorithms. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The main aim of this lecture.
Introduction to Algorithms Jiafen Liu Sept
Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 1 Prof. Charles E. Leiserson.
Algorithms Lecture 1. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The main aim of this.
Introduction to Algorithms Lecture 1. Introduction The methods of algorithm design form one of the core practical technologies of computer science. The.
CMPT 438 Algorithms. Why Study Algorithms? Necessary in any computer programming problem ▫Improve algorithm efficiency: run faster, process more data,
Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 1 Prof. Charles E. Leiserson.
CMPT 438 Algorithms.
Algorithms Presented By:- Mr. Anup Ashok Shinde BBA (C.A) Dept.
Presentation transcript:

Introduction to Algorithms 6.046J/18.401J LECTURE 1 Analysis of Algorithms Insertion sort Merge sort Prof. Charles E. Leiserson

Course information 1. Staff 2. Prerequisites 3. Lectures 4. Recitations 5. Handouts 6. Textbook (CLRS) 7. Extra help 8. Registration 9.Problem sets 10.Describing algorithms 11.Grading policy 12.Collaboration policy Course information handout © 2001–4 by Charles E. Leiserson September 8, 2004 Introduction to Algorithms

Analysis of algorithms The theoretical study of computer-program performance and resource usage. What’s more important than performance? modularity correctness maintainability functionality robustness user-friendliness programmer time simplicity extensibility reliability © 2001–4 by Charles E. Leiserson September 8, 2004 Introduction to Algorithms

Why study algorithms and performance? Algorithms help us to understand scalability. Performance often draws the line between what is feasible and what is impossible. Algorithmic mathematics provides a language for talking about program behavior. Performance is the currency of computing. The lessons of program performance generalize to other computing resources. Speed is fun! © 2001–4 by Charles E. Leiserson September 8, 2004 Introduction to Algorithms

The problem of sorting Input: sequence of numbers. Output: permutation such that Example: Input: 8 2 4 9 3 6 Output: 2 3 4 6 8 9 © 2001–4 by Charles E. Leiserson September 8, 2004 Introduction to Algorithms

Insertion sort “pseudocode” INSERTION-SORT September 8, 2004 © 2001–4 by Charles E. Leiserson September 8, 2004 Introduction to Algorithms

Insertion sort “pseudocode” INSERTION-SORT September 8, 2004 © 2001–4 by Charles E. Leiserson September 8, 2004 Introduction to Algorithms

Example of insertion sort 8 2 4 9 3 6 © 2001–4 by Charles E. Leiserson September 8, 2004 Introduction to Algorithms

Example of insertion sort © 2001–4 by Charles E. Leiserson September 8, 2004 Introduction to Algorithms

Example of insertion sort 8 2 4 9 3 6 © 2001–4 by Charles E. Leiserson September 8, 2004 Introduction to Algorithms

Example of insertion sort © 2001–4 by Charles E. Leiserson September 8, 2004 Introduction to Algorithms

Example of insertion sort © 2001–4 by Charles E. Leiserson September 8, 2004 Introduction to Algorithms

Example of insertion sort © 2001–4 by Charles E. Leiserson September 8, 2004 Introduction to Algorithms

Example of insertion sort © 2001–4 by Charles E. Leiserson September 8, 2004 Introduction to Algorithms

Example of insertion sort © 2001–4 by Charles E. Leiserson September 8, 2004 Introduction to Algorithms

Example of insertion sort © 2001–4 by Charles E. Leiserson September 8, 2004 Introduction to Algorithms

Example of insertion sort © 2001–4 by Charles E. Leiserson September 8, 2004 Introduction to Algorithms

Example of insertion sort done © 2001–4 by Charles E. Leiserson September 8, 2004 Introduction to Algorithms

Running time • The running time depends on the input: an already sorted sequence is easier to sort. • Parameterize the running time by the size of the input, since short sequences are easier to sort than long ones. • Generally, we seek upper bounds on the running time, because everybody likes a guarantee. © 2001–4 by Charles E. Leiserson September 8, 2004 Introduction to Algorithms

Kinds of 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. © 2001–4 by Charles E. Leiserson September 8, 2004 Introduction to Algorithms

Machine-independent time What is insertion sort’s worst-case time? It depends on the speed of our computer: relative speed (on the same machine), absolute speed (on different machines). BIG IDEA: • Ignore machine-dependent constants. • Look at growth of T(n) as n → ∞ . “Asymptotic Analysis” © 2001–4 by Charles E. Leiserson September 8, 2004 Introduction to Algorithms

Θ-notation Drop low-order terms; ignore leading constants. Math: Θ(g(n)) = { f (n) : there exist positive constants c1, c2, and n0 such that 0 ≤ c1 g(n) ≤ f (n) ≤ c2 g(n) for all n ≥ n0 } Engineering: Drop low-order terms; ignore leading constants. Example: 3n3 + 90n2 – 5n + 6046 = Θ(n3) © 2001–4 by Charles E. Leiserson September 8, 2004 Introduction to Algorithms

Asymptotic performance When n gets large enough, a Θ(n2) algorithm always beats a Θ(n3) algorithm. We shouldn’t ignore asymptotically slower algorithms, however. Real-world design situations often call for a careful balancing of engineering objectives. Asymptotic analysis is a useful tool to help to structure our thinking. © 2001–4 by Charles E. Leiserson September 8, 2004 Introduction to Algorithms

Insertion sort analysis Worst case: Input reverse sorted. [arithmetic series] Average case: All permutations equally likely. Is insertion sort a fast sorting algorithm? • Moderately so, for small n. • Not at all, for large n. © 2001–4 by Charles E. Leiserson September 8, 2004 Introduction to Algorithms

Merge sort MERGE-SORT A[1 . . n] 1. If n = 1, done. 2. Recursively sort A[ 1 . . n/2 ] and A[ n/2+1 . . n ] . 3. “Merge” the 2 sorted lists. Key subroutine: MERGE © 2001–4 by Charles E. Leiserson September 8, 2004 Introduction to Algorithms

Merging two sorted arrays © 2001–4 by Charles E. Leiserson September 8, 2004 Introduction to Algorithms

Merging two sorted arrays © 2001–4 by Charles E. Leiserson September 8, 2004 Introduction to Algorithms

Merging two sorted arrays © 2001–4 by Charles E. Leiserson September 8, 2004 Introduction to Algorithms

Merging two sorted arrays © 2001–4 by Charles E. Leiserson September 8, 2004 Introduction to Algorithms

Merging two sorted arrays © 2001–4 by Charles E. Leiserson September 8, 2004 Introduction to Algorithms

Merging two sorted arrays © 2001–4 by Charles E. Leiserson September 8, 2004 Introduction to Algorithms

Merging two sorted arrays © 2001–4 by Charles E. Leiserson September 8, 2004 Introduction to Algorithms

Merging two sorted arrays © 2001–4 by Charles E. Leiserson September 8, 2004 Introduction to Algorithms

Merging two sorted arrays © 2001–4 by Charles E. Leiserson September 8, 2004 Introduction to Algorithms

Merging two sorted arrays © 2001–4 by Charles E. Leiserson September 8, 2004 Introduction to Algorithms

Merging two sorted arrays © 2001–4 by Charles E. Leiserson September 8, 2004 Introduction to Algorithms

Merging two sorted arrays © 2001–4 by Charles E. Leiserson September 8, 2004 Introduction to Algorithms

Merging two sorted arrays Time = Θ(n) to merge a total of n elements (linear time). © 2001–4 by Charles E. Leiserson September 8, 2004 Introduction to Algorithms

Analyzing merge sort MERGE-SORT A[1 . . n] 1. If n = 1, done. 2. Recursively sort A[ 1 . . n/2 ] and A[ n/2+1 . . n ] . 3. “Merge” the 2 sorted lists Abuse Sloppiness: Should be , but it turns out not to matter asymptotically. © 2001–4 by Charles E. Leiserson September 8, 2004 Introduction to Algorithms

Recurrence for merge sort • We shall usually omit stating the base case when T(n) = Θ(1) for sufficiently small n, but only when it has no effect on the asymptotic solution to the recurrence. • CLRS and Lecture 2 provide several ways to find a good upper bound on T(n). © 2001–4 by Charles E. Leiserson September 8, 2004 Introduction to Algorithms

Recursion tree Solve T(n) = 2T(n/2) + cn, where c > 0 is constant. © 2001–4 by Charles E. Leiserson September 8, 2004 Introduction to Algorithms

Recursion tree Solve T(n) = 2T(n/2) + cn, where c > 0 is constant. T(n) © 2001–4 by Charles E. Leiserson September 8, 2004 Introduction to Algorithms

Recursion tree Solve T(n) = 2T(n/2) + cn, where c > 0 is constant. © 2001–4 by Charles E. Leiserson September 8, 2004 Introduction to Algorithms

Recursion tree Solve T(n) = 2T(n/2) + cn, where c > 0 is constant. © 2001–4 by Charles E. Leiserson September 8, 2004 Introduction to Algorithms

Recursion tree Solve T(n) = 2T(n/2) + cn, where c > 0 is constant. © 2001–4 by Charles E. Leiserson September 8, 2004 Introduction to Algorithms

Recursion tree Solve T(n) = 2T(n/2) + cn, where c > 0 is constant. © 2001–4 by Charles E. Leiserson September 8, 2004 Introduction to Algorithms

Recursion tree Solve T(n) = 2T(n/2) + cn, where c > 0 is constant. © 2001–4 by Charles E. Leiserson September 8, 2004 Introduction to Algorithms

Recursion tree Solve T(n) = 2T(n/2) + cn, where c > 0 is constant. © 2001–4 by Charles E. Leiserson September 8, 2004 Introduction to Algorithms

Recursion tree Solve T(n) = 2T(n/2) + cn, where c > 0 is constant. © 2001–4 by Charles E. Leiserson September 8, 2004 Introduction to Algorithms

Recursion tree Solve T(n) = 2T(n/2) + cn, where c > 0 is constant. © 2001–4 by Charles E. Leiserson September 8, 2004 Introduction to Algorithms

Recursion tree Solve T(n) = 2T(n/2) + cn, where c > 0 is constant. © 2001–4 by Charles E. Leiserson September 8, 2004 Introduction to Algorithms

Conclusions • Θ(n lg n) grows more slowly than Θ(n2). • 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! © 2001–4 by Charles E. Leiserson September 8, 2004 Introduction to Algorithms