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

Post on 18-Jan-2018

227 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

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

TRANSCRIPT

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 powder (1/2 pound)Mix the sugar, baking powder and flour, mix in beaten

eggs, melted butter and bake at 325F for 40 mins.

Cooking exampleSalad: 25m prep, 0m cooking

Chicken noodle: 10m prep, 40 min cooking

Rice pudding:15 mins prep, 20m cooking

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….

First try

5:00pm 5:25pm5:35pm

5:50pm

6:15pm 6:10pm

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

Prep time Cook time

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?

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.

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.

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).

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)

Figure 8-1

Informal definition of an algorithm used in a computer

Informal definition

Finding the largest integer among five integers

Defining actions in FindLargest algorithm

FindLargest refined

Generalization of FindLargest

Three constructs

Algorithm representation

• Flowchart – A flowchart is a pictorial representation of

an algorithm.

• Pseudocode – Pseudocode is an Englishlike

representation of an algorithm.

Flowcharts for three constructs

Pseudocode for three constructs

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

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

Find Largest

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

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

return largest

“pseudocode”

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

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

Example of insertion sort8 2 4 9 3 6

Example of insertion sort8 2 4 9 3 6

Example of insertion sort8 2 4 9 3 6

2 8 4 9 3 6

Example of insertion sort8 2 4 9 3 6

2 8 4 9 3 6

Example of insertion sort8 2 4 9 3 6

2 8 4 9 3 6

2 4 8 9 3 6

Example of insertion sort8 2 4 9 3 6

2 8 4 9 3 6

2 4 8 9 3 6

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

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

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

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

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

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

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

Merging two sorted arrays

20

13

7

2

12

11

9

1

Merging two sorted arrays

20

13

7

2

12

11

9

1

1

Merging two sorted arrays

20

13

7

2

12

11

9

1

1

20

13

7

2

12

11

9

Merging two sorted arrays

20

13

7

2

12

11

9

1

1

20

13

7

2

12

11

9

2

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

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

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

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

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

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

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

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

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

Merging two sorted arrays

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

Searching

• Searching– The process of finding the location of a

target among a list of objects.

– Sequential search– Binary search

Figure 8-19

Search concept

Figure 8-20: Part I

Example of a sequential search

Figure 8-20: Part II

Example of a sequential search

Figure 8-21

Example of a binary search

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

top related