Efficiency and Computational Complexity (Part 2) Rate of growth of - - PowerPoint PPT Presentation

efficiency and computational complexity part 2 rate of
SMART_READER_LITE
LIVE PREVIEW

Efficiency and Computational Complexity (Part 2) Rate of growth of - - PowerPoint PPT Presentation

Efficiency and Computational Complexity (Part 2) Rate of growth of functions Source:https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer- 2


slide-1
SLIDE 1

Efficiency and Computational Complexity (Part 2)

slide-2
SLIDE 2

Rate of growth of functions

2 Source:https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer- science-and-programming-in-python-fall-2016/lecture-slides-code/

slide-3
SLIDE 3

3 Source:https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer- science-and-programming-in-python-fall-2016/lecture-slides-code/

slide-4
SLIDE 4

4 Source:https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer- science-and-programming-in-python-fall-2016/lecture-slides-code/

About Big O notation

slide-5
SLIDE 5

5 Source:https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer- science-and-programming-in-python-fall-2016/lecture-slides-code/

About Big O notation

slide-6
SLIDE 6

6 Source:https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer- science-and-programming-in-python-fall-2016/lecture-slides-code/

About Big O notation

slide-7
SLIDE 7

7 Source:https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer- science-and-programming-in-python-fall-2016/lecture-slides-code/

About Big O notation

slide-8
SLIDE 8

8 Source:https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer- science-and-programming-in-python-fall-2016/lecture-slides-code/

About Big O notation

slide-9
SLIDE 9

9 Source:https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer- science-and-programming-in-python-fall-2016/lecture-slides-code/

About Big O notation

slide-10
SLIDE 10

10

Merging two sorted lists (arrays)

Given two sorted arrays, merge them to get a single ordered array

p q p+q

Reference RG Dromey book

slide-11
SLIDE 11

11

8 11 16 17 44 b 1 2 3 4 15 18 42 51 a 1 2 3 58 71 74 6 7 5 i j c 1 2 3 k 5 6 7 4 9 10 11 8

Merging two sorted lists (arrays)

B[j] < A[i]

slide-12
SLIDE 12

12

8 11 16 17 44 b 1 2 3 4 15 18 42 51 a 1 2 3 58 71 74 6 7 5 i j 8 c 1 2 3 k 5 6 7 4 9 10 11 8

Merging two sorted lists (arrays)

B[j] < A[i] C[k]=B[j]

slide-13
SLIDE 13

13

8 11 16 17 44 b 1 2 3 4 15 18 42 51 a 1 2 3 58 71 74 6 7 5 i j 8 c 1 2 3 k 5 6 7 4 9 10 11 8

Merging two sorted lists (arrays)

j=j+1 k=k+1

slide-14
SLIDE 14

14

8 11 16 17 44 b 1 2 3 4 15 18 42 51 a 1 2 3 58 71 74 6 7 5 i j 8 11 c 1 2 3 k 5 6 7 4 9 10 11 8

Merging two sorted lists (arrays)

B[j] < A[i] C[k]=B[j]

slide-15
SLIDE 15

15

8 11 16 17 44 b 1 2 3 4 15 18 42 51 a 1 2 3 58 71 74 6 7 5 i j 8 11 c 1 2 3 k 5 6 7 4 9 10 11 8

Merging two sorted lists (arrays)

j=j+1 k=k+1

slide-16
SLIDE 16

16

8 11 16 17 44 b 1 2 3 4 15 18 42 51 a 1 2 3 58 71 74 6 7 5 i j 8 11 15 c 1 2 3 k 5 6 7 4 9 10 11 8

Merging two sorted lists (arrays)

A[i] < B[j] C[k]=A[i]

slide-17
SLIDE 17

17

8 11 16 17 44 b 1 2 3 4 15 18 42 51 a 1 2 3 58 71 74 6 7 5 i j 8 11 15 c 1 2 3 k 5 6 7 4 9 10 11 8

Merging two sorted lists (arrays)

i=i+1 k=k+1

slide-18
SLIDE 18

18

8 11 16 17 44 b 1 2 3 4 15 18 42 51 a 1 2 3 58 71 74 6 7 5 i j 8 11 15 16 c 1 2 3 k 5 6 7 4 9 10 11 8

Merging two sorted lists (arrays)

B[j] < A[i] C[k]=B[j]

slide-19
SLIDE 19

19

8 11 16 17 44 b 1 2 3 4 15 18 42 51 a 1 2 3 58 71 74 6 7 5 i j 8 11 15 16 c 1 2 3 k 5 6 7 4 9 10 11 8

Merging two sorted lists (arrays)

j=j+1 k=k+1

slide-20
SLIDE 20

20

8 11 16 17 44 b 1 2 3 4 15 18 42 51 a 1 2 3 58 71 74 6 7 5 i j 8 11 15 16 c 1 2 3 k 17 5 6 7 4 9 10 11 8

Merging two sorted lists (arrays)

B[j] < A[i] C[k]=B[j]

slide-21
SLIDE 21

21

8 11 16 17 44 b 1 2 3 4 15 18 42 51 a 1 2 3 58 71 74 6 7 5 i j 8 11 15 16 c 1 2 3 k 17 5 6 7 4 9 10 11 8

Merging two sorted lists (arrays)

j=j+1 k=k+1

slide-22
SLIDE 22

22

8 11 16 17 44 b 1 2 3 4 15 18 42 51 a 1 2 3 58 71 74 6 7 5 i j 8 11 15 16 c 1 2 3 k 17 18 5 6 7 4 9 10 11 8

Merging two sorted lists (arrays)

A[i] < B[j] C[k]=A[i]

slide-23
SLIDE 23

23

8 11 16 17 44 b 1 2 3 4 15 18 42 51 a 1 2 3 58 71 74 6 7 5 i j 8 11 15 16 c 1 2 3 k 17 18 5 6 7 4 9 10 11 8

Merging two sorted lists (arrays)

i=i+1 k=k+1

slide-24
SLIDE 24

24

8 11 16 17 44 b 1 2 3 4 15 18 42 51 a 1 2 3 58 71 74 6 7 5 i j 8 11 15 16 c 1 2 3 k 17 18 42 5 6 7 4 9 10 11 8

Merging two sorted lists (arrays)

A[i] < B[j] C[k]=A[i]

slide-25
SLIDE 25

25

8 11 16 17 44 b 1 2 3 4 15 18 42 51 a 1 2 3 58 71 74 6 7 5 i j 8 11 15 16 c 1 2 3 k 17 18 42 5 6 7 4 9 10 11 8

Merging two sorted lists (arrays)

i=i+1 k=k+1

slide-26
SLIDE 26

26

8 11 16 17 44 b 1 2 3 4 15 18 42 51 a 1 2 3 58 71 74 6 7 5 i j 8 11 15 16 c 1 2 3 k 17 18 42 44 5 6 7 4 9 10 11 8

Merging two sorted lists (arrays)

B[j] < A[i] C[k]=B[j]

slide-27
SLIDE 27

27

8 11 16 17 44 b 1 2 3 4 15 18 42 51 a 1 2 3 58 71 74 6 7 5 i j 8 11 15 16 c 1 2 3 k 17 18 42 44 5 6 7 4 9 10 11 8

Merging two sorted lists (arrays)

j=j+1 k=k+1

slide-28
SLIDE 28

28

8 11 16 17 44 b 1 2 3 4 15 18 42 51 a 1 2 3 58 71 74 6 7 5 i j 8 11 15 16 c 1 2 3 k 17 18 42 44 5 6 7 4 51 9 10 11 8

Merging two sorted lists (arrays)

A[i] < B[j] C[k]=A[i]

slide-29
SLIDE 29

29

8 11 16 17 44 b 1 2 3 4 15 18 42 51 a 1 2 3 58 71 74 6 7 5 i j 8 11 15 16 c 1 2 3 k 17 18 42 44 5 6 7 4 51 9 10 11 8

Merging two sorted lists (arrays)

k=k+1

slide-30
SLIDE 30

30

8 11 16 17 44 b 1 2 3 4 15 18 42 51 a 1 2 3 58 71 74 6 7 5 i j 8 11 15 16 c 1 2 3 k 17 18 42 44 5 6 7 4 51 58 71 74 9 10 11 8

Merging two sorted lists (arrays)

copy the remaining B[j] to C[k]

slide-31
SLIDE 31

31

Merging two sorted lists (arrays)

# Algorithm Merge def merge(A, B): C=[] i,j=0,0 while ( (i<len(A)) and (j<len(B)) ): if (A[i] <= B[j]): C.append(A[i]) i+=1 else: C.append(B[j]) j+=1 C += A[i:] C += B[j:] return C

Order of time complexity O(p+q) p=len(A) q=len(B) O(n)

slide-32
SLIDE 32

32

Partitioning

Given a randomly ordered array of n elements, partition the elements into two subsets such that elements <= x are in one subset and elements > x are in the other set. x = 17 Reference RG Dromey book

slide-33
SLIDE 33

33

Partitioning

Note: In place partitioning another array/list not allowed Approach:

slide-34
SLIDE 34

34

Partitioning

def partition(arr,x): i = 0 # leftcounter j = len(arr)-1 # rightcounter while i < j and arr[i] <= x: # postion for the first left counter i += 1 while i < j and arr[j] > x: # position for the first right counter j -= 1 if arr[j]>x: # special case when x is less than all elements j -= 1 while i<j: temp = arr[i] arr[i] = arr[j] arr[j] = temp i+=1 j-=1 while arr[i] <= x: i += 1 while arr[j] > x: j -= 1 return j

slide-35
SLIDE 35

35

Partitioning

def partition(arr,x): i = 0 # leftcounter j = len(arr)-1 # rightcounter while i < j and arr[i] <= x: # postion for the first left counter i += 1 while i < j and arr[j] > x: # position for the first right counter j -= 1 if arr[j]>x: # special case when x is less than all elements j -= 1 while i<j: temp = arr[i] arr[i] = arr[j] arr[j] = temp i+=1 j-=1 while arr[i] <= x: i += 1 while arr[j] > x: j -= 1 return j

Order of time complexity O(n)