Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 1 Prof. Charles E. Leiserson.

Slides:



Advertisements
Similar presentations
Analysis of Computer Algorithms
Advertisements

Introduction to Algorithms 6.046J/18.401J
Introduction to Algorithms 6.046J/18.401J
Introduction to Algorithms 6.046J Lecture 1 Prof. Shafi Goldwasser Prof. Erik Demaine.
Analysis of Algorithms
ADA: 5. Quicksort1 Objective o describe the quicksort algorithm, it's partition function, and analyse its running time under different data conditions.
Introduction to Algorithms Jiafen Liu Sept
Chapter 2. Getting Started. Outline Familiarize you with the to think about the design and analysis of algorithms Familiarize you with the framework to.
A Basic Study on the Algorithm Analysis Chapter 2. Getting Started 한양대학교 정보보호 및 알고리즘 연구실 이재준 담당교수님 : 박희진 교수님 1.
Spring 2015 Lecture 5: QuickSort & Selection
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
1 SORTING Dan Barrish-Flood. 2 heapsort made file “3-Sorting-Intro-Heapsort.ppt”
CS421 - Course Information Website Syllabus Schedule The Book:
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 2 Elements of complexity analysis Performance and efficiency Motivation: analysis.
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.
Analysis of Algorithms 7/2/2015CS202 - Fundamentals of Computer Science II1.
Analysis of Algorithms COMP171 Fall Analysis of Algorithms / Slide 2 Introduction * What is Algorithm? n a clearly specified set of simple instructions.
Introduction CIS 606 Spring The sorting problem Input: A sequence of n numbers 〈 a 1, a 2, …, a n 〉. Output: A permutation (reordering) 〈 a’ 1,
Unit 1. Sorting and Divide and Conquer. Lecture 1 Introduction to Algorithm and Sorting.
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 Algorithm design and analysis
Introduction to Algorithms Jiafen Liu Sept
CSC 201 Analysis and Design of Algorithms Lecture 03: Introduction to a CSC 201 Analysis and Design of Algorithms Lecture 03: Introduction to a lgorithms.
Mon 29 Sep 2014Lecture 4 1. Running Time Performance analysis Techniques until now: Experimental Cost models counting execution of operations or lines.
{ CS203 Lecture 7 John Hurley Cal State LA. 2 Execution Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
Introduction to Algorithms Jiafen Liu Sept
Lecture 2 Computational Complexity
Algorithm Efficiency CS 110: Data Structures and Algorithms First Semester,
1 Computer Algorithms Lecture 3 Asymptotic Notation Some of these slides are courtesy of D. Plaisted, UNC and M. Nicolescu, UNR.
CS 3343: Analysis of Algorithms
Algorithm Analysis An algorithm is a clearly specified set of simple instructions to be followed to solve a problem. Three questions for algorithm analysis.
2IL50 Data Structures Fall 2015 Lecture 2: Analysis of Algorithms.
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
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.
Lecture 2 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
File Organization and Processing Week 13 Divide and Conquer.
The Selection Problem. 2 Median and Order Statistics In this section, we will study algorithms for finding the i th smallest element in a set of n elements.
CMPT 438 Algorithms. Why Study Algorithms? Necessary in any computer programming problem ▫Improve algorithm efficiency: run faster, process more data,
A Lecture /24/2015 COSC3101A: Design and Analysis of Algorithms Tianying Ji Lecture 1.
DR. Gatot F. Hertono, MSc. Design and Analysis of ALGORITHM (Session 2)
ALGORITHMS THIRD YEAR BANHA UNIVERSITY FACULTY OF COMPUTERS AND INFORMATIC Lecture three Dr. Hamdy M. Mousa.
Introduction to Algorithms Jiafen Liu Sept
1Computer Sciences Department. Book: Introduction to Algorithms, by: Thomas H. Cormen Charles E. Leiserson Ronald L. Rivest Clifford Stein Electronic:
Algorithm Analysis CS 400/600 – Data Structures. Algorithm Analysis2 Abstract Data Types Abstract Data Type (ADT): a definition for a data type solely.
1/6/20161 CS 3343: Analysis of Algorithms Lecture 2: Asymptotic Notations.
Foundations II: Data Structures and Algorithms
Spring 2015 Lecture 2: Analysis of Algorithms
Introduction to Algorithms (2 nd edition) by Cormen, Leiserson, Rivest & Stein Chapter 2: Getting Started.
Introduction to Complexity Analysis. Computer Science, Silpakorn University 2 Complexity of Algorithm algorithm คือ ขั้นตอนการคำนวณ ที่ถูกนิยามไว้อย่างชัดเจนโดยจะ.
E.G.M. PetrakisAlgorithm Analysis1  Algorithms that are equally correct can vary in their utilization of computational resources  time and memory  a.
1 Ch. 2: Getting Started. 2 About this lecture Study a few simple algorithms for sorting – Insertion Sort – Selection Sort (Exercise) – Merge Sort Show.
Lecture 2 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
2IL50 Data Structures Spring 2016 Lecture 2: Analysis of Algorithms.
Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 1 Prof. Charles E. Leiserson.
BITS Pilani Pilani Campus Data Structure and Algorithms Design Dr. Maheswari Karthikeyan Lecture1.
1 Asymptotes: Why? How to describe an algorithm’s running time? (or space, …) How does the running time depend on the input? T(x) = running time for instance.
CS6045: Advanced Algorithms Sorting Algorithms. Sorting Input: sequence of numbers Output: a sorted sequence.
Algorithm Complexity is concerned about how fast or slow particular algorithm performs.
Lecture 2 Algorithm Analysis
CMPT 438 Algorithms.
Introduction to Algorithms
分治法.
Introduction to Algorithms Prof. Charles E. Leiserson
Unit 1. Sorting and Divide and Conquer
CS 3343: Analysis of Algorithms
CS 3343: Analysis of Algorithms
CS 3343: Analysis of Algorithms
Algorithms Presented By:- Mr. Anup Ashok Shinde BBA (C.A) Dept.
Presentation transcript:

Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 1 Prof. Charles E. Leiserson

Welcome to Introduction to Algorithms, Fall 2001 Handouts 1. Course Information 2. Calendar 3. Registration (MIT students only) 4. References 5. Objectives and Outcomes 6. Diagnostic Survey

Course information 1. Staff8. Website 2. Distance learning 9. Extra help 3. Prerequisites10.Registration (MIT only) 4. Lectures11.Problem sets 5. Recitations12.Describing algorithms 6. Handouts13.Grading policy 7. Textbook (CLRS) 14.Collaboration policy ▲ Course information handout

Analysis of algorithms The theoretical study of computer-program performance and resource usage. What ’ s more important than performance? modularity user-friendliness correctness programmer time maintainability simplicity functionality extensibility robustness reliability

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. The lessons of program performance generalize to other computing resources. Speed is fun!

The problem of sorting Input: sequence of numbers. Output: permutation such that a'1 ≤ a'2 ≤ … ≤ a'n Example: Input: Output:

Insertion sort

Example of insertion sort

Example of insertion sort

Example of insertion sort

Example of insertion sort

Example of insertion sort

Example of insertion sort

Example of insertion sort

Example of insertion sort

Example of insertion sort

Example of insertion sort

Example of insertion sort done

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.

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.

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”

Θ-notation 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 = Θ(n3)

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.

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.

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

Merging two sorted

Merging two sorted

Merging two sorted

Merging two sorted

Merging two sorted

Merging two sorted arrays

Merging two sorted arrays

○ Merging two sorted arrays

Merging two sorted arrays

Merging two sorted arrays

Merging two sorted arrays

Merging two sorted arrays

Merging two sorted arrays Time = Θ(n) to merge a total of n elements (linear time).

Analyzing merge sort T(n) MERGE-SORT A[1.. n] Θ(1) 1. If n = 1, done. 2T(n/2) 2. Recursively sort A[ 1.. n/2 ] Abuse and A[ n/2+1.. n ]. Θ(n) 3. “ Merge ” the 2 sorted lists Sloppiness: Should be T( n/2 ) + T( n/2 ), but it turns out not to matter asymptotically.

Recurrence for merge sort T(n) = Θ(1) if n = 1; 2T(n/2) + Θ(n) if n > 1. 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).

Recursion tree Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.

Recursion tree Solve T(n) = 2T(n/2) + cn, where c > 0 is constant. T(n)

Recursion tree Solve T(n) = 2T(n/2) + cn, where c > 0 is constant. cn T(n/2)

Recursion tree Solve T(n) = 2T(n/2) + cn, where c > 0 is constant. cn cn/2 cn/2 T(n/4) T(n/4)

Recursion tree Solve T(n) = 2T(n/2) + cn, where c > 0 is constant. cn cn/2 cn/2 T(n/4) T(n/4) T(n/4) T(n/4) Θ(1)

Recursion tree Solve T(n) = 2T(n/2) + cn, where c > 0 is constant. cn cn/2 cn/2 h = lg n T(n/4) T(n/4) T(n/4) T(n/4) Θ(1)

Recursion tree Solve T(n) = 2T(n/2) + cn, where c > 0 is constant. cn cn cn/2 cn/2 h = lg n T(n/4) T(n/4) T(n/4) T(n/4) Θ(1)

Recursion tree Solve T(n) = 2T(n/2) + cn, where c > 0 is constant. cn cn cn/2 cn/2 cn h = lg n T(n/4) T(n/4) T(n/4) T(n/4) Θ(1)

Recursion tree Solve T(n) = 2T(n/2) + cn, where c > 0 is constant. cn cn cn/2 cn/2 cn h = lg n T(n/4) T(n/4) T(n/4) T(n/4) cn Θ(1)

Recursion tree Solve T(n) = 2T(n/2) + cn, where c > 0 is constant. cn cn cn/2 cn/2 cn h = lg n T(n/4) T(n/4) T(n/4) T(n/4) cn Θ(1) Θ(n) # leaves = n

Recursion tree Solve T(n) = 2T(n/2) + cn, where c > 0 is constant. cn cn cn/2 cn/2 cn h = lg n T(n/4) T(n/4) T(n/4) T(n/4) cn Θ(n) Θ(1) Θ(n lg n) # leaves = n

Conclusions Θ(n lg n) grows more slowly than Θ( n 2). 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!