1 MT258 Computer Programming and Problem Solving Unit 9.

36
1 MT258 Computer Programming and Problem Solving Unit 9

Transcript of 1 MT258 Computer Programming and Problem Solving Unit 9.

Page 1: 1 MT258 Computer Programming and Problem Solving Unit 9.

1

MT258

Computer Programming and

Problem Solving

Unit 9

Page 2: 1 MT258 Computer Programming and Problem Solving Unit 9.

2

UNIT NineFrom algorithms to problem solving

Page 3: 1 MT258 Computer Programming and Problem Solving Unit 9.

3

Stack

The stack data structure is characterized as ‘last in, first out’ or LIFO.

Stack can be implemented using contiguous arrays or using linked lists with pointers.

Page 4: 1 MT258 Computer Programming and Problem Solving Unit 9.

4

Stack define as array :

#define MAXSTACK 10 typedef char StackEntry; typedef struct stack { int top; StackEntry entry[MAXSTACK]; } Stack;

Page 5: 1 MT258 Computer Programming and Problem Solving Unit 9.

5

Stack define as pointer

typedef struct node { StackEntry entry; stack node *next; } Node;

typedef struct stact { Node* top; } Stack;

Page 6: 1 MT258 Computer Programming and Problem Solving Unit 9.

6

Stack

Primitive operators :• creating a stack• pushing (insert an element to a stack)• popping (delete an element from a stack)• checking the emptiness of the stack• checking the fullness of the stack

Page 7: 1 MT258 Computer Programming and Problem Solving Unit 9.

7

Stack

Please refer to the example in page 478-484 in our textbook ( C How to Program).

Please refer to the example in page 83-85 in our reference book (Data Structure & Program Design in C).

Page 8: 1 MT258 Computer Programming and Problem Solving Unit 9.

8

Queue

The queue has a characteristic of ‘first in,first out’.

Page 9: 1 MT258 Computer Programming and Problem Solving Unit 9.

9

Queue define as array

#define MAXQUEUE 10; typedef char QueueEntry; typedef struct queue { int count; int front; int rear; QueueEntry entry[MAXQUEUE]; }Queue;

Page 10: 1 MT258 Computer Programming and Problem Solving Unit 9.

10

Queue define as pointer

typedef char QueueEntry; typedef struct queuenode { QueueEntry info; struct queuenode *next; } QueueNode; typedef struct queue { QueueNode *front; QueueNode *rear; } Queue;

Page 11: 1 MT258 Computer Programming and Problem Solving Unit 9.

11

Queue

Primitive operators :• creating a queue• appending an element to a queue• removing an element from a queue• checking the current queue size• checking the emptiness of the queue• checking the fullness of the queue

Page 12: 1 MT258 Computer Programming and Problem Solving Unit 9.

12

Queue

Please refer to the example in page 484-490 in our textbook ( C How to Program).

Please refer to the example in page 135-137 in our reference book (Data Structure & Program Design in C).

Page 13: 1 MT258 Computer Programming and Problem Solving Unit 9.

13

Big-O Notation

If f(n) and g(n) are functions defined for positive integers, then to write f(n) = O(g(n))

It means that there exists a constant c such that |f(n)| <= c|g(n)|

Page 14: 1 MT258 Computer Programming and Problem Solving Unit 9.

14

Examples

linear function• f(n) = 100n• since f(n) <= c n for any larger constant c>100 • then f(n) is O(n)

• f(n) = 4n + 200• since f(n) <= 5n whenever n >=200• then f(n) is O(n)

Page 15: 1 MT258 Computer Programming and Problem Solving Unit 9.

15

Examples

Quadratic function• f(n) = 3n2 + 100n• since f(n) <= c n for any larger constant c>3 • then f(n) is O(n2)

Page 16: 1 MT258 Computer Programming and Problem Solving Unit 9.

16

Common orders

Constant - O(1) linear time - O(n) quadratic time - O(n2) cubic time - O(n3) exponential time - O(2n) logarithmic time - O(log n) and O(n log n)

Page 17: 1 MT258 Computer Programming and Problem Solving Unit 9.

17

Growing speed (starting from more efficient for large n)

1 lg n n n lg n n2

n3

2n

Page 18: 1 MT258 Computer Programming and Problem Solving Unit 9.

18

Activity

Find the big-o notation of the followings:

• 20 n3 + 10 n lg n + 5

• 3 lg n + lg(lg(n)

• 2100

Page 19: 1 MT258 Computer Programming and Problem Solving Unit 9.

19

Activity

20 n3 + 10 n lg n + 5 since n lg n < n3 f(n) = 20 n3 + 10n lg n + 5 <= 20n3 + 10n3 + 5n3 <= 35 n3 since f(n) <= c n for any larger

constant c>35 = O(n3)

Page 20: 1 MT258 Computer Programming and Problem Solving Unit 9.

20

Activity

3 lg n + lg(lg(n) f(n) = 3 lg n + lg(lg(n)) <= 3 lg n + lg n <= 4 lg n since f(n) <= c n for any

larger constant c>4 = O(lg n)

Page 21: 1 MT258 Computer Programming and Problem Solving Unit 9.

21

Activity

2100

f(n) = O(1)

Page 22: 1 MT258 Computer Programming and Problem Solving Unit 9.

22

Activity

For each of the following pairs of function, find the smallest integer value of n > 1 for which the first becomes larger than the second.

• n2, 15n+5

• 0.1n, 10 lg n

• 2n, 8n4

• 0.1n2, 100n lg n

Page 23: 1 MT258 Computer Programming and Problem Solving Unit 9.

23

Activity

n = 16 : n2 = 256, 15n+5 = 245 n = 997 : 0.1n = 99.70, 10 lg n = 99.61 n = 21 : 2n = 2097152, 8n4 = 1555848 n = 13747 : 0.1n2 = 18898000, 100n lg n = 18897766

Page 24: 1 MT258 Computer Programming and Problem Solving Unit 9.

24

Activity Assume you have just bought the fastest PC tha

t runs at 1GHz (1,000,000,000Hz). Also assume the CPU is so good that each instruction can be performed within one clock cycle, i.e. this super PC can handle 1 billion instructions per second (1 gips or 1,000 mips). Now you have 64 inputs (n=64) to be executed in three different algorithms• How long would it take to execute the algorithm that ta

kes log2 (n) steps?

• How long would it take to execute the algorithm that takes n3 steps?

• How many years for the algorithm that takes 2n steps?

Page 25: 1 MT258 Computer Programming and Problem Solving Unit 9.

25

Activity

log2 (64) = 6 (26 = 64) Thus, it takes 6 nanoseconds.

643 = 262144 nanoseconds or .000262 seconds

2n= 1.8 * 10 19 nanoseconds Thus, 584.9 years

Page 26: 1 MT258 Computer Programming and Problem Solving Unit 9.

26

Activity void Arrange2(int *a, int n) { int i, j, k, min; for (i=0; i < n; i++) { k = i; for (j = 1; j < n; j++) if (a[j] < a[k]) k = j; min = a[k]; a[k] = a[i]; a[i] = min; } }

Page 27: 1 MT258 Computer Programming and Problem Solving Unit 9.

27

Activity

Number of steps : n ( 4 (n - 1) + 2 + 2 + 4) + 2 = n ( 4n + 4) + 2 = 4 n2 + 4n + 2 = O(n2)

Page 28: 1 MT258 Computer Programming and Problem Solving Unit 9.

28

Sorting algorithms

insertion sort selection sort bubble sort mergesort quicksort

Page 29: 1 MT258 Computer Programming and Problem Solving Unit 9.

29

Insertion sort

Starting with the second key, each item is extracted and placed in relative order with the items preceding it.

Extra variable is required to aid the comparisons.

Lots of swapping O(n2)

Page 30: 1 MT258 Computer Programming and Problem Solving Unit 9.

30

Selection sort

Starting with the last position, the largest unsorted key is swapped into the last unsorted position.

No extra variable is required to aid the comparisons

the movement of data is limited to one-to-one exchange.

O(n2)

Page 31: 1 MT258 Computer Programming and Problem Solving Unit 9.

31

Bubble sort

They are very easy to understand. Each key is compared and swapped with the

adjacent key in the list. O(n2)

Page 32: 1 MT258 Computer Programming and Problem Solving Unit 9.

32

Mergesort

The list is divided into sublists, each sublist is sorted, then the sublists are merged together in proper order.

It is one of divide and conquer sorting. The drawback of array implementation is the additio

nal memory space, a temporary array with the same size as the original list is required.

O(n lg n)

Please refer to the example in page 304-306 in our reference book (Data Structure & Program Design in C).

Page 33: 1 MT258 Computer Programming and Problem Solving Unit 9.

33

Quicksort The list is divided into sublists, each sublist

is sorted, then the sublists are merged together in proper order.

It is one of divide and conquer sorting. The drawback of array implementation is th

e additional memory space, a temporary array with the same size as the original list is required.

It requires a pivot. O(n lg n)

Page 34: 1 MT258 Computer Programming and Problem Solving Unit 9.

34

Activity

You need to fill in the numbers for pass 1 and the remaining passes for merging part of Mergesort.

Pass 0: {66}{33}{40}{22}{55}{88}{60}{11}{80}{20}{50}{44}{77}{60}

Pass 1: { }{ }{ }{ }{ } { }{ }

Page 35: 1 MT258 Computer Programming and Problem Solving Unit 9.

35

Activity

Pass 1: {33,66} {22,40} {55, 88} {11, 60} {20, 80} {44,50} {60, 77} Pass 2: {22, 33, 40, 66} {11, 55, 60, 88} {20, 44, 50, 80} {60, 77} Pass 3: {11, 22, 33, 40, 55, 60, 66, 88} {20, 44, 50, 60, 77, 80} Pass 4: {11, 20, 22, 33, 40, 44, 50, 55, 60, 60, 66,

77, 80, 88}

Page 36: 1 MT258 Computer Programming and Problem Solving Unit 9.

36

Activity Suppose Data contains 1,000,000 elements.

Using the binary search algorithm, how many comparisons are required to find the location of an item in the Data array even in the worst case? (log 2 n)

210 > 1000 220 > 10002 = 1,000,000 It only requires about 20 comparisons to find the

location of an item in a data array with 1,000,000 elements.