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

Notes - Unit 1 Part 2file

Algorithm design is the process of creating step-by-step procedures for solving problems, involving techniques like pseudocode and flowcharts. Common approaches include top-down design, stepwise methodology, and a mathematical approach, each emphasizing breaking down problems into manageable parts. Additionally, recursion is a programming technique where a function calls itself to solve subproblems, requiring careful definition of base cases to avoid infinite loops.

Uploaded by

simeonbanda73
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 views11 pages

Notes - Unit 1 Part 2file

Algorithm design is the process of creating step-by-step procedures for solving problems, involving techniques like pseudocode and flowcharts. Common approaches include top-down design, stepwise methodology, and a mathematical approach, each emphasizing breaking down problems into manageable parts. Additionally, recursion is a programming technique where a function calls itself to solve subproblems, requiring careful definition of base cases to avoid infinite loops.

Uploaded by

simeonbanda73
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/ 11

BoCS1100 INTRODUCTION TO PROGRAMMING

UNIT 1 - Part 2

WHAT IS ALGORITHM DESIGN ?


Algorithm design is the process of developing a step-by-step procedure or set of
instructions for solving a particular problem or accomplishing a specific task. It involves
analyzing a problem, identifying its essential components, and designing an algorithm to
solve it. Good algorithm formulation and design involves creating algorithms that are
accurate, efficient, and easy to understand and implement.
The design of an algorithm may involve the use of pseudocode or flowcharts to
represent the steps involved in solving the problem. The algorithm design process is an
essential step in software development, as it ensures that the resulting software solution
is effective, efficient, and archives the tasks and goals it was intended to solve.

Algorithm Representation
The two most common ways to represent algorithms are :
1. Pseudocode :- Pseudocode is a high-level description of an algorithm that uses
a mixture of natural language and programming language syntax. It is used to
describe the steps of an algorithm in a way that is easy to read and understand,
without being tied to a specific programming language. Pseudocode often
includes keywords such as "if," "while," and "for" to describe control structures,
and uses indentation and other formatting conventions to indicate the flow of the
algorithm.
An Example of PseudoCode

BEGIN
Let : A be = 1
Let : B be = 3
Let : C be A + B
Show Value of : C
END
2. Flowcharts :- A flowchart is a visual representation of an algorithm that uses
symbols and arrows to depict the sequence of steps involved in solving a
problem. Flowcharts use different shapes to represent various elements of the
algorithm, such as start and end points, input/output operations, decision points,
and process steps. Arrows connect the different shapes to show the flow of
control between them. Flowcharts are useful for illustrating the logic of an
algorithm and can be used to communicate the algorithm to others who may not
have a programming background.
An Example of A Flowchart

ALGORITHM DESIGN ?
The 3 most commonly used approaches used to develop algorithms are the Top-down
design , the stepwise approach and the attitude of users (AoU) approach.

The Top-Down Algorithm Design Approach


The Top-down design is an algorithm design approach that involves breaking down a
complex problem into smaller units, sub-problems or modules, solving them individually,
and then combining them to solve the larger problem. The top-down design approach
starts with the problem statement and then proceeds to break down the problem into
smaller, more manageable sub-problems. Each sub-problem is solved individually and
then combined with other sub-problems to solve the larger problem.
The top-down design approach is useful when dealing with complex problems that
cannot be easily solved by a single algorithm. It helps to divide the problem into smaller,
more manageable components, allowing for easier implementation and testing of
individual algorithms. This approach is commonly used in large software development
and is an effective way to design and implement large and complex computer programs.
The top-down design approach begins with the identification of the overall problem that
needs to be solved, and then breaks it down into smaller and more manageable
sub-problems or modules. Each sub-problem is further broken down into smaller
modules until the problem is broken down into its smallest components.
The top-down design approach is sometimes referred to as "divide and conquer"
because it involves dividing a complex problem into smaller and more manageable
pieces that can be tackled more easily.
One of the key advantages of top-down design is that it allows programmers to focus on
the high-level design of the system before focusing on the small details of the program.
It also makes it easier to manage large and complex projects by breaking them down
into smaller, more manageable components.
However, one potential drawback of this approach is that it can sometimes result in a
lack of attention to detail or oversimplification of complex problems. Additionally, it can
sometimes be difficult to identify the correct level of detail for each unit or module (how
far to go in dividing the problem) for instance if you divide a car into engine, body, and
wheels. Should the wheel be further divided into rim and tyre ? such situations can lead
to rework or inefficiencies in the development process.

The top-down design approach involves the following 6 steps:


1. Problem Definition :- Define the problem statement and identify the main
components or sub-problems that need to be solved. This step involves
understanding the objective or task that a computer program should achieve.
Understanding the task or objective is important because it helps in the the next
step.
2. Decomposition :- Break down the main problem into smaller sub-problems. This
involves dividing the objective of the program into multiple parts and assigning the
parts to different team members of the programming team.
3. Algorithm Design :- Design algorithms to solve each sub-problem. This involves
developing or formulating algorithms for each individual part of the program.
4. Implementation :- Implement each algorithm to solve the sub-problem. This
involves writing and analyzing the algorithm for each part of the program to ensure
that the algorithm parts are correct, efficient and achieve their objective.
5. Integration :- Combine the sub-solutions to solve the main problem. This step
involves combining all the algorithms for the individual parts to form 1 large
algorithm.
6. Testing :- Test the complete solution for correctness and efficiency. After integrating
all the algorithm parts, it is important to test the combined algorithm assembled from
the smaller algorithms.

The Stepwise Methodology Approach


The Stepwise Approach is an algorithm formulation method which is a variation of the
Top-down / divide and conquer approach where the task to be carried out is broken
down into a number of sub-objectives, each of which can be described by an algorithm
which is smaller and simpler. Because each such sub-algorithm is simpler than the
entire process the designer usually has a clearer idea of how to efficiently construct it,
and can therefore sketch it in more detail than if he tried to handle the whole algorithm
at once. The sub-algorithms can themselves be broken into smaller pieces which are
even simpler and can again be expressed in even more detail and precision. The
stepwise algorithm continues this way until each of its steps is sufficiently detailed and
precise to allow execution by the computer processor.
In the Stepwise approach you start with a general set of steps to solve the problem,
defining each in turn. Once you have defined each of the steps you then break the
problem down into a series of smaller sub-steps. Once this is complete you keep on
going until you have described the problem in such a level of detail that you can code a
solution to the problem.

A Visual Representation of the Stepwise Approach

The stepwise methodology for developing an algorithm involves the following steps:
1- Understand the problem : The first step in developing an algorithm is to clearly
understand the problem that needs to be solved. This involves identifying the inputs,
outputs, constraints, and requirements of the problem.
2 - Define the problem : Once you understand the problem, the next step is to define it
in a clear and concise manner. This involves breaking the problem down into smaller
and more manageable parts.
3 - Plan the solution : After defining the problem, you need to plan the solution by
identifying the steps or operations required to solve the problem. This can be done
using techniques such as flowcharts, pseudocode, or other visual aids.
4 - Develop the algorithm : Using the plan developed in the previous step, you can
now start developing the algorithm. This involves writing the code in a programming
language that will solve the problem.
5 - Testing : Once the algorithm has been developed, it needs to be tested to ensure
that it works correctly and meets the requirements of the problem. If there are any
issues or errors, the algorithm needs to be refined and modified until it works correctly.
6 - Document the algorithm : It is important to document the algorithm by providing a
detailed description of the problem, the inputs and outputs, and the steps involved in
solving the problem. This documentation can be used by other developers or users who
may need to understand or modify the algorithm in the future.

Stepwise Algorithm Development Example


Goal : Design an algorithm for a domestic servant robot to make a cup of instant
coffee.
Below are the initial version of the algorithm can be the following :

(1) boil water


(2) put coffee in cup
(3) add water to cup

The steps in this algorithm are not detailed enough for the robot to be able to execute
them. Each step must therefore be refined into a sequence of simpler steps, each
specified
in more detail than the original. Thus the step:

(1) boil water

might be refined into :

(1.1) fill boiler with water


(1.2) switch on boiler
(1.3) wait until the water boils
(1.4) wait for the boiler to switch off
The Mathematical Approach to Algorithm Design
Another approach to algorithm design is the mathematical approach. This approach is a
variation of the Top-Down design. However, in this approach strong emphasis is placed
on using well known mathematical principles and theories while developing an
algorithm. For example: the mathematical principles of reiteration, factorisation and
elimination can be used to eliminate duplicate steps in the algorithm during its
development.
The following are the major steps in the mathematical approach to algorithm design :
1. Define the problem :- The first step is to define the problem and its requirements.
This involves identifying the input and output of the algorithm, as well as any
constraints or limitations.
2. Identify the mathematical principles :- Once the problem is defined, the next step
is to identify the mathematical principles that can be used to solve the problem. This
could involve using algebraic equations, calculus, probability theory, or other
mathematical concepts.
3. Choose an algorithmic approach :- With the mathematical principles identified,
the next step is to choose an algorithmic approach that can be used to solve the
problem. This could involve using a well-known algorithm, modifying an existing
algorithm, or developing a new algorithm from scratch.
4. Develop the algorithm :- Once the algorithmic approach is chosen, the next step is
to develop the algorithm using a step-by-step process. The algorithm should be
logical, efficient, and accurate.
5. Test and refine the algorithm :- After the algorithm is developed, it should be
tested using various inputs and outputs to ensure that it works correctly and
produces accurate results. The users should also look for ways to optimize the
algorithm, such as reducing computation time or improving accuracy.
6. Document and communicate :- Finally, the algorithm should be documented and
communicated to other stakeholders, such as developers, managers, and users.
This documentation should include the assumptions, limitations, and potential
issues of the algorithm.
An important point to remember about the mathematical approach is that is is not
entirely a new approach, but is simply a variation of the Top-Down approach.
Throughout the process, an algorithm designer should ensure that they stick to and
maintain a mathematical perspective and ensure that the algorithm is based on sound
mathematical principles. The algorithm developers should be analytical, logical, and
detail-oriented, while also being creative and adaptable to ensure that the algorithm
meets the requirements of the problem.

Attitude of Users In Developing Algorithms


While there are many methods used to formulate and develop algorithms. One of the
most important principles which computer programmers must ensure to remember at all
times is the principles commonly referred to as Attitude of Users (AOU). This principle
states that programmers should strive to put the users of the computer program at the
center of the algorithm. This means that programmers must strive to be user centric
while developing their algorithms. The objective behind this principle is to understand
the user needs and to develop an algorithm that is tailored to the needs of the users
and solves the problem correctly. This approach ensures that the algorithm is
user-friendly, efficient, and effective in solving the problem.
An important fact to note about this principle is that is is not a stand alone approach to
algorithm design but the principles of this idea must be factored into whatever algorithm
design a programmer aims to use to develop an algorithm.
What is Recursion ?
Recursion is a programming technique that involves a unit of a program calling
(executing) itself one or more times to solve a problem. In other words, a unit of a
program is said to be recursive if it can call (execute) itself during its execution.
Recursion can be used to solve problems that can be broken down into smaller and
simpler subproblems. In such cases, the unit breaks down the problem into smaller
subproblems, solves each subproblem recursively, and then combines the results to
obtain the solution to the original problem.
The recursive unit has two parts: the base case and the recursive case. The base case
is the simplest version of the problem that can be solved directly without further
recursion. The recursive case is the more complex version of the problem that involves
breaking it down into smaller subproblems and solving them recursively until we reach
the base case.
It is important to note that recursion can lead to infinite loops if the base case is not
properly defined or if the recursion is not stopped at some point. Therefore, the base
case must be carefully chosen to ensure that the recursion eventually terminates.
Recursion is often used in programming to solve problems related to trees, graphs, and
other data structures that have a recursive structure. It can also be used to implement
certain algorithms, such as sorting algorithms and search algorithms.
Although recursion can be a powerful and elegant technique, it can also be less efficient
than other methods of solving problems, particularly when dealing with large datasets.
Therefore, it is important to carefully consider the problem and the available resources
before deciding to use recursion.
Recursion is a powerful programming technique that is often used to solve problems
with inductive characterisation. Inductive characterisation refers to problems that can
be solved by breaking them down into smaller, simpler versions of themselves.
The key idea behind recursion is to solve a problem by dividing it into smaller
subproblems, each of which is a smaller instance of the original problem. The solution
to the original problem is then obtained by combining the solutions to the subproblems.
Recursion Example

The Factorial Problem.

In mathematics, the factorial of a non-negative integer n, denoted by n!, is the product of all positive
integers from 1 to n. For example, the factorial of 4 is 4 × 3 × 2 × 1 = 24.

The factorial function can be defined recursively as follows:

n! = 1, if n = 0

n! = n × (n-1)!, if n > 0

This recursive definition of the factorial function shows that to compute the factorial of n, we need to
compute the factorial of (n-1) and multiply it by n.

Factorial has a number of applications in mathematics, statistics, and computer science. For example,
it is used in combinatorics to compute the number of ways in which objects can be arranged or
selected. In programming, factorial is used to implement various algorithms, such as recursive
algorithms and dynamic programming algorithms.

Solution

The factorial of a number n is defined as the product of all positive integers less than or equal to n. We
can use recursion to compute the factorial of a number by breaking the problem down into a smaller
subproblem:

● To compute the factorial of n, we first need to compute the factorial of n-1.


● To compute the factorial of n-1, we first need to compute the factorial of n-2.
● And so on, until we reach the base case of computing the factorial of 1.

Once we have computed the factorial of 1, we can then use the results of the subproblems to
compute the factorial of n:

;}}

In this example, the base case is when n is equal to 1, and the recursive step involves computing the
factorial of n-1 and multiplying it by n.

Recursion is a powerful tool for solving problems with inductive characterisation because it allows us
to express the solution in terms of smaller subproblems, which are often easier to solve. However, it is
important to ensure that the recursion terminates at some point, otherwise we will end up with an
infinite loop. The base case provides the stopping condition for the recursion, ensuring that it
terminates after a finite number of steps.

int factorial(int n) {
if (n == 1) {
return 1;
} else {
return n * factorial(n-1);
}
}

You might also like