Algorithms - Mu

57
1 Algorithms

Transcript of Algorithms - Mu

Page 1: Algorithms - Mu

1

Algorithms

Page 2: Algorithms - Mu

Algorithms Introduction

Recipe for baking a cake….

• 2 sticks butter

• 2 cups flour

• 1 cup sugar

• 4 eggs

• 1 cup milk

• 1 tsp baking powder

• Cocoa powder (1/2 pound)

Mix the sugar, baking powder and flour, mix in beaten eggs, melted butter and bake at 325F for 40 mins.

Page 3: Algorithms - Mu

Cooking example

Salad:

25m prep, 0m cooking

Chicken noodle:

10m prep, 40 min cooking

Rice pudding:

15 mins prep, 20m cooking

Page 4: Algorithms - Mu

In what order should Martha make

the dishes?

• Martha can work on preparing one dish at

a time, however once something is

cooking, she can prepare another dish.

• How quickly can she get all the dishes

ready?

• She starts at 5pm, and her guests will

arrive at 6pm….

Page 5: Algorithms - Mu

First try

5:00pm 5:25pm

5:35pm 5:50pm

6:15pm 6:10pm

(25,0) (10,40) (15,20)

Prep time Cook time

Page 6: Algorithms - Mu

Second try

5:00pm 5:10pm

5:25pm 5:50pm

5:50pm 5:45pm

(10,40) (15,20) (25,0)

First work on dishes with shortest preparation time?

Page 7: Algorithms - Mu

This rule may not work all the time

Suppose the required times are:

Bulgur (5,10) Lentils (10, 60) Lamb (15, 75)

Shortest prep time order: start at 5pm, and

finish lamb at 6:45pm

Longest cooking time first: food ready at

6:30pm.

Page 8: Algorithms - Mu

What if she had to make several

dishes?

• For 3 dishes, there are only 6 possible

orders. SCR,SRC,RSC,RCS,CSR,CRS.

• The number of possible orderings of 10

dishes is 3,628,800.

• For 15 dishes the number of possible

orderings is 1,307,674,368,000!

• This leads to a combinatorial explosion.

Page 9: Algorithms - Mu

Key Idea

• Order dishes in longest cooking time order.

• Chicken noodle soup goes first (40 mins of cook time), next is the Rice pudding (20 mins of cook time), followed by the Salad (0 mins of cook time).

• This is the best ordering. In other words, no other order can take less time.

• This does not work if there are very few stovetops (now the problem becomes really difficult).

Page 10: Algorithms - Mu

What if we had a small number of

burners?

• Problem becomes

very difficult if we

have 2, 3, 4 burners..

• Problem can be

solved optimally if we

only have one burner

(Johnson, 1954)

Page 11: Algorithms - Mu

Figure 8-1

Informal definition of an algorithm

used in a computer

Informal definition

Page 12: Algorithms - Mu

Finding the largest integer

among five integers

Page 13: Algorithms - Mu

Defining actions in FindLargest algorithm

Page 14: Algorithms - Mu

FindLargest refined

Page 15: Algorithms - Mu

Generalization of FindLargest

Page 16: Algorithms - Mu

Three constructs

Page 17: Algorithms - Mu

Algorithm representation

• Flowchart

– A flowchart is a pictorial representation of

an algorithm.

• Pseudocode

– Pseudocode is an Englishlike

representation of an algorithm.

Page 18: Algorithms - Mu

Flowcharts for three constructs

Page 19: Algorithms - Mu

Pseudocode for three constructs

Page 20: Algorithms - Mu

Write an algorithm to find the largest of a

set of numbers.

Page 21: Algorithms - Mu

FindLargest

Input: A list of positive integers

1. Set Largest to 0

2. while (more integers)

2.1 if (the integer is greater than Largest)

then

2.1.1 Set largest to the value of the

integer

End if

End while

3. Return Largest

End

Find largest

Page 22: Algorithms - Mu

Find Largest

FIND-LARGEST (A, n)⊳ largest

largest ← 0

for i ← 1 to n

if A[i] > largest

largest ← A[i]

return largest

“pseudocode”

Page 23: Algorithms - Mu

The problem of sorting

Input: sequence a1, a2, …, an of numbers.

Example:

Input: 8 2 4 9 3 6

Output: 2 3 4 6 8 9

Output: a'1, a'2, …, a'n such that

a'1 <= a'2 <= … <= a'n

Page 24: Algorithms - Mu

Insertion sort

INSERTION-SORT (A, n) ⊳ A[1 . . n]

for j ← 2 to n

do key ← A[ j]

i ← j – 1

while i > 0 and A[i] > key

do A[i+1] ← A[i]

i ← i – 1

A[i+1] = key

“pseudocode”

i j

key sorted

A:

1 n

Page 25: Algorithms - Mu

Example of insertion sort

8 2 4 9 3 6

Page 26: Algorithms - Mu

Example of insertion sort

8 2 4 9 3 6

Page 27: Algorithms - Mu

Example of insertion sort

8 2 4 9 3 6

2 8 4 9 3 6

Page 28: Algorithms - Mu

Example of insertion sort

8 2 4 9 3 6

2 8 4 9 3 6

Page 29: Algorithms - Mu

Example of insertion sort

8 2 4 9 3 6

2 8 4 9 3 6

2 4 8 9 3 6

Page 30: Algorithms - Mu

Example of insertion sort

8 2 4 9 3 6

2 8 4 9 3 6

2 4 8 9 3 6

Page 31: Algorithms - Mu

Example of insertion sort

8 2 4 9 3 6

2 8 4 9 3 6

2 4 8 9 3 6

2 4 8 9 3 6

Page 32: Algorithms - Mu

Example of insertion sort

8 2 4 9 3 6

2 8 4 9 3 6

2 4 8 9 3 6

2 4 8 9 3 6

Page 33: Algorithms - Mu

Example of insertion sort

8 2 4 9 3 6

2 8 4 9 3 6

2 4 8 9 3 6

2 4 8 9 3 6

2 3 4 8 9 6

Page 34: Algorithms - Mu

Example of insertion sort

8 2 4 9 3 6

2 8 4 9 3 6

2 4 8 9 3 6

2 4 8 9 3 6

2 3 4 8 9 6

Page 35: Algorithms - Mu

Example of insertion sort

8 2 4 9 3 6

2 8 4 9 3 6

2 4 8 9 3 6

2 4 8 9 3 6

2 3 4 8 9 6

2 3 4 6 8 9 done

Page 36: Algorithms - Mu

https://www.youtube.com/watch?v=ROalU37

9l3U&list=PL58zywNQ04Laefu_tC8oMwb

H4M929HPnG&index=13

Page 37: Algorithms - Mu

Merge sort

MERGE-SORT 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: MERGE

Page 38: Algorithms - Mu

Merging two sorted arrays

20

13

7

2

12

11

9

1

Page 39: Algorithms - Mu

Merging two sorted arrays

20

13

7

2

12

11

9

1

1

Page 40: Algorithms - Mu

Merging two sorted arrays

20

13

7

2

12

11

9

1

1

20

13

7

2

12

11

9

Page 41: Algorithms - Mu

Merging two sorted arrays

20

13

7

2

12

11

9

1

1

20

13

7

2

12

11

9

2

Page 42: Algorithms - Mu

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

Page 43: Algorithms - Mu

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

Page 44: Algorithms - Mu

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

Page 45: Algorithms - Mu

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

Page 46: Algorithms - Mu

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

Page 47: Algorithms - Mu

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

11

Page 48: Algorithms - Mu

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

11

20

13

12

Page 49: Algorithms - Mu

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

11

20

13

12

12

Page 50: Algorithms - Mu

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

11

20

13

12

12

Page 51: Algorithms - Mu

Merging two sorted arrays

https://www.youtube.com/watch?v=XaqR3G

_NVoo&index=9&list=PL58zywNQ04Laefu

_tC8oMwbH4M929HPnG

Page 52: Algorithms - Mu

Searching

• Searching

– The process of finding the location of a

target among a list of objects.

– Sequential search

– Binary search

Page 53: Algorithms - Mu

Figure 8-19

Search concept

Page 54: Algorithms - Mu

Figure 8-20: Part I

Example of a sequential search

Page 55: Algorithms - Mu

Figure 8-20: Part II

Example of a sequential search

Page 56: Algorithms - Mu

Figure 8-21

Example of a binary search

Page 57: Algorithms - Mu

References

• www.cs.umd.edu/~samir/DSTTalk.ppt

• http://courses.csail.mit.edu/6.046/spring04/lectures/l1.ppt

• http://www.csie.ntnu.edu.tw/~violet/cs92/ch08.PPT