Lecture 1: Introduction#
Algorithm#
Procedure mapping each input to a single output (deterministic)
Algorithm solves a problem if it returns a correct output for every problem input
Examples: An algorithm tp solve birthday matching
maintain a record of names and birthdays
interview each student in some order
if birthday exists in record, return found pairs
else add name and birthday to record
return none if last student inteviewed without success
Correctness#
Induction: recursion for arbitrarily large inputs
Example: proof of correctness of birthday matching lagorithm
induct on
: the number of student in the recordHypothesis; if first
contain match, return match before interviewing studentBase case:
, first contains no matchAssume for induction hypothesis holds for
, and consider .If first
contains a match, already returned a match by inductionElse first
do not have match, so if first has match, match containsThen algoithm checks directly whether birday of student
exists in first .
Efficiency#
How fast does an algorithm produce a correct output?
Could measure time, but want performance to ne machine independent.
Idea!!! count number of fixed-time operations algorithm takes to return - asympototic
Expect to depend on the size of input: larger input suggests longer time
Efficient if returns in
polynomial time
with respect to inputSometimes no efficient algorithm exists for a problem:
L20
Asympototic Notation: ignore constant factors and lower order terms
upper bounds
, lower bounds , tight bounds
Model of Computation#
specification for what operations on the machine can be performed in
timeModel in this class is called the
Word-RAM
Machine word: block of
bits ( is the word size of a -bit Word-RAM)Memory: addressable sequence of machine words
Processor supports many constant time operation on a
number of words (integers):integer arithmetic: (+, -, *, //, %)
logical operators: (&&, ||, !, ==, <, >, <=, >=)
given word
, can read work at address , write word to address
Memory address must be able to access every place in memory
Requirement:
# bits represent the largest memory address,e.g.,32-bit words -> max ~ 4 GB memory, 64-bit words -> max ~ 16 exabytes of memory
In order to precisely calculate the resources used by an algorithm, we need to model how long a computer takes to perform basic operations.
Specifying such a set of operations provides a model of computation upon which we can base our analysis.
In this class, we will use the w-bit Word- RAM model of computation, which models a computer as a random access array of machine words called memory, together with a processor that can perform operations on the memory.
A machine word is a sequence of Word-RAM
processor can perform basic binary operations on two machine words in constant time, including addition, subtraction, multiplication, integer division, modulo, bitwise operations, and binary comparisons.
In addition, given a word Word-RAM
has a word size of at least Word-RAM
model of a byte-addressable 64-bit machine allows inputs up to ∼ 1010 GB
in size.