Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Introduction to Algorithms

Section 4.1

Prof. Nathan Wodarz


Math 209 - Fall 2008

Contents
1 Algorithms 2
1.1 Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.2 Properties of Algorithms . . . . . . . . . . . . . . . . . . . . . . 2

2 Examples of Algorithms 2
2.1 Pseudocode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
2.2 Maximum of Three Numbers . . . . . . . . . . . . . . . . . . . . 3
2.3 Tracing Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.4 Maximum Value of a Sequence . . . . . . . . . . . . . . . . . . . 5

1
1 Algorithms
1.1 Algorithms
Algorithms

• An algorithm is a step-by-step method of solving a problem.

• Roughly, a solution that can be accomplished by a computer.

• Named after al-Khowārizmı̄, 9th century Persian mathematician

• His work was also the source of word algebra

1.2 Properties of Algorithms


Properties of Algorithms

• We want algorithms to have the following properties:

– Input: The algorithm receives some input


– Output: The algorithm should produce some output
– Precision: Every step of the algorithm should be stated precisely
– Determinism: Each step produces a unique result, dependent only on
the input and the results of the preceding steps
– Finiteness: The algorithm terminates after finitely many steps
– Correctness: The output produced correctly solves the problem
– Generality: The algorithm applies to a set of inputs

2 Examples of Algorithms
2.1 Pseudocode
Pseudocode

• Use pseudocode to specify algorithms

• Meant to resemble code of computer languages

2
• We relax syntax requirements of actual computer languages
• Only care about eliminating ambiguity
• Idea: Can translate pseudocode into actual code with minimal effort

2.2 Maximum of Three Numbers


Finding the Maximum of Three Numbers
• Goal: Give an algorithm describing how to find maximum of three numbers
– Should take three numbers as input: a, b and c
– Should give maximum of these three as output (large)
– Actual numbers or order of numbers should not matter
• Begin by setting large = a
• Compare large and b. If b > large, set b = large
• Compare large and c. If c > large, set c = large
• Output large
• No matter which of a, b and c is maximum, returns correct maximum

Finding the Maximum of Three Numbers (Pseudocode)


Find the largest of the numbers a, b and c
Input: a, b, c
Output: large (the largest of a, b and c)

1. max3(a,b,c){
2. large = a
// if b is greater than large, update large
3. if (b > large)
4. large =b
// if c is greater than large, update large
5. if (c > large)
6. large = c
7. return large
8. }

3
2.3 Tracing Algorithms
Tracing Algorithms

• Given an algorithm, we may wish to check how it works on a particular


input

• We use a trace to follow the execution

• Follow values of variables through execution

Trace of max3
Find the largest of the numbers a, b and c
Input: a, b, c
Output: large (the largest of a, b and c)

1. max3(a,b,c){
2. large = a
// if b is greater than large, update large
3. if (b > large)
4. large = b
// if c is greater than large, update large
5. if (c > large)
6. large = c
7. return large
8. }

Input: a = 7, b = 2, c = 9

Input: a = 4, b = 6, c = 6

4
2.4 Maximum Value of a Sequence
Finding the Maximum Value of a Sequence
Find the largest of the numbers s1 , s2 , . . . , sn
Input: s, n
Output: large (the largest value
in the sequence s)

1. max(s,n){
2. large = s1
3. for i = 2 to n
4. if (si > large)
5. large = si
6. return large
7. }

Input: s = 3, 6, 1, 6, 11, 7;
n=6

Summary
Summary
You should be able to:

• Understand what an algorithm is

• Write simple algorithms

• Trace an algorithm

You might also like