1.1L Introduction
1.1L Introduction
Algorithmic Foundations
And Problem Solving
Lecture 1 Introduction
Dr Yushi Li
Department of Intelligent Science
Module information …
Teaching Team
Module leader: Dr Jia Wang
Office Room SD537
Email: [email protected]
Office hours: 10:00-12:00, Monday
Dr. Yushi Li
Office Room SD561
Email: [email protected]
Office hours: 10:00-12:00, Monday
3
Teaching & Learning: Structure
Lectures
Assignments Examination
/Class Tests 4
(Intro + MI)
Teaching & Learning: Teaching
Lectures
Group 1
Venue: SIP-SC176
Time: 9:00 -11:00 Tuesday
Group 2
Venue: SIP-SC176
Time: 14:00 -16:00 Tuesday
Tutorials:
Group 1
Venue: SIP-SC176
Time: 11:00 -13:00 Friday
Group 2
Venue: SIP-SC176
Time: 14:00 -16:00 Friday
5
(Intro + MI)
Teaching & Learning: Assessment
6
(Intro + MI)
More about Assessment
➢ plagiarism is the practice of presenting the
thoughts or writings of another or others as
original
– consequence? Please refer to the student handbook
for answer.
➢ What happen if you are ill when there is a
deadline for assignment?
– Get medical proof and ask for extension
– This is not an excuse to commit plagiarism
7
(Intro + MI)
Contents (Tentative)
methodology Asymp Brute Divide Dynamic Greedy Space/Time Branch & Complexity
totic force & Programmin Bound Theory
idea g
Conquer
problems
Efficiency Big-O
Textbook
Introduction to the Design
and Analysis of Algorithms
Anany V. Levitin
Villanova University
Addison Wesley
Additional Reading
Introduction to Algorithms
Thomas H. Cormen,
Charles E. Leiserson,
Ronald L. Rivest
The MIT Press
9
(Intro + MI)
Questions?
10 (Intro + MI)
Why INT102?
• Algorithm design is a foundation for efficient
and effective programs
Algorithms + Data Structures = Programs
*Donald Knuth is the author of the famous book “The Art of Computer Programming”
11 (Intro + MI)
Aims
What do we mean by good?
➢ To give an overview of the study of algorithms in terms of their
efficiency.
How to achieve?
➢ To introduce the standard algorithmic design patterns employed in
the development of efficient algorithmic solutions.
Can we prove?
➢ To describe the analysis of algorithms in terms of the use of formal
models of Time and Space.
Can all problems be solved efficiently?
➢ To give a brief introduction to the subject of computational
complexity theory and its use in classifying computational problems.
12 (Intro + MI)
Ready to start …
Learning outcomes
14
(Intro + MI)
What is an algorithm?
15
(Intro + MI)
Desk Assembly Manual
Algorithm
16
(Intro + MI)
Why do we study algorithms?
• Example: given a sequence of numbers, say,
1, 3, 15, 90, 100, 101, 203, 305
and a number X check if X is in the sequence.
– How do you check it?
– If X=1, how many comparisons do you need?
– If X=100, how many comparisons do you need?
– If X=305, how many comparisons do you need?
– If X=102, how many comparisons do you need?
17
(Intro + MI)
A Straightforward Solution
Compare X with number in the sequence one by one.
If X equals to a number, then answer “Yes”, stop.
Otherwise “No.”
• Example: given a sequence of numbers, say,
1, 3, 15, 90, 100, 101, 203, 305
and a number X check if X is in the sequence.
– How do you check it?
– If X=1, how many comparisons do you need?
– If X=100, how many comparisons do you need?
– If X=305, how many comparisons do you need?
– If X=102, how many comparisons do you need?
18
(Intro + MI)
A Trick Solution
19
(Intro + MI)
A Trick Solution
• Example: given a sequence of numbers, say,
1, 3, 15, 90, 100, 101, 203, 305
and a number X check if X is in the sequence.
– How do you check it?
– If X=1, how many comparisons do you need?
– If X=100, how many comparisons do you need?
– If X=305, how many comparisons do you need?
– If X=102, how many comparisons do you need?
20
(Intro + MI)
Lesson to learn
21
(Intro + MI)
Some Well-known Computational Problems
• Sorting
• Searching
• Graph and Combinatorial Problems
– Shortest paths in a graph
– Minimum spanning tree
– Traveling salesman problem
– Knapsack problem
– ……
• Problem in Number Theory
– Primality testing
– ……
How to represent
algorithms …
Algorithm vs Program
Still remember? An algorithm is a sequence of precise and concise
instructions that guide a computer to solve a specific problem
25
Pseudo Code
iteration i p
Another way to before 1
describe algorithm 1 1 3
is by pseudo code 2 2 9
3 3 27
p = 1 4 4 81
for i = 1 to n do end 5
p = p * x suppose n=4,
output p x=3
trace table
similar to programming language
Yes
Computing sum of all (keyboard) input numbers:
sum = 0 ➢ this loop is also
repeat
executed for
ask for a number
sum = sum + number undetermined
until (user wants to stop) number of times
output sum ➢ How it differs
from while-loop?
36
Pseudo Code: exercise
37
Hint (1)
38
Hint (2)
List all factors of a given positive integer x
for i = 1 to x do
begin
you may use
if (x%i == 0) then the operator
output i '%'.
end a%b means the
remainder of
a divided b
39
Convert to while loops
Find the product of all integers in the interval [x, y],
assuming that x and y are both integers
product = 1
i = x
while i <= y do
begin
product = product * i
i = i+1
end
output product
40
Convert to while loops (2)
41
Convert to repeat loops
Find the product of all integers in the interval [x, y],
assuming that x and y are both integers
product = 1
if x <= y then begin
i = x
repeat
product = product * i
i = i+1
until i > y
output product
end
42
Convert to repeat loops (2)
43
Learning outcomes
44