We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 57
Algorithms, programming
* Algorithm: how to systematically perform a task
* Write down as a sequence of steps
* “Recipe”, or program
* Programming language describes the steps
* What is a step? Degrees of detail
* “Arrange the chairs” vs “Make 8 rows with 10
chairs in each row"Our focus
+ Algorithms that manipulate information
* Compute numencal functions — f(x,y) = x’
* Reorganize data — arrange in ascending order
* Optimization — find the shortest route
* And more ...
* Solve Sudoku, play chess, correct spelling ...Greatest common divisor
* gcd(m,n)
* Largest k such that k divides m and k divides n
* gcd(8,12) = 4
* gcd(18,25) = 1
* 1 divides every number
* At least one common divisor for every m,nComputing gcd(m, n)
* List out factors of m
* List out factors of n
* Report the largest number that appears on both lists
* Is this a valid algorithm?
* Finite presentation of the “recipe”
* Terminates after a finite number of stepsComputing gcd(m, n)
* Factors of m must be between 1 and m
* Test each number in this range
* If it divides m without a remainder, add it to list of
factors
* Example: gcd( 14,63)
* Factors of 14
12 3 4 5 6 7 8 9 1011 12 13 14Computing gcd(14, 63)
* Factors of 14 12 7 44
* Factors of 63 13 7 9 2163
* Construct list of common factors
* For each factor Co if it is a factor of 63
* Return largest factor in this list: 7 aeComputing gcd(14, 63)
* Factors of 14 12 7 14
* Factors of 63 1 3 7 9 21 6
* Construct list of common factors
* For each factor epi ff it is a factor of 63
* Return largest factor in this list! 7 aeAn algorithm for gcd(m,n_ 8
« Use fm, fn for list of factors of m, n, respectively
* For each i from 1 to m, add 1 to fm if i divides m
* For each j from 1 ton, add j to fn if ) divides n
* Use cf for list of common factors
[12,44] [13,344.63]
* For each f in fim, add f to cf if f also appeafs in fnAn algorithm for gcd(m,n)
* Use fm, fr for list of factors of m, n, respectively
* For each i from 1 to m, add i to fm if i divides m
* For each j from 1 ton, add 3 to fn if j divides n
* Use cf for list of common factors
* For each f in fm, add f to cf if f also appears in fn
* Return largest (rightmost) value in cfOur first Python program
def gcd(m,n):
fa - 0
for i in range(1 ool):
if (Ki) == 0:
fm. append( i)
fn- (]
for j tn range(1,n+1)
if (rij) = @:
fn. append(})
ef - (1)
for f in fm:
if f in fn:
cf .append(f)
return(cf[-1])Some points to note
« Use names to remember intermediate values
em, n, fm, fn, cf, i, j, f
* Values can be single items or collections
“m,n, i, j, f are single numbers
* fm, fn, cf are lists of numbers
* Assign values to names
* Explicitly, fn = (J, and implicitly, for f in cf:
* Update them, fn.append(i)Some points to note ...
* Program is a sequence of steps
* Some steps are repeated
* Do the same thing for each item in a list
+ Some steps are executed conditionally
* Do something if a value meets some
requirement if (mu) --0An algorithm for.gcd(m, n)
+ Use fm, fn for list of factors of m, n, respectively
* For each i from 1 tom, add 1 to fm if 1 divides m
* For each ) from 1 ton. add j to fn if j divides n
* Use cf for list of common factors
* For each f in fm, add f to cf if f also appears in fn
* Return largest (rightmost) value in cfCan we do better?
* We scan from 1 to m to compute fm and again from
1 to n to compute fn
* Why not a single scan from 1 to max(m,n)?
* For each i in 1 to max(m,n), add i to fmifi
divides m and add i to fn if 1 divides nEven better?
* Why compute two lists and then compare them to
compute common factors cf? Do it in one shot.
* For each i in 1 to max(m,n), if t divides m and i
also divides n, then add 1 to cf
* Actually, any common factor must be less than
min(m,n)
* For each 1 in 1 to min(m,n), if . divides m and i
also divides n, then add 1 to cfA shorter Python program e
def gcd(m,n):
f=
for i in range(1,min(m,n)+1):
if (mi) == @ and (n%i) == 0:
cf .append(i)
return(cf[-1])A shorter Python program
def gcd(m,n):
~@f=D
for i in range(1,min(m,n}+1):
if (mi) == @ and (n%i) == @:
cf -append(i)
return(cf[-1))Do we need lists at all?
* We only need the largest common factor
* 1 will always be a common factor
* Each time we find a larger common factor, discard
the previous one
* Remember the largest common factor seen so far
and return it
* mrcf — most recent common factorNo lists!
def gcd(m,n):
for i in rengeCi,mintn n+):
if (Wi) == @ and (n#i) <= @:
mrcf = i
return(mrcf)Scan backwards?
* To find the largest common factor, start at the end
and work backwards
* Let i run from min(m,n) to 1
* First common factor that we find will be gcd!No lists! - oo
trv im in cage (| me (madd)
fy
def gcd(m,n):
i = min(m,n)
Cwhile>j> @:
LF (néi) == @ and (n%i) == 0: |
return(i)h— bx
else:
lei-lA new kind of repetition
while condition:
~*\step 1
“istep 2
j.- -
step k
* Don't know in advance how many times we will
repeat the steps
* Should be careful to ensure the loop terminates —
eventually the condition should become faise!Summary
* With a little thought, we have dramatically
simplified our naive algonthm
* Though the newer versions are simpler, they still
take time proportional to the values m and n
* Amuch more efficient approach is possibleAlgorithm for gcdCm,n)
* To find the largest common factor, start at the end
and work backwards
* Let i run from min(m,n) to 1
* First common factor that we find will be gcd!Euclid’s algorithm
* Consider gcd(m,n) with m>n
* If n divides m, return n
* Otherwise, compute gcd(n,m-n) and return that
valueEuclid’s algorithm
def gcd(m,n):
# Assume m >= n
ifmen:
(m,n) = (n,m)
if (mtn) == @:
return(n)
else:
diff = m-n
# diff > n? Possible!
return(gcd(max(n, diff) ,min(n,diff))Euclid’s algorithm =! °}
>
wY J
def gcd(m,n): \
Lu (#) Assume m >= n. Con ne’ rm t
ifmen: ¢)
(n,n) = (nym Miym
v * R= Sy Yr sn
if (mn) == 0: m2 92
return(n) A / y ne?
else: v Gs C1 ee 4 de
diff = m-n : ALh _ as
# diff > n? Possible!
Leiria return(gcd(max(n, diff) ,min(n, diff)
—— esEuclid’s algorithm, again
def gcd(m,n):
if m= n
(m,n) = (n,m)
while (mn) != @:
diff = m-n
# diff > n? Possible!
(m,n) = Cmax(n, diff) ,min(n, dif f))
return(n)Euclid’s algorithm, again
def gcd(m,n):
- — >
ifm=n
(m,n) = (nym)
while (tin) (1=)0: # “= .
diff = m-n
# diff > n? Possible!
Caen - (max(n, diff) ,min(n, diff)
return(n)Even better
* Suppose n does not divide =
* Thenm « qn + r, where q is the quotient, r is the
remainder when we divide m by n
* Assume d divides both m and n
* Thenm = ad,n = bdEven better
+ Suppose n does not divide =
+ fr, where q is the quotient, r is the
iad ler when we divide m by n
* Assume fi divities both m and n
" mitts n alm
* Soad = Ye oatzolanat,Euclid’s algorithm, revisited
def gcd(m,n):
if m= n
(m,n) = (n,m)
while (nn) t= 0:
(m,n) = (nmin) # mn < n, always!
return(n)Efficiency
* Can show that the second version of Euclid’s
algorithm takes time proportional to the number of
digits in m
* If mis 1 billion (10%), the naive algorithm takes
billions of steps, but this algorithm takes tens of
stepsgra (10) 2)
Efficiency bret
lav dug galt.) >!
* Can show that the second version of Euclid’s
algorithm takes time proportional to the number of
digits in m
* Ifmis 1 billion (10%, the naive algorithm takes
billions of steps, but this algonthm takes tens of
stepsInstalling Python
* Python is available on all platforms: Linux, MacOS
and Windows :
* Two main flavours of Python
* Python 2.7
* Python 3+ (currently 3.5.x)
* We will work with Python 3+Python 2.7 vs Python 3
* Python 2.7 is a “static” older version
* Many libraries for scientific and statistical
computing are still in Python 2.7, hence still
“alive”
* Python 3 is mostly identical to Python 2.7
* Designed to better incorporate new features
* Will highlight some differences as we go alongDownloading Python 3.5
* Any Python 3 version should be fine, but the latest
is 3.5.x
* On Linux, it should normally be installed by
default, else use the package manager
* For MacOS and Windows, download and install
from https://www.python.org/downloads/release/
python-350/
* If you have problems installing Python, search
online or ask someone!Downloading Python 3.5
* Any Python 3 version should be fine, but the latest
ts 3.5.x
* On Linux, it should normally be installed by
default, else use the package manager
* For MacOS and Windows, download and install
from https://www.python.org/downloads/release/
python-350/
* If you have problems installing Python, search
online or ask someone!Interpreters vs compilers
* Programming languages are “high level”, for
humans to understand
* Computers need “lower level” instructions
* Compiler: Translates high level programming
language to machine level instructions, generates
“executable” code
* Interpreter: Itself a program that runs and directly
“understands” high level programming languageInterpreters vs compilers
* Programming languages are “high level”, for
humans to understand
* Computers need “lower level” instructions
* Compiler: Translates high level programming
language to machine level instructions, generates
“executable” code
* Interpreter: Itself a program that runs and directly
“understands” high level programming languagePython interpreter
* Python is basically an interpreted language
* Load the Python interpreter
* Send Python commands to the interpreter to be
executed
* Easy to interactively explore language features
* Can load complex programs from files
* >>> from filename import *Installing Python
* Python is available on all platforms: Linux, MacOS
and Windows
* Two main flavours of Python
* Python 2.7
« Python 3+ (currently 3.5.x)
* We will work with Python 3+Python 2.7 vs Python 3
* Python 2.7 is a “static” older version
* Many libraries for scientific and statistical
computing are still in Python 2.7, hence still
“alive”
* Python 3 is mostly identical to Python 2.7
* Designed to better incorporate new features
* Will highlight some differences as we go alongDownloading Python 3.5
* Any Python 3 version should be fine, but the latest
is 3.5.x
* On Linux, it should normally be installed by
default, else use the package manager
* For MacOS and Windows, download and install
from https://www.python.org/downloads/release/
python-350/
* If you have problems installing Python, search
online or ask someone!Interpreters vs compilers
* Programming languages are “high level”, for
humans to understand
* Computers need “lower level” instructions
* Compiler: Translates high level programming
lanquage to machine level instructions, generates
“executable” code
* Interpreter: Itself a program that runs and directly
“understands” high level programming languagePython interpreter
* Python is basically an interpreted language
* Load the Python interpreter
* Send Python commands to the interpreter to be
executed
* Easy to interactively explore language features
* Can load complex programs from files
* >>> from filename import *[ranecorn a}
Lee
Le ono 8)
ac si)
ae)
fm-0
Le Ua SDD)
acted]
tase SP
-0
for f in fo
ay
Oto)
retern(cf[-1)>. tiene perenne cere ees Me serait ME en
edad edone py eee ed
peal sienelicl
Aieetethd
aed Soddn oh Ta LaD. A Aah e
BEAR le trait ed @.2 (clang-
pers
pet oe aot)
Pint Sieh ee ee!
cense” for mere inforastionetd
brSome resources
* The online Python tutorial is a good place to start:
https://docs.python.org/3/tutorial/index.ntm!
* Here are some books, again available online:
* Dive into Python 3, Mark Pilgrim
http://www.diveintopython3.net/
* Think Python, 2nd Edition, Allen B. Downey
http://greenteapress.com/wp/think-python-2e/Learning programming
* Programming cannot be learnt theoretically
* Must write and execute your code to fully
appreciate the subject
* Python syntax is light and is relatively easy to learn
* Go for it!