0% found this document useful (0 votes)
16 views39 pages

Introduction to Algorithms and Analysis

The document provides an introduction to algorithms, emphasizing their importance in problem-solving and programming. It outlines the characteristics of good algorithms, methods for writing them, and the analysis of their efficiency in terms of time and space complexity. Additionally, it includes examples of algorithms and their development steps, along with assignments for practice.
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)
16 views39 pages

Introduction to Algorithms and Analysis

The document provides an introduction to algorithms, emphasizing their importance in problem-solving and programming. It outlines the characteristics of good algorithms, methods for writing them, and the analysis of their efficiency in terms of time and space complexity. Additionally, it includes examples of algorithms and their development steps, along with assignments for practice.
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

VIT Bhopal University

Bhopal-Indore Highway, Kothrikalan, Sehore,


Madhya Pradesh – 466114.

Course Code
CSE1021
Introduction to Problem Solving
and Programming

Lecture -2 Credits 4
Unit -1
(8-Hours)

Lokesh Malviya
VIT Bhopal
01
Unit
Introduction to Computer Problem
Solving
Lecture -2
What is an It is a step-by-step
algorithm?

instruction for solving a


▪ An algorithm defined as a particular task in finite
sequence of definite and amount of time.
effective instructions, which
terminates with production
of correct output with given • Algorithm is an effective
input. method for solving a problem
expressed as finite sequence
of instructions.
Writing the
Algorithm
● Algorithm is generally ● Sometimes algorithms are
developed before the actual written using pseudocodes,
coding is done. i.e. a language similar to the
● It is written using English programming language to be
like language so that it is used.
easily understandable even
by non-programmers.
Writing algorithm for solving a
problem offers these advantages

● Promotes effective communication between team
members
● Enables analysis of problem at hand
● Acts as blueprint for coding
● Assists in debugging
● Becomes part of software documentation for future
reference during maintenance phase
characteristics of a good and
correct algorithm −
● Has a set of inputs
● Steps are uniquely defined
● Has finite number of steps
● Produces desired output
Here is the algorithm for going to
the market to purchase a pen.
Step 1:- Get dressed to go to market.
Step 2:- Check your wallet for money.
Step 3:- is there is no money in the wallet replenish it.
Step 4:- Go to the shop.
Step 5:- Ask for your favorite brand of pen.
Step 6:- If pen is not available, go to step 7else to step 10.
Step 7:- Give money to shopkeeper.
Step 8:- Keep the purchased pen safely.
Step 9:- Go back to home.
Step 10:- Ask for other brand of pen.
Step 11:- Go to Step 7.
Let us now create an algorithm to
check whether a number is positive
or negative.

1. Print “ Give any number”


2. Read num
3. if(num==0) print “You entered 0”
4. if (num>0) print “you entered a positive number”
5. if (num<0) print “You entered a negative number”
Questions
1. Write an Algorithm for calculating Square of a number.
2. Write an Algorithm to multiply two numbers.

Answer 1: solution Answer 2:- solution


1) Start 1) Start
2) Give input as a number 2) Accept Number one
3) Read the number 3) Accept Number two
4) Self Multiply of input 4) Multiply both the numbers
5) Print the result. 5) Print the result.
6) End 6) End
Algorithm analysis
● Analysing an algorithm means solving a problem
statement of an algorithm with different
approaches.

● It also means to check for the best algorithm.


Algorithm analysis
Analysis of an algorithm performed in two phases:
1. Prior estimate: It is concerned with
theoretical and mathematical calculation that uses
asymptotic notations for its representation.
2. Posterior estimate: It is concerned with
writing a program and therefore depends on
operating system, compiler, etc.
Algorithm analysis
● Algorithm analysis is determination of time,
amount of memory utilized and other resources by
the system to execute a particular task.

● The running time of an algorithm for particular


input based upon the number of operations executed
on that particular size of the input.

● The greater the number of operations performed,


the longer the running time of an algorithm.
Algorithm analysis
● Consider the input size denoted as n, and then the running time of that
problem is function f(n).
● Running time of an algorithm measured using following steps:
1. Computing primary functions: Primary functions are instructions with
constant implementation time.
(a) Assignment of variables.
(b) Performing arithmetic operations like addition of numbers,
subtraction of numbers, etc.
(c) Comparing two numbers.
(d) Defining a function and calling that function.
2. Computing operation of input size n: Growth rate represented in algorithm
as function f(n).
Comparing algorithms
● Algorithms are compared based on its time and space
complexity.
● Algorithms with less time complexity and less memory space
is said to be a better algorithm.
Performance analysis of an algorithm depends on two factors:
1. Space complexity: The amount of memory space required
by an algorithm to complete its execution.

2. Time complexity: The amount of time required by an


algorithm to complete its execution. Number of step count
taken by algorithm to complete its task.
Comparing algorithms
● There are three types of step count:
(a) Best case: The best-case step count is the minimum
number of steps executed for given parameters.
(b) Worst case: The worst-case step count is the
maximum number of steps executed for given
parameters.
(c) Average case: The average case step count is the
average number of steps executed for given
parameters
There are three different
Asymptotic notations: big O, big Theta
Notation
Asymptotic Notation is used (Θ), and big Omega (Ω).
to describe the running time •Big-Θ is used when the
of an algorithm - how much running time is the same for
time an algorithm takes all cases,
with a given input, n. •Big-O for the worst case
running time
•Big-Ω for the best case
running time.
Calculating Unit of time
● Algorithm sum(A,n) A->array and n->number of elements in
the array
● {
● Sum=0;……………………………………#(1 unit of time)
● For(i=0; i<n; i++)
● #(n+1 unit of time)
● #(+1 for the end false condition)
● {
● Sum=sum+A[i];…… #( n unit of time)
● }
● return sum ;…………………………#(1 unit of time)
● }
● Every Single Statement in an algorithm takes one unit of time
Counting for Sum(A,n)
● F(n) = 1 + n+1+ n+ 1
● F(n) = 2n+3
● F(n) = O(n)

● Space A = n
● n = 1
● sum = 1
● i =1
● total s(n)= n+3
Assignment for class
● Sum(a,b)
● {
● return a+b;
● }
● f(n) =?
● Solution: # Takes 2 unit of time(constant) one for arithmetic
operation and one for #return.(as per above
conventions) cost=2 no of times=1
● f(n)= O(1)
Assignment for Class
● int count = 0;
● for (int i = 0; i < N; i++)
● for (int j = 0; j < i; j++)
● count++;
Adding three numbers and print
the sum.
Step 1: Fulfilling the pre-requisites
1. The problem that is to be solved by this algorithm: Add 3 numbers and print
their sum.
2. The constraints of the problem that must be considered while solving the
problem: The numbers must contain only digits and no other characters.
3. The input to be taken to solve the problem: The three numbers to be added.
4. The output to be expected when the problem is solved: The sum of the three
numbers taken as the input.
5. 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 ‘+’ operator, or
bit-wise, or any other method.
Step 2: Designing the algorithm
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 (Initialization).
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

Step 3: Testing the algorithm by implementing it in a programming


language.
Descriptive Analysis of Algorithm
Let’s look at a very simple algorithm called find_max().

Problem: Given a list of positive numbers, return the largest number on


the list.

Inputs: A list L of positive numbers. This list must contain at least one
number.
(Asking for the largest number in a list of no numbers is not a
meaningful question.)

Outputs: A number n, which will be the largest number of the list.


Algorithm

Step 1:- Set max to 0.

Step 2 :- For each number x in the list L, compare it to max. If x is


larger, set max to x.

Step 3:- max is now set to the largest number in the list.
Algorithm Development Steps
Thanks!

Do you have
any
questions? lokesh.malviya2020@[Link]
+91 9893096304

You might also like