0% found this document useful (0 votes)
28 views

Unit1 Daa 2023 Nep

Uploaded by

twitterkatoolkit
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)
28 views

Unit1 Daa 2023 Nep

Uploaded by

twitterkatoolkit
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
You are on page 1/ 16

UNIT 1

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.

 An algorithm has zero or more inputs. Each instruction that contains a


fundamental operator must accept zero or more inputs.
 Output: An algorithm produces at least one output. Every instruction that
contains a fundamental operator must accept zero or more inputs.
 Definiteness: All instructions in an algorithm must be unambiguous, precise, and
easy to interpret. By referring to any of the instructions in an algorithm, one can
clearly understand what is to be done. Every fundamental operator in an
instruction must be defined without any ambiguity.
 Finiteness: An algorithm must terminate after a finite number of steps in all test
cases. Every instruction that contains a fundamental operator must be terminated
within a finite amount of time. Infinite loops or recursive functions without base
conditions do not possess finiteness.
 Effectiveness: An algorithm must be developed using very basic, simple, and
feasible operations so that one can trace it out using just paper and pencil.
Properties of Algorithm:
 It should terminate after a finite time.
 It should produce at least one output.
 It should take zero or more input.
 It should be deterministic, and giving the same output for the same input case.
 Every step in the algorithm must be effective, i.e.,e. every step should do some
work.

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.

It consists of the following three steps:


 Divide
 Solve
 Combine
8. Greedy Algorithm:
In this type of algorithm, the solution is built part by part. The solution for the next part is
built based on the immediate benefit of the next part. The one solution that gives the most
benefit will be chosen as the solution for the next part.
9. Dynamic Programming Algorithm:
This algorithm uses the concept of using the already found solution to avoid repetitive
calculation of the same part of the problem. It divides the problem into smaller overlapping
subproblems and combine.
10. Randomized Algorithm:
In the greedy algorithm, we use a random number so it gives immediate benefit. The random
number helps in deciding the expected outcome.
The advantages of Algorithms:
 It is easy to understand.
 An algorithm is a step-wise representation of a solution to a given problem.
 In an algorithm, the problem is broken down into smaller pieces or steps,
making it easier for the programmer to convert it into an actual program.
The disadvantage of Algorithms:
 Writing an algorithm takes a long time, so it is time-consuming.
 Understanding complex logic through algorithms can be very difficult.
 Branching and looping statements are difficult to show in
Algorithms(important). How to Design an Algorithm?
To design an algorithm, the following things are needed as a prerequisite:
1. The problem that is to be solved by this algorithm i.e. clear problem definition.
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 is to be expected when the problem is solved.
5. The solution to this problem is within the given constraints.
Then the algorithm is written with the help of the above parameters such that it solves the
problem.
Example: Consider the example to add three numbers and print the sum.

Step 1: Fulfilling the prerequisites


As discussed above, to write an algorithm, its prerequisites must be fulfilled.
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 is the three numbers to be added.
4. The output to be expected when the problem is solved is the sum of the three
numbers taken as the input i.e. a single integer value.
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 the ‘+’ operator, or bitwise,
or any other method.
Step 2: Designing the algorithm
Now let’s design the algorithm with the help of the above prerequisites:
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 the variable sum
7. END
Step 3: Testing the algorithm by implementing it.
(vii)Coding an Algorithm
a.The coding / implementation of an algorithm is done using a suitable programming
language such as C, C++, JAVA
Fundamentals of Algorithmic Problem Solving:
A sequence of steps involved in designing and analyzing an algorithm is shown in the figure
below.

Figure: Flow diagram for designing and analyzing an algorithm


1. .Understanding the Problem.
This is the first step in designing an algorithm.
Read the problem’s description carefully to understand the problem statement completely.
Ask questions for clarifying the doubts about the problem.
Identify the problem types and use existing algorithms to find a solution.
The input (instance) to the problem and the range of the input are fixed.

(ii) Decision Making.


The Decision making process is based on the following: (a) Ascertaining the capabilities of
the computational device. In a random-access machine (RAM), instructions are executed one
after another (the central assumption is that one operation at a time). Accordingly, algorithms
designed to be executed on such machines are called sequential algorithms.
In some newer computers, operations are executed concurrently, i.e., in parallel. Algorithms
that take advantage of this capability are called parallel algorithms. The choice of
computational devices, such processors and memory, is mainly based on space and time
efficiency.

(b) Choosing between Exact and Approximate Problem Solving.


The next principal decision is to choose between solving the problem exactly and solving it
approximately.
An algorithm used to solve the problem exactly and produce a correct result is called an exact
algorithm.
If the problem is too complex and not able to get exact solution, then we have to choose an
algorithm called an approximation algorithm, i.e., produces an approximate answer. E.,
extracting square roots, solving nonlinear equations, and evaluating definite integrals.
(c) Algorithm Design Techniques.

An algorithm design technique (or “strategy” or “paradigm”) is a general approach to solving


problems algorithmically that is applicable to a variety of problems from different areas of
computing.

(v) Analyzing an Algorithm


a. For an algorithm, the most important thing is efficiency. In fact, there are two kinds of
algorithm efficiency. They are:
b. Time efficiency, indicating how fast the algorithm runs, and
c. space efficiency, indicating how much extra memory it uses.
d. The efficiency of an algorithm is determined by measuring both time efficiency and space
efficiency.
e. So, factors to analyze an algorithm are:
1. Time efficiency of an algorithm
2. Space efficiency of an algorithm
3. Simplicity of an algorithm
4. Generality of an algorithm

(vi) Coding an Algorithm

a. The coding / implementation of an algorithm is done by a suitable programming language


like C, C++, or JAVA.
b. The transition from an algorithm to a program can be done either incorrectly or JAVA.
Implementing an algorithm correctly is necessary. The algorithm's power should not reduce
by inefficient implementation.
c. Standard tricks like computing a loop’s invariant (an expression that does not change its
value) outside the loop, collecting common sub-expressions, replacing expensive operations
by cheap ones, selection of programming language, and so on should be known to the
programmer.
d. Typically, such improvements can speed up a program only by a constant factor, whereas
a better algorithm can make a difference in running time by orders of magnitude.
e. It is very essential to write optimized code (efficient code) to reduce the burden of the
compiler.
f. The role of algorithms in computing The most important problem types related to
computing are:
Sorting:
The sorting problem is to rearrange the items of a given list in non-decreasing (ascending)
order. The searching problem deals with finding a given value, called a search key, in a given
set.
String processing: A string is a sequence of characters from an alphabet. Strings comprise
letters, numbers, and special characters; bit strings, which comprise zeros and ones; and gene
sequences, which can be modeled by strings of characters from the four-charact er
alphabet
{A, C, G, T}. It is very useful in bioinformatics. Searching for a given word in a text is called
string matching.
Graph problems: A graph is a collection of points called vertices, some of which are
connected by line segments called edges. Some of the graph problems are graph traversal,
shortest path algorithm, topological sort, traveling salesman problem, and the graph-coloring
problem, and so on.
Combinatorial problems: These are problems that ask, explicitly or implicitly, to find a
combinatorial object such as a permutation, a combination, or a subset that satisfies certain
constraints.
Geometric problems: Geometric algorithms deal with geometric objects such as points,
lines, and polygons. Geometric algorithms are used in algorithms and number theory.
Numerical problems are problems that involve mathematical equations, systems of equations,
computing definite integrals, evaluating functions, and so on. The majority of such
mathematical problems can be solved only approximately.

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.

Fundamentals of the Analysis of Algorithm Efficiency:

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.
Algorithm analysis is an important part of computational complexity theory, which provides
theoretical estimation for the required resources of an algorithm to solve a specific
computational problem. Most algorithms are designed to work with inputs of arbitrary length.
Analysis of algorithms is the determination of the amount of time and space resources
required to execute them.

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 compiler used in generating the machine code.

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

Worst-Case, Best-Case, and Average-Case Efficiencies

Analysis of an algorithm is the process of analyzing the problem-solving capability of the


algorithm in terms of the time and size required (the size of memory for storage while
implementation). However, the main concern of the analysis of algorithms is the required
time or performance. Generally, we perform the following types of analysis.

Worst-case - The maximum number of steps taken on any instance of size a.


Best-case - The minimum number of steps taken on any instance of size a.
The index of the first element in A that matches K or -1 if there is no match.

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.)

Average case − An average number of steps taken on any instance of size a.


ALGORITHM SequentialSearch(A[0 .. n - 1], K)

//Searches for a given value in a given array by sequential search

//Input: An array A[0 .. n -1] and a search key K

//Output: The index of the first element of A that matches K

II or -1 if there are no matching elements

i+-0

while i <n and A[i] ≠ K do

i+-i+1

ifi < n then return i

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

Cworst(n), its running time on the worst-case inputs.

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.

You might also like