Introduction to Algorithms 6.046J Lecture 1 Prof. Shafi Goldwasser Prof. Erik Demaine.

Slides:



Advertisements
Similar presentations
2. Getting Started Heejin Park College of Information and Communications Hanyang University.
Advertisements

Introduction to Algorithms 6.046J/18.401J
Introduction to Algorithms 6.046J/18.401J
Growth-rate Functions
Introduction to Algorithms 6.046J
Analysis of Algorithms
Chapter 2. Getting Started. Outline Familiarize you with the to think about the design and analysis of algorithms Familiarize you with the framework to.
Spring 2015 Lecture 5: QuickSort & Selection
What is an Algorithm? (And how do we analyze one?)
Introduction to Analysis of Algorithms
CS421 - Course Information Website Syllabus Schedule The Book:
CS107 Introduction to Computer Science
CSE 830: Design and Theory of Algorithms Dr. Eric Torng.
CS3381 Des & Anal of Alg ( SemA) City Univ of HK / Dept of CS / Helena Wong 2. Analysis of Algorithms - 1 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.
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
Analysis of Algorithms Algorithm Input Output © 2014 Goodrich, Tamassia, Goldwasser1Analysis of Algorithms Presentation for use with the textbook Data.
1 Chapter 24 Developing Efficient Algorithms. 2 Executing Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
{ 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.
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
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.
Lecture 2 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
File Organization and Processing Week 13 Divide and Conquer.
Analyzing algorithms & Asymptotic Notation BIO/CS 471 – Algorithms for Bioinformatics.
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.
Divide & Conquer  Themes  Reasoning about code (correctness and cost)  recursion, induction, and recurrence relations  Divide and Conquer  Examples.
Getting Started Introduction to Algorithms Jeff Chastine.
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.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part 1. Complexity Bounds.
1/6/20161 CS 3343: Analysis of Algorithms Lecture 2: Asymptotic Notations.
Midterm Review 1. Midterm Exam Thursday, October 15 in classroom 75 minutes Exam structure: –TRUE/FALSE questions –short questions on the topics discussed.
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 คือ ขั้นตอนการคำนวณ ที่ถูกนิยามไว้อย่างชัดเจนโดยจะ.
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.
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.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
Lecture 2 Algorithm Analysis
CMPT 438 Algorithms.
分治法.
Introduction to Algorithms Prof. Charles E. Leiserson
Unit 1. Sorting and Divide and Conquer
CS 3343: Analysis of Algorithms
Algorithms Algorithm Analysis.
CS 3343: Analysis of Algorithms
Analysis of Algorithms
CS 3343: Analysis of Algorithms
Algorithms + Data Structures = Programs -Niklaus Wirth
Introduction to Algorithms 6.046J
Algorithms + Data Structures = Programs -Niklaus Wirth
Ch 2: Getting Started Ming-Te Chi
Ch. 2: Getting Started.
Introduction To Algorithms
Algorithms Presented By:- Mr. Anup Ashok Shinde BBA (C.A) Dept.
Presentation transcript:

Introduction to Algorithms 6.046J Lecture 1 Prof. Shafi Goldwasser Prof. Erik Demaine

L1.2 Welcome to Introduction to Algorithms, Spring 2004 Handouts 1.Course InformationCourse Information 2.Course CalendarCalendar 3.Problem Set 1 4.Akra-Bazzi Handout

L1.3 Course information 1.StaffStaff 2.Prerequisites 3.Lectures & RecitationsRecitations 4.HandoutsHandouts 5.Textbook (CLRS)Textbook (CLRS) 6.Website 8.Extra Help 9.Registration 10.Problem sets 11.Describing algorithms 12.Grading policy 13.Collaboration policyCollaboration policy

L1.4 What is course about? The theoretical study of design and analysis of computer algorithms Basic goals for an algorithm: always correct always terminates This class: performance  Performance often draws the line between what is possible and what is impossible.

L1.5 Design and Analysis of Algorithms Analysis: predict the cost of an algorithm in terms of resources and performance Design: design algorithms which minimize the cost

L1.6 Our Machine Model Generic Random Access Machine (RAM) Executes operations sequentially Set of primitive operations: –Arithmetic. Logical, Comparisons, Function calls Simplifying assumption: all ops cost 1 unit –Eliminates dependence on the speed of our computer, otherwise impossible to verify and to compare

L1.7 The problem of sorting Input: sequence  a 1, a 2, …, a n  of numbers. Example: Input: Output: Output: permutation  a' 1, a' 2, …, a' n  such that a' 1  a' 2  …  a' n.

L1.8 Insertion sort I NSERTION -S ORT (A, n) ⊳ A[1.. n] for j ← 2 to n dokey ← A[ j] i ← j – 1 while i > 0 and A[i] > key doA[i+1] ← A[i] i ← i – 1 A[i+1] = key “pseudocode” ij key sorted A:A: 1n

L1.9 Example of insertion sort

L1.10 Example of insertion sort

L1.11 Example of insertion sort

L1.12 Example of insertion sort

L1.13 Example of insertion sort

L1.14 Example of insertion sort

L1.15 Example of insertion sort

L1.16 Example of insertion sort

L1.17 Example of insertion sort

L1.18 Example of insertion sort

L1.19 Example of insertion sort done

L1.20 Running time The running time depends on the input: an already sorted sequence is easier to sort. Major Simplifying Convention: Parameterize the running time by the size of the input, since short sequences are easier to sort than long ones.  T A (n) = time of A on length n inputs Generally, we seek upper bounds on the running time, to have a guarantee of performance.

L1.21 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: (NEVER) Cheat with a slow algorithm that works fast on some input.

L1.22 Machine-independent time What is insertion sort’s worst-case time? B IG I DEAS : Ignore machine dependent constants, otherwise impossible to verify and to compare algorithms Look at growth of T(n) as n → ∞. “Asymptotic Analysis”

L1.23  -notation Drop low-order terms; ignore leading constants. Example: 3n n 2 – 5n =  (n 3 ) DEF:  (g(n)) = { f (n) :there exist positive constants c 1, c 2, and n 0 such that 0  c 1 g(n)  f (n)  c 2 g(n) for all n  n 0 } Basic manipulations:

L1.24 Asymptotic performance n T(n)T(n) n0n0. Asymptotic analysis is a useful tool to help to structure our thinking toward better algorithm We shouldn’t ignore asymptotically slower algorithms, however. Real-world design situations often call for a careful balancing When n gets large enough, a  (n 2 ) algorithm always beats a  (n 3 ) algorithm.

L1.25 Insertion sort analysis Worst case: Input reverse sorted. Average case: All permutations equally likely. Is insertion sort a fast sorting algorithm? Moderately so, for small n. Not at all, for large n. [arithmetic series]

L1.26 Example 2: Integer Multiplication Let X = A B and Y = C D where A,B,C and D are n/2 bit integers Simple Method: XY = (2 n/2 A+B)(2 n/2 C+D) Running Time Recurrence T(n) < 4T(n/2) + 100n Solution T(n) =  (n 2 )

L1.27 Better Integer Multiplication Let X = A B and Y = C D where A,B,C and D are n/2 bit integers Karatsuba: XY = (2 n/2 +2 n )AC+2 n/ 2(A-B)(C-D) + (2 n/2 +1) BD Running Time Recurrence T(n) < 3T(n/2) + 100n Solution:  (n) = O(n log 3 )

L1.28 Example 3:Merge sort M ERGE -S ORT 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: M ERGE

L1.29 Merging two sorted arrays

L1.30 Merging two sorted arrays

L1.31 Merging two sorted arrays

L1.32 Merging two sorted arrays

L1.33 Merging two sorted arrays

L1.34 Merging two sorted arrays

L1.35 Merging two sorted arrays

L1.36 Merging two sorted arrays

L1.37 Merging two sorted arrays

L1.38 Merging two sorted arrays

L1.39 Merging two sorted arrays

L1.40 Merging two sorted arrays

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

L1.42 Analyzing merge sort M ERGE -S ORT 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 T(n)  (1) 2T(n/2)  (n) Sloppiness: Should be T(  n/2  ) + T(  n/2  ), but it turns out not to matter asymptotically.

L1.43 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. Lecture 2 provides several ways to find a good upper bound on T(n).

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

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

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

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

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

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

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

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

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

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

L1.54 Recursion tree Solve T(n) = 2T(n/2) + cn, where c > 0 is constant. cn cn/4 cn/2  (1) … h = lg n cn #leaves = n (n)(n) Total  (n lg n) …

L1.55 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.