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

CSC 1204: Data Structures and Algorithms

J. Kizito

Makerere University

e-mail: john.kizito@mak.ac.ug
www: https://www.socnetsolutions.com/~jona
materials: https://www.socnetsolutions.com/~jona/materials/CSC1204
e-learning environment: http://muele.mak.ac.ug
office: block A, level 3, department of computer science
alt. office: institute of open, distance, and eLearning

Algorithms Design

Kizito (Makerere University) CSC 1204 Oct, 2013 1 / 11


Overview

1 Algorithm Design

Kizito (Makerere University) CSC 1204 Oct, 2013 2 / 11


Algorithm Design

Algorithms
How do we solve problems?

We “just do”
Guesswork-and-luck
Trial-and-error
Experience (possibly someone else’s)
“Scientifically”

Kizito (Makerere University) CSC 1204 Oct, 2013 3 / 11


Algorithm Design

Algorithms
The Problem-solving Process (1)

Kizito (Makerere University) CSC 1204 Oct, 2013 4 / 11


Algorithm Design

Algorithms
The Problem-solving Process (2)

Kizito (Makerere University) CSC 1204 Oct, 2013 5 / 11


Algorithm Design

Algorithm
Definition

A sequence of instructions specifying the steps required to accomplish


some task
Working definition: A sequence of instructions describing how to do a
task (as opposed to actually executing the instructions)
Instructions are given in the order in which they are performed
(“executed”)
An unambiguous description that makes clear what has to be
implemented

Kizito (Makerere University) CSC 1204 Oct, 2013 6 / 11


Algorithm Design

Algorithms
Properties of an Algorithm

An algorithm expects a defined set of inputs


An algorithm produces a defined set of outputs
An algorithm is guaranteed to terminate and produce a result, always
stopping after a finite time
Most algorithms are guaranteed to produce the correct result
If an algorithm imposes a requirement on its inputs (called a
precondition), that requirement must be met.
For example, a precondition might be that an algorithm will only
accept positive numbers as an input. If preconditions aren’t met, then
the algorithm is allowed to fail by producing the wrong answer or
never terminating

Kizito (Makerere University) CSC 1204 Oct, 2013 7 / 11


Algorithm Design

Algorithms
Components of an Algorithm

Variables and values


Instructions
Sequences (of instructions)
Procedures (involving instructions)
Selections (between instructions)
Repetitions (of instructions)
Also required: Documentation

Kizito (Makerere University) CSC 1204 Oct, 2013 8 / 11


Algorithm Design

Example: find max()


Problem: Given a list of positive numbers, return the largest number on the list
Inputs: A list L of positive numbers. Must contain at least one number
Outputs: A number n, which will be the largest number of the list

Algorithm
1 Set max to 0
2 For each number x in the list L, compare it to max. If x is larger, set max to x
3 max is now set to the largest number in the list

An Implementation in C Meets the criteria for being an algorithm?


int find max(char *L) { Is it unambiguous?
int x, max = 0;
Does it have defined inputs and
for (x = 0; L[x] != ’\0’; x++)
outputs?
if (x > max)
max = x; Is it guaranteed to terminate?
return max; Does it produce the correct result?
}Kizito (Makerere University) CSC 1204 Oct, 2013 9 / 11
Algorithm Design

Example: find max()


Problem: Given a list of positive numbers, return the largest number on the list
Inputs: A list L of positive numbers. Must contain at least one number
Outputs: A number n, which will be the largest number of the list

Algorithm
1 Set max to 0
2 For each number x in the list L, compare it to max. If x is larger, set max to x
3 max is now set to the largest number in the list

An Implementation in C Meets the criteria for being an algorithm?


int find max(char *L) { Is it unambiguous?
int x, max = 0;
Does it have defined inputs and
for (x = 0; L[x] != ’\0’; x++)
outputs?
if (x > max)
max = x; Is it guaranteed to terminate?
return max; Does it produce the correct result?
}Kizito (Makerere University) CSC 1204 Oct, 2013 9 / 11
Algorithm Design

Example: find max()


Problem: Given a list of positive numbers, return the largest number on the list
Inputs: A list L of positive numbers. Must contain at least one number
Outputs: A number n, which will be the largest number of the list

Algorithm
1 Set max to 0
2 For each number x in the list L, compare it to max. If x is larger, set max to x
3 max is now set to the largest number in the list

An Implementation in C Meets the criteria for being an algorithm?


int find max(char *L) { Is it unambiguous?
int x, max = 0;
Does it have defined inputs and
for (x = 0; L[x] != ’\0’; x++)
outputs?
if (x > max)
max = x; Is it guaranteed to terminate?
return max; Does it produce the correct result?
}Kizito (Makerere University) CSC 1204 Oct, 2013 9 / 11
Algorithm Design

find max(): A Recursive Version


Algorithm
1 If L is of length 1, return the first item of L
2 Set v1 to the first item of L
3 Set v2 to the output of performing find max() on the rest of L
4 If v1 is larger than v2, return v1. Otherwise, return v2

Implementation in C Meets the criteria for being an algorithm?


int find max(char *L) { Is it unambiguous?
int v1, v2;
Does it have defined inputs and
if (strlen(L) == 1)
outputs?
return L[0];
v1 = L[0]; Is it guaranteed to terminate?
v2 = find max(&L[1]); Does it produce the correct result?
if (v1 > v2) return v1; Formally, one would provide a careful
else return v2; proof of correctness
}
Kizito (Makerere University) CSC 1204 Oct, 2013 10 / 11
Algorithm Design

find max(): A Recursive Version


Algorithm
1 If L is of length 1, return the first item of L
2 Set v1 to the first item of L
3 Set v2 to the output of performing find max() on the rest of L
4 If v1 is larger than v2, return v1. Otherwise, return v2

Implementation in C Meets the criteria for being an algorithm?


int find max(char *L) { Is it unambiguous?
int v1, v2;
Does it have defined inputs and
if (strlen(L) == 1)
outputs?
return L[0];
v1 = L[0]; Is it guaranteed to terminate?
v2 = find max(&L[1]); Does it produce the correct result?
if (v1 > v2) return v1; Formally, one would provide a careful
else return v2; proof of correctness
}
Kizito (Makerere University) CSC 1204 Oct, 2013 10 / 11
Algorithm Design

find max(): A Recursive Version


Algorithm
1 If L is of length 1, return the first item of L
2 Set v1 to the first item of L
3 Set v2 to the output of performing find max() on the rest of L
4 If v1 is larger than v2, return v1. Otherwise, return v2

Implementation in C Meets the criteria for being an algorithm?


int find max(char *L) { Is it unambiguous?
int v1, v2;
Does it have defined inputs and
if (strlen(L) == 1)
outputs?
return L[0];
v1 = L[0]; Is it guaranteed to terminate?
v2 = find max(&L[1]); Does it produce the correct result?
if (v1 > v2) return v1; Formally, one would provide a careful
else return v2; proof of correctness
}
Kizito (Makerere University) CSC 1204 Oct, 2013 10 / 11
Algorithm Design

More on Algorithms

See document on “Algorithm design”

Kizito (Makerere University) CSC 1204 Oct, 2013 11 / 11

You might also like