Algorithms - Chapter 1 Intro
Algorithms - Chapter 1 Intro
Prescribed Book
Anany Levitin, Introduction to the Design and Analysis of Algorithms, International Edition, Addison-Wesley, 2003
Definitions
An algorithm is a sequence of unambiguous instructions for solving a problem
For obtaining a required output for any legitimate input in a finite amount of time Does not require implementation in software Not an answer but a method for deriving an answer
Background
Named after Muhammad ibn Musa al-Khwarizmi 9th century Persian mathematician Algorithmics is more than a branch of computer science. It is the core of computer science and, in all fairness, can be said to be relevant to most of science, business and technology.
David Harel, Algorithmics: the Spirit of Computing
Donald Knuth
Professor Emeritus of The Art of Computer Programming at Stanford University
Notion of an Algorithm
problem algorithm input computer output
Each step of the algorithm must be unambiguous The range of inputs must be specified carefully The same algorithm can be represented in different ways Several algorithms for solving the same problem may exist with different properties
What is an Algorithm?
Recipe, process, method, technique, procedure, routine, with the following requirements:
Finiteness
Terminates after a finite number of steps
Definiteness
Rigorously and unambiguously specified
Input
Valid inputs are clearly specified
Output
Can be proved to produce the correct output given a valid input
Effectiveness
Steps are sufficiently simple and basic
Example: Sorting
Statement of problem:
Input: A sequence of n numbers <a1, a2, , an > Output: A reordering of the input sequence <a1, a2, , an> s.t. ai aj whenever i < j
Selection Sort
Input: array a[1],..,a[n] Output: array a sorted in non-decreasing order Algorithm:
for i=1 to n swap a[i] with smallest of a[i],,a[n] for i1 to n do min i for j i+1 to n do if a[j] < a[min] min j swap a[i] and a[min]
Constraints:
It is night and you have 1 flashlight. Max of 2 on the bridge at one time. All start on the same side Those crossing must have the flashlight with them. The flashlight must be walked back and forth (no throwing) People walk at different speeds: person A = 1 minute to cross, person B = 2 minutes, person C = 5 minutes, person D = 10 minutes A pair walks at the speed of the slower persons pace
Extension to Exercise
This is an instance of a problem. How would you generalise it? Can you derive an algorithm to solve this generalised problem?
Must output the minimum time required for crossing Are there any special cases to watch out for? Are there any constraints on the input?
Extension Solution
Input: a list <a> of crossing times for n people, numbered 1, , n Output: total time to cross Strategy: use 1 & 2 as shuttles and send the others across in pairs
for i 2 to n/2 do t a[2] t t + a[1] t t + a[i*2] t t + a[2] t t + a[2] return t // 1 & 2 across // 1 back // i*2 & (i*2)-1 across // 2 back // 1 & 2 across
Extension Problems
This is an inadequate solution that falsely assumes certain inputs List may not be sorted in ascending order
Sort <a>
Is not optimal for all inputs, e.g. {1, 20, 21, 22}
Can you quantify the nature of these inputs? Suggest an alternative
Decide on computation type, approximation, data structures and algorithm design technique Design an Algorithm
Prove Correctness
Make sure you are solving the correct problem and for all legitimate inputs
Analyzing an Algorithm
Consider time efficiency, space efficiency, simplicity, generality, optimality Analysis can be empirical or theoretical
Coding an Algorithm
Graph Problems
Graph Traversal, Shortest Path, Graph Colouring
Combinatorial Problems
Find a combinatorial object - permutation, combination, subset subject to constraints
Geometric Problems
Closest-Pair, Convex-Hull
Numerical Problems
Solving systems of equations, computing definite integrals, evaluating functions, etc.
Dynamic programming
Technique for solving problems with overlapping subdomains
Course Structure
Introduction (Ch. 1) - today Fundamentals of the Analysis of Algorithms (Ch. 2)
Asymptotic notations, analysis of recursive and non-recursive algorithms, empirical analysis
Practicals
Weekly 3-hour mini prac-exams Open Book Given a problem specification that is solvable using the algorithm design strategies presented in the course
Design Algorithm Code it in C++ or Java Submit it for automatic marking
No internet, e-mail or communicating with class-mates But can get limited help from tutors 1st one is a practice one