1 algorithms. algorithms introduction recipe for baking a cake. 2 sticks butter 2 cups flour 1 cup...

57
1 Algorithms

Upload: allen-weaver

Post on 18-Jan-2018

227 views

Category:

Documents


0 download

DESCRIPTION

Cooking example Salad: 25m prep, 0m cooking Chicken noodle: 10m prep, 40 min cooking Rice pudding: 15 mins prep, 20m cooking

TRANSCRIPT

Page 1: 1 Algorithms. 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

1

Algorithms

Page 2: 1 Algorithms. 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

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: 1 Algorithms. 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

Cooking exampleSalad: 25m prep, 0m cooking

Chicken noodle: 10m prep, 40 min cooking

Rice pudding:15 mins prep, 20m cooking

Page 4: 1 Algorithms. 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

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: 1 Algorithms. 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

First try

5:00pm 5:25pm5:35pm

5:50pm

6:15pm 6:10pm

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

Prep time Cook time

Page 6: 1 Algorithms. 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

Second try

5:00pm 5:10pm5:25pm 5:50pm

5:50pm 5:45pm

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

First work on dishes with shortest preparation time?

Page 7: 1 Algorithms. 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

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:45pmLongest cooking time first: food ready at

6:30pm.

Page 8: 1 Algorithms. 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

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: 1 Algorithms. 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

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: 1 Algorithms. 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

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: 1 Algorithms. 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

Figure 8-1

Informal definition of an algorithm used in a computer

Informal definition

Page 12: 1 Algorithms. 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

Finding the largest integer among five integers

Page 13: 1 Algorithms. 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

Defining actions in FindLargest algorithm

Page 14: 1 Algorithms. 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

FindLargest refined

Page 15: 1 Algorithms. 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

Generalization of FindLargest

Page 16: 1 Algorithms. 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

Three constructs

Page 17: 1 Algorithms. 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

Algorithm representation

• Flowchart – A flowchart is a pictorial representation of

an algorithm.

• Pseudocode – Pseudocode is an Englishlike

representation of an algorithm.

Page 18: 1 Algorithms. 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

Flowcharts for three constructs

Page 19: 1 Algorithms. 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

Pseudocode for three constructs

Page 20: 1 Algorithms. 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

Write an algorithm to find the largest of a set of numbers.

Page 21: 1 Algorithms. 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

FindLargestInput: A list of positive integers

1. Set Largest to 02. 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 ifEnd while

3. Return LargestEnd

Find largestFind largest

Page 22: 1 Algorithms. 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

Find Largest

FIND-LARGEST (A, n) ⊳ largestlargest ← 0for i ← 1 to n

if A[i] > largestlargest ← A[i]

return largest

“pseudocode”

Page 23: 1 Algorithms. 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

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: 1 Algorithms. 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

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

for j ← 2 to ndo key ← A[ j]

i ← j – 1while i > 0 and A[i] > key

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

A[i+1] = key

“pseudocode”

i j

keysorted

A:1 n

Page 25: 1 Algorithms. 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

Example of insertion sort8 2 4 9 3 6

Page 26: 1 Algorithms. 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

Example of insertion sort8 2 4 9 3 6

Page 27: 1 Algorithms. 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

Example of insertion sort8 2 4 9 3 6

2 8 4 9 3 6

Page 28: 1 Algorithms. 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

Example of insertion sort8 2 4 9 3 6

2 8 4 9 3 6

Page 29: 1 Algorithms. 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

Example of insertion sort8 2 4 9 3 6

2 8 4 9 3 6

2 4 8 9 3 6

Page 30: 1 Algorithms. 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

Example of insertion sort8 2 4 9 3 6

2 8 4 9 3 6

2 4 8 9 3 6

Page 31: 1 Algorithms. 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

Example of insertion sort8 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: 1 Algorithms. 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

Example of insertion sort8 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: 1 Algorithms. 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

Example of insertion sort8 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: 1 Algorithms. 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

Example of insertion sort8 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: 1 Algorithms. 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

Example of insertion sort8 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: 1 Algorithms. 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

https://www.youtube.com/watch?v=ROalU379l3U&list=PL58zywNQ04Laefu_tC8oMwbH4M929HPnG&index=13

Page 37: 1 Algorithms. 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

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: 1 Algorithms. 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

Merging two sorted arrays

20

13

7

2

12

11

9

1

Page 39: 1 Algorithms. 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

Merging two sorted arrays

20

13

7

2

12

11

9

1

1

Page 40: 1 Algorithms. 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

Merging two sorted arrays

20

13

7

2

12

11

9

1

1

20

13

7

2

12

11

9

Page 41: 1 Algorithms. 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

Merging two sorted arrays

20

13

7

2

12

11

9

1

1

20

13

7

2

12

11

9

2

Page 42: 1 Algorithms. 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

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: 1 Algorithms. 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

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: 1 Algorithms. 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

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: 1 Algorithms. 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

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: 1 Algorithms. 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

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: 1 Algorithms. 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

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: 1 Algorithms. 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

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: 1 Algorithms. 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

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: 1 Algorithms. 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

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: 1 Algorithms. 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

Merging two sorted arrays

https://www.youtube.com/watch?v=XaqR3G_NVoo&index=9&list=PL58zywNQ04Laefu_tC8oMwbH4M929HPnG

Page 52: 1 Algorithms. 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

Searching

• Searching– The process of finding the location of a

target among a list of objects.

– Sequential search– Binary search

Page 53: 1 Algorithms. 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

Figure 8-19

Search concept

Page 54: 1 Algorithms. 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

Figure 8-20: Part I

Example of a sequential search

Page 55: 1 Algorithms. 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

Figure 8-20: Part II

Example of a sequential search

Page 56: 1 Algorithms. 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

Figure 8-21

Example of a binary search

Page 57: 1 Algorithms. 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

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