+ All Categories
Home > Documents > CS32 Discussion Week 7 - yellowstone.cs.ucla.eduyellowstone.cs.ucla.edu/~muhao/cs32s18/week7.pdf ·...

CS32 Discussion Week 7 - yellowstone.cs.ucla.eduyellowstone.cs.ucla.edu/~muhao/cs32s18/week7.pdf ·...

Date post: 29-Jul-2018
Category:
Upload: dinhbao
View: 224 times
Download: 0 times
Share this document with a friend
43
CS32 Discussion Week 7 Muhao Chen [email protected] http://yellowstone.cs.ucla.edu/~muhao / 1
Transcript

CS32 DiscussionWeek 7

Muhao Chen

[email protected]

http://yellowstone.cs.ucla.edu/~muhao/

1

Outline

•Big-O Notation

•Sorting

•Tree

2

Complexity

•Quantify the efficiency of a program.

•The magnitude of time and space cost for an algorithm given certain size of input.•Time complexity: quantifies the run time.•Space complexity: quantifies the usage of the

memory (or sometimes hard disk drives, cloud disk drives, etc.).

3

4

5

Formal Definition of Big-O

• Let T(n) be the function that measures the runtime of the program given n size of input

• Let g(n) be another function defined on the real number field.• T(n) = O(g(n)) iff ∃M > 0 and ∃N > 0 s.t. ∀n>N : T(n) ≦

M*g(n)

6

When the input size n grows above certain scale N, the runtime T(n) is of the same or less

magnitude of the function g inside O( )Upper bound

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

Permutation

32

void permutation(vector<int>& nums, int start) {if (start == nums.size() - 1) {

for(int i=0; i<nums.size(); ++i)cout << nums[i] << ' ,'; cout << endl;

}permutation(nums, start + 1);for (int i=start+1; i<nums.size(); ++i) {

swap(nums[start], nums[i]);permutation(nums, start + 1);swap(nums[start], nums[i]);

}}

permutation(nums, 0); //call this function

O(n!)

33

Tree

34

35

36

37

n - 1

38

39

20+21+…+2h=2h+1-1

40

41

42

0

Bugs in your software are actually special features :)

43


Recommended