Unit1 Daa 2023 Nep
Unit1 Daa 2023 Nep
Introduction
The word Algorithm means ” A set of finite rules or instructions to be followed in
calculations or other problem-solving operations ”
Or
” A procedure for solving a mathematical problem in a finite number of steps that frequently
involves recursive operations”.
Therefore, an refers to a sequence of finite steps to solve a particular problem.
Use of Algorithms:
Algorithms play a crucial role in various fields and have many applications. Some of the key
areas where algorithms are used include:
1. Computer Science: Algorithms form the basis of computer programming and are
used to solve problems ranging from simple sorting and searching to complex
tasks such as artificial intelligence and machine learning.
2. Mathematics: Algorithms are used to solve mathematical problems, such as
finding the optimal solution to a system of linear equations or finding the shortest
path in a graph.
3. Operations Research: Algorithms are used to optimize and make decisions in
fields such as transportation, logistics, and resource allocation.
4. Artificial Intelligence: Algorithms are the foundation of artificial intelligence
and machine learning and are used to develop intelligent systems that can perform
tasks such as image recognition, natural language processing, and decision-
making.
5. Data Science: Algorithms are used to analyze, process, and extract insights from
large amounts of data in fields such as marketing, finance, and healthcare.
These are just a few examples of the many applications of algorithms. The use of algorithms
is continually expanding as new technologies and fields emerge, making it a vital component
of modern society.
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
perform a task in programming to get the expected output.
The algorithms designed are language-independent, i.e.,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.
What are the characteristics of an algorithm?
Clear and Unambiguous: The algorithm should be unambiguous. Each of its
steps should be clear in all aspects and must lead to only one meaning.
Well-Defined Inputs: If an algorithm says to take inputs, they should be well--
defined. It may or may not take input.
Well-Defined Outputs: The algorithm must clearly define what output will be
yielded and it should be well-defined as well. It should produce at least one
output.
Finite- the algorithm must be finite, i.e.e. it should terminate after a finite time.
Feasible: The algorithm must be simple, generic, and practical, such that it can
be executed with the available resources. It must not contain some future
technology or anything.
Language Independent: The Algorithm designed must be s i m p l e ,
i.e. it must be just plain instructions that can be implemented in any language, and
yet the output will be the same, as expected.
Performance analysis:
To judge the algorithm, check the following questionnaires:
1. Does it do what we want it to do?
2. Does it work correctly according to the original specification?
3. Is there documentation to describe how it works?
4. Is the code readable?
SPECIFICATIONS OF AN ALGORITHM:
1. Comments: begin with a double slash.
Ex: // This program gives the biggest number among 3 numbers
2. Blocks: are indicated within braces { }.
Ex:
{
statements…..
statements…..
}
3. Identifiers: An identifier is a name given to a variable. It begins with a letter.
4. Assignment value: ‘:= ‘This symbol is used as an assignment operator which
assigns a value to a variable.
Ex: Syntax: < variable name>:=Expression.
5. Relational and logical operators: give a true or false value. >, <, >=, <=, etc….
Ex.: a >b, a<b, c >=d. Ex: and' represented &&, or' represented ||.
6. Array: It is indicated with square brackets [ ], Array name must begin with a letter
and' represented be a reserved word. There are two types of arrays: single-
dimensional and multidimensional arrays. Example: a[10].
7. Algorithm: It starts with the keyword ‘algorithm’ and next to that the name of the
algorithm written in syntax: algorithm name< parameter list>
//comments
{…………….
…………}
..}
8. Different control statements:
i.If statement:
Syntax: if <condition>then
< statements>.
ii.If-else statement:
Syntax: if <condition>then
< statements >
else
< statements>.
iii.Switch:
Syntax: case
{
:<condition>: <statement 1>;
:<condition>: <statement 2>;
.
.
.
:< condition>: < statement n>;
:else <Statement>;
}
iv.While loop:
Syntax:
while<Condition>do
{
<statements>.
}
v.Repeat:
Syntax for else <statement 1>;
<Statement 2>;
.
.
until<Condition>;
vi.For loop:
and syntax for variable:= value to final value step do
{
<statements>.
}
Types of Algorithms:
There are several types of algorithms available. Some important algorithms are:
1. Brute Force Algorithm:
It is the simplest approach to a problem. A brute force algorithm is the first approach that
comes to finding when we see a problem.
2. Recursive Algorithm:
A recursive algorithm is based on recursion. In this case, a problem is broken into several
sub-parts and called the same function again and again.
3. Backtracking Algorithm:
The backtracking algorithm builds the solution by searching among all possible solutions.
Using this algorithm, we keep on building the solution following criteria. Whenever a
solution fails, we trace back to the failure point, build the next solution, and continue this
process until we find the solution or all possible solutions are exhausted.
4. Searching Algorithm:
Searching algorithms are the ones that are used for searching elements or groups of elements
from a particular data structure. They can be of different types based on their approach or the
data structure in which the element should be found.
5. Sorting Algorithm:
Sorting is arranging a group of data in a particular manner according to the requirement. The
algorithms that help in performing this function are called sorting algorithms. Generally,
sorting algorithms are used to sort groups of data in an increasing or decreasing manner.
6. Hashing Algorithm:
Hashing algorithms work similarly to the searching algorithm. But they contain an index with
a key ID. In hashing, a key is assigned to specific data.
7. Divide and Conquer Algorithm:
This algorithm breaks a problem into sub-problems, solves a single sub-problem, and merges
the solutions to get the final solution.
Algorithms as a Technology.
There are a huge number of real-life applications where algorithms play a major and vital role. A
few of such applications are discussed below.
The Internet, without which it is difficult to imagine a day, is the result of clever and
efficient algorithms. With the aid of these algorithms, various sites on the Internet are able to
manage and manipulate this large volume of data. Finding good routes on which the data will
travel and using a search engine to find pages on which particular information is present.
Another great milestone is the Human Genome Project, which has made great progress
towards the goal of identifying the 100,000 genes in human DNA, determining the sequences
of the 3 billion chemical base pairs that make up human DNA, storing this huge amount of
information in databases, and developing tools for data analysis. Each of these steps
required sophisticated and efficient algorithms.
The day-to-day electronic commerce activities are hugely dependent on our personal
information such as credit/debit card numbers, passwords, bank statements, OTPs, and so on.
The core technologies used include public-key cryptography and digital signatures which are
based on numerica
The approach of linear programming is also one such technique which is widely used in
manufacturing and other commercial enterprises where resources need to be allocated
sparingly in the most beneficial way. An institution may want to determine where to spend
money buying advertising in order to maximize the chances of their institution to grow.
The shortest path algorithm also has an extensive use as i n a transportation firm such as
a trucking or railroad company, which may have a financial interest in finding the shortest
path through a road or rail network because taking the shortest path results in lower labor or
fuel costs. Or a routing node on the Internet may need to find the shortest path through the
network to route a message quickly.
Even an application that does not require algorithm content at the application level relies
heavily on algorithms as the application depends on hardware, GUI, networking, or object
orientation, and all of these make extensive use of algorithms.
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,
The complexity of an algorithm describes its efficiency of the algorithm in terms of the
amount of memory required to process the data and the processing time.
Analysis Framework:
The complexity of an algorithm is analyzed from two perspectives: time and space.
1. Time Complexity
It is a function that describes the amount of time required to run an algorithm in terms of the
size of the input. "Time" can mean the number of memory accesses performed, the number of
comparisons between integers, the number of times some inner loop is executed, or some
other natural unit related to the amount of real time the algorithm will take. Components that
affect Time Efficiency are:
A. Speed of the Computer,
B. Choice of the Programming Language,
C. Compiler Used,
D. Choice of the Algorithm,
E. Size of Inputs / Outputs.
2. Space Complexity
An algorithm’s efficiency is defined as a function of some parameter n indicating the
algorithm’s input size. In most cases, selecting such a parameter is quite straightforward. For
example, it will be the size of the list for problems of sorting, searching.
a. Program space: storing into machine language(programming language)
b. Data space: stores the data as constraints, variables,keywords, etc.
c. Stack space: stores the run address along with the local variables.
3. Units for Measuring Running Time: Some standard unit of time measurement such as a
second, or millisecond, and so on can be used to measure the running time of a program
after implementing the algorithm.
Drawbacks
Dependence on the speed of a particular computer.
Dependence on the quality of a program implementing the algorithm.
The difficulty of clocking the actual running time of the program. So, we need metric to
measure an algorithm’s efficiency that does not depend on these extraneous factors. One
possible approach is to count the number of times each of the algorithm’s operations is
executed. This approach is excessively difficult.
Orders of Growth
A difference in running times on small inputs is not what really distinguishes efficient
algorithms from inefficient ones. Some common orders of growth seen often in complexity
analysis are:
O(1) constant,
O(log n) logarithmic,
O(n) linear,
O(n log n log n"
O(n^2 ) quadratic,
O(n^3 ) cubic, and
O(n^k)
polynomial.
Table: Values (approximate) of several functions important for the analysis of algorithms
Example, sequential search. This is a straightforward algorithm that searches for a given item
(some search key K) in a list of n elements by checking successive elements of the list until either a
match with the search key is found or the list is exhausted. Here is the algorithm's pseudocode, in
which, for simplicity, a list is implemented as an array. (It also assumes that the second condition
A[i] ≠ K will not be checked if the first one, which checks that the array's index does not exceed its
upper bound, fails.)
i+-0
i+-i+1
else return -1
Clearly, the running time of this algorithm can be quite different for the same list size n. In the
worst case, when there are no matching elements or the first matching element happens to be
the last one on the list, the algorithm makes the largest number of key comparisons among all
possible inputs of size n:
cworst(n) = n.
The worst-case efficiency of an algorithm is its efficiency for the worst-case input of size n, which
is an input (or inputs) of size n for which the algorithm runs the longest among all possible inputs
of that size. The way to determine the worst-case efficiency of an algorithm is, in principle, quite
straightforward: we analyze the algorithm to see what kind of inputs yield the largest value of the
basic operation's count C(n) among all possible inputs of size n and then compute this worst-case
value Cworst(n). (For sequential search, the answer was obvious.)
Clearly, the worst-case analysis provides very important information about an algorithm's
efficiency by bounding its running time from above. In other words, it guarantees that for any
instance of size n, the running time will not exceed
The best-case efficiency of an algorithm is its efficiency for the best-case input of size n, which is
an input (or inputs) of size n for which the algorithm runs the fastest among all possible inputs of
that size. Accordingly, we can analyze the best-case efficiency as follows. First, we determine the
kind of inputs for which the count C(n) will be the smallest among all possible inputs of size n.
(Note that the best case does not mean the smallest input; it means the input of size n for which the
algorithm runs the fastest.) Then we ascertain the value of C (n) on these most convenient inputs.
For example, for sequential search, best-case inputs are lists of size n with their first elements equal
to a search key; accordingly, Cbw(n) = 1. The analysis of the best-case efficiency is not nearly as
important as that of the worst-case efficiency. But it is not completely useless, either. Though we
should not expect to get best-case inputs, we might be able to take advantage of the fact
that for some algorithms a good best -case performance extends to some useful types of inputs
close to being the best-case ones. For example, there is a sorting algorithm (insertion sort) for
which the best-case inputs are already sorted arrays on which the algorithm works very fast.
Moreover, this good best-case efficiency deteriorates only slightly for almost sorted arrays. Thus,
such an algorithm might well be the method of choice for applications dealing with almost sorted
arrays. And, of course, if the best-case efficiency of an algorithm is unsatisfactory,
It should be clear from our discussion, however, that neither the worst-case analysis nor its best -
case counterpart yields the necessary information about an algorithm's behavior on a "typical"
or "random" input. This is the information that the average-case efficiency seeks to provide. To
analyze the algorithm's average-case efficiency, we must make some assumptions about possible
inputs of size n.