INTRODUCTION
Selina Ochukut
selinao@uonbi.ac.ke
What is an algorithm?
• An algorithm is any well-defined
computational procedure that takes some
value, or set of values, as input and produces
some value, or set of values, as output. An
algorithm is thus a sequence of computational
steps that transform the input into the output.
What is an algorithm
Example:
• A procedure for returning web pages
containing given keywords
• A procedure for computing n! given n
• An instruction for assembling a piece of
furniture
• Various sorting and searching algorithms
Why care about correctness?
• Suppose your program crashes or gives
incorrect answers 1 time in 100,000
Does this matter for:
• Low usage webpage?
• High usage webpage?
• Banking system?
• Aircraft jet engine control system?
Why care about efficiency?
Beware:
• Recent history is that hardware improved rapidly
• This compensated for many poor programs 3GHz
desktop with 2GB RAM on a small program
• almost any implementation will work ‘instantly’
• These might give a false sense that efficiency
does not matter.
Why care about efficiency?
But:
• Clock speeds are ‘stuck’
• Not all programs can exploit multi-core, GPGPU,
clouds, but must run on the single core
• Mobile devices are much less powerful
• Also ‘inefficient’ consumes more energy
poorly written apps will drain the mobile device
battery
2% (?) of world electricity goes in server farms
Running Time: “finite” but how big?
• Most algorithms transform input data into
output data.
• The running time of an algorithm typically grows
with the input size.
• Average case time is often difficult to determine.
• We (often) focus on the worst case running time
it is easier to analyse
Theoretical Analysis
• Uses a “high-level” description of the
algorithm instead of an implementation
• Characterizes running time as a function of
the input size, n.
• Takes into account all possible inputs
• Allows us to evaluate the speed of an
algorithm independently of the
hardware/software environment
Pseudocode
• High-level description of an algorithm
• More structured than English prose Less
detailed than a program
• Preferred notation for describing
algorithms
• Hides program design issues
Example
• Algorithm arrayMax(A, n)
Input array A of n integers
Output maximum element of A
currentMax = A[0]
for i = 1 to n - 1 do
if A[i] > currentMax then
currentMax = A[i]
return currentMax
Primitive Operations
• Basic computations performed by an
algorithm Identifiable in pseudocode
• Largely independent from the programming
language
• Exact definition not important
• Assumed to take a constant amount of time
in the “RAM model”
The Random Access Machine
(RAM) Model
• A CPU
• A potentially-unbounded bank of memory cells,
each of which can hold an arbitrary number or
character
• Memory cells are numbered and accessing any
cell in memory takes unit time.
• Note that RAM can stand for both “Random
Access Machine” and “Random Access Memory,”
Which is an unfortunate, but standard, over-
loading.
Limitations of RAM model
• “… can hold an arbitrary number …” ?
Can we really expect to store
“9385663592861518003516661777357777771771
773747177174715717777761365661618161616”
in one cell on a real computer?
• Here, we ignore such “bignum” issues: “all
numbers are of equal size, as they all fit in a
single register of the CPU”
• 64bits (signed int) allows up to
9,223,372,036,854,775,807
Counting Primitive Operations
• By inspecting the pseudocode, we can
determine the maximum number of primitive
operations executed by an algorithm, as a
function of the input size
Algorithm arrayMax(A, n) # operations
currentMax =A[0] ?
for i = 1 to n - 1 do ?
if A[i] > currentMax then ?
currentMax= A[i] ?
return currentMax ?
Total INCLASS EXERCISE
Counting Primitive Operations
• Worst case number of primitive operations
executed as a function of the input size, n
Algorithm arrayMax(A, n) # operations
currentMax =A[0] 2
for i = 1 to n - 1 do 1
if A[i] > currentMax then 2(n-1)
currentMax A[i] 2 (n -1) (worst case)
{ increment counter: i++ } 2 (n - 1) (“hidden”)
{ test counter: i ≤ (n-1) } 2 (n - 1) (“hidden”)
return currentMax 1
Total (8n-4)
Counting is “Vague”
• Consider “c =A[i]” then ‘full’ process can be
– get A = pointer to start of array A, and store into a register
– get i, and store into a register
– compute A+i = pointer to location of A[i], and store back
into a register
– get value of “*(A+i)” (from RAM) and store value it into a
register
– Copy the value into the location of c in the RAM
• might not want to count all this, e.g. just count
• ‘plus’ of “A+i”
• the assignment
Counting is “Vague” (cont)
• There can be multiple right answers – if you
get‘2’ and I count ‘4’ then it does not mean you
are wrong!
• Note: If I think an answer is ‘4’ then ‘2’ is
probably also acceptable – but “2 n” probably will
not be.
• It is most important to be able to know what is
happening in the underlying process
– be able to link to C and assembly level notions
• • be able to use this to give a reasonably
consistent justification of your answers

1. introduction

  • 1.
  • 2.
    What is analgorithm? • An algorithm is any well-defined computational procedure that takes some value, or set of values, as input and produces some value, or set of values, as output. An algorithm is thus a sequence of computational steps that transform the input into the output.
  • 3.
    What is analgorithm Example: • A procedure for returning web pages containing given keywords • A procedure for computing n! given n • An instruction for assembling a piece of furniture • Various sorting and searching algorithms
  • 4.
    Why care aboutcorrectness? • Suppose your program crashes or gives incorrect answers 1 time in 100,000 Does this matter for: • Low usage webpage? • High usage webpage? • Banking system? • Aircraft jet engine control system?
  • 5.
    Why care aboutefficiency? Beware: • Recent history is that hardware improved rapidly • This compensated for many poor programs 3GHz desktop with 2GB RAM on a small program • almost any implementation will work ‘instantly’ • These might give a false sense that efficiency does not matter.
  • 6.
    Why care aboutefficiency? But: • Clock speeds are ‘stuck’ • Not all programs can exploit multi-core, GPGPU, clouds, but must run on the single core • Mobile devices are much less powerful • Also ‘inefficient’ consumes more energy poorly written apps will drain the mobile device battery 2% (?) of world electricity goes in server farms
  • 7.
    Running Time: “finite”but how big? • Most algorithms transform input data into output data. • The running time of an algorithm typically grows with the input size. • Average case time is often difficult to determine. • We (often) focus on the worst case running time it is easier to analyse
  • 8.
    Theoretical Analysis • Usesa “high-level” description of the algorithm instead of an implementation • Characterizes running time as a function of the input size, n. • Takes into account all possible inputs • Allows us to evaluate the speed of an algorithm independently of the hardware/software environment
  • 9.
    Pseudocode • High-level descriptionof an algorithm • More structured than English prose Less detailed than a program • Preferred notation for describing algorithms • Hides program design issues
  • 10.
    Example • Algorithm arrayMax(A,n) Input array A of n integers Output maximum element of A currentMax = A[0] for i = 1 to n - 1 do if A[i] > currentMax then currentMax = A[i] return currentMax
  • 11.
    Primitive Operations • Basiccomputations performed by an algorithm Identifiable in pseudocode • Largely independent from the programming language • Exact definition not important • Assumed to take a constant amount of time in the “RAM model”
  • 12.
    The Random AccessMachine (RAM) Model • A CPU • A potentially-unbounded bank of memory cells, each of which can hold an arbitrary number or character • Memory cells are numbered and accessing any cell in memory takes unit time. • Note that RAM can stand for both “Random Access Machine” and “Random Access Memory,” Which is an unfortunate, but standard, over- loading.
  • 13.
    Limitations of RAMmodel • “… can hold an arbitrary number …” ? Can we really expect to store “9385663592861518003516661777357777771771 773747177174715717777761365661618161616” in one cell on a real computer? • Here, we ignore such “bignum” issues: “all numbers are of equal size, as they all fit in a single register of the CPU” • 64bits (signed int) allows up to 9,223,372,036,854,775,807
  • 14.
    Counting Primitive Operations •By inspecting the pseudocode, we can determine the maximum number of primitive operations executed by an algorithm, as a function of the input size Algorithm arrayMax(A, n) # operations currentMax =A[0] ? for i = 1 to n - 1 do ? if A[i] > currentMax then ? currentMax= A[i] ? return currentMax ? Total INCLASS EXERCISE
  • 15.
    Counting Primitive Operations •Worst case number of primitive operations executed as a function of the input size, n Algorithm arrayMax(A, n) # operations currentMax =A[0] 2 for i = 1 to n - 1 do 1 if A[i] > currentMax then 2(n-1) currentMax A[i] 2 (n -1) (worst case) { increment counter: i++ } 2 (n - 1) (“hidden”) { test counter: i ≤ (n-1) } 2 (n - 1) (“hidden”) return currentMax 1 Total (8n-4)
  • 16.
    Counting is “Vague” •Consider “c =A[i]” then ‘full’ process can be – get A = pointer to start of array A, and store into a register – get i, and store into a register – compute A+i = pointer to location of A[i], and store back into a register – get value of “*(A+i)” (from RAM) and store value it into a register – Copy the value into the location of c in the RAM • might not want to count all this, e.g. just count • ‘plus’ of “A+i” • the assignment
  • 17.
    Counting is “Vague”(cont) • There can be multiple right answers – if you get‘2’ and I count ‘4’ then it does not mean you are wrong! • Note: If I think an answer is ‘4’ then ‘2’ is probably also acceptable – but “2 n” probably will not be. • It is most important to be able to know what is happening in the underlying process – be able to link to C and assembly level notions • • be able to use this to give a reasonably consistent justification of your answers