Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 1 Algorithms

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

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

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

5 First try 5:00pm5:25pm 5:35pm 5:50pm 6:15pm 6:10pm (25,0) (10,40) (15,20) Prep timeCook time

6 Second try 5:00pm5:10pm 5:25pm 5:50pm 5:45pm (10,40) (15,20) (25,0) First work on dishes with shortest preparation time?

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

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

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

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

11 Figure 8-1 Informal definition of an algorithm used in a computer Informal definition

12 Finding the largest integer among five integers

13 Defining actions in FindLargest algorithm

14 FindLargest refined

15 Generalization of FindLargest

16 Three constructs

17 Algorithm representation Flowchart –A flowchart is a pictorial representation of an algorithm. Pseudocode –Pseudocode is an Englishlike representation of an algorithm.

18 Flowcharts for three constructs

19 Pseudocode for three constructs

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

21 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

22 Find Largest FIND-LARGEST (A, n) ⊳ largest largest ← 0 for i ← 1 to n ifA[i] > largest largest ← A[i] return largest “pseudocode”

23 The problem of sorting Input: sequence a 1, a 2, …, a n 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

24 Insertion sort INSERTION-SORT (A, n) ⊳ A[1.. n] for j ← 2 to n dokey ← A[ j] i ← j – 1 while i > 0 and A[i] > key doA[i+1] ← A[i] i ← i – 1 A[i+1] = key “pseudocode” ij key sorted A:A: 1n

25 Example of insertion sort 824936

26 824936

27 824936 284936

28 824936 284936

29 824936 284936 248936

30 824936 284936 248936

31 824936 284936 248936 248936

32 824936 284936 248936 248936

33 824936 284936 248936 248936 234896

34 824936 284936 248936 248936 234896

35 824936 284936 248936 248936 234896 234689done

36 https://www.youtube.com/watch?v=ROalU37 9l3U&list=PL58zywNQ04Laefu_tC8oMwb H4M929HPnG&index=13

37 Merge sort M ERGE -S ORT 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: M ERGE

38 Merging two sorted arrays 20 13 7 2 12 11 9 1

39 Merging two sorted arrays 20 13 7 2 12 11 9 1 1

40 Merging two sorted arrays 20 13 7 2 12 11 9 1 1 20 13 7 2 12 11 9

41 Merging two sorted arrays 20 13 7 2 12 11 9 1 1 20 13 7 2 12 11 9 2

42 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

43 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

44 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

45 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

46 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

47 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

48 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 20 13 12

49 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 20 13 12

50 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 20 13 12

51 Merging two sorted arrays https://www.youtube.com/watch?v=XaqR3G _NVoo&index=9&list=PL58zywNQ04Laefu _tC8oMwbH4M929HPnG

52 Searching –The process of finding the location of a target among a list of objects. –Sequential search –Binary search

53 Figure 8-19 Search concept

54 Figure 8-20: Part I Example of a sequential search

55 Figure 8-20: Part II Example of a sequential search

56 Figure 8-21 Example of a binary search

57 References www.cs.umd.edu/~samir/DSTTalk.ppt http://courses.csail.mit.edu/6.046/spring04/lectur es/l1.ppt http://www.csie.ntnu.edu.tw/~violet/cs92/ch08.P PT


Download ppt "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."

Similar presentations


Ads by Google