Chapter 1 (I) - Introduction To Algorithms
Chapter 1 (I) - Introduction To Algorithms
• Course Information
Dr. Talal Ali
Design and Analysis of Algorithms School of Computer Science and Technology, Xiamen University
(CST207) [email protected]
Lecture (1) - week (1) Office: A1-383
2025 • Consultation hours:
✓Monday 11:00 AM – 1:00 PM
✓Wednesday 2:00 PM – 4:00 PM
CST207
Course Learning Outcomes
At the end of the course, students will be able to:
➔ Display ability to utilize mathematical tools for analyzing
algorithms for solving different cases of problems correctly and
efficiently.
➔ Evaluate the suitability of algorithms in solving computing related
problems e.g. path finding.
➔ Demonstrate problem solving skills via concepts, techniques, and
proficiencies in algorithm analysis which leads to potential
applications in self development stage e.g., furthering studies.
➔ Interpret the correctness of algorithms using mathematical
proofs.
3
Course Content Outline
1. Introduction to Algorithms
2. Analysing Algorithms.
3. Recursive algorithm and recursive equation
4. Divide-and-conquer algorithms
5. Dynamic programming
6. Greedy algorithms.
7. Graph algorithms.
8. NP complete Theory.
9. Backtracking.
10. Branch and Bound.
4
Introduction to the class
Learning hours per week are 4 hours. It will be mix between lecture,
lab and solving problems.
Consultation hours :
→Contact me any time through email, Microsoft team if you have any question, or if you want
to come to my office.
→ I have 4 consultation hours per week:
Monday 11:00 AM – 1:00 PM
Wednesday 02:00 PM – 4:00 PM
Attendance:
Attendance less than 80%
→ Students may be barred from examination/final assessment.
→ International student may not be able to renew their Student Visa.
5
Introduction to the class
Course materials - Moodle
Lecture slides:
→ University rule - uploaded within three days as scheduled class time
→ Normally be ready before the class.
6
Introduction to the class
Class rules:
Respect each other.
Be kind, polite, and courteous to others.
No discrimination – any kind.
Contribute equally during group work.
7
Assessment
10
Chapter 1
Introduction to Algorithms
11
Agenda
Why Study Algorithms
Algorithm Definition
Characteristics of an Algorithm
Algorithms VS. Programs
Algorithms Design Techniques
Algorithms Classification
Algorithms Analysis
How to calculate the Running Time of an Algorithm
How to write an Algorithm
Steps for developing an Algorithm
Algorithm Phases
Operations in Algorithms
Representing Algorithms 12
Introduction to Algorithms
13
Why Study Algorithms
14
Why Study Algorithms
15
Definitions
The 'Algorithm' is given after the name of Abu Jaifar Muhammad ibn Musa Al-
Khwarizmi, Ninth Century, and it is defined as follows :
→An algorithm is a set of rules for carrying out calculation either by hand or on
a machine.
→An algorithm is a sequence of computational steps that transform the input
into the output.
→An algorithm is a sequence of operations performed on data that have to be
organized in data structures.
→A finite set of instruction that specify a sequence of operations to be carried
out in order to solve a specific problem or class of problems.
→An algorithm is an abstraction of a program to be executed on a physical
machine (model of computation).
16
Algorithm Properties
17
Algorithm Properties
Every algorithm should have five characteristics:
1. Input
2. Output
3. Definiteness
4. Effectiveness
5. Termination
(1) and (2) mean an algorithm must produce outputs and may have inputs.
(3) requires operations to be clear and unambiguous (e.g., no instructions like 'compute x/0’).
(4) means each step must be executable by a person with basic tools (e.g. pencil and paper) in
finite time.
(5) ensures the algorithm finishes after a set number of steps, ideally in a reasonable time.
Definition: An algorithm is a sequence of definite and effective steps that terminates with correct
output from given input.
18
Simple Example
Let us try to present the scenario of a man brushing his own teeth as an algorithm as follows:
→Step 1: Take the brush
→Step 2: put the paste on it
→Step 3: Start brushing
→Step 4: Clean
→Step 5: Wash
→Step 6: Stop
Without knowing the statement of problem, one might think this is a car-cleaning algorithm due
to ambiguities in each step (e.g., Step 1 could mean a toothbrush or paintbrush).
For an instruction to be algorithmic, it must be unambiguous.
Even rewriting Step 2 as 'apply toothpaste' raises questions like where to apply it.
A definite and effective instruction ensures successful execution, but proper termination must
also be considered when designing an algorithm.
19
Algorithms VS. Programs
20
Algorithms VS. Programs
Steps:
1. Let the value of the first be the largest value denoted by BIG.
2. Let R denote the number of the remaining numbers. R= n-1.
3. If R !=0 then it is implied that the list is still not exhausted. Therefore look
for the next number called NEW.
4. Now R becomes R – 1.
5. If NEW is greater than BIG then replace BIG with the value of NEW.
6. Repeat steps 3 to 5 until R becomes zero.
7. Print BIG
8. Stop
End of algorithm
21
Algorithms Design Techniques
25
Algorithms Classification
Greedy Algorithms:
→Greedy algorithms solve optimization problems by choosing the best option at each step
without considering future consequences, aiming for a global optimum by making local
optimal choices.
Branch and Bound Algorithms:
→Branch and bound algorithms solve optimization problems by forming a tree of subproblems.
→At each node, upper and lower bounds are applied. If they match, the solution is feasible; if
not, the problem is split into subproblems.
→This process continues, trimming the tree using the best known solution until all nodes are
solved or discarded.
26
Algorithms Classification
Brute Force Algorithms: A brute force algorithm tests all possibilities until a
solution is found. It can be:
→Optimizing: Finds the best solution (e.g., the optimal path for a traveling salesman).
→Satisfying: Stops when a solution good enough is found (e.g., a path within 10% of optimal).
Randomized Algorithms: A randomized algorithm uses random numbers at least
once in its process.
→Examples:
✓ In Quick Sort, a random pivot is chosen.
✓ Factoring large numbers by randomly selecting divisors.
27
Algorithms Analysis
Algorithm analysis estimates the time an algorithm takes based on the size of the
input (N).
For example, it might measure comparisons in a sorting algorithm or operations in
matrix multiplication.
This helps in choosing between different algorithms.
The analysis does not provide exact time measurements, as this would depend on
factors like computer type, user load, processor speed, and compiler optimization.
Factors like computer speed affect program performance, but analysis focuses on
the algorithm itself, not the specific hardware.
Exact operation counts are impractical, especially as N grows large. The difference
between algorithms with N + 5 and N + 250 operations becomes negligible.
28
Algorithms Analysis
Although directly analyzing algorithm speed can be accurate, it's often complex and time-consuming.
We will instead analyze algorithms in a higher-level language (e.g., C, C++, Python, Java, pseudocode).
Analysis Rules:
→Assume a basic time unit.
→Each of the following operations takes 1 time unit: assignment, single I/O, Boolean operations,
numeric comparisons, arithmetic operations, function return, array indexing, and pointer
dereferencing.
→Selection statements (e.g., if, switch) take the time to evaluate the condition plus the maximum time
of individual clauses.
→For loops, consider the time for the loop body, check, update, and setup, assuming the maximum
number of iterations.
→Function calls take 1 time unit for setup plus the time for parameter calculations and function
execution.
29
Algorithms Analysis
30
Algorithms Analysis
32
Part 2
33
How to write an Algorithm
In order to write an algorithm, the following things are needed as a prerequisite:
→The problem that is to be solved by the algorithm.
→The constraints of the problem must be considered while solving the problem.
→The input to be taken to solve the problem.
→The expected output when the problem is solved.
→The solution to the problem and taking into consideration the given constraints.
The algorithm is written with help of the above parameters such that it solves the
problem.
34
Steps for developing an Algorithm
1. Define the problem: State the problem you are trying to solve in clear and
concise terms.
2. List the inputs (Information needed to solve the problem) and the outputs
(what the algorithm will produce as a result).
3. Describe the steps needed to convert or manipulate the inputs to produce the
outputs. Start at a high level first and keep refining the steps until they are
effectively computable operations.
4. Test the algorithm: choose data sets and verify that your algorithm works.
35
Algorithm Phases
Design
Analyse
Implement
Experiment
36
Algorithm Phases – Design Phase
The first stage is to identify the problem and fully understand it.
Consultation with people interested in similar problems.
37
Algorithm Phases – Analysis Phase
38
Algorithm Phases – Design Phase
Algorithm
Design Analysis
39
Algorithm Phases – Implement Phase
Experiment and test your algorithm with different cases and variables.
Detect if the algorithm results match the desired and expected output.
If there is any fault after testing the algorithm, you may amend the
previous phases.
41
Algorithm Phases
43
Algorithm design and analysis process
44
Operations in Algorithms
Sequential operations:
→A sequential operation carries out a single well-defined task. When that task is finished, the
algorithm moves on to the next operation.
Conditional operations:
→A conditional operation is the “question-asking” instructions of an algorithm. It asks a
question and then selects the next operation to be executed according to the question-
answer.
Iterative operations:
→An iterative operation is a “looping” instruction of an algorithm. It tells us not to go on to the
next instruction, but, instead, to go back and repeat the execution of a previous block of
instructions.
45
Representing Algorithms
46
Representing Algorithms
1. In terms of Natural Language:
→ Example: Assume the first item is largest. Look at each of the remaining
items in the list and if it is larger than the largest item so far, make a note of it.
The last noted item is the largest in the list when the process is complete.
Advantages:
→Familiar.
Disadvantages:
→Verbose
→Imprecise -> Ambiguity
→Rarely used for complex or technical algorithms.
47
Representing Algorithms
2. In terms of Formal Programming Language
→Example: #Sum of natural numbers up to num
num = int(input(“enter a number”))
if num < 0:
print(“Enter a positive number”)
else:
sum = 0
Advantages:
while (num > 0):
→Precise → Unambiguous sum += num
→Will ultimately program in these languages. num -= 1
print (“The sum is “, sum)
Disadvantages:
→It has syntactic details which are not important in the algorithm design phase.
→Not familiar to the person who is not interested in this programming language.
48
Representing Algorithms
50
Representing Algorithms - Pseudocode
What is Pseudocode?
→Pseudocode (derived from pseudo and code) is a compact and informal high-
level description of a computer programming algorithm that uses the
structural conventions of some programming languages, but typically omits
details that are not essential for the understanding of the algorithm, such as
subroutines, variable declarations and system-specific code.
Notes:
→No standard pseudo code syntax exists.
→A program in pseudocode is not an executable program.
→Flowcharts can be thought of a graphical alternative to pseudocode.
51
Pseudocode Language Constructs
Computation / Assignment
→ Compute Var1 as the sum of x and y
→ Assign expression to var2
→ Increment / decrement counter 1
Input / Output
→ Input: Get var1, var2, …
→ Output: Display var1, var2, …
52
Pseudocode Language Constructs
Selection
Single-Selection IF
1. IF condition THEN ( IF condition is true, then do subordinate statement 1, etc.
if condition is false, then skip statements)
1.1 statement 1
1.2 etc.
Double-Selection IF
2. IF condition THEN ( IF condition is true, then do subordinate statement 1, etc.
if condition is false, then skip statements and execute statements under ELSE)
1.1 statement 1
1.2 etc.
53
Pseudocode Language Constructs
Selection
Double-Selection IF
3. ELSE (else if condition is not true, then do subordinate statement 2, etc.)
3.1 statement 2
3.2 statement 3
4. SWITCH expression TO
4.1 case 1: action 1
4.2 case 2: action 2
4.3 etc.
4.4 default action x
54
Pseudocode Language Constructs
Repetition
5. WHILE condition (while condition is true, then do subordinate statements)
5.1 statement 1
5.2 etc.
6. DO-WHILE structure (like WHILE, but tests condition at the end of the loop. Thus, statements in
the structure will always be executed at least once.)
DO
6.1 statement 1
6.2 etc.
WHILE condition
7. FOR structure ( a specialized version of WHILE for repeating execution of statements a specific
number of times)
FOR bounds on repetition
8.1 statement 2
8.2 etc.
55
Pseudocode Example
Express an algorithm to get two numbers from the user (dividend and divisor), testing to make
sure that the divisor number is not zero, and display their quotient using pseudocode.
→Solution:
1. Get dividend, divisor
2. IF divisor = 0 THEN
1. Display error message "divisor must be non-zero”
2. Exit algorithm
3. Compute quotient = dividend / divisor
4. Display dividend, divisor, quotient
56
Pseudocode Exercises
57
Representing Algorithms - Flowcharts
A flowchart is a graphical representation of an algorithm. Once the flowchart is
drawn, it becomes easy to write the program in any high-level language.
58
Representing Algorithms - Flowcharts
59
Representing Algorithms - Flowcharts
Advantages of flowcharts
Makes logic clear.
Effective analysis
Easy to code and test.
Disadvantages of flowcharts
Difficult to use for large programs.
Difficult to modify.
60
Flowchart Construct
Flowchart Construct
61
Flowchart Construct
Flowchart Construct
62
Flowcharts Construct
Flowchart Construct
63
Flowchart Construct
Switch case tests the value of a
variable and compares it with
multiple cases. Once the case match
is found, a block of statements
associated with that case is executed.
Each case in a block of a switch has a
different name/number which is
referred to as an identifier.
The value provided by the user is
compared with all the cases inside
the switch block until the match is
found.
64
Flowchart - Exercise
Draw a flowchart to find the largest of three numbers A, B, and C.
Solution:
65
Flowchart - Exercise
Draw a flowchart to print sum of first 100
natural numbers:
→ Solution:
66
Flowchart - Exercise
Draw a flowchart to calculate
factorial 4:
→Solution:
4! = 4 × 3 × 2 × 1
67
Flowchart Exercises
Draw flowchart to express the following:
1. Add Two Numbers.
2. Check if a Number is Positive or Negative.
3. Find the biggest of three (3) Numbers.
4. Print Numbers from 1 to 100.
5. Issue for driving license.
6. Calculate the Sum and Average of 𝑛 Numbers.
68
References
Required References
1. Sandeep Sen (Author), Amit Kumar (Author) , Design and Analysis of Algorithms: A
Contemporary Perspective, Cambridge University Press (May 23, 2019).
Further Readings
1. Divyansh Pratap Singh Parmar, Data structure and Algorithm: COMPUTER SCIENCE,
Independently published, 2019.
2. Michael Soltys-Kulinicz , Introduction To The Analysis Of Algorithms, World Scientific
Publishing Co Pte Ltd , 3rd edition (March 30, 2018).
3. Rosen, K. H. Discrete Mathematics and Its Applications.
4. Cormen, T. H., Leiserson, C. E., Rivest, R. L., & Stein, C. (2022). Introduction to algorithms. MIT
press.