Algorithm
Algorithm
and Notation
Algorithm
Statements that provide solution
1. Create a fb page
2. Create a post
3. Click boost post
Finiteness
Definiteness
Input
Output
Effectiveness
Example of Finiteness
Properties of an Algorithm
Example 1:
PROBLEM SOLVING PROCESS
Problem Domain
Machine Domain
Solution Domain
WRITING AN ALGORITHM
Step 1 - START
Step 2 - DECLARE three integers a, b
& c Step 3 - DEFINE values of a & b
Step 4 - ADD values of a & b
Step 5 - STORE output of Step 4 to c
Step 6 - PRINT c
Step 7 - STOP
floo
r
3.14
ceiling
5.65
mod
5%2
Mathematical Functions in C
Mathematical Functions in Java
ALGORITHM EFFICIENCY
Criteria
Time Complexity
Space Complexity
Method
Emperical Method
Theoretical Method
Process
Big O-Notation
Time Complexities
Linear Time
Constant Time
Quadratic Time
Types of measurement
Worst Case
Average Case
Best Case
Constant Time
Example:
a = 10
b = 10
c=a+5*b
Linear Time
Example 1: Example 2:
a=0 a=0
while b < n Do while b < n Do
b=b+1 b=b+3
Quadratic Time
Must be proportional
N = 6 //36 times
N = 10 //100 times
Q 1:
O(1) or O(n)?
Constant time (example)
A=0
While a < 11 Do
A=a+1
Q 2:
Linear Time
Series 1
Graphical Representation of
O(n)
Time = .266
Time = .281
Logarithmic
Log2(16) = 2 x 2 x 2 x 2
=4
Log2(8) = 2 x 2 x 2
=3
FM-AA-CIA-15 Rev. 0 10-July-2020
Study Guide in CC 104 (Data Structures and Algorithms) Module 1 Basic Concepts and Notation
MODULE TITLE
MODULE OVERVIEW
This module aims to enable you to understand the concepts and properties of
algorithm, the problem solving process, different mathematical functions that can be used
to analyze an algorithm and measuring algorithm complexity using Big – Oh Notation.
Problem solving and programming exercises were included in every section in order to test
your understanding of the lesson.
LEARNING OBJECTIVES
LEARNING CONTENTS
What is Algorithm?
Study Guide in CC 104 (Data Structures and Algorithms) Module 1 Basic Concepts and Notation
As you can see, there are certain steps that must be followed. These steps are in
specific order, even though some of the steps could be rearranged. For example, steps 5
and 6 can be reversed. You will also notice that aside from the sequence of steps that must
be performed, the entire process is finite and reach its end point which enable you to
achieve your goal (in this case, making a cup of tea).
Study Guide in CC 104 (Data Structures and Algorithms) Module 1 Basic Concepts and Notation
Introduction
Programming is a problem-solving process, i.e., the problem is identified, the data
to manipulate and work on is distinguished and the expected result is determined. It is
implemented in a machine known as a computer and the operations provided by the
machine is used to solve the given problem.
Problem domain includes the input or the raw data to process, and the output or
the processed data. For instance, in sorting a set of numbers, the raw data is set of numbers
in the original order and the processed data is the sorted numbers.
Study Guide in CC 104 (Data Structures and Algorithms) Module 1 Basic Concepts and Notation
Source: JEDI Teacher’s Manual for Data Structures and Algorithm
The machine domain consists of storage medium and processing unit. The storage
medium – bits, bytes, words, etc – consists of serially arranged bits that are addressable
as a unit. The processing units allow us to perform basic operations that include arithmetic,
comparison and so on.
Solution domain, on the other hand, links the problem and machine domains. It is
at the solution domain where structuring of higher level data structures and synthesis of
algorithms are of concern.
EXAMPLE:
Problem – Design an algorithm to add two numbers and display the result
Step 1 – START
Step 2 – Declare three integers a, b & c
Step 3 – Define values of a & b
Step 4 – Add values of a & b
Study Guide in CC 104 (Data Structures and Algorithms) Module 1 Basic Concepts and Notation
Algorithms tell the programmers how to code the program. Alternately, the algorithm
can be represented using a flow chart (which was discussed during your CC 102 class) or
using a pseudocode.
Source:
https://www.tutorialspoint.com/data_stru
ctures_algorithms/algorithms_basics.ht
m
Study Guide in CC 104 (Data Structures and Algorithms) Module 1 Basic Concepts and Notation
2. Show the algorithm for computing the density of a given square. Use the
following as your inputs:
M – Mass of the object
V – Volume of the square (S6)
D=M/V
- Density of the object
3. Compute the greatest common divisor of two nonnegative integers’ p and q as
follows: If q is 0, the answer is p. If not, divide p by q and take the remainder r.
The answer is the greatest common divisor of q and r.
Introduction
Mathematical functions are useful in the creation and analysis of algorithms. In this
section, some of the most basic and most commonly used functions with their properties
are listed.
Identities
Study Guide in CC 104 (Data Structures and Algorithms) Module 1 Basic Concepts and Notation
The following are the identities related to the mathematical functions defined above
a. ⎣ -5.3 ⎦
b. ⎣ 6.14 ⎦
c. 8 mod 7
d. 3 mod –4
e. –5 mod 2
f. 10 mod 11
g. ⎾ (15 mod –9) + ⎣ 4.3 ⎦ ⏋
Algorithm efficiency is measured in two criteria: space utilization and time efficiency.
Time complexity (Time Efficiency) of an algorithm quantifies the amount of time
taken by an algorithm to run as a function of the length of the input.
o Execution time - amount of time spent in executing instructions of a given
algorithm. Notation: T(n). Several factors that affect the execution time
include:
Study Guide in CC 104 (Data Structures and Algorithms) Module 1 Basic Concepts and Notation
input size
Instruction type
machine speed
quality of source code of the algorithm implementation
quality of the machine code generated from the source code
by the compiler
Space complexity (Space Utilization) of an algorithm quantifies the amount of
space or memory taken by an algorithm to run as a function of the length of the input.
- Time and space complexity depends on lots of things like hardware,
operating system, processors, etc.
There are several methods on how to measure the efficiency of an algorithm. Some
of these methods include Empirical Method, Theoretical Method, Input and the Process.
The Input
- Algorithms should be given the same input to compare them fairly.
- Algorithms may be compared when they are given inputs of the same
size to keep the efficiency analysis manageable but still fair.
- Factors to consider when analysing algorithms using Input:
Input Size
Study Guide in CC 104 (Data Structures and Algorithms) Module 1 Basic Concepts and Notation
The Process
- The input is not under the control of the algorithm designer. The
algorithm should is supposed to work for all valid inputs.
- Process is what the designer can control.
- One way to analyze algorithms is to find an upper bound to the time and
space required by an algorithm.
The space required is the total memory that needs to be allocated.
This includes the input, constants and variables at runtime.