Download presentation

Presentation is loading. Please wait.

Published byMaya Coppin Modified over 2 years ago

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

2
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

3
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

4
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.

5
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

6
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

7
L1.7 The problem of sorting Input: sequence a 1, a 2, …, a n of numbers. Example: Input: 8 2 4 9 3 6 Output: 2 3 4 6 8 9 Output: permutation a' 1, a' 2, …, a' n such that a' 1 a' 2 … a' n.

8
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

9
L1.9 Example of insertion sort 824936

10
L1.10 Example of insertion sort 824936

11
L1.11 Example of insertion sort 824936 284936

12
L1.12 Example of insertion sort 824936 284936

13
L1.13 Example of insertion sort 824936 284936 248936

14
L1.14 Example of insertion sort 824936 284936 248936

15
L1.15 Example of insertion sort 824936 284936 248936 248936

16
L1.16 Example of insertion sort 824936 284936 248936 248936

17
L1.17 Example of insertion sort 824936 284936 248936 248936 234896

18
L1.18 Example of insertion sort 824936 284936 248936 248936 234896

19
L1.19 Example of insertion sort 824936 284936 248936 248936 234896 234689done

20
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.

21
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.

22
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”

23
L1.23 -notation Drop low-order terms; ignore leading constants. Example: 3n 3 + 90n 2 – 5n + 6046 = (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:

24
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.

25
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]

26
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 )

27
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 )

28
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

29
L1.29 Merging two sorted arrays 20 13 7 2 12 11 9 1

30
L1.30 Merging two sorted arrays 20 13 7 2 12 11 9 1 1

31
L1.31 Merging two sorted arrays 20 13 7 2 12 11 9 1 1 20 13 7 2 12 11 9

32
L1.32 Merging two sorted arrays 20 13 7 2 12 11 9 1 1 20 13 7 2 12 11 9 2

33
L1.33 Merging two sorted arrays 20 13 7 2 12 11 9 1 1 20 13 7 2 12 11 9 2 20 13 7 12 11 9

34
L1.34 Merging two sorted arrays 20 13 7 2 12 11 9 1 1 20 13 7 2 12 11 9 2 20 13 7 12 11 9 7

35
L1.35 Merging two sorted arrays 20 13 7 2 12 11 9 1 1 20 13 7 2 12 11 9 2 20 13 7 12 11 9 7 20 13 12 11 9

36
L1.36 Merging two sorted arrays 20 13 7 2 12 11 9 1 1 20 13 7 2 12 11 9 2 20 13 7 12 11 9 7 20 13 12 11 9 9

37
L1.37 Merging two sorted arrays 20 13 7 2 12 11 9 1 1 20 13 7 2 12 11 9 2 20 13 7 12 11 9 7 20 13 12 11 9 9 20 13 12 11

38
L1.38 Merging two sorted arrays 20 13 7 2 12 11 9 1 1 20 13 7 2 12 11 9 2 20 13 7 12 11 9 7 20 13 12 11 9 9 20 13 12 11

39
L1.39 Merging two sorted arrays 20 13 7 2 12 11 9 1 1 20 13 7 2 12 11 9 2 20 13 7 12 11 9 7 20 13 12 11 9 9 20 13 12 11 20 13 12

40
L1.40 Merging two sorted arrays 20 13 7 2 12 11 9 1 1 20 13 7 2 12 11 9 2 20 13 7 12 11 9 7 20 13 12 11 9 9 20 13 12 11 20 13 12

41
L1.41 Merging two sorted arrays 20 13 7 2 12 11 9 1 1 20 13 7 2 12 11 9 2 20 13 7 12 11 9 7 20 13 12 11 9 9 20 13 12 11 20 13 12 Time = (n) to merge a total of n elements (linear time).

42
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.

43
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).

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

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

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

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

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

49
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

50
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

51
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

52
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 …

53
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) …

54
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) …

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

Similar presentations

OK

Introduction to Algorithms (2 nd edition) by Cormen, Leiserson, Rivest & Stein Chapter 2: Getting Started.

Introduction to Algorithms (2 nd edition) by Cormen, Leiserson, Rivest & Stein Chapter 2: Getting Started.

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on tv engineering Converter word para ppt online Ppt on personal grooming and etiquette Ppt on review writing job Ppt on media research institute Ppt on tourism in rajasthan Simple ppt on fossil fuel for students Ppt on conceptual art emphasizes Ppt on review of related literature introduction Ppt on interest rate parity