Introduction To Algorithms
Introduction To Algorithms
ALGORITHMS
UNIT I:
Introduction to computer problem solving: Introduction, the problem-solving
aspects, Top-down design, Implementation of Algorithms, program verification, The
Efficiency and Analysis of Algorithm.
Fundamentals of Algorithms: Exchanging the values of two variables, Counting,
Summation of set of Numbers, Factorial Computation, Sine function computation,
Generation of Fibonacci Sequence, Reversing the Digits of an Integer, Base
conversion, character to number conversion.
INTRODUCTION TO COMPUTER PROBLEM SOLVING:
INTRODUCTION TO ALGORITHMS
The word algorithm comes from the name of Persian author:Abu Ja’far Mohammed
Ibu Musa Al Khowarismi [825 AD] Al Khowarismi means[Algorithm
Algorithms can be simple and complex depending on what you want to achieve.
It can be understood by taking the 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 is cooked perfectly. Every time you use your phone,
computer, laptop, or calculator you are using Algorithms. Similarly, algorithms help to do a
task in programming to get the expected output.
The Algorithm designed are language-independent, i.e. they are just plain instructions that
can be implemented in any language, and yet the output will be the same, as expected.
1. Algorithms are necessary for solving complex problems efficiently and effectively.
2. They help to automate processes and make them more reliable, faster, and easier to
perform.
3. Algorithms also enable computers to perform tasks that would be difficult or impossible
for humans to do manually.
4. They are used in various fields such as mathematics, computer science, engineering,
finance, and many others to optimize processes, analyze data, make predictions, and
provide solutions to problems
PROPERTIES OF ALGORITHM:
____________________________________________________________________________________________
Designed and compiled by Prof Prakash Naik, HOD BCA Dept, Anjuman Institute of management and Computer
Application,Bhatkal. Cell No : 9945925320. Email : prakashkwr328@gmail.com
3. It should take zero or more input.
4. It should be deterministic means giving the same output for the same input case.
5. Every step in the algorithm must be effective i.e. every step should do some work.
1. Input: An algorithm requires one or more inputs, which are the values supplied to the
algorithm before processing
2. Output: An algorithm must produce one or more outputs, which are the outcome after
every step has been completed
3. Finiteness: An algorithm must have a finite number of steps to complete a task. It should
have a defined endpoint and not enter an endless loop
4. Definiteness: Each step in an algorithm should be clearly defined and detailed.
5. Effectiveness: The steps in an algorithm should be doable and effective
ADVANTAGES OF ALGORITHM
DISADVANTAGES OF ALGORITHM
TOP-DOWN DESIGN
____________________________________________________________________________________________
Designed and compiled by Prof Prakash Naik, HOD BCA Dept, Anjuman Institute of management and Computer
Application,Bhatkal. Cell No : 9945925320. Email : prakashkwr328@gmail.com
A top-down design is the decomposition of a system into smaller parts in order to divide larger
systems into sub-systems.A top-down design is generally a plan made in plain, simple
English for the program.
It is known as:
• divide and conquer
• stepwise refinement
• structured programming
The top-down design must never incorporate references to library functions or syntactic
elements specific to a particular language.That is the reason why top-down designs are
written in plain English. The concept driving a top-down design is to break up the task that a
program executes into a very few extensive subtasks.
The highest level is known as the main module, top level or level 0. At this point, the volume
of subtasks must be small. Most programs at this level usually include three to seven
subtasks.
Division of tasks into subtasks essentially splits the problem into various smaller programs,
which helps developers to easily code these simpler parts.
IMPLEMENTATION OF ALGORITHMS
Algorithm Implementation refers to the process of translating a
well-defined algorithm into a computer program that can be
executed by a computer. This involves writing code in a
programming language to follow the steps and procedures outlined
by the algorithm, ensuring that the input is processed correctly to
produce the desired output.
____________________________________________________________________________________________
Designed and compiled by Prof Prakash Naik, HOD BCA Dept, Anjuman Institute of management and Computer
Application,Bhatkal. Cell No : 9945925320. Email : prakashkwr328@gmail.com
Implementing them requires a deep understanding of both the
problem domain and the chosen programming
language. Efficiency and accuracy are paramount, as they can
greatly affect the performance and reliability of the software
application.
PROGRAM VERIFICATION
Program verification is the process of ensuring that a program behaves as
expected and meets its specifications
WHY IS IT NEEDED?
1. Testing can show errors but not their absence
2. Software errors in critical systems can cause major disasters
3. Mathematics can be used to state program properties and to
prove a program correct for all inputs
4. We then say that the program has been verified correct
The efficiency of an algorithm can be in terms of time and space. The algorithm efficiency can
be analyzed by the following ways.
a. Analysis Framework.
b. Asymptotic Notations and its properties.
c. Mathematical analysis for Recursive algorithms.
d. Mathematical analysis for Non-recursive algorithms.
1.ANALYSIS FRAMEWORK
The analysis framework for algorithms is a method for determining how much time
and space an algorithm requires to run.
____________________________________________________________________________________________
Designed and compiled by Prof Prakash Naik, HOD BCA Dept, Anjuman Institute of management and Computer
Application,Bhatkal. Cell No : 9945925320. Email : prakashkwr328@gmail.com
The two factors of Algorithm Complexity are:
1. Time Factor: Time is measured by counting the number of key operations such as
comparisons in the sorting algorithm.
2. Space Factor: Space is measured by counting the maximum memory space required by
the algorithm to run/execute.
1. TIME COMPLEXITY
The time complexity of an algorithm refers to the amount of time required by the algorithm to
execute and get the result. This can be for normal operations, conditional if-else statements,
loop statements, etc..
HOW TO CALCULATE, TIME COMPLEXITY?
The time complexity of an algorithm is also calculated by determining the following 2
components:
Constant time part: Any instruction that is executed just once comes in this part. For
example, input, output, if-else, switch, arithmetic operations, etc.
Variable Time Part: Any instruction that is executed more than once, say n times, comes in
this part. For example, loops, recursion, etc.
Therefore Time complexity T(P) of any algorithm P is T(P) = C + TP(I), where C is the
constant time part and TP(I) is the variable part of the algorithm, which depends on the
instance characteristic.
Step 1: START
Step 2: Get n elements of the array in arr and the number to be searched in x
Step 3: Start from the leftmost element of arr[] and one by one compare x with each element
of arr[]
Step 4: If x matches with an element, Print True.
Step 5: If x doesn’t match with any of the elements, Print False.
Step 6: END
Here, There are 2 variables arr[], and x, where the arr[] is the variable part of n elements and
x is the fixed part. Hence S(P) = 1+n. So, the space complexity depends on n(number of
elements). Now, space depends on data types of given variables and constant types and it
will be multiplied accordingly.
____________________________________________________________________________________________
Designed and compiled by Prof Prakash Naik, HOD BCA Dept, Anjuman Institute of management and Computer
Application,Bhatkal. Cell No : 9945925320. Email : prakashkwr328@gmail.com
2.: The space complexity of an algorithm refers to the amount of memory
required by the algorithm to store the variables and get the result. This can be for inputs,
temporary operations, or outputs
1. Fixed Part: This refers to the space that is required by the algorithm. For example, input
variables, output variables, program size, etc.
2. Variable Part: This refers to the space that can be different based on the implementation
of the algorithm. For example, temporary variables, dynamic memory allocation, recursion
stack space, etc.
Therefore Space complexity S(P) of any algorithm P is S(P) = C + SP(I), where C is the fixed
part and S(I) is the variable part of the algorithm, which depends on instance characteristic.
1. PRIORI ANALYSIS:
“Priori” means “before”. Hence Priori analysis means checking the algorithm before its
implementation. In this, the algorithm is checked when it is written in the form of
theoretical steps. This Efficiency of an algorithm is measured by assuming that all other
factors, for example, processor speed, are constant and have no effect on the
implementation. This is done usually by the algorithm designer.
This analysis is independent of the type of hardware and language of the compiler. It
gives the approximate answers for the complexity of the program.
2. POSTERIOR ANALYSIS:
“Posterior” means “after”. Hence Posterior analysis means checking the algorithm after
its implementation. In this, the algorithm is checked by implementing it in any
programming language and executing it. This analysis helps to get the actual and real
analysis report about correctness(for every possible input/s if it shows/returns correct
output or not), space required, time consumed, etc. That is, it is dependent on the
language of the compiler and the type of hardware used.
.FUNDAMENTALS OF ALGORITHMS:
____________________________________________________________________________________________
Designed and compiled by Prof Prakash Naik, HOD BCA Dept, Anjuman Institute of management and Computer
Application,Bhatkal. Cell No : 9945925320. Email : prakashkwr328@gmail.com
1.EXCHANGING THE VALUES OF TWO VARIABLESStep 2:
Problem : Given the two variables A and B exchange the values of the variables.
ALGORITHM DESCRIPTION :
1. Accept from the user values in A and B
2. Store the value of A into tempervary varriable T
3. Store the value of variable B to A
4. Store to B the original value of A stores in T
ALGORITHM SWAP A B
Step 1 : Start
40 55
Step 2 : READ A,B
Step 3 print “Before swapping value in A and B is “, A,B T
Step 4: T<- A 40
Step 5 A<-B
Step 6 B<-T A B
Step 7 print “After swapping value in A and B is “, A,B
55 40
2.SUMMATION OF SET OF NUMBERS
Summation means adding the number, which is given in the series.
Note: Computer can add two numbers at a time and return the sum of two numbers.
So for this purpose we assign the variable sum as zero.
Algorithm:
Step 1: Start
Step 3 READ N
____________________________________________________________________________________________
Designed and compiled by Prof Prakash Naik, HOD BCA Dept, Anjuman Institute of management and Computer
Application,Bhatkal. Cell No : 9945925320. Email : prakashkwr328@gmail.com
6.2 sum = sum + num [ Update value of num into sum]
Step 8 stop
[TO BE CONTINUED]
____________________________________________________________________________________________
Designed and compiled by Prof Prakash Naik, HOD BCA Dept, Anjuman Institute of management and Computer
Application,Bhatkal. Cell No : 9945925320. Email : prakashkwr328@gmail.com