Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
86 views

Introduction To Algorithms

ALGORITHM MODULE 1 NOTEES
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
86 views

Introduction To Algorithms

ALGORITHM MODULE 1 NOTEES
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

INTRODUCTION TO

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

WHAT IS ALGORITHM : The Word Algorithm is a step by step procedure to solve a


problem.
In other words it is a procedure for solving a mathematical problem in a finite number of steps
or a set of finite rules or instructions to be followed in calculations or other problem-solving
operations. Therefore Algorithm refers to a sequence of finite steps to solve a particular
problem.

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.

WHAT IS THE NEED FOR ALGORITHMS?

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:

1. It should terminate after a finite time.


2. It should produce at least one 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
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.

WHAT ARE THE CHARACTERISTICS OF AN ALGORITHM?

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

1. It is a step-wise representation of a solution to a given problem, which makes it easy to


understand.
2. An algorithm uses a definite procedure.
3. It is not dependent on any programming language, so it is easy to understand for anyone
even without programming knowledge.
4. Every step in an algorithm has its own logical sequence so it is easy to debug.
5. By using algorithm, the problem is broken down into smaller pieces or steps hence, it is
easier for programmer to convert it into an actual program

DISADVANTAGES OF ALGORITHM

1. Writing algorithm takes a long time.


2. An Algorithm is not a computer program, it is rather a concept of how a program should be

PROBLEM SOLVING ASPECT


Problem solving is a creative process. It is an act of defining a problem, determining the
cause of the problem, identifying and selecting alternatives for a solution and implementing a
solution. A problem can be solved successfully only after making an effort to understand the
problem. The important aspect to be considered in problem-solving is the ability to view a
problem from a variety of angles

PROGRAM DEVELOPMENT STEPS


The various steps involved in Program Development are:
1. Defining or Analyzing the problem
2. Design (Algorithm)
3. Coding
4. Documenting the program
5. Compiling and Running the Program
6. Testing and Debugging
7. Maintenance

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.

ADVANTAGES TO TOP DOWN DESIGN:


• Breaking the problem into parts helps us to clarify what needs to be done.
• At each step of refinement, the new parts become less complicated and, therefore, easier to
figure out.
• Parts of the solution may turn out to be reusable.
• Breaking the problem into parts allows more than one person to work on the solution.

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.

 Broad understanding of computer science concepts.


 Proficiency in one or more programming languages.
 Ability to translate theoretical models into practical solutions.
 Debugging skills to ensure correctness and efficiency.

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

HERE ARE SOME TECHNIQUES USED IN PROGRAM VERIFICATION:

 Formal: Uses mathematical proof to verify correctness


 Informal: Uses subjective human reasoning
 Static: Assesses the system by using the source code without
executing it
 Dynamic: Assesses the system by executing it first

EFFICIENCY AND ANALYSIS OF ALGORITHM

ALGORITHM EFFICIENCY is a measure of how many resources an algorithm uses to


process data.
The ANALYSIS OF ALGORITHMS is the process of determining the computational
complexity of an algorithm, which is the amount of resources needed to execute.

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.

Therefore the complexity of an algorithm can be divided into two types:

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.

Example: Consider the below algorithm of Linear Search

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.

In the above example , the time complexity is calculated as follows:


Step 1: Constant Time
Step 2: Variable Time (Taking n inputs)
Step 3: Variable Time (Till the length of the Array (n) or the index of the found
element)
Step 4: Constant Time
Step 5: Constant Time
Step 6: Constant Time
Hence, T(P) = 1 + n + n(1 + 1) + 1 = 2 + 3n, which can be said as T(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
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

How to calculate Space Complexity?


The space complexity of an algorithm is calculated by determining the following 2
components:

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.

To analyze an algorithm's running time, you can follow these steps:


e. Implement the algorithm
f. Determine how long each basic operation takes
g. Identify unknown quantities that describe how often the basic operations are
executed
h. Create a realistic model for the program's input
i. Analyze the unknown quantities, assuming the modeled input
j. Calculate the total running time by multiplying the time by the frequency for each
operation, then adding all the products

HOW TO ANALYZE AN ALGORITHM?


For a standard algorithm to be good, it must be efficient. Hence the efficiency of an algorithm
must be checked and maintained. It can be in two stages

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.

sum= sum+a1 (Here a1 is the first number) then we do

sum=sum+a2 (Here the new value of sum contains a1+a2) then

sum=sum+a3 (Here the new value of sum contains a1+a2+a3) and so on

sum=sum+an (Here the new value of sum contains a1+a2+a3+.........+ an )

Algorithm:

Step 1: Start

Step 2: print “ How many Numbers to Read”

Step 3 READ N

Step 4 Initialize the variable sum = 0

Step 5 Initialize the variable count = 1

Step 6 WHILE count<=N

6.1 READ num [ Read each numbers in NUM]

____________________________________________________________________________________________
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]

6.3 count = count +1 [ increment count by 1 until it reaches N]

6.4 goto step 6

Step 7 print “Sum of series is :” , 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

You might also like