0% found this document useful (0 votes)
6 views70 pages

Chapter 1 (I) - Introduction To Algorithms

Uploaded by

pearlmilklopof
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)
6 views70 pages

Chapter 1 (I) - Introduction To Algorithms

Uploaded by

pearlmilklopof
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/ 70

1

• 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.

 Assignment, Project, Practical Exam:


→ Will be uploaded to Moodle.
→ Submit your work before the deadline.
→ Always name your submission by using your ID and name.

 Midterm, Final exam:


→ Close book exams.
→ Midterm will be during the normal hours of our class.
→ Final exam will be arranged by the Exam unit.

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

Method of Assessment Total


Continuous Assessment
• Assignment 15%
• Project 20%
• Midterm 15%
• Practical Exam 10%
Final Assessment
• Final Exam 40%
8
Assessment

Method of Assessment Time


Continuous Assessment
• Assignment Week 6
• Project Week 8
• Midterm Week 7 - 9
• Practical Exam Week 14
Final Assessment
• Final Exam Set by the Exam Unit
9
The allowed Programming Language

The algorithms can be applied using any programming language, (e.g.


C, C++, Java, Python, etc.)
 In the university labs, we have Code Blocks IDE. Thus, the allowed
programming language for this course will be C or C++ to solve
assignments, projects, practical exam, etc.
 In the lecture, we will use pseudocode to represent the algorithms.
In case I provide the code, the code could be in C, C++, or Python.

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

A common misconception is that computers can do anything, when in reality, it's


the person behind the computer doing the work.
People think that simply entering a search query into a computer will yield the
desired information, believing the computer performs this task.
In reality, the work is done by a man-made procedure, known as a search
algorithm.
The computer’s role is merely to execute the search quickly and store
information in an organized way.
This search is guided by a program, designed to match the user’s needs.

13
Why Study Algorithms

With faster processors, performance is often considered less important than


other software qualities like security, extensibility, and reusability.
However, in computational science, large problem sizes make performance
critical, as longer computation times result in slower outcomes and higher costs,
especially when using external CPU resources.
The study of algorithms provides a way to express performance in relation to
problem size.

14
Why Study Algorithms

Solving and coding a problem requires understanding specifications,


organizing thoughts, and breaking down tasks.
This process can be challenging for complex programs.
Writing an algorithm saves time during construction and testing.
 Familiarity with various strategies helps identify which algorithm
solves the problem most efficiently.

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

An algorithm must have the following properties:


Finiteness: It must finish after a set number of steps.
Clarity: Each step must be unambiguous.
Sequence: Steps must have defined order, with clear start and stop points.
Inputs/Outputs: Required inputs and results must be specified.
Feasibility: Every step must be performable.
Correctness: It should solve the problem accurately.
Well-ordered and effectively computable operations, avoiding tasks like
dividing by zero or square roots of negative numbers.

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

In computational theory, an algorithm and a program are distinct.


A program doesn’t need to meet the finiteness condition, like an operating system that runs in a
“wait” loop until new jobs arrive. It only stops if the system crashes.
Since our programs always terminate, we often use 'algorithm' and 'program' interchangeably.
The design phase creates an algorithm, while the implementation phase turns it into a program.
 A program is the concrete expression of an algorithm in a specific programming language.
 Example:
Problem: finding the largest value number among 𝑛 numbers.
Input: The value of 𝑛 and 𝑛 numbers.
Output: The largest value number.

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

For a given problem, there are many ways to design


algorithms for it.
The following is a list of several popular design approaches:
→Divide-and-Conquer (D and C).
→Dynamic Programming .
→Greedy Approach.
→Backtracking Algorithms.
→Branch-and-Bound.
→Randomized Algorithms.
22
Algorithms Classification
Algorithms that use a similar problem-solving approach can be
grouped together. This classification is neither exhaustive nor disjoint.
Algorithm types we will consider include:
→Recursive algorithms
→Greedy algorithms
→Backtracking algorithms
→Branch and bound algorithms
→Divide and conquer algorithms
→Brute force algorithms
→Dynamic programming algorithms
→Randomized algorithms
23
Algorithms Classification
Recursive Algorithms:
→Solves base cases directly
→Recurs with a simpler subproblem
→ Perform extra work to transform the subproblem solution into the solution for the
original problem.
→Examples:
→Counting elements in a list.
→Checking if a value exists in a list.
Backtracking Algorithms: Use a depth-first recursive search. A backtracking algorithm:
1. Checks if a solution is found; if so, returns it.
2. For each possible choice:
→Make the choice
→Recur
→If recursion finds a solution, return it
3. If no choices remain, return failure.
24
Algorithms Classification
Divide and Conquer Algorithms:
→These split a problem into smaller subproblems, solve them recursively, and combine the
results.
→ It is considered "divide and conquer" if there are at least two recursive calls (e.g., Quick Sort,
Merge Sort).
Dynamic Programming Algorithms:
→Dynamic programming stores past results to optimize future solutions.
→It solves optimization problems with multiple solutions by finding the "best" one.
→It requires "optimal substructure" (where the best solution contains subproblem solutions)
and "overlapping subproblems" (subproblem solutions are reusable).
→Unlike divide and conquer, subproblems typically overlap.

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

Another approach to algorithm analysis is to estimate the resources an algorithm


needs, which depends on specific computational models. Here are several
commonly used models:
→Random Access Machine (RAM): Measures time and space, typical for traditional serial
computers.
→Parallel Random Access Machine (PRAM): Considers parallel time, number of processors, and
read/write constraints.
→Message Passing Model: Evaluates communication costs (number of messages) and
computational costs.
→Turing Machine: Focuses on time and space, representing an abstract theoretical model.

30
Algorithms Analysis

Algorithm analysis evaluates performance based on models and


metrics such as:
→Input Size: The length or amount of input data.
→Running Time: The number of primitive operations executed. We typically
focus on the worst-case running time unless stated otherwise.
→Order of Growth: We analyze the growth rate of running time by considering
only the leading term in a time formula (e.g., in 𝑛2 + 100𝑛 + 5000, the leading
term is 𝑛2 ).
Most algorithms handle inputs of arbitrary length, and their efficiency
is expressed as a function relating input size to the number of steps
(time complexity) or storage needed.
31
Algorithms Analysis

 Theoretical analysis often uses asymptotic measures (Big O, Ω, Θ) to estimate


performance for large inputs.
 Asymptotic estimates are preferred because they focus on the algorithm's
general behavior without being affected by implementation details.
 For example, if binary search on a sorted set of N elements takes unit time per
lookup, the time needed is at most 𝑙𝑜𝑔2 𝑁 + 1 units.

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

 Analysis of an algorithm is the


theoretical study of computer program
performance and resource usage.
Many solution algorithms can be derived
for a given problem.
The next step is to analyze those
proposed solution algorithms and
implement the best suitable solution.

38
Algorithm Phases – Design Phase

Algorithm

Design Analysis

Methods and techniques which yield a good Mathematical comparison of algorithms


and a useful algorithms to solve a problem. without implementing it.
The comparison in terms of:
• Time complexity
• Space complexity

39
Algorithm Phases – Implement Phase

Writing and coding the algorithm.


Be sure to use the programming language you are familiar with.
If you are part of a team, choose the best programmer to write the code.
To write an efficient algorithm, understand and write each line in your
code carefully.
40
Algorithm Phases – Experiment 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

The design and analysis of algorithms is a circular process.


After completing each part of this circle, and testing the results of the
algorithm, if the results are matching the expected output, the operation
will be ended.
42
Algorithm Phases

The quality of good algorithm can be measured by:


Time
Memory
Accuracy
Sequence
Generality

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

How do you express algorithms in a clear, precise, and unambiguous manner?


How to Represent algorithms?
1. In terms of Natural Language.
2. In terms of Formal Programming Language.
3. In terms of Pseudocode.
4. Flowcharts.

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

3. In terms of Pseudocode: 1. Get array list and its size n


→ Example: 2. Assign max = list[1]
3. For i = 2 to n Do
1. IF (list[i] > max ) THEN
1. max = list[i]
2. End if
Advantages: 4. End For
→A middle-ground compromise. 5. Display max
→Resembles many popular programming languages.
→Relatively free of grammatical rules.
→Only well-defined statements are included “A programming language without details”.
Disadvantages:
→Compared with flowchart it is difficult to understand the program logic
49
Representing Algorithms - Pseudocode
Consists of natural language like statements that precisely describe the steps of
an algorithm or program.
Statements describe actions.
Focuses on the logic of the algorithm or program and avoids language-specific
elements.
Written at a level so that the desired programming code can be generated almost
automatically from each statement.
The steps are numbered. Subordinate numbers and/or indentation are used for
statements in selection and repetition structures.

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

 Write Pseudocode to express the following:


1. Add Two Numbers. (Simple Pseudocode Example).
2. Check if a Number is Positive or Negative (Pseudocode If Else Example).
3. Find the biggest of three (3) Numbers (Pseudocode If Else Example).
4. Print Numbers from 1 to 100. (Pseudocode For Loop Example)
5. Issue for driving license (Pseudocode If Else Example)
6. Calculate the Sum and Average of 𝑛 Numbers. (Pseudocode For Loop
Example).

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.

You might also like