algorithms - mu
TRANSCRIPT
![Page 1: Algorithms - Mu](https://reader031.vdocument.in/reader031/viewer/2022013005/61ccd2ec6ad265378f547f75/html5/thumbnails/1.jpg)
1
Algorithms
![Page 2: Algorithms - Mu](https://reader031.vdocument.in/reader031/viewer/2022013005/61ccd2ec6ad265378f547f75/html5/thumbnails/2.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013005/61ccd2ec6ad265378f547f75/html5/thumbnails/3.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013005/61ccd2ec6ad265378f547f75/html5/thumbnails/4.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013005/61ccd2ec6ad265378f547f75/html5/thumbnails/5.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013005/61ccd2ec6ad265378f547f75/html5/thumbnails/6.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013005/61ccd2ec6ad265378f547f75/html5/thumbnails/7.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013005/61ccd2ec6ad265378f547f75/html5/thumbnails/8.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013005/61ccd2ec6ad265378f547f75/html5/thumbnails/9.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013005/61ccd2ec6ad265378f547f75/html5/thumbnails/10.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013005/61ccd2ec6ad265378f547f75/html5/thumbnails/11.jpg)
Figure 8-1
Informal definition of an algorithm
used in a computer
Informal definition
![Page 12: Algorithms - Mu](https://reader031.vdocument.in/reader031/viewer/2022013005/61ccd2ec6ad265378f547f75/html5/thumbnails/12.jpg)
Finding the largest integer
among five integers
![Page 13: Algorithms - Mu](https://reader031.vdocument.in/reader031/viewer/2022013005/61ccd2ec6ad265378f547f75/html5/thumbnails/13.jpg)
Defining actions in FindLargest algorithm
![Page 14: Algorithms - Mu](https://reader031.vdocument.in/reader031/viewer/2022013005/61ccd2ec6ad265378f547f75/html5/thumbnails/14.jpg)
FindLargest refined
![Page 15: Algorithms - Mu](https://reader031.vdocument.in/reader031/viewer/2022013005/61ccd2ec6ad265378f547f75/html5/thumbnails/15.jpg)
Generalization of FindLargest
![Page 16: Algorithms - Mu](https://reader031.vdocument.in/reader031/viewer/2022013005/61ccd2ec6ad265378f547f75/html5/thumbnails/16.jpg)
Three constructs
![Page 17: Algorithms - Mu](https://reader031.vdocument.in/reader031/viewer/2022013005/61ccd2ec6ad265378f547f75/html5/thumbnails/17.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013005/61ccd2ec6ad265378f547f75/html5/thumbnails/18.jpg)
Flowcharts for three constructs
![Page 19: Algorithms - Mu](https://reader031.vdocument.in/reader031/viewer/2022013005/61ccd2ec6ad265378f547f75/html5/thumbnails/19.jpg)
Pseudocode for three constructs
![Page 20: Algorithms - Mu](https://reader031.vdocument.in/reader031/viewer/2022013005/61ccd2ec6ad265378f547f75/html5/thumbnails/20.jpg)
Write an algorithm to find the largest of a
set of numbers.
![Page 21: Algorithms - Mu](https://reader031.vdocument.in/reader031/viewer/2022013005/61ccd2ec6ad265378f547f75/html5/thumbnails/21.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013005/61ccd2ec6ad265378f547f75/html5/thumbnails/22.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013005/61ccd2ec6ad265378f547f75/html5/thumbnails/23.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013005/61ccd2ec6ad265378f547f75/html5/thumbnails/24.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013005/61ccd2ec6ad265378f547f75/html5/thumbnails/25.jpg)
Example of insertion sort
8 2 4 9 3 6
![Page 26: Algorithms - Mu](https://reader031.vdocument.in/reader031/viewer/2022013005/61ccd2ec6ad265378f547f75/html5/thumbnails/26.jpg)
Example of insertion sort
8 2 4 9 3 6
![Page 27: Algorithms - Mu](https://reader031.vdocument.in/reader031/viewer/2022013005/61ccd2ec6ad265378f547f75/html5/thumbnails/27.jpg)
Example of insertion sort
8 2 4 9 3 6
2 8 4 9 3 6
![Page 28: Algorithms - Mu](https://reader031.vdocument.in/reader031/viewer/2022013005/61ccd2ec6ad265378f547f75/html5/thumbnails/28.jpg)
Example of insertion sort
8 2 4 9 3 6
2 8 4 9 3 6
![Page 29: Algorithms - Mu](https://reader031.vdocument.in/reader031/viewer/2022013005/61ccd2ec6ad265378f547f75/html5/thumbnails/29.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013005/61ccd2ec6ad265378f547f75/html5/thumbnails/30.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013005/61ccd2ec6ad265378f547f75/html5/thumbnails/31.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013005/61ccd2ec6ad265378f547f75/html5/thumbnails/32.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013005/61ccd2ec6ad265378f547f75/html5/thumbnails/33.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013005/61ccd2ec6ad265378f547f75/html5/thumbnails/34.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013005/61ccd2ec6ad265378f547f75/html5/thumbnails/35.jpg)
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://reader031.vdocument.in/reader031/viewer/2022013005/61ccd2ec6ad265378f547f75/html5/thumbnails/36.jpg)
https://www.youtube.com/watch?v=ROalU37
9l3U&list=PL58zywNQ04Laefu_tC8oMwb
H4M929HPnG&index=13
![Page 37: Algorithms - Mu](https://reader031.vdocument.in/reader031/viewer/2022013005/61ccd2ec6ad265378f547f75/html5/thumbnails/37.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013005/61ccd2ec6ad265378f547f75/html5/thumbnails/38.jpg)
Merging two sorted arrays
20
13
7
2
12
11
9
1
![Page 39: Algorithms - Mu](https://reader031.vdocument.in/reader031/viewer/2022013005/61ccd2ec6ad265378f547f75/html5/thumbnails/39.jpg)
Merging two sorted arrays
20
13
7
2
12
11
9
1
1
![Page 40: Algorithms - Mu](https://reader031.vdocument.in/reader031/viewer/2022013005/61ccd2ec6ad265378f547f75/html5/thumbnails/40.jpg)
Merging two sorted arrays
20
13
7
2
12
11
9
1
1
20
13
7
2
12
11
9
![Page 41: Algorithms - Mu](https://reader031.vdocument.in/reader031/viewer/2022013005/61ccd2ec6ad265378f547f75/html5/thumbnails/41.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013005/61ccd2ec6ad265378f547f75/html5/thumbnails/42.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013005/61ccd2ec6ad265378f547f75/html5/thumbnails/43.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013005/61ccd2ec6ad265378f547f75/html5/thumbnails/44.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013005/61ccd2ec6ad265378f547f75/html5/thumbnails/45.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013005/61ccd2ec6ad265378f547f75/html5/thumbnails/46.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013005/61ccd2ec6ad265378f547f75/html5/thumbnails/47.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013005/61ccd2ec6ad265378f547f75/html5/thumbnails/48.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013005/61ccd2ec6ad265378f547f75/html5/thumbnails/49.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013005/61ccd2ec6ad265378f547f75/html5/thumbnails/50.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013005/61ccd2ec6ad265378f547f75/html5/thumbnails/51.jpg)
Merging two sorted arrays
https://www.youtube.com/watch?v=XaqR3G
_NVoo&index=9&list=PL58zywNQ04Laefu
_tC8oMwbH4M929HPnG
![Page 52: Algorithms - Mu](https://reader031.vdocument.in/reader031/viewer/2022013005/61ccd2ec6ad265378f547f75/html5/thumbnails/52.jpg)
Searching
• Searching
– The process of finding the location of a
target among a list of objects.
– Sequential search
– Binary search
![Page 53: Algorithms - Mu](https://reader031.vdocument.in/reader031/viewer/2022013005/61ccd2ec6ad265378f547f75/html5/thumbnails/53.jpg)
Figure 8-19
Search concept
![Page 54: Algorithms - Mu](https://reader031.vdocument.in/reader031/viewer/2022013005/61ccd2ec6ad265378f547f75/html5/thumbnails/54.jpg)
Figure 8-20: Part I
Example of a sequential search
![Page 55: Algorithms - Mu](https://reader031.vdocument.in/reader031/viewer/2022013005/61ccd2ec6ad265378f547f75/html5/thumbnails/55.jpg)
Figure 8-20: Part II
Example of a sequential search
![Page 56: Algorithms - Mu](https://reader031.vdocument.in/reader031/viewer/2022013005/61ccd2ec6ad265378f547f75/html5/thumbnails/56.jpg)
Figure 8-21
Example of a binary search
![Page 57: Algorithms - Mu](https://reader031.vdocument.in/reader031/viewer/2022013005/61ccd2ec6ad265378f547f75/html5/thumbnails/57.jpg)
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