algorithmic foundations comp108 comp108 algorithmic foundations basics prudence wong...

59
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Basics Prudence Wong http://www.csc.liv.ac.uk/~pwong/teaching/ comp108/201112

Upload: anne-stevens

Post on 17-Jan-2016

224 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Basics Prudence Wong pwong/teaching/comp108/201112

Algorithmic FoundationsCOMP108

COMP108Algorithmic FoundationsBasics

Prudence Wonghttp://www.csc.liv.ac.uk/~pwong/teaching/comp108/201112

Page 2: Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Basics Prudence Wong pwong/teaching/comp108/201112

Algorithmic FoundationsCOMP108

Crossing Bridge @ Night

1 min

2 min

5 min

10 min

each time, 2 persons share a torchthey walk @ speed of slower person

Target: all cross the bridge

Can you do it in 17 mins?

Page 3: Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Basics Prudence Wong pwong/teaching/comp108/201112

Algorithmic FoundationsCOMP108

3

(Basics)

Module InformationDr Prudence Wong

Rm 3.18 Ashton Building, [email protected]

office hours: Wed 10-11am, Thu 3-4pm

DemonstratorsMr Mihai Burcea, Mr Jude-Thaddeus Ojiaku

ReferencesMain: Introduction to the Design and Analysis of

Algorithms. A. V. Levitin. Addison Wesley.

Reference: Introduction to Algorithms. T. H. Cormen, C. E. Leiserson, R. L. Rivest, C. Stein. The MIT Press

Page 4: Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Basics Prudence Wong pwong/teaching/comp108/201112

Algorithmic FoundationsCOMP108

4

(Basics)

Module Information (2)Teaching, Assessments and Help

33 lectures, 11 tutorials

2 assessments (20%), 1 written exam (80%)

Office hours, email

Tutorials/LabsLocation :

Seminar Rooms G12 or 2.23 (theoretical) or

Labs 1, 2, 3 (practical)

Week 2: Seminar Rooms, G12 Ashton Bldg or 2.23 Holt Bldg

Class Tests: Weeks 7 & 11 (15 Mar & 3 May)

Page 5: Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Basics Prudence Wong pwong/teaching/comp108/201112

Algorithmic FoundationsCOMP108

5

(Basics)

Why COMP108?Pre-requisite for: COMP202, COMP218

(COMP202 is pre-requisite for COMP309)

Algorithm design is a foundation for efficient and effective programs

"Year 1 modules do not count towards honour classification…"

(Career Services: Employers DO consider year 1 module results)

Page 6: Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Basics Prudence Wong pwong/teaching/comp108/201112

Algorithmic FoundationsCOMP108

6

(Basics)

Aims To give an overview of the study of algorithms in

terms of their efficiency.

To introduce the standard algorithmic design paradigms employed in the development of efficient algorithmic solutions.

To describe the analysis of algorithms in terms of the use of formal models of Time and Space.

What do we mean by good?

How to achieve?

Can we prove?

Page 7: Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Basics Prudence Wong pwong/teaching/comp108/201112

Algorithmic FoundationsCOMP108

Ready to start …

Learning outcomes

Able to tell what an algorithm is & have some understanding why we study algorithms

Able to use pseudo code to describe algorithm

Page 8: Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Basics Prudence Wong pwong/teaching/comp108/201112

Algorithmic FoundationsCOMP108

8

(Basics)

What is an algorithm?A sequence of precise and concise

instructions that guide you (or a computer) to solve a specific problem

Daily life examples: cooking recipe, furniture assembly manual(What are input / output in each case?)

Input Algorithm Output

Page 9: Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Basics Prudence Wong pwong/teaching/comp108/201112

Algorithmic FoundationsCOMP108

9

(Basics)

Why do we study algorithms?

Given a map of n cities & traveling cost between them.

What is the cheapest way to go from city A to city B?

The obvious solution to a problem may not be efficient

Simple solution Compute the cost of each

path from A to B Choose the cheapest one

108

5

7 9

6

11

95

2

123

65

4

5

73

1

5

3

A

B

Page 10: Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Basics Prudence Wong pwong/teaching/comp108/201112

Algorithmic FoundationsCOMP108

10

(Basics)

Shortest path to go from A to B

How many paths between A & B? involving 1 intermediate city?

Simple solution Compute the cost of each

path from A to B Choose the cheapest one

A

B2

The obvious solution to a problem may not be efficient

Page 11: Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Basics Prudence Wong pwong/teaching/comp108/201112

Algorithmic FoundationsCOMP108

11

(Basics)

Shortest path to go from A to B

How many paths between A & B? involving 3 intermediate cities?

A

B2

Number of paths= 2

Simple solution Compute the cost of each

path from A to B Choose the cheapest one

The obvious solution to a problem may not be efficient

Page 12: Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Basics Prudence Wong pwong/teaching/comp108/201112

Algorithmic FoundationsCOMP108

Number of paths= 2 Number of paths= 2 + 3

12

(Basics)

Shortest path to go from A to B

How many paths between A & B? involving 3 intermediate cities?

A

B

3

Simple solution Compute the cost of each

path from A to B Choose the cheapest one

The obvious solution to a problem may not be efficient

Page 13: Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Basics Prudence Wong pwong/teaching/comp108/201112

Algorithmic FoundationsCOMP108

13

(Basics)

Shortest path to go from A to B

How many paths between A & B? involving 3 intermediate cities?

A

B

Number of paths= 2 + 3

Simple solution Compute the cost of each

path from A to B Choose the cheapest one

The obvious solution to a problem may not be efficient

Page 14: Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Basics Prudence Wong pwong/teaching/comp108/201112

Algorithmic FoundationsCOMP108

Number of paths= 2 + 3

14

(Basics)

Shortest path to go from A to B

How many paths between A & B? involving 3 intermediate cities?

A

B

6

Number of paths= 2 + 3 + 6= 11

Simple solution Compute the cost of each

path from A to B Choose the cheapest one

The obvious solution to a problem may not be efficient

Page 15: Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Basics Prudence Wong pwong/teaching/comp108/201112

Algorithmic FoundationsCOMP108

15

(Basics)

Shortest path to go from A to B

How many paths between A & B? involving 5 intermediate cities?

A

B

For large n, it’s impossible to check all paths!We need more sophisticated solutions

TOO MANY!!

Simple solution Compute the cost of each

path from A to B Choose the cheapest one

The obvious solution to a problem may not be efficient

Page 16: Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Basics Prudence Wong pwong/teaching/comp108/201112

Algorithmic FoundationsCOMP108

16

(Basics)

Shortest path to go from A to B

A

B

There is an algorithm, called Dijkstra's algorithm,

that can compute this shortest path efficiently.

Page 17: Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Basics Prudence Wong pwong/teaching/comp108/201112

Algorithmic FoundationsCOMP108

17

(Basics)

Idea of Dijkstra's algorithm

108

5

7 9

6

11

95

2

123

65

4

5

73

1

5

3

A

B10

5

7

0

Go one step from A, label the neighbouring cities with the cost.

Page 18: Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Basics Prudence Wong pwong/teaching/comp108/201112

Algorithmic FoundationsCOMP108

18

(Basics)

Idea of Dijkstra's algorithm

108

5

7 9

6

11

95

2

123

65

4

5

73

1

5

3

Choose city with smallest cost and go one step from there.

A

B

5

7

0

10

Page 19: Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Basics Prudence Wong pwong/teaching/comp108/201112

Algorithmic FoundationsCOMP108

19

(Basics)

108

5

7 9

6

11

95

2

123

65

4

5

73

1

5

3

Idea of Dijkstra's algorithm

If an alternative cheaper path is found, record this path and erase the more expensive one.

A

B

7

0

This path must NOT be used because we find a cheaper alternative path

108

5

Then repeat & repeat …

Page 20: Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Basics Prudence Wong pwong/teaching/comp108/201112

Algorithmic FoundationsCOMP108

20

(Basics)

Shortest path to go from A to B

A

B

Lesson to learn:Brute force algorithm may run slowly. We need more sophisticated algorithms.

Page 21: Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Basics Prudence Wong pwong/teaching/comp108/201112

Algorithmic FoundationsCOMP108

How to represent algorithms … Able to tell what an algorithm is and have

some understanding why we study algorithms

Able to use pseudo code to describe algorithm

Page 22: Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Basics Prudence Wong pwong/teaching/comp108/201112

Algorithmic FoundationsCOMP108

22

(Basics)

Algorithm vs Program

Algorithms are free from grammatical rules Content is more important than form Acceptable as long as it tells people how to perform a

task

Programs must follow some syntax rules Form is important Even if the idea is correct, it is still not acceptable if

there is syntax error

An algorithm is a sequence of precise and concise instructions that guide a person/computer to solve a specific problem

Page 23: Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Basics Prudence Wong pwong/teaching/comp108/201112

Algorithmic FoundationsCOMP108

23

(Basics)

Compute the n-th powerInput: a number x & a non-negative integer n

Output: the n-th power of x

Algorithm: 1. Set a temporary variable p to 1.2. Repeat the multiplication p = p * x for n times.3. Output the result p.

Page 24: Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Basics Prudence Wong pwong/teaching/comp108/201112

Algorithmic FoundationsCOMP108

Pseudo Code pseudo code:p = 1for i = 1 to n do p = p * xoutput p

C++:p = 1;for (i=1; i<=n; i++) p = p * x;cout << p << endl;

C:p = 1;for (i=1; i<=n; i++) p = p * x;printf("%d\n", p);

Java:p = 1;for (i=1; i<=n; i++) p = p * x;System.out.println(p);

Pascal:p := 1;for i := 1 to n do p := p * x;writeln(p);

24

(Basics)

Page 25: Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Basics Prudence Wong pwong/teaching/comp108/201112

Algorithmic FoundationsCOMP108

25

(Basics)

Pseudo Codeiteration i p

start 1

1 1 3

2 2 9

3 3 27

4 4 81

end 5

suppose n=4, x=3

trace table

p = 1for i = 1 to n do p = p * xoutput p

Another way to describe algorithm is by pseudo code

similar to programming language

more like EnglishCombination of

both

Page 26: Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Basics Prudence Wong pwong/teaching/comp108/201112

Algorithmic FoundationsCOMP108

26

(Basics)

Pseudo Code: conditionalConditional statement

if condition then statement

if condition then statementelse statement

if a > 0 then b = aelse b = -a output b

if a < 0 then a = -ab = aoutput b

What is computed?absolute

value

Page 27: Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Basics Prudence Wong pwong/teaching/comp108/201112

Algorithmic FoundationsCOMP108

27

(Basics)

Pseudo Code: iterative (loop)Iterative statement

for var = start_value to end_value do statement

while condition do statement

repeat statementuntil condition

condition to CONTINUE the loop

condition to STOP the loop

condition for while loop is NEGATION of condition for repeat-until loop

Page 28: Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Basics Prudence Wong pwong/teaching/comp108/201112

Algorithmic FoundationsCOMP108

28

(Basics)

for loop

i=1

i <= n?

sum = sum+i

No

Yes

Sum of 1st n nos.:

input: nsum = 0for i = 1 to n dobegin sum = sum + iendoutput sum

i=i+1

sum=0

for var = start_value to end_value do statement

the loop is executed n times

Page 29: Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Basics Prudence Wong pwong/teaching/comp108/201112

Algorithmic FoundationsCOMP108

29

(Basics)

for loop

iteration i sum

start 0

1 1 1

2 2 3

3 3 6

4 4 10

end 5

suppose n=4

Sum of 1st n nos.:

input: nsum = 0for i = 1 to n dobegin sum = sum + iendoutput sum

for var = start_value to end_value do statement

the loop is executed n times

Page 30: Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Basics Prudence Wong pwong/teaching/comp108/201112

Algorithmic FoundationsCOMP108

30

(Basics)

while loopwhile condition do

statement

Sum of 1st n numbers:

input: nsum = 0i = 1 while i <= n dobegin sum = sum + i i = i + 1endoutput sum

do the same as for-loop in previous slides

condition to CONTINUE the loop

Page 31: Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Basics Prudence Wong pwong/teaching/comp108/201112

Algorithmic FoundationsCOMP108

31

(Basics)

Sum of all input numbers:

sum = 0while (user wants to continue) dobegin ask for a number sum = sum + numberendoutput sum

while loop – example 2execute undetermined number of times

continue?

ask for numbersum = sum+number

No

Yes

Page 32: Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Basics Prudence Wong pwong/teaching/comp108/201112

Algorithmic FoundationsCOMP108

32

(Basics)

repeat-untilrepeat statementuntil condition

condition to STOP the loop

Sum of all input numbers:

sum = 0repeat ask for a number sum = sum + numberuntil (user wants to stop)output sum

Stop?

ask for numbersum = sum+number

Yes

No

also execute undetermined number of times

How it differs from while-loop?

execute at least

once

Page 33: Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Basics Prudence Wong pwong/teaching/comp108/201112

Algorithmic FoundationsCOMP108

More Example 1

33

(Basics)

input: m, nr = mq = 0while r = n dobegin r = r − n q = q + 1endoutput r and q

(@ end of) iteration r q

14

0

1 10

1

2 6 2

3 2 3

suppose m=14, n=4

(@ end of) iteration r q

1 9 1

2 4 2

suppose m=14, n=5

(@ end of ) iteration r q

1 7 1

2 0 2

suppose m=14, n=7

What is computed?

remainder & quotient

Page 34: Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Basics Prudence Wong pwong/teaching/comp108/201112

Algorithmic FoundationsCOMP108

input: m, nif m < n then swap m & ni = nwhile i = 1 dobegin if m%i==0 && n%i==0 then output i i = i-1end

suppose m=15, n=6

More Example 2

34

(Basics)

(@ end of ) iteration

output (this

iteration)

i

4

1 4 3

2 2

3 2 1

4 1 0

suppose m=12, n=4

6

1 5

2 4

3 3

4 3 2

5 1

6 1 0

What values are output?

a%b remainder ofa divided b

all common factors

Page 35: Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Basics Prudence Wong pwong/teaching/comp108/201112

Algorithmic FoundationsCOMP108

More Example 3input: m, nif m < n then swap m & ni = nfound = falsewhile i = 1 && !found dobegin if m%i==0 && n%i==0 then found = true else i = i-1endoutput i

What value is output?

Questions:what value of found makes the loop stop?when does found change to such value?

Highest Common Factor (HCF)

Greatest Common Divisor (GCD)

35

(Basics)

Page 36: Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Basics Prudence Wong pwong/teaching/comp108/201112

Algorithmic FoundationsCOMP108

36

(Basics)

Pseudo Code: ExerciseWrite a while-loop to

1.Find the product of all integers in interval [x, y]

e.g., if x is 2 & y is 5, then output is 2*3*4*5 = 120

assuming x and y are both integers

product = ??i = ??while ?? dobegin ?? i = ??endoutput ??

Page 37: Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Basics Prudence Wong pwong/teaching/comp108/201112

Algorithmic FoundationsCOMP108

37

(Basics)

Pseudo Code: ExerciseWrite a while-loop to

1.Find the product of all integers in interval [x, y]

assuming x and y are both integers

product = 1i = xwhile i <= y dobegin product = product * i i = i+1endoutput product

Page 38: Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Basics Prudence Wong pwong/teaching/comp108/201112

Algorithmic FoundationsCOMP108

38

(Basics)

Pseudo Code: Exercise 2Write a while-loop for this:

2.Given two positive integers x and y, list all factors of x which are not factors of y

if x is 30 & y is 9, output is 2, 5, 6, 10, 15, 30 (not 1 or 3)

i = ??

while ?? do

begin

if ?? then

output ??

i = ??

end

Page 39: Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Basics Prudence Wong pwong/teaching/comp108/201112

Algorithmic FoundationsCOMP108

39

(Basics)

Pseudo Code: Exercise 2Write a while-loop for this:

2.Given two positive integers x and y, list all factors of x which are not factors of y

i = 1

while i <= x do

begin

if x%i == 0 && y%i != 0 then

output i

i = i+1

end

Page 40: Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Basics Prudence Wong pwong/teaching/comp108/201112

Algorithmic FoundationsCOMP108

Challenges …

Convert while-loops to for-loops & repeat-loop

Page 41: Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Basics Prudence Wong pwong/teaching/comp108/201112

Algorithmic FoundationsCOMP108

41

(Basics)

Convert to for loops Find the product of all integers in interval [x, y]

assuming x and y are both integersproduct = ??

for i = ?? to ?? do

begin

??

end

output ??

Page 42: Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Basics Prudence Wong pwong/teaching/comp108/201112

Algorithmic FoundationsCOMP108

42

(Basics)

Convert to for loops Find the product of all integers in interval [x, y]

assuming x and y are both integersproduct = 1

for i = x to y do

begin

product = product * i

end

output product

Page 43: Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Basics Prudence Wong pwong/teaching/comp108/201112

Algorithmic FoundationsCOMP108

43

(Basics)

Convert to repeat loopsFind the product of all integers in interval [x, y]

assuming x and y are both integers

product = 1

i = x

repeat

product = product * i

i = i+1

until i > y

output product What’s wrong with this?

If given x is larger than y?

Page 44: Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Basics Prudence Wong pwong/teaching/comp108/201112

Algorithmic FoundationsCOMP108

44

(Basics)

Convert to repeat loopsFind the product of all integers in interval [x, y]

assuming x and y are both integers

product = 1

if x <= y then begin

i = x

repeat

product = product * i

i = i+1

until i > y

end

output product

Page 45: Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Basics Prudence Wong pwong/teaching/comp108/201112

Algorithmic FoundationsCOMP108

45

(Basics)

Convert to for loops (2)Given two positive integers x and y, list all

factors of x which are not factors of yfor i = ?? to ?? do

begin

if ?? then

??

end

Page 46: Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Basics Prudence Wong pwong/teaching/comp108/201112

Algorithmic FoundationsCOMP108

46

(Basics)

Convert to for loops (2)Given two positive integers x and y, list all

factors of x which are not factors of yfor i = 1 to x do

begin

if x%i == 0 && y%i != 0 then

output i

end

Page 47: Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Basics Prudence Wong pwong/teaching/comp108/201112

Algorithmic FoundationsCOMP108

47

(Basics)

Convert to repeat loops (2)Given two positive integers x and y, list all

factors of x which are not factors of yi = ??

repeat

if ?? then

??

i = ??

until ??

Page 48: Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Basics Prudence Wong pwong/teaching/comp108/201112

Algorithmic FoundationsCOMP108

48

(Basics)

Convert to repeat loops (2)Given two positive integers x and y, list all

factors of x which are not factors of yi = 1

repeat

if x%i==0 && y%i!=0 then

output i

i = i+1

until i > x

Page 49: Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Basics Prudence Wong pwong/teaching/comp108/201112

Algorithmic FoundationsCOMP108

Searching …

Page 50: Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Basics Prudence Wong pwong/teaching/comp108/201112

Algorithmic FoundationsCOMP108

Searching Input: n numbers a1, a2, …, an; and a number X

Output: determine if X is in the sequence or not

Algorithm (Sequential search):

1. From i=1, compare X with ai one by one as long as i

<= n.

2. Stop and report "Found!" when X = ai .

3. Repeat and report "Not Found!" when i > n.

50

(Basics)

Page 51: Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Basics Prudence Wong pwong/teaching/comp108/201112

Algorithmic FoundationsCOMP108

Sequential Search 12 34 2 9 7 5 six numbers

7 number X 12 34 2 9 7 5

7 12 34 2 9 7 5

7 12 34 2 9 7 5

7 12 34 2 9 7 5

7 found!

To find 7

51

(Basics)

Page 52: Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Basics Prudence Wong pwong/teaching/comp108/201112

Algorithmic FoundationsCOMP108

Sequential Search (2) 12 34 2 9 7 510

12 34 2 9 7 510

12 34 2 9 7 510

12 34 2 9 7 510

12 34 2 9 7 510

12 34 2 9 7 510 not found!

To find 10

52

(Basics)

Page 53: Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Basics Prudence Wong pwong/teaching/comp108/201112

Algorithmic FoundationsCOMP108

Sequential Search – Pseudo Codei = 1

while i <= n do

begin

if X == a[i] then

report "Found!" and stop

else

i = i+1

end

report "Not Found!"

Challenge: Modify it to include

stopping conditions in the

while loop

53

(Basics)

Page 54: Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Basics Prudence Wong pwong/teaching/comp108/201112

Algorithmic FoundationsCOMP108

Number of comparisons?i = 1

while i <= n do

begin

if X == a[i] then

report "Found!" & stop

else

i = i+1

end

report "Not Found!"

Best case: X is 1st no. 1 comparison

Worst case: X is last OR X is not found n comparisons

54

(Basics)

How many comparisons this algorithm requires?

Page 55: Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Basics Prudence Wong pwong/teaching/comp108/201112

Algorithmic FoundationsCOMP108

Finding maximum / minimum...

2nd max / min…

Page 56: Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Basics Prudence Wong pwong/teaching/comp108/201112

Algorithmic FoundationsCOMP108

56

(Basics)

Finding max from n +ve numbersinput: a[1], a[2], ..., a[n]M = 0i = 0while (i < n) dobegin i = i + 1 M = max(M, a[i])endoutput M

What about minimum?

Page 57: Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Basics Prudence Wong pwong/teaching/comp108/201112

Algorithmic FoundationsCOMP108

57

(Basics)

Finding min from n +ve numbersinput: a[1], a[2], ..., a[n]M = a[1]i = 1while (i < n) dobegin i = i + 1 M = min(M, a[i])endoutput M

How many comparisons?

n-1

Page 58: Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Basics Prudence Wong pwong/teaching/comp108/201112

Algorithmic FoundationsCOMP108

58

(Basics)

Finding 1st and 2nd mininput: a[1], a[2], ..., a[n]M1 = min(a[1], a[2])M2 = max(a[1], a[2])i = 2while (i < n) dobegin i = i + 1 if (a[i] < M1) then M2 = M1, M1 = a[i] else if (a[i] < M2) then M2 = a[i]endoutput M1, M2

Two variables: M1, M2

How to update M1, M2?

Page 59: Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Basics Prudence Wong pwong/teaching/comp108/201112

Algorithmic FoundationsCOMP108

59

(Basics)

Finding location of minimuminput: a[1], a[2], ..., a[n]loc = 1 // location of the min numberi = 1while (i < n) dobegin i = i + 1 if (a[i] < a[loc]) then loc = iendoutput a[loc]

(@ end of)

Iterationloc a[loc] i

1234

Example

a[]={50,30,40,20,10}

50

30

30

20

10

1

2

2

4

5

1

2

3

4

5