You are on page 1of 18

Algorithm

and
Complexity
Double Tap to Add Subtitle

PAGE 1
Lesson 1: Intro to Algorithm and Complexity

Lesson 2

Course Lesson 3
Outline
Lesson 4

Lesson 5

PAGE 2
Objective

▪ Define Algorithm Course


▪ Design and create simple Objective
algorithm
▪ Implement and Test Algorithm

PAGE 3
Introduction
▪ What is an Algorithm? Algorithm Basics

▪ The word Algorithm means “a process or set of rules to be followed in calculations or other problem-
solving operations”. Therefore Algorithm refers to a set of rules/instructions that step-by-step define
how a work is to be executed to get the expected results.
▪ It can be understood by taking an example of cooking a new recipe. To cook a new recipe, one reads
the instructions and steps and executes them one by one, in the given sequence. The result thus
obtained is the new dish cooked perfectly. Similarly, algorithms help to do a task in programming to get
the expected output.

Introduction PAGE 4
Second Skill Third Skill Conclusion
Introduction
▪ What is an Algorithm? Algorithm Basics

Introduction PAGE 5
Second Skill Third Skill Conclusion
What are the Characteristics of an Algorithm?
An algorithm is a finite set of instructions that if followed, accomplishes a particular task. In addition, all
algorithms must satisfy following criteria (characteristics)

Input Output Definiteness

- Zero or more quantities - At least one quantity is Each instruction is clear &
are externally supplied. produced. unambiguous,
- Well-Defined Inputs If an - Well-Defined Outputs Clear and Unambiguous: The
algorithm says to take The algorithm must clearly algorithm should be clear and
define what output will be unambiguous. Each of its
inputs, it should be well steps should be clear in all
yielded and it should be
defined inputs aspects and must lead to only
well-defined as well. one meaning.

Characteristic of Algorithm PAGE 6


Introduction Third Skill Conclusion
What are the Characteristics of an Algorithm?
An algorithm is a finite set of instructions that if followed, accomplishes a particular task. In addition, all
algorithms must satisfy following criteria (characteristics)

Finiteness Effectiveness

For all input cases it terminates Every instruction must be very basic so that
after finite number of steps, The it can be carried out, in principle, by a
person using only pencil and paper
algorithm must be finite, i.e. it (Feasible Instructions)
should not end up in infinite
Language Independent
loops or similar.
Feasible

Characteristic of Algorithm PAGE 7


First Skill Third Skill Conclusion
Advantages and Disadvantage of Algorithms

Advantages of Algorithms

- It is easy to understand.
- The algorithm is a step-wise representation of a solution to a given
problem.
- In Algorithm the problem is broken down into smaller pieces or steps
hence, it is easier for the programmer to convert it into an actual
program.
Characteristic of Algorithm
PAGE 8
Introduction And its Advantage Third Skill Conclusion
Advantages and Disadvantage of Algorithms

Disadvantages of Algorithms

Writing an algorithm takes a long time so it is time-consuming.


Branching and Looping statements are difficult to show in Algorithms.

Characteristic of Algorithm
PAGE 9
Introduction And its Advantage Third Skill Conclusion
How to Design an Algorithm?

Design and Implement Algorithm PAGE 10


Introduction Characteristic of Algorithm Conclusion
How to Design an Algorithm?
Algorithm (or program) Specification is the process of translating a solution of a problem into an algorithm. This can be done in
three ways:

1. Steps or instructions can be written by using a Natural Language like English.


In this case we have to ensure that the resulting instructions are definite.
2. Graphic representations called Flowcharts can also be used for specifications,
but they work well only if the algorithm is small and simple
3. Use of Pseudo Code is the most preferred option to specify an algorithm. It is
represented by using a Natural Language (English) and C / C++

Design and Implement Algorithm PAGE 11


Introduction Characteristic of Algorithm Conclusion
To write an algorithm, the following things are
needed as a pre-requisite

1. The problem is to be solved by this algorithm.


2. The constraints of the problem must be considered while solving
the problem.
3. The input to be taken to solve the problem.
4. The output to be expected when the problem is solved.
5. The solution to this problem, in the given constraints.

Then the algorithm is written with the help of the above parameters
such that it solves the problem.

Design and Implement Algorithm PAGE 12


Introduction Characteristic of Algorithm Conclusion
Example:

Consider the example to add three numbers and print the sum.

Step 1: Fulfilling the pre-requisites


As discussed above, to write an algorithm, its pre-requisites must be fulfilled.
1. The problem that is to be solved by this algorithm:
Add 3 numbers and print their sum.
1. The constraints of the problem must be considered while solving the problem:
The numbers must contain only digits and no other characters.
2. The input to be taken to solve the problem:
The three numbers to be added.
3. The output to be expected when the problem is solved:
The sum of the three numbers taken as the input.
4. The solution to this problem, in the given constraints:
The solution consists of adding the 3 numbers. It can be done with the
help of the ‘+’ operator, or bit-wise, or any other method.

Design and Implement Algorithm PAGE 13


Introduction Characteristic of Algorithm Conclusion
Example:

Consider the example to add three numbers and print the sum.

Step 2: Designing the algorithm


Now let’s design the algorithm with the help of the above pre-requisites:
Algorithm to add 3 numbers and print their sum:

1. START
2. Declare 3 integer variables num1, num2, and num3.
3. Take the three numbers, to be added, as inputs in variables num1, num2, and
num3 respectively.
4. Declare an integer variable sum to store the resultant sum of the 3 numbers.
5. Add the 3 numbers and store the result in the variable sum.
6. Print the value of variable sum
7. END

Design and Implement Algorithm PAGE 14


Introduction Characteristic of Algorithm Conclusion
#include <bits/stdc++.h>
using namespace std;

int main()
{
// Variables to take the input of
// the 3 numbers
int num1, num2, num3;

// Variable to store the resultant sum


int sum;

// Take the 3 numbers as input Output


cout << "Enter the 1st number: ";
Enter the 1st number: 0
cin >> num1;
Enter the 2nd number: 0
cout << " " << num1 << endl;
Enter the 3rd number: -1577141152
cout << "Enter the 2nd number: "; The sum of the 3 numbers is: -1577141152
cin >> num2;
cout << " " << num2 << endl;

cout << "Enter the 3rd number: ";


cin >> num3;
cout << " " << num3;
// Calculate the sum using + operator
// and store it in variable sum
sum = num1 + num2 + num3;

// Print the sum


cout << "\nSum of the 3 numbers is: "
<< sum;

return 0;
}
// This code is contributed by shivanisinghss2110

Design and Implement Algorithm PAGE 15


Introduction Characteristic of Algorithm Conclusion
Here is what we learned
Lesson ▪ You can now define what is algorithm

Summary ▪ Can know discuss Characteristic of a algorithm


▪ Can Design and Implement Algorithim

Conclusion PAGE 16
First Skill Second Skill Third Skill
Lesson Following the Specification and Pre-requisite Create a ALGORITHIM
that using the steps presented here
Activity

Conclusion PAGE 17
First Skill Second Skill Third Skill
Lesson 1: Intro to Algorithm and Complexity

Lesson 2: A Review of Mathematical Induction

Course Lesson 3
Outline
Lesson 4

Lesson 5

PAGE 18

You might also like