0% found this document useful (0 votes)
19 views

1.1L Introduction

Uploaded by

roseateankhs.0g
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

1.1L Introduction

Uploaded by

roseateankhs.0g
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 44

INT102

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

Dr. Pengfei Fan


Office Room SD559
Email: [email protected]
Office hours: 15:00-17:00 Wednesday

3
Teaching & Learning: Structure

Lectures

Tutorials Help channels

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

• 2 assessments (20% of the final mark)


Assessment 1: week 6 - week 7 (10%)
Assessment 2: week 12 – week 13 (10%)
• Final Examination (80% of the final mark)
A written examination
Date: TBA.

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

Sorting Selection Merge- Count


/ sort sorting
Bubble/i
nsertion
Searching Binary-
searchin
g
String Horspool
marching algorithm

Graph DFS/BFS Bellman- MST Traveling


ford/Warshal salesman,
l
Dikstra’s
For Job
Assembly-
line Shortest assignment
path
Complexity P/NP
Text Book and Reference

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

• More from Donald Knuth*:


“ A person well-trained in computer science knows how to
deal with algorithms: how to construct them,
understand them, manipulate them, analyze them. ...
(This knowledge) is a mental tool that will be a definite
aid to the understanding of other subjects, whether they
be chemistry, linguistics, or music, etc. …”

*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

➢Able to tell what an algorithm is and have


some understanding why we study algorithms

➢Able to use pseudo code to describe algorithm

14
(Intro + MI)
What is an algorithm?

• A sequence of precise and concise instructions


that guide you (or a computer) to solve a class
of specific problem

Input Algorithm Output

• Daily life examples: cooking recipe, furniture


assembly manual
(What are input / output in each case?)

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

Observation: the sequence is ordered (sorted).

1.If the sequence is empty, then answer “No”, stop.


2.Compare X with number in the middle of the sequence.
3.If X equals to the number, then answer “Yes”, stop.
4.If X is greater than the number, go to step 1 with the
subsequence on the right of the number.
5.If X is less than the number, go to step 1 with the
subsequence on the left of the number.

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

The most straightforward


(or brute force) algorithm
for a problem may run
slowly. We need more
sophisticated solutions.

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

Algorithms are free from grammatical rules


– Content is more important than form
– Acceptable as long as it tells people how to perform a
task

Programs must follow some syntax rules


– Form is important
– Even if the idea is correct, it is still not acceptable if there
is syntax error
More Generally: Program = Data Structure + Algorithm 24
Computing the n-th power
Input: a number x & a non-negative integer n
Output: the n-th power of x
Algorithm:
1. Set a temporary variable p to 1.
2. Repeat the multiplication p = p * x for n times.
3. Output the result p.

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

more like English Combination of both 26


Pseudo Code: statement
Assignment statement
variable = expression
e.g., assign the expression 3 * 4
to the variable result
result = 3 * 4
compound statements
begin
statement1
statement2
end
27
Pseudo Code: conditional
Conditional statement if a < 0 then
if condition then a = -a
statement abs = a
if condition then output abs
statement
else
statement if a > 0 then
abs = a
else
What do these two abs = -a
algorithms compute? output abs
28
Pseudo Code: iterative (loop)
Iterative statement
for var = start_value to end_value do
statement
condition to CONTINUE the loop
while condition do
statement
repeat
statement condition to STOP the loop
until condition

condition for while loop is


NEGATION of
condition for repeat-until loop 29
for loop
for var = start_value to end_value do
statement

1. var is first assigned the Computing sum of the


value start_value. first n numbers:
2. If var  end_value, input n
execute the statement. sum = 0
3. var is incremented by 1 for i = 1 to n do
and then go back to step begin
2. sum = sum + i
end
output sum
the loop is executed for n times
30
for loop
for var = start_value to end_value do
statement

Computing sum of the


i=1 first n numbers:
No
input n
i <= n? sum = 0
Yes for i = 1 to n do
sum = sum+i begin
sum = sum + i
end
output sum
the loop is executed for n times
31
for loop
for var = start_value to end_value do
statement

suppose n = 4 Computing sum of the


iteration i sum first n numbers:
before 0 input n
1 1 1 sum = 0
2 2 3 for i = 1 to n do
3 3 6 begin
sum = sum + i
4 4 10
end
end 5 output sum
the loop is executed for n times
32
while loop condition to CONTINUE the loop

while condition do 1. if condition is true,


execute the statement
statement
2. else stop
3. go back to step 1
Computing sum of the first n numbers:
input n
sum = 0
i = 1
while i <= n do
begin
sum = sum + i this while-loop
i = i + 1 performs the same
end task as the for-loop in
output sum the previous slides
33
while loop – example 2
this loop is executed for
undetermined number of
times
Computing sum of all (keyboard) input numbers:
sum = 0
while (user wants to continue) do
begin
ask for a number
sum = sum + number
end No
continue?
output sum
Yes
ask for number
sum = sum+number 34
(Intro + MI)
repeat-until conditio
n to STOP
repeat the loop 1. execute the statement
statement 2. if condition is true,
until condition stop
3. go back to step 1

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?
35
repeat-until conditio
n to STOP
repeat the loop ask for number
statement sum = sum+number
until condition
No
Stop?

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

Write for-loops for the followings


1.Find the product of all integers in the interval
[x, y], assuming that x and y are both integers
– e.g., if x and y are 2 and 5, then the output should
be 2*3*4*5 = 120
2.List all factors of a given positive integer x
– e.g., if x is 60, then the output should be 1, 2, 3, 4,
5, 6, 10, 12, 15, 20, 30, 60

37
Hint (1)

Find the product of all integers in the interval [x, y],


assuming that x and y are both integers
product = 1
for i = x to y do
begin
product = product * i
end
output product

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)

List all factors of a given positive integer x


i = 1
while i <= x do
begin
if x%i == 0 then
output i
i = i+1
end

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)

List all factors of a given positive integer x


i = 1
repeat
if x%i == 0 then
output i
i = i+1
until i > x

43
Learning outcomes

✓Able to tell what an algorithm is and have some


understanding why we study algorithms
✓Able to use pseudo code to describe algorithm

44

You might also like