0% found this document useful (0 votes)
18 views44 pages

Adsa-1 Unit

Notes

Uploaded by

appai3517
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)
18 views44 pages

Adsa-1 Unit

Notes

Uploaded by

appai3517
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/ 44

MC4101

ADVANCED DATA STRUCTURES AND ALGORITHMS

UNIT I: ROLE OF ALGORITHMS IN COMPUTING & COMPLEXITY ANALYSIS


MC4101
MC4101

Continues………
MC4101

Algorithms as a Technology
MC4101

Time and Space complexity of algorithms


MC4101
MC4101
MC4101

CONSTANT VALUE=3

VARIABLE VALUE=5
MC4101

AB-VARIABLE

10-CONSTANT
MC4101
MC4101
MC4101

Asymptotic analysis

Average and worst-case analysis


MC4101

EXAMPLE
MC4101

Asymptotic notation
MC4101
MC4101

EXAMPLE SUM:
MC4101
MC4101
MC4101

Importance of efficient algorithms

Why is efficiency important?


When you write small demonstration programs, it doesn’t really matter
whether your algorithm is efficient; on a modern computer you’ll get results
very quickly t.o

However, in real-world applications, efficiency is very important.


 A large program or application will have many algorithms. Inefficiency
quickly adds up and programs become slow and unresponsive.
 A recent trend in computing is ‘big data’, working with very large
datasets. The efficiency of an algorithm often gets worse rapidly as the
size of the dataset increases. Big data requires us to come up with new
algorithms, and efficiency is extremely important to be able to get
results in a useful amount of time.
 Users are very sensitive to delays. How would you feel if your favourite
search engine become ten times slower?
 Although computers are getting faster, we always expect them to do
more. The need for efficient algorithms has not gone away.
 Some applications are time critical. It doesn’t matter how accurately you
can predict the weather for tomorrow if you can’t calculate it until the
day after!
 Many modern businesses rely on having the fastest algorithms, such as
search and comparison engines, logistics companies, and companies
doing financial forecasting and medical analysis.
Efficiency versus understandability

When you’re getting started with programming, it’s also important that
algorithms are clear and simple to understand. While it’s important to consider
efficiency, it’s not necessary to try and find the most efficient algorithm. This
is a very hard task and a very efficient algorithm may be harder to understand.
MC4101

Remember, you often need to return to your code and understand it in order to
make changes, and other people may also need to work with your code.

Efficiency is only one factor in choosing or designing an algorithm.

Efficiency versus quality

 Recall that you can also have alternative algorithms that give different
results for the same problem.
For example, different search engines return different results for the
same query.
 The search algorithms used by the major search engines are regularly
changed and improved. Efficiency is very important for getting results
back to searchers quickly, but the results need to be high quality. It’s no
use returning poor results quickly.
 When designing an algorithm, you often need to balance time efficiency
with the quality of the results.


MC4101

Space Efficiency

In some cases, the amount of space/memory consumed has to be examined.


For example, in dealing with huge amounts of data or in programming
embedded systems, memory consumed must be analyzed. Space/memory
usage components are:

 Space of Instruction: Compiler, compiler settings, and target machine


(CPU), all have an impact on space of instruction.
 Data Space: Data size/dynamically allocated memory, static program
variables are all factors affecting data space.
 Stack Space at Runtime: Compiler, run-time function calls and
recursion, local variables, and arguments all have an impact on stack
space at runtime.
Time Efficiency

Obviously, the faster a program/function completes its objective, the better


the algorithm. The actual running time is determined by a number of
factors:

 Computer’s Speed: processor (not simply clock speed), I/O, and so


on.
 Compiler: The compiler, as well as the compiler parameters.
 Amount of Data: The amount of data, for example, whether to search
a lengthy or a short list.
MC4101

 Actual Data: The actual data, such as whether the name is first or
last in a sequential search.
Approaches to Time Efficiency

Asymptotic Categorization
This employs basic categories to offer a basic notion of performance,
which is also called an order of magnitude. If the algorithms are similar, the
data amount is little, or performance is essential, then the next method can
be explored further.

Estimation of running time


Estimation of running time depends on two things, i.e., Code Analysis and
Code Execution, which are elaborated below:

Code Analysis: We can accomplish the following things by analyzing the


code
 Operation Counts: Choose the most frequently performed operation(s)
and count how many times each one is performed.
 Step Counts: Calculate the total number of steps and potentially lines of
code that program executes.
 Benchmarking: Running the software on a variety of data sets and
comparing the results.

Program performance measurement


MC4101

Factors Determining Algorithm’s Performance

To compare algorithms, we consider a collection of parameters or components


such as the amount of memory required by the algorithm, its execution speed,
how easy it is to comprehend and execute, and so on.

In general, an algorithm’s performance is determined by the following factors:

 Is that algorithm giving you the perfect solution to your problem?


 Is it straightforward to comprehend?
 Is it simple to put into practice?
 How much memory (space) is needed to solve the problem?
 How long does it take to remedy the issue?

When we aim to analyze an algorithm, we just look at space and time


requirements of that algorithm and neglect anything else. On the basis of this
information, an algorithm’s performance may alternatively be described as a
technique of determining the space and time requirements of an algorithm.

The following metrics are used to evaluate the performance of an algorithm:

 The amount of space necessary to perform the algorithm’s task (Space


Complexity). It consists of both program and data space.
MC4101

 The time necessary to accomplish the algorithm’s task (Time Complexity).

Space Complexity
When we create a problem-solving algorithm, it demands the use of computer
memory to complete its execution. Memory is necessary for the following
purposes in any algorithm:

 To keep track of software instructions.


 To keep track of constant values.
 To keep track of variable values.
 Additionally, for a few additional things such as function calls, jump
statements, and so on.
When software is running, it often utilizes computer memory for the following
reasons:

 The amount of memory needed to hold compiled versions of instructions,


which is referred to as instruction space.
 The amount of memory utilized to hold information about partially run
functions at the time of a function call, which is known as the
environmental stack.
 The amount of memory needed to hold all of the variables and constants,
which is referred to as data space.
Example
We need to know how much memory is required to store distinct datatype
values to compute the space complexity (according to the compiler). Take a
look at the following code:

int square(int a)

return a*a;

}
MC4101

In the preceding piece of code, variable ‘a’ takes up 2 bytes of memory, and
the return value takes up another 2 bytes, i.e., it takes a total of 4 bytes of
memory to finish its execution, and for any input value of ‘a’, this 4 byte
memory is fixed. Constant Space Complexity is the name given to this type of
space complexity.

Time Complexity

Every algorithm needs a certain amount of computer time to carry out its
instructions and complete the operation. The amount of computer time
required is referred to as time complexity. In general, an algorithm’s execution
time is determined by the following:

 It doesn’t matter if it’s on a single processor or a multi-processor


computer.
 It doesn’t matter if it’s a 32-bit or 64-bit computer.
 The machine read and write speeds.
 The time taken by an algorithm to complete arithmetic, logical, return
value, and assignment operations, among other things.
 Data to be entered
Example
Calculating an algorithm’s Time Complexity based on the system
configuration is a challenging undertaking since the configuration varies from
one system to the next. We must assume a model machine with a certain setup
to tackle this challenge. As a result, we can compute generalized time
complexity using that model machine. Take a look at the following code:

int sum(int a, int b)

return a+b;

}
MC4101

In the following example code, calculating a+b takes 1 unit of time, and
returning the value takes 1 unit of time, i.e., it takes two units of time to
perform the task, and it is unaffected by the input values for a and b. This
indicates it takes the same amount of time for all input values, i.e., 2 units.

Recurrences:
MC4101

Types of substitution method

1) Forward Substitution Method

2) Backward Substitution Method


MC4101
MC4101
MC4101
MC4101
MC4101

Recursion

 Recursion is a coding technique/ design in which a function calls


itself directly or indirectly and the corresponding function is called as
recursive function. Using this many problems can be solved easily with
less time.
 Note that to use recursion, the programming language and platform
should support this. All major languages including C, C++, Java, Python,
Go and others support Recursion.
 Fortan 77, an early programming language does not support recursion.

There are two major parts in recursion:

 base condition: the condition in which a value is returned and no further


functions are called.
 recursion condition: the way return value of another function call is used to
generate the return value of the parent function call.

Flow chart of recursion

Types of recursion
There are two types of recursion:

 Direct recursion

 Indirect recursion
MC4101

Direct Recursion :
A function fun is called direct recursive if it calls the same function fun.

void directFun()
{
// Some code....

directFun();

// Some code...
}
C
Copy

Indirect Recursion
A function fun is called indirect recursive if it calls another function say fun
_new and fun _new calls fun directly or indirectly.

void indirectFun1()
{
// Some code...

indirectFun2();

// Some code...
}
void indirectFun2()
{
// Some code...

indirectFun1();

// Some code...
}
MC4101

Recursion Tree Method

1. Recursion Tree Method is a pictorial representation of an iteration method


which is in the form of a tree where at each level nodes are expanded.

2. In general, we consider the second term in recurrence as root.

3. It is useful when the divide & Conquer algorithm is used.

4. It is sometimes difficult to come up with a good guess. In Recursion tree,


each root and child represents the cost of a single sub problem .How to find Nth
Highest Salary in SQL

5. We sum the costs within each of the levels of the tree to obtain a set of pre-
level costs and then sum all pre-level costs to determine the total cost of all
levels of the recursion.

6. A Recursion Tree is best used to generate a good guess, which can be


verified by the Substitution Method.

Example 1

Consider T (n) = 2T + n2

We have to obtain the asymptotic bound using recursion tree method.

Solution: The Recursion tree for the above recurrence is


MC4101
MC4101

Example 2: Consider the following recurrence

T (n) = 4T +n

Obtain the asymptotic bound using recursion tree method.

Solution: The recursion trees for the above recurrence


MC4101

Example 3: Consider the following recurrence

Obtain the asymptotic bound using recursion tree method.

Solution: The given Recurrence has the following recursion tree

When we add the values across the levels of the recursion trees, we get a
value of n for every level. The longest path from the root to leaf is
MC4101

Data structures and algorithms.


MC4101
MC4101
MC4101
MC4101
MC4101
MC4101

You might also like