Efficiency and Computational Complexity (Part 2)pkalra/col100/slides/11...35 Partitioning def...

35
Efficiency and Computational Complexity (Part 2)

Transcript of Efficiency and Computational Complexity (Part 2)pkalra/col100/slides/11...35 Partitioning def...

Page 1: Efficiency and Computational Complexity (Part 2)pkalra/col100/slides/11...35 Partitioning def partition(arr,x): i = 0 # leftcounter j = len(arr)-1 # rightcounter while i < j and

Efficiency and Computational Complexity

(Part 2)

Page 2: Efficiency and Computational Complexity (Part 2)pkalra/col100/slides/11...35 Partitioning def partition(arr,x): i = 0 # leftcounter j = len(arr)-1 # rightcounter while i < j and

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/

Page 3: Efficiency and Computational Complexity (Part 2)pkalra/col100/slides/11...35 Partitioning def partition(arr,x): i = 0 # leftcounter j = len(arr)-1 # rightcounter while i < j and

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/

Page 4: Efficiency and Computational Complexity (Part 2)pkalra/col100/slides/11...35 Partitioning def partition(arr,x): i = 0 # leftcounter j = len(arr)-1 # rightcounter while i < j and

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

Page 5: Efficiency and Computational Complexity (Part 2)pkalra/col100/slides/11...35 Partitioning def partition(arr,x): i = 0 # leftcounter j = len(arr)-1 # rightcounter while i < j and

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

Page 6: Efficiency and Computational Complexity (Part 2)pkalra/col100/slides/11...35 Partitioning def partition(arr,x): i = 0 # leftcounter j = len(arr)-1 # rightcounter while i < j and

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

Page 7: Efficiency and Computational Complexity (Part 2)pkalra/col100/slides/11...35 Partitioning def partition(arr,x): i = 0 # leftcounter j = len(arr)-1 # rightcounter while i < j and

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

Page 8: Efficiency and Computational Complexity (Part 2)pkalra/col100/slides/11...35 Partitioning def partition(arr,x): i = 0 # leftcounter j = len(arr)-1 # rightcounter while i < j and

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

Page 9: Efficiency and Computational Complexity (Part 2)pkalra/col100/slides/11...35 Partitioning def partition(arr,x): i = 0 # leftcounter j = len(arr)-1 # rightcounter while i < j and

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

Page 10: Efficiency and Computational Complexity (Part 2)pkalra/col100/slides/11...35 Partitioning def partition(arr,x): i = 0 # leftcounter j = len(arr)-1 # rightcounter while i < j and

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

Page 11: Efficiency and Computational Complexity (Part 2)pkalra/col100/slides/11...35 Partitioning def partition(arr,x): i = 0 # leftcounter j = len(arr)-1 # rightcounter while i < j and

11

8 11 16 17 44 b

1 2 3 4 0

15 18 42 51 a

1 2 3 0

58 71 74

6 7 5

i

j

c

1 2 3 0

k

5 6 7 4 9 10 11 8

Merging two sorted lists (arrays)

B[j] < A[i]

Page 12: Efficiency and Computational Complexity (Part 2)pkalra/col100/slides/11...35 Partitioning def partition(arr,x): i = 0 # leftcounter j = len(arr)-1 # rightcounter while i < j and

12

8 11 16 17 44 b

1 2 3 4 0

15 18 42 51 a

1 2 3 0

58 71 74

6 7 5

i

j

8 c

1 2 3 0

k

5 6 7 4 9 10 11 8

Merging two sorted lists (arrays)

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

Page 13: Efficiency and Computational Complexity (Part 2)pkalra/col100/slides/11...35 Partitioning def partition(arr,x): i = 0 # leftcounter j = len(arr)-1 # rightcounter while i < j and

13

8 11 16 17 44 b

1 2 3 4 0

15 18 42 51 a

1 2 3 0

58 71 74

6 7 5

i

j

8 c

1 2 3 0

k

5 6 7 4 9 10 11 8

Merging two sorted lists (arrays)

j=j+1 k=k+1

Page 14: Efficiency and Computational Complexity (Part 2)pkalra/col100/slides/11...35 Partitioning def partition(arr,x): i = 0 # leftcounter j = len(arr)-1 # rightcounter while i < j and

14

8 11 16 17 44 b

1 2 3 4 0

15 18 42 51 a

1 2 3 0

58 71 74

6 7 5

i

j

8 11 c

1 2 3 0

k

5 6 7 4 9 10 11 8

Merging two sorted lists (arrays)

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

Page 15: Efficiency and Computational Complexity (Part 2)pkalra/col100/slides/11...35 Partitioning def partition(arr,x): i = 0 # leftcounter j = len(arr)-1 # rightcounter while i < j and

15

8 11 16 17 44 b

1 2 3 4 0

15 18 42 51 a

1 2 3 0

58 71 74

6 7 5

i

j

8 11 c

1 2 3 0

k

5 6 7 4 9 10 11 8

Merging two sorted lists (arrays)

j=j+1 k=k+1

Page 16: Efficiency and Computational Complexity (Part 2)pkalra/col100/slides/11...35 Partitioning def partition(arr,x): i = 0 # leftcounter j = len(arr)-1 # rightcounter while i < j and

16

8 11 16 17 44 b

1 2 3 4 0

15 18 42 51 a

1 2 3 0

58 71 74

6 7 5

i

j

8 11 15 c

1 2 3 0

k

5 6 7 4 9 10 11 8

Merging two sorted lists (arrays)

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

Page 17: Efficiency and Computational Complexity (Part 2)pkalra/col100/slides/11...35 Partitioning def partition(arr,x): i = 0 # leftcounter j = len(arr)-1 # rightcounter while i < j and

17

8 11 16 17 44 b

1 2 3 4 0

15 18 42 51 a

1 2 3 0

58 71 74

6 7 5

i

j

8 11 15 c

1 2 3 0

k

5 6 7 4 9 10 11 8

Merging two sorted lists (arrays)

i=i+1 k=k+1

Page 18: Efficiency and Computational Complexity (Part 2)pkalra/col100/slides/11...35 Partitioning def partition(arr,x): i = 0 # leftcounter j = len(arr)-1 # rightcounter while i < j and

18

8 11 16 17 44 b

1 2 3 4 0

15 18 42 51 a

1 2 3 0

58 71 74

6 7 5

i

j

8 11 15 16 c

1 2 3 0

k

5 6 7 4 9 10 11 8

Merging two sorted lists (arrays)

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

Page 19: Efficiency and Computational Complexity (Part 2)pkalra/col100/slides/11...35 Partitioning def partition(arr,x): i = 0 # leftcounter j = len(arr)-1 # rightcounter while i < j and

19

8 11 16 17 44 b

1 2 3 4 0

15 18 42 51 a

1 2 3 0

58 71 74

6 7 5

i

j

8 11 15 16 c

1 2 3 0

k

5 6 7 4 9 10 11 8

Merging two sorted lists (arrays)

j=j+1 k=k+1

Page 20: Efficiency and Computational Complexity (Part 2)pkalra/col100/slides/11...35 Partitioning def partition(arr,x): i = 0 # leftcounter j = len(arr)-1 # rightcounter while i < j and

20

8 11 16 17 44 b

1 2 3 4 0

15 18 42 51 a

1 2 3 0

58 71 74

6 7 5

i

j

8 11 15 16 c

1 2 3 0

k 17

5 6 7 4 9 10 11 8

Merging two sorted lists (arrays)

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

Page 21: Efficiency and Computational Complexity (Part 2)pkalra/col100/slides/11...35 Partitioning def partition(arr,x): i = 0 # leftcounter j = len(arr)-1 # rightcounter while i < j and

21

8 11 16 17 44 b

1 2 3 4 0

15 18 42 51 a

1 2 3 0

58 71 74

6 7 5

i

j

8 11 15 16 c

1 2 3 0

k 17

5 6 7 4 9 10 11 8

Merging two sorted lists (arrays)

j=j+1 k=k+1

Page 22: Efficiency and Computational Complexity (Part 2)pkalra/col100/slides/11...35 Partitioning def partition(arr,x): i = 0 # leftcounter j = len(arr)-1 # rightcounter while i < j and

22

8 11 16 17 44 b

1 2 3 4 0

15 18 42 51 a

1 2 3 0

58 71 74

6 7 5

i

j

8 11 15 16 c

1 2 3 0

k 17 18

5 6 7 4 9 10 11 8

Merging two sorted lists (arrays)

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

Page 23: Efficiency and Computational Complexity (Part 2)pkalra/col100/slides/11...35 Partitioning def partition(arr,x): i = 0 # leftcounter j = len(arr)-1 # rightcounter while i < j and

23

8 11 16 17 44 b

1 2 3 4 0

15 18 42 51 a

1 2 3 0

58 71 74

6 7 5

i

j

8 11 15 16 c

1 2 3 0

k 17 18

5 6 7 4 9 10 11 8

Merging two sorted lists (arrays)

i=i+1 k=k+1

Page 24: Efficiency and Computational Complexity (Part 2)pkalra/col100/slides/11...35 Partitioning def partition(arr,x): i = 0 # leftcounter j = len(arr)-1 # rightcounter while i < j and

24

8 11 16 17 44 b

1 2 3 4 0

15 18 42 51 a

1 2 3 0

58 71 74

6 7 5

i

j

8 11 15 16 c

1 2 3 0

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]

Page 25: Efficiency and Computational Complexity (Part 2)pkalra/col100/slides/11...35 Partitioning def partition(arr,x): i = 0 # leftcounter j = len(arr)-1 # rightcounter while i < j and

25

8 11 16 17 44 b

1 2 3 4 0

15 18 42 51 a

1 2 3 0

58 71 74

6 7 5

i

j

8 11 15 16 c

1 2 3 0

k 17 18 42

5 6 7 4 9 10 11 8

Merging two sorted lists (arrays)

i=i+1 k=k+1

Page 26: Efficiency and Computational Complexity (Part 2)pkalra/col100/slides/11...35 Partitioning def partition(arr,x): i = 0 # leftcounter j = len(arr)-1 # rightcounter while i < j and

26

8 11 16 17 44 b

1 2 3 4 0

15 18 42 51 a

1 2 3 0

58 71 74

6 7 5

i

j

8 11 15 16 c

1 2 3 0

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]

Page 27: Efficiency and Computational Complexity (Part 2)pkalra/col100/slides/11...35 Partitioning def partition(arr,x): i = 0 # leftcounter j = len(arr)-1 # rightcounter while i < j and

27

8 11 16 17 44 b

1 2 3 4 0

15 18 42 51 a

1 2 3 0

58 71 74

6 7 5

i

j

8 11 15 16 c

1 2 3 0

k 17 18 42 44

5 6 7 4 9 10 11 8

Merging two sorted lists (arrays)

j=j+1 k=k+1

Page 28: Efficiency and Computational Complexity (Part 2)pkalra/col100/slides/11...35 Partitioning def partition(arr,x): i = 0 # leftcounter j = len(arr)-1 # rightcounter while i < j and

28

8 11 16 17 44 b

1 2 3 4 0

15 18 42 51 a

1 2 3 0

58 71 74

6 7 5

i

j

8 11 15 16 c

1 2 3 0

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]

Page 29: Efficiency and Computational Complexity (Part 2)pkalra/col100/slides/11...35 Partitioning def partition(arr,x): i = 0 # leftcounter j = len(arr)-1 # rightcounter while i < j and

29

8 11 16 17 44 b

1 2 3 4 0

15 18 42 51 a

1 2 3 0

58 71 74

6 7 5

i

j

8 11 15 16 c

1 2 3 0

k 17 18 42 44

5 6 7 4

51

9 10 11 8

Merging two sorted lists (arrays)

k=k+1

Page 30: Efficiency and Computational Complexity (Part 2)pkalra/col100/slides/11...35 Partitioning def partition(arr,x): i = 0 # leftcounter j = len(arr)-1 # rightcounter while i < j and

30

8 11 16 17 44 b

1 2 3 4 0

15 18 42 51 a

1 2 3 0

58 71 74

6 7 5

i

j

8 11 15 16 c

1 2 3 0

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]

Page 31: Efficiency and Computational Complexity (Part 2)pkalra/col100/slides/11...35 Partitioning def partition(arr,x): i = 0 # leftcounter j = len(arr)-1 # rightcounter while i < j and

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)

Page 32: Efficiency and Computational Complexity (Part 2)pkalra/col100/slides/11...35 Partitioning def partition(arr,x): i = 0 # leftcounter j = len(arr)-1 # rightcounter while i < j and

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

Page 33: Efficiency and Computational Complexity (Part 2)pkalra/col100/slides/11...35 Partitioning def partition(arr,x): i = 0 # leftcounter j = len(arr)-1 # rightcounter while i < j and

33

Partitioning

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

Page 34: Efficiency and Computational Complexity (Part 2)pkalra/col100/slides/11...35 Partitioning def partition(arr,x): i = 0 # leftcounter j = len(arr)-1 # rightcounter while i < j and

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

Page 35: Efficiency and Computational Complexity (Part 2)pkalra/col100/slides/11...35 Partitioning def partition(arr,x): i = 0 # leftcounter j = len(arr)-1 # rightcounter while i < j and

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)