KDU-SEN204-DesignandAnalysisofAlgorithm
KDU-SEN204-DesignandAnalysisofAlgorithm
KDU-SEN 204
Outlines
• Basic of an Algorithm
• Methodology of Analysis
• Divide and Conquer
• Greedy Algorithm
• Approximation Algorithms
• Graph Theory
• Heap Algorithms
• Sorting Techniques
• Searching Techniques
Basic of an Algorithm
Week 1
BASIC ALGORITHM CONCEPTS
1.0 Introduction
2.0 Objectives
3.0 What is an Algorithm?
3.1 Structure of an Algorithm
3.2 Characteristics of an Algorithm
3.2.1 Advantages of Algorithms
3.2.2 Disadvantages of Algorithms
3.3 How to write an Algorithms
3.4 Need of Algorithm
3.5 Measures need to be defined when comparing Algorithm
3.6 Reason for Analyzing Algorithms
3.7 Pseudocode
3.7.1 Advantages of Pseudocode
3.7.2 Disadvantages of Pseudocode
3.7.3 Differences between Algorithm and Pseudocode
3.7.4 Problem Case/ Example
INTRODUCTION
• Algorithm and complexity analysis is a very fundamental topic of
computer science today.
• An algorithm can be defined as a finite set of steps, which has to
be followed while carrying out a particular problem. It is
nothing but a process of executing actions step by step.
• Every computer science student should know how to define an
algorithm and they should also know how to measure the
usefulness of that Algorithm.
• More precisely, an algorithm is correct, if, for each input instance,
it gets the correct output and gets terminated.
• The concept of an algorithm originated as a means of recording
procedures for solving mathematical problems such as finding
the common divisor of two numbers or multiplying two
numbers.
OBJECTIVES
By the end of this unit, you should be able to:
• Define and describe what an algorithm is
• Enumerate the different characteristics of an algorithm
• Examine some of the advantages of algorithms
• Identify some shortcomings or disadvantages of
algorithms
• Look at the concept of a pseudocode
• Examine some benefits and shortcomings of a pseudocode
• Make a comparison between and algorithm and a
pseudocode
• Look at the various reasons why an algorithm is needed.
Algorithm
• An algorithm is a step-by-step procedure or a set of
rules/instructions designed to solve a specific problem or
accomplish a task. It's a fundamental concept in computer
science and various other fields, providing a structured
approach to problem-solving.
• From the definition we have two (2) keywords:
1) Step-by-step instructions:
– Algorithms are essentially a list of instructions or rules that, when
followed in a specific order, lead to a desired outcome.
2) Problem-solving tool:
– They are used to solve problems or perform computations in a
systematic way
• They are used by both human beings and computers.
Examples of Algorithms for Humans
• Common examples of algorithms for humans
include:
Examples in Computer Science:
• Algorithms are used in a wide variety
of applications, including:
–Searching,
–Sorting,
–Data analysis,
–Machine learning, and more
Structure of an Algorithm
An algorithm has the following structure
1. Input Step
2. Assignment Step
3. Decision Step
4. Repetitive Step
5. Output Step
Characteristics/Properties of an Algorithm
• An algorithm should have the following
characteristics or properties −
– Input: Algorithms typically take one or more inputs as data.
– Output: They produce a specific output or result based on
the input and the steps in the algorithm.
– Definiteness: Each step in an algorithm must be clearly and
unambiguously defined.
– Effectiveness: The algorithm should be able to produce the
correct output for the given input.
– Finiteness: The algorithm must terminate after a finite
number of steps.
– It should be written in simple English.
• Generality: An algorithm must be generic enough to solve
all problems of a particular class.
• Feasibility − Should be feasible with the available resources.
• Flexibility: It must be flexible enough to carry out desired
changes with no efforts.
• Efficient: The term efficiency is measured in terms of time
and space required by an algorithm to implement. Thus, an
algorithm must ensure that it takes little time and less
memory space meeting the acceptable limit of
development time.
• Independent: An algorithm must be language independent,
which means that it should mainly focus on the input and
the procedure required to derive the output instead of
depending upon the language.
Advantages of Algorithms
• Effective Communication: Since it is written in a natural
language like English, it becomes easy to understand the
step-by step delineation of a solution to any particular
problem.
• Easy Debugging: A well-designed algorithm facilitates
easy debugging to detect the logical errors that occurred
inside the program.
• Easy and Efficient Coding: An algorithm is nothing but a
blueprint of a program that helps develop a program.
• Independent of Programming Language: Since it is a
language-independent, it can be easily coded by
incorporating any high-level language.
Disadvantages of Algorithms
• Developing algorithms for complex problems
would be time consuming and difficult to
understand.
• It is a challenging task to understand complex
logic through algorithms.
Common Algorithm Types:
1) Searching Algorithms: Used to find specific
data within a collection of data, such as linear
search or binary search.
2) Sorting Algorithms: Used to arrange data in a
specific order, such as bubble sort, insertion
sort, or merge sort.
3) Divide and Conquer: This approach breaks
down a problem into smaller subproblems,
solves them independently, and then
combines the solutions.
4) Dynamic Programming: This technique
solves complex problems by breaking them
into smaller, overlapping subproblems and
storing the solutions to avoid redundant
calculations.
5) Greedy Algorithms: These algorithms make
locally optimal choices at each step with the
hope of finding a globally optimal solution.
6) Brute Force Algorithms: These algorithms
try all possible solutions to find the best
one.
Algorithm Design Considerations:
• Efficiency: An algorithm's performance,
measured by its time and space complexity, is
a crucial factor.
• Accuracy: The algorithm should produce the
correct output for all valid inputs.
• Readability: An algorithm should be easy to
understand and maintain.
• Scalability: The algorithm should be able to
handle increasing amounts of data or
complexity.
How to Write an Algorithm
• Example 1:
Example 2 of an Algorithm
• Design an Algorithm to find the area of
triangle.
• Steps
1. START
2. READ BASE LENGTH, HEIGHT
3. AREA = ½ * Base length * Height
4. PRINT BASE LENGTH, HEIGHT, AREA
5. STOP
• Example3: Write an algorithm to compute the
area and circumference of a cycle given the
diameter d. Use the formular A= πr^2 and C= 2πr
• Solution
Step 1: Start
Step 2: Get the diameter d
Step 3: Compute r=(d/2)
Step 4: Compute A= πr^2
Step 5: Compute C= 2πr
Step 6: Display the results
Step 7: Stop
• Example 4: Write an algorithm that tells you
how to wash dishes
• Solution
Step 1: start
Step 2: scrape food off dishes
Steps 3: wash the dishes with soap and water
Step 4: Rinse the dishes
Step 5: Dry them
Step 6: Stop
• Example 5: Write an algorithm to evaluate the
equation y = a(b-c)^2/d+2
• Solution
Step 1: start
Step 2: Input the value of a, b, c, d
Step 3: The value of y is to be calculated
Step 4: Calculate the value of b-c and denote f
• Step 5: Calculate the square of f
Step 6: Multiply f by a and denote g
Step 7: Calculate the value of d+2 and denote h
Step 8: divide h by g
Step 9: We get the value of y
Step 10: Print y
Step 11: Stop
Algorithm Cont.
• From the data structure point of view, following
are some important categories of algorithms −
– Search − Algorithm to search an item in a data
structure.
– Sort − Algorithm to sort items in a certain order.
– Insert − Algorithm to insert item in a data structure.
– Update − Algorithm to update an existing item in a
data structure.
– Delete − Algorithm to delete an existing item from a
data structure.
Need of Algorithms (Why do we need Algorithms?)
1. To understand the basic idea of the problem.
2. To find an approach to solve the problem.
3. To improve the efficiency of existing techniques.
4. To understand the basic principles of designing the
algorithms.
5. To compare the performance of the algorithm with respect
to other techniques.
6. It is the best method of description without describing the
implementation detail.
7. The Algorithm gives a clear description of requirements and
goal of the problem to the designer.
8. A good design can produce a good solution.
9. To understand the flow of the problem.
10. To measure the behavior (or performance) of the
methods in all cases (best cases, worst cases, average cases)
11. With the help of an algorithm, we can also identify the
resources (memory, input-output) cycles required by the
algorithm.
12. With the help of algorithm, we convert art into a science.
13. To understand the principle of designing.
14. We can measure and analyze the complexity (time and
space) of the problems concerning input size without
implementing and running it; it will reduce the cost of design.
Reasons for analyzing algorithms:
1. To predict the resources that the algorithm
requires
– Computational Time (CPU consumption).
– Memory Space (RAM consumption).
– Communication bandwidth consumption.
2. To predict the running time of an algorithm
– Total number of primitive operations executed.
Summary
• In summary, algorithms are the
foundation of problem-solving in
computer science and other
fields, providing a structured and
systematic approach to achieving
desired outcomes.
Pseudocode
• In programming, pseudocode is a simplified,
informal way to outline the logic of an algorithm
or program using plain English, acting as a
blueprint before actual code is written.
• Purpose:
• Pseudocode helps programmers plan and design
algorithms and programs by focusing on the
logic and structure without needing to worry
about specific syntax or a particular
programming language.
Characteristics of Pseudocode:
• It's written in a natural language (like English)
rather than a formal programming language.
• It's not meant to be compiled or executed by a
computer; it's for human readability and
understanding.
• It uses keywords and phrases that are commonly
understood by programmers, such as "INPUT,"
"OUTPUT," "WHILE," "FOR," "IF," "THEN," "ELSE".
• It's a flexible tool, allowing for easy modification
and refinement of the algorithm's logic.
Benefits of Pseudocode :
• Improved Communication: Pseudocode facilitates clear
communication of algorithm logic among programmers, even
those who may not be familiar with the same programming
language.
• Enhanced Planning: It helps in the initial planning and design of
algorithms, allowing programmers to think through the
problem and its solution before writing any actual code.
• Debugging: By focusing on the logic first, pseudocode can help
identify potential problems or errors in the algorithm's design
before the code is written.
• Learning: Pseudocode is a great tool for learning programming
concepts and algorithms, as it allows learners to focus on the
underlying logic without getting bogged down in syntax.
Example of a Pseudocode
Advantages of Pseudocode
• It can be quickly transformed into an actual
programming language than a flowchart since it
is similar to a programming language.
• The layman or user can easily understand it.
• It can be easily modified as compared to
flowcharts.
• Its implementation is beneficial for structured,
designed elements.
• It can easily detect an error before transforming
it into a code.
Disadvantages of Pseudocode
• Since it does not incorporate any standardized
style or format, it can vary from one user or
programmer to another.
• Error possibility is higher while transforming into
a code.
• It may require a tool for extracting out the
Pseudocode and facilitate the drawing
flowcharts.
• It does not depict/show the design.
Difference between Algorithm and Pseudocode
Week 2
How to Measure an Algorithm's Resource Consumption