0% found this document useful (0 votes)
15 views167 pages

ADS ALL 5 UNIT NOTES Final

The document discusses the role of algorithms in computing, covering their definitions, characteristics, and importance in efficiency analysis. It explains various algorithm design techniques, types of efficiencies (best, worst, average, and amortized), and asymptotic notations (Big O, Omega, Theta). Additionally, it highlights the significance of studying algorithms for problem-solving and performance measurement in computer science.

Uploaded by

Hariharan SK
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)
15 views167 pages

ADS ALL 5 UNIT NOTES Final

The document discusses the role of algorithms in computing, covering their definitions, characteristics, and importance in efficiency analysis. It explains various algorithm design techniques, types of efficiencies (best, worst, average, and amortized), and asymptotic notations (Big O, Omega, Theta). Additionally, it highlights the significance of studying algorithms for problem-solving and performance measurement in computer science.

Uploaded by

Hariharan SK
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/ 167

UNIT I ROLE OF ALGORITHMS IN COMPUTING & COMPLEXITY ANALYSIS

Algorithms – Algorithms as a Technology -Time and Space complexity of algorithms- Asymptotic


analysis- average and worst-case analysis-Asymptotic notation-Importance of efficient algorithms
Program performance measurement - Recurrences: The Substitution Method – The Recursion-Tree
Method- Data structures and algorithms.

1.Why is the need of studying algorithms?


From a practical standpoint, a standard set of algorithms from different areas of computing must be known, in
addition to be able to design them and analyze their efficiencies. From a theoretical standpoint the study of algorithms
is the cornerstone of computer science.

2.What is an algorithm?
An algorithm is a sequence of unambiguous instructions for solving a problem, i.e., for obtaining a required output
for any legitimate input in finite amount of time.
An algorithm is step by step procedure to solve a problem.

3.Give the diagram representation of Notion of algorithm.

4. Define Algorithm.
An algorithm is a sequence of unambiguous instructions for solving a problem, i.e., for obtaining a
required output for any legitimate input in a finite amount of time.
5.Define order of an algorithm.
The order of an algorithm is a standard notation of an algorithm that has been developed to represent
function that bound the computing time for algorithms. The order of an algorithm is a way of defining
its efficiency. It isusually referred as Big O notation.
6.What are the features of efficient algorithm?
 Free of ambiguity
 Efficient in execution time
 Concise and compact
 Completeness
 Definiteness
 Finiteness
7. Define Asymptotic Notations.
The notation that will enable us to make meaningful statements about the time and space complexities
of a program. This notation is called asymptotic notation. Some of the asymptotic notation are 1. Big
Oh notation, 2. Theta notation, 3. Omega notation, 4. LittleOh notation.
a. What is best-case efficiency?
The best-case efficiency of an algorithm is its efficiency for the best-case input of size n, which is an
input or inputs for which the algorithm runs the fastest among all possible inputs of that size.
8.What are the fundamental steps involved in algorithmic problem solving?
The fundamental steps are
a. Understanding the problem
b. Ascertain the capabilities of computational device
c. Choose between exact and approximate problem solving
d. Decide on appropriate data structures
e. Algorithm design techniques
f. Methods for specifying the algorithm
g. Proving an algorithms correctness
h. Analyzing an algorithm
i. Coding an algorithm

9.What is an algorithm design technique?


An algorithm design technique is a general approach to solving problems algorithmically that isapplicable to a
variety of problems from different areas of computing.
10.what are the types of algorithm efficiencies?
The two types of algorithm efficiencies are
a. Time efficiency: indicates how fast the algorithm runs
b. Space efficiency: indicates how much extra memory the algorithm needs
11.Mention some of the important problem types?
Some of the important problem types are as follows
a. Sorting
b. Searching
c. String processing
d. Graph problems
e. Combinatorial problems
f. Geometric problems
g. Numerical problems
12.What is worst-case efficiency?
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 allpossible inputs of
that size.
13.What is best-case efficiency?
The best-case efficiency of an algorithm is its efficiency for the best-case input of size n, whichis an inputor
inputs for which the algorithm runs the fastest among allpossible inputs of that size.
14.What is average case efficiency?
The average case efficiency of an algorithm is its efficiency for an average case input of size
n. It providesinformation about an algorithm behavior on a ―typical‖ or ―rando m‖ input.
15.What is amortized efficiency?
In some situations a single operation can be expensive, but the total time for theentire sequence of n
suchoperations is always significantly better that the worst case efficiency of that single operation multiplied
by
n. this is called amortized efficiency.
16.Define O-notation?
A function t(n) is said to be in O(g(n)), denoted by t(n) ε O(g(n)), if t(n) is bounded above by some constant
multiple of g(n) for all large n, i.e., if there exists some positive constant c and some non-negative integer n0
such that
T (n) <=cg (n) for all n >= n0
17.Define Ω-notation?
A function t(n) is said to be in Ω (g(n)), denoted by t(n) ε Ω (g(n)), if t(n) is bounded below by some constant
multiple of g(n) for all large n, i.e., if there exists some positive constant c and some non-negative integer n0
such that
T (n) >=cg (n) for all n >=n0
18.Define θ-notation?
A function t(n) is said to be in θ (g(n)), denoted by t(n) ε θ (g(n)), if t(n) is boundedboth above & below by
some constant multiple of g(n) for all large n, i.e., if there exists some positive constants c1 & c2 and some
nonnegative integer n0 such that
c2g (n) <= t (n) <= c1g (n) for all n >= n0
19.Mention the useful property, which can be applied to the asymptotic notations and its use?
If t1(n) ε O(g1(n)) and t2(n) ε O(g2(n)) then t1(n)+t2(n) ε max {g1(n),g2(n)} this property is also true for
Ω and θ notations. This property will be useful inanalyzing algorithms that comprise of two consecutive
executable parts.
20.What are the basic asymptotic efficiency classes?
The various basic efficiency classes are
a. Constant : 1
b. Logarithmic : log n
c. Linear : n
d. N-log-n : nlog n
e. Quadratic : n2
f. Cubic : n3
g. Exponential : 2n
h. Factorial : n!

Write an algorithm for Towers of Hanoi problem using recursion and analyze the complexity (No of disc-4).

Why study Algorithm?

As the speed of processor increases, performance is frequently said to be less central than
other software quality characteristics (e.g. security, extensibility, reusability etc.). However,
large problem sizes are commonplace in the area of computational science, which makes
performancea very important factor.

Write any four Characteristics of Algorithms.


o Input: It should externally supply zero or more quantities.
o Output: It results in at least one quantity.
o Definiteness: Each instruction should be clear and ambiguous.
o Finiteness: An algorithm should terminate after executing a finite number of steps.
o Effectiveness: Every instruction should be fundamental to be carried out, in
principle, bya person using only pen and paper.
o Feasible: It must be feasible enough to produce each instruction.
o Flexibility: It must be flexible enough to carry out desired changes with no efforts.
o Efficient: The term efficiency is measured in terms of time and space required by an
algorithm to implement. Thus, an algorithm must ensure that it takes little time and
less memory space meeting the acceptable limit of development time.
o Independent: An algorithm must be language independent, which means that it
should mainly focus on the input and the procedure required to derive the output
instead of depending upon the language.

Advantages of an Algorithm
o Effective Communication: Since it is written in a natural language like English, it
becomes easy to understand the step-by-step delineation of a solution to any
particular problem.
o Easy Debugging: A well-designed algorithm facilitates easy debugging to detect the
logical errors that occurred inside the program.
o Easy and Efficient Coding: An algorithm is nothing but a blueprint of a program
thathelps develop a program.
o Independent of Programming Language: Since it is a language- independent, it
can beeasily coded by incorporating any high-level language.

Disadvantages of an Algorithm
o Developing algorithms for complex problems would be time-consuming and difficult to
understand.
o It is a challenging task to understand complex logic through algorithms.

Why Need forAlgorithm?

1. To understand the basic idea of the problem.

2. To find an approach to solve the problem.

3. To improve the efficiency of existing techniques.

4. To understand the basic principles of designing the algorithms.

5. To compare the performance of the algorithm with respect to other techniques.


6. It is the best method of description without describing the implementation detail.

7. The Algorithm gives a clear description of requirements and goal of the problem to
thedesigner.

8. A good design can produce a good solution.

9. To understand the flow of the problem.

10. To measure the behavior (or performance) of the methods in all cases (bestcases,
worst cases, average cases)

11. With the help of an algorithm, we can also identify the resources (memory, input-
output) cycles required by the algorithm.
12. With the help of algorithm, we convert art into a science.

13. To understand the principle of designing.

14. We can measure and analyze the complexity (time and space) of the problems
concerninginput size without implementing and running it; it will reduce the cost of design.

What are types of Analysis of algorithm?

The analysis is a process of estimating the efficiency of an algorithm. There are two
fundamental parameters based on which we can analysis the algorithm:

o Space Complexity: The space complexity can be understood as the amount of space
required by an algorithm to run to completion.
o Time Complexity: Time complexity is a function of input size n that refers to the
amount of time needed by an algorithm to run to completion.

Generally, we make three types of analysis, which is as follows:

o Worst-case time complexity: For 'n' input size, the worst-case time complexity can
be defined as the maximum amount of time needed by an algorithm to complete its
execution. Thus, it is nothing but a function defined by the maximum number of
steps performed on an instance having an input size of n.

Average case time complexity: For 'n' input size, the average-case time

complexity can be defined as the average amount of time needed by an


algorithm to complete its execution. Thus, it is nothing but a function defined by the
average number of steps performed on an instance having an input sizeof n.
o Best case time complexity: For 'n' input size, the best-case time complexity can be
defined as the minimum amount of time needed by an algorithm to complete its
execution. Thus, it is nothing but a function defined by the minimum number of steps
performed on an instance having an input size of n.
1.Write a program for Linear Search and find the time complexity and space

complexity.

Linear Search complexity

Now, let's see the time complexity of linear search in the best case, average case, and
worst case. We will also see the space complexity of linear search.

What is Time Complexity ?

1. Time Complexity
Case Time Complexity

Best Case O(1)

Average Case O(n)

Worst Case O(n)

Best Case Complexity - In Linear search, best case occurs when the element we are
finding is at the first position of the array. The best-case time complexityof linear search
is O(1).

Average Case Complexity - The average case time complexity of linear searchis O(n).

Worst Case Complexity - In Linear search, the worst case occurs when the element we
are looking is present at the end of the array. The worst-case in linear search could be
when the target element is not present in the given array, and we have to traverse the
entire array. The worst-case time complexity of linear search is O(n).

The time complexity of linear search is O(n) because every element in the arrayis
compared only once.

2. Space Complexity
Space Complexity O(1)

The space complexity of linear search is O(1)


Implementation of Linear Search
Now, let's see the programs of linear search in different programming languages.
Program: Write a program to implement linear search in C language. #include
<stdio.h>
int linearSearch(int a[], int n, int val) {
// Going through array sequencially
for (int i = 0; i < n; i++)
{

if (a[i] == val)
return i+1;
}

return -1;
}
int main() {

int a[] = {70, 40, 30, 11, 57, 41, 25, 14, 52}; // given arrayint val
=41; // value to be searched
int n = sizeof(a) / sizeof(a[0]); // size of arrayint res
= linearSearch(a, n, val); // Store result
printf("Theelements of the array are - ");
for (int i = 0; i < n; i++)
printf("%d ", a[i]);
printf("\nElement to be searched is - %d", val); if (res
== -1)
printf("\nElement is not present in the array");else
printf("\nElement is present at %d position of array", res);return
0;
}
How to approximate the time taken by the Algorithm?

So, to find it out, we shall first understand the types of the algorithm we have.There
aretwo types of algorithms:

1. Iterative Algorithm: In the iterative approach, the function repeatedly runs until the
condition is met or it fails. It involves the looping construct.
2. Recursive Algorithm: In the recursive approach, the function calls itself until the
condition is met. It integrates the branching structure.

2.Explain different types of design

Techniques.

Algorithm Design Techniques

The following is a list of several popular design approaches:

1. Divide and Conquer Approach: It is a top-down approach. The algorithms which


follow the divide & conquer techniques involve three steps:

o Divide the original problem into a set of subproblems.


o Solve every subproblem individually, recursively.
o Combine the solution of the sub problems (top level) into a solution of the whole
original problem.
2. Greedy Technique: Greedy method is used to solve the optimization problem. An
optimization problem is one in which we are given a set of input values, which are required
either to be maximized or minimized (known as objective), i.e. some constraints or
conditions.

o Greedy Algorithm always makes the choice (greedy criteria) looks best at the
moment, to optimize a given objective.
o The greedy algorithm doesn't always guarantee the optimal solution however it generally
produces a solution that is very close in value to the optimal.

3. Dynamic Programming: Dynamic Programming is a bottom-up approach we solve all


possible small problems and then combine them to obtain solutions for bigger problems.

This is particularly helpful when the number of copying subproblems is exponentially


large.Dynamic Programming is frequently related to Optimization Problems.

4. Branch and Bound: In Branch & Bound algorithm a given subproblem, which cannot be
bounded, has to be divided into at least two new restricted subproblems. Branch and Bound
algorithm are methods for global optimization in non-convex problems. Branch and Bound
algorithms can be slow, however in the worst case they require effort that grows
exponentially with problem size, but in some cases we are lucky, and the method coverage
with much less effort.

6. Backtracking Algorithm: Backtracking Algorithm tries each possibility until they find
the right one. It is a depth-first search of the set of possible solution. During the search, if an
alternative doesn't work, then backtrack to the choice point, the place which presented
different alternatives, and tries the next alternative.

Example 1: In Quick Sort, using a random number to choose a pivot.

Why is Asymptotic Notation Important?

1. They give simple characteristics of an algorithm's efficiency.

2. They allow the comparisons of the performances of various algorithms.

3. Explain different types of Asymptotic Notations


with an example.

Asymptotic Notations:

The word Asymptotic means approaching a value or curve arbitrarily closely (i.e., as some sort
of limit is taken).

Asymptotic notations are used to write fastest and slowest possible running time for an
algorithm. These are also referred to as 'best case' and 'worst case' scenarios respectively.

"In asymptotic notations, we derive the complexity concerning the size of the input.
(Example in terms of n)"

Asymptotic Notation is a way of comparing function that ignores constant factors and
small input sizes. Three notations are used to calculate the running timecomplexity of an
algorithm:

1. Big-oh notation: Big-oh is the formal method of expressing the upper bound of an
algorithm's running time. It is the measure of the longest amount of time. The function f (n) =
O (g (n)) [read as "f of n is big-oh of g of n"] if and only if exist positive constant c and such
that

1. f (n) ⩽ k.g (n)f(n)⩽k.g(n) for n>n0n>n0 in all case

Hence, function g (n) is an upper bound for function f (n), as g (n) grows fasterthan f (n)

For Example:
1. 1. 3n+2=O(n) as 3n+2≤4n for all n≥2
2. 2. 3n+3=O(n) as 3n+3≤4n for all n≥3

Hence, the complexity of f(n) can be represented as O (g (n))

2. Omega () Notation: The function f (n) = Ω (g (n)) [read as "f of n is omega of gof n"] if
andonly if there exists positive constant c and n0 such that

F (n) ≥ k* g (n) for all n, n≥ n0


For Example:
f (n) =8n2+2n-3≥8n2-3
=7n2+(n2-3)≥7n2 (g(n))
Thus, k1=7

Hence, the complexity of f (n) can be represented as Ω (g (n))

3. Theta (θ): The function f (n) = θ (g (n)) [read as "f is the theta of g of n"] if andonly if
thereexists positive constant k1, k2 and k0 such that

k1 * g (n) ≤ f(n)≤ k2 g(n)for all n, n≥ n0

For Example:
3n+2= θ (n) as 3n+2≥3n and 3n+2≤ 4n, for n
k1=3,k2=4, and n0=2

Hence, the complexity of f (n) can be represented as θ (g(n)).


The Theta Notation is more precise than both the big-oh and Omega notation. Thefunction f (n)
= θ (g (n)) if g(n) is both an upper and lower bound.

4.What is recurrence relation? Explain substitution method and recursion tree methods.

Recurrence Relation

A recurrence is an equation or inequality that describes a function in terms of its values on


smaller inputs. To solve a Recurrence Relation means to obtain a function defined on the
natural numbers that satisfy the recurrence.

For Example, the Worst Case Running Time T(n) of the MERGE SORT Procedures is
described by the recurrence.
T (n) = θ (1) if n=1 2T

+ θ (n) if n>1
There are four methods for solving Recurrence:

1. Substitution Method
2. Iteration Method
3. Recursion Tree Method
4. Master Method

5.Explain substitution method and recursion tree methods.

1.Substitution Method:

The Substitution Method Consists of two main steps:

1. Guess the Solution.


2. Use the mathematical induction to find the boundary condition and shows that the
guess is correct.

For Example1 Solve the equation by Substitution Method.

T (n) = T +n

We have to show that it is asymptotically bound by O (log n).

Solution:
We have to show that for some constant c

1. T (n) ≤c logn.
Put this in given Recurrence Equation.

T (n) ≤c log +1

≤c log + 1 = c logn-clog2 2+1


≤c logn for c≥1
Thus T (n) =O logn.

Example2 Consider the Recurrence

T (n) = 2T + n n>1

Find an Asymptotic bound on T.

Recursion Tree Method

1. Recursion Tree Method is a pictorial representation of an iteration method whichis 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
childrepresents the cost of a single subproblem.

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
theSubstitution 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


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

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

Data structures and algorithms


What is an Algorithm?

An algorithm is a process or a set of rules required to perform calculations or some


otherproblem-solving operations especially by a computer. The formal definition
of an algorithm is that it contains the finite set of instructions which are being carried in a
specific order to perform the specific task. It is not the complete program or code; it is just a
solution (logic) of a problem, which can be representedeither as an informal description
using a Flowchart or Pseudocode.

Characteristics of an Algorithm

The following are the characteristics of an algorithm:

o Input: An algorithm has some input values. We can pass 0 or some input value to an
algorithm.
o Output: We will get 1 or more output at the end of an algorithm.
o Unambiguity: An algorithm should be unambiguous which means that the
instructions inan algorithm should be clear and simple.
o Finiteness: An algorithm should have finiteness. Here, finiteness means that the
algorithm should contain a limited number of instructions, i.e., the instructions
should be countable.
o Effectiveness: An algorithm should be effective as each instruction in an algorithm
affects the overall process.
o Language independent: An algorithm must be language-independent so that the
instructions in an algorithm can be implemented in any of thelanguages with the
same output.

Dataflow of an Algorithm
o Problem: A problem can be a real-world problem or any instance from the real-
world problem for which we need to create a program or the set of instructions. The
set of instructions is known as an algorithm.
o Algorithm: An algorithm will be designed for a problem which is a step by step
procedure.
o Input: After designing an algorithm, the required and the desired inputs are provided
to the algorithm.
o Processing unit: The input will be given to the processing unit, and the processing
unit will produce the desired output.
o Output: The output is the outcome or the result of the program.
5. Why do we need Algorithms?

We need algorithms because of the following reasons:

o Scalability: It helps us to understand the scalability. When we have a big real-


worldproblem, we need to scale it down into small-small steps to easilyanalyze the
problem.
o Performance: The real-world is not easily broken down into smaller steps. If the
problem can be easily broken into smaller steps means that the problem is feasible.

Let's understand the algorithm through a real-world example. Suppose we want tomake a
lemon juice, so following are the steps required to make a lemon juice:

Step 1: First, we will cut the lemon into half.

Step 2: Squeeze the lemon as much you can and take out its juice in a container.Step 3: Add

twotablespoon sugar in it.

Step 4: Stir the container until the sugar gets dissolved.

Step 5: When sugar gets dissolved, add some water and ice in it. Step 6:

Store the juice in a fridge for 5 to minutes.

Step 7: Now, it's ready to drink.

The above real-world can be directly compared to the definition of the algorithm. We cannot
perform the step 3 before the step 2, we need to follow the specific order to make lemon
juice. An algorithm also says that each and every instruction should be followed in a specific
order to perform a specific task.

Now we will look an example of an algorithm in programming.

We will write an algorithm to add two numbers entered by the user.

The following are the steps required to add two numbers entered by the user:

Step 1: Start
Step 2: Declare three variables a, b, and sum.Step 3:

Enter the values of a and b.

Step 4: Add the values of a and b and store the result in the sum variable,

i.e.,sum=a+b.Step 5: Print sum

Step 6: Stop

Factors of an Algorithm

The following are the factors that we need to consider for designing analgorithm:

o Modularity: If any problem is given and we can break that problem into small-small
modules or small-small steps, which is a basic definition of an algorithm, it means
that this feature has been perfectly designed for the algorithm.
o Correctness: The correctness of an algorithm is defined as when the given inputs
produce the desired output, which means that the algorithm has been designed
algorithm.The analysis of an algorithm has been done correctly.
o Maintainability: Here, maintainability means that the algorithm should be designed
in a very simple structured way so that when we redefine the algorithm, no major
change will be done in the algorithm.
o Functionality: It considers various logical steps to solve the real-worldproblem.
o Robustness: Robustness means that how an algorithm can clearly define ourproblem.
o User-friendly: If the algorithm is not user-friendly, then the designer will not be able
to explain it to the programmer.
o Simplicity: If the algorithm is simple then it is easy to understand.
o Extensibility: If any other algorithm designer or programmer wants to use your
algorithm then it should be extensible.
Importance of Algorithms
1. Theoretical importance: When any real-world problem is given to us and we break
the problem into small-small modules. To break down the problem, we should know
all the theoretical aspects.
2. Practical importance: As we know that theory cannot be completed without the
practical implementation. So, the importance of algorithm can be considered as both
theoretical and practical.

Issues of Algorithms

The following are the issues that come while designing an algorithm:

o How to design algorithms: As we know that an algorithm is a step-by-


stepprocedure so we must follow some steps to design an algorithm.
o How to analyze algorithm

efficiencyApproaches of Algorithm

The following are the approaches used after considering both the theoretical and
practical importance of designing an algorithm:

o Brute force algorithm: The general logic structure is applied to design an


algorithm. It is also known as an exhaustive search algorithm that searches all the
possibilities to provide the required solution. Such algorithms are of two types:
1. Optimizing: Finding all the solutions of a problem and then take out the best
solution or if the value of the best solution is known then it will terminate if
the best solution is known.
2. Sacrificing: As soon as the best solution is found, then it will stop.
o Divide and conquer: It is a very implementation of an algorithm. It allows you to
design an algorithm in a step-by-step variation. It breaks down the algorithm to solve
the problem in different methods. It allows you to break down the problem into
different methods, and valid output is produced for the valid input. This valid output
is passed to some other function.
o Greedy algorithm: It is an algorithm paradigm that makes an optimal choice on
each iteration with the hope of getting the best solution. It is easy to implement and
has a faster execution time. But, there are very rare cases in which it provides the
optimal solution.
o Dynamic programming: It makes the algorithm more efficient by storing the
intermediate results. It follows five different steps to find the optimal solution for the
problem:
1. It breaks down the problem into a sub problem to find the optimal solution.
2. After breaking down the problem, it finds the optimal solution out of theses
u b problems.
3. Stores the result of the sub problems is known as memorization.
4. Reuse the result so that it cannot be recomputed for the same sub problems.
5. Finally, it computes the result of the complex program.
o Branch and Bound Algorithm: The branch and bound algorithm can be applied to
only integer programming problems. This approach divides all the sets of feasible
solutions into smaller subsets. These subsets are further evaluated to find the best
solution.
o Randomized Algorithm: As we have seen in a regular algorithm, we have
predefined input and required output. Those algorithms that have some defined set of
inputs and required output, and follow some described steps are known as
deterministic algorithms. What happens that when the random variable is introduced
in the randomized algorithm?. In a randomized algorithm, some random bits are
introduced by the algorithm and added in the input to produce the output, which is
random in nature. Randomized algorithms are simpler and efficient than the
deterministic algorithm.
o Backtracking: Backtracking is an algorithmic technique that solves the problem
recursively and removes the solution if it does not satisfy the constraints of a
problem.
The major categories of algorithms are given below:

o Sort: Algorithm developed for sorting the items in a certain order.


o Search: Algorithm developed for searching the items inside a data structure.
o Delete: Algorithm developed for deleting the existing element from the datastructure.
o Insert: Algorithm developed for inserting an item inside a data structure.
o Update: Algorithm developed for updating the existing element inside a data
structure.
Algorithm Analysis

The algorithm can be analyzed in two levels, i.e., first is before creating the algorithm, and
second is after creating the algorithm. The following are the two analysis of an algorithm:

o Priori Analysis: Here, priori analysis is the theoretical analysis of an algorithm which
is done before implementing the algorithm. Various factors can be considered before
implementing the algorithm like processor speed, which has no effect on the
implementation part.
o Posterior Analysis: Here, posterior analysis is a practical analysis of an algorithm.
The practical analysis is achieved by implementing the algorithm using any
programming language. This analysis basically evaluate that how much running time
and space taken by the algorithm.

Algorithm Complexity

The performance of the algorithm can be measured in two factors:

o Time complexity: The time complexity of an algorithm is the amount of time


required to complete the execution. The time complexity of an algorithm is denoted
by the big O notation. Here, big O notation is the asymptotic notation to represent the
time complexity. The time complexity is mainly calculated by counting the number of
steps to finish the execution. Let's understand the time complexity through an
example.
sum=0;
// Suppose we have to calculate the sum of n numbers.
for i=1 to n
sum=sum+i;
// when the loop ends then sum holds the sum of the n numbers
return sum;

In the above code, the time complexity of the loop statement will be atleast n, andif the
value of n increases, then the time complexity also increases. While the complexity of the
code, i.e., return sum will be constant as its value is not dependent on the value of n and will
provide the result in one step only. We generally consider the worst-time complexity as it is
the maximum time taken for any given input size.

o Space complexity: An algorithm's space complexity is the amount of space required


to solve a problem and produce an output. Similar to the time complexity, space
complexityis also expressed in big O notation.

For an algorithm, the space is required for the following purposes:

1. To store program instructions


2. To store constant values
3. To store variable values
4. To track the function calls, jumping statements, etc.
Auxiliary space: The extra space required by the algorithm, excluding the input size, is
known as an auxiliary space. The space complexity considers both the spaces, i.e., auxiliary
space, and space used by the input.So,

Space complexity = Auxiliary space + Input size.

Types of Algorithms

The following are the types of algorithm:

o Search Algorithm
o Sort Algorithm

Search Algorithm

On each day, we search for something in our day to day life. Similarly, with the case of
computer, huge data is stored in a computer that whenever the user asks for any data then the
computer searches for that data in the memory and provides that data to the user. There are
mainly two techniques available to search the data in an array:
o Linear search
o Binary search

Linear Search

Linear search is a very simple algorithm that starts searching for an element or a value from
the beginning of an array until the required element is not found. It compares the element to
be searched with all the elements in an array, if the match is found, then it returns the index
of the element else it returns -1. This algorithm can be implemented on the unsorted list.

Binary Search

A Binary algorithm is the simplest algorithm that searches the element very quickly. It is
used to search the element from the sorted list. The elements must be stored in sequential
order or the sorted manner to implement the binary algorithm. Binary search cannot be
implemented if the elements are stored in a random manner. It is used to find the middle
element of the list.

Sorting Algorithms

Sorting algorithms are used to rearrange the elements in an array or a given data structure
either in an ascending or descending order. The comparison operator decides the new order
of the elements.

Why do we need a sorting algorithm?


o An efficient sorting algorithm is required for optimizing the efficiency of other
algorithms like binary search algorithm as a binary search algorithm requires an array
to be sorted in a particular order, mainly in ascending order.
o It produces information in a sorted order, which is a human-readable format.
o Searching a particular element in a sorted list is faster than the unsorted list.

UNIT II HIERARCHICAL DATA STRUCTURES

Binary Search Trees: Basics – Querying a Binary search tree – Insertion and Deletion- Red Black
trees: Properties of Red-Black Trees – Rotations – Insertion – Deletion -B-Trees: Definition of B -
trees – Basic operations on B-Trees – Deleting a key from a B-Tree- Heap – Heap Implementation –
Disjoint Sets - Fibonacci Heaps: structure – Mergeable-heap operationsDecreasing a key and deleting
a node-Bounding the maximum degree.

1. Define tree.
A tree is a finite set of one or more nodes such that there is a specially designated node called the root.
The remaining nodes are partitioned into n>=0 disjoint sets T1, T2, …, Tn, where each of these sets
is a tree. T1,
…,Tn are called the subtrees of the root.
2. Define the following terms: node, leaf node, ancestors, siblings of a node
Node: Eachelement of a binary tree is called node of a tree. Each node may be a root of a tree with
zero or more sub trees.
Leaf node: A node with no children (successor) is called leaf node or terminal node.
Ancestor: Node n1 is an ancestor of node n2 if n1 is either a father of n2 or father of some
ancestor of n2.
Siblings: Two nodes are siblings if they are the children of the same parent.
3. Define level of a node, degree of a node, degree of a tree, height and depth of a tree. Level of a
node: The root node is at level 1. If a node is at level l, then its children are at level i+1.
Degree of a node: The number of sub trees of a node is called as degree of a node.The degree of a
tree is the maximum of the degree ofthe nodes in the tree.
The height or depth of a tree is defined to be the maximum level of any nodein the tree.
4. What are the ways to represent Binary trees in memory?
1. Array representation (or) Sequential Representation.
2. Linked List representation (or) Node representation.
5. Define binary tree.
Binary tree is a finite set of elements that is either empty or is partitionedinto three disjoint subsets.
The first subset contains the single element called the root of tree. The other two subsets are themselves
binary tree called the left and right sub tree of original tree. In other words, a binary tree is a treein
which each node can have a maximum of
two children.
6. Define Full binary tree (or) Complete binary tree
A full binary tree of depth k is a binary tree of depth k having 2k – 1 nodes. In other words, all the levels
in the binary tree should contain the maximum number of nodes.
7. Define strictly binary tree
If every non leaf node in a binary tree has non empty left and right sub trees thenthe tree is termed as strictly
binary tree. In other words, a strictly binary tree contains leaf nodes and non leaf nodes of degree
8. Define expression tree
An expression tree is built up from the simple operands and operators of an(arithmetic or logical)
expression by placing the simple operands as the leaves of a binary tree and the operators as the interior
nodes.

9. Traverse the given tree using Inorder, Preorder and Postorder traversals.

Given tree:
A

B C

D E F G

 Inorder : DHH B E A F C I G J I J
 Preorder: ABD HE C FGIJ
 Postorder: HDEBFIJGCA

10. How many null branches can a binary tree have with 20 node?
21 null branches
Let us take a tree with 5 nodes (n=5)

Null Branches

It will have only 6 (ie,5+1) null branches. In general, a binary tree withn nodes hasexactly n+ 1 null
node. Thus a binary tree with 20 nodes will have 21 null branches.
11. What is a binary search tree?
A binary search tree is a binary tree. It may be empty. If it is not empty then, it satisfies the
following properties.
1. Every element has a key & the keys are distinct.
2. The keys in the left sub tree is smaller than the key in the root.
3. Keys in the right sub tree is larger than the key in the root.
4. The left & right sub trees are also BST.
12. How will you construct binary search tree?
a. Make the first node as the root node.
b. To insert the next node into the BST, search for the value in the BST. Ifthe value isfound in the
BST, then a duplicate value cannot be inserted into the BST.
c. If the element is not found, add the element at the point where the search
becomesunsuccessful.

13. Define the term skewed tree?


In skewed tree all the nodes are skewed in one direction either left or right. Left Skewed
Tree: A tree in which all nodes are skewed in left direction. Right Skewed Tree: A tree
in which allnodes are skewed in right direction.
14. What is the maximum number of nodes in level i of a binary tree and what is themaximum number
of nodes in a binary tree of depth k?
The maximum number of nodes in level i of a binary tree = 2i-1
The maximum number of nodes in a binary tree of depth k = 2k-1, where k>
15. What are the non-linear data structures?
Non-Linear Data Structure- A data structure which represents a hierarchical arrangement
of elements. Examples: Graphs and trees.
16. Define balanced search tree.
Balanced search tree have the structure of binary tree and obey binary search tree properties with that it
always maintains the height as O(log n) by means of a special kind of rotations. Eg. AVL, Splay, B-tree.
17. What are the drawbacks of AVL trees?
The drawbacks of AVL trees are
 Frequent rotations
 The need to maintain balances for the tree’s nodes
 Overall complexity, especially of the deletion operation.
18. Define B-tree?
A B-tree of order m in an m-way search tree that is either empty or is of height
≥1 and
1. The root node has at least 2 children
2. All nodes other than the root node and failure nodes have at least m/2children.
3. All failure nodes are at same level.
19. Explain AVL rotation.
Manipulation of tree pointers is centered at the pivot node to bring the tree back into height balance. The visual
effect of this pointer manipulation so to rotatethe sub tree whose root is the pivot node. This operation is referred
as AVL rotation.
20. What are the different types of Rotation in AVL Tree?
Two types of rotation are
1. single rotation
2. double rotation.
21. Explain Hashing.
Hashing is a technique used to identify the location of an identifier ‘x’ inthe memory bysome
arithmetic functions like f(x), which gives address of ‘x’ in the table.
22. Explain Hash Function. Mention Different types of popular hash function. Hash Function takes an
identifier and computes the address of that identifier in the hash table.1.Division method
2.Square method
3.Foldingmethod
24..Define Splay Tree.
A splay tree is a self-adjusting binary search treewith the additional property that recently accessed
elements are quick to access again. It performs basic operations such as insertion,look-up and removal in
O(log n) amortized time.
25.Write short notes on Heap.
Heap is a special case of balanced binary tree data structure where the root-node key iscomparedwith its
children and arranged accordingly. If α has child node βthen −
key(α) ≥ key(β)

26.Define Binomial Heap.


A Binomial Heap is a collection of Binomial Trees A Binomial Tree of order 0 has 1 node. ABinomial Tree
of order k can be constructed by taking two binomial trees of order k-1, and making one as leftmost child
of other.
A Binomial Tree of order k has following properties.
a) It has exactly 2k nodes.
b) It has depth as k.
c) There are exactly kCi nodes at depth i for i = 0, 1, . . . , k.
d) The root has degree k and children of root are themselves Binomial Trees withorder k-1, k-2,.. 0 from
left to right.
26.Define Fibonacci Heaps.
Fibonacci heap is a data structure for priority queue operations, consisting of acollection
of heap-ordered trees. It has a better amortized running time than many other priority queuedatastructures
including the binary heap and binomialheap.
27.Write notes on Hash Set.
Implements Set Interface.
Underlying data structure for HashSet is hashtable.
As it implements the Set Interface, duplicate values are not allowed. Objects that you insert inHashSet are not
guaranteed to be inserted in sameorder.
Objects are inserted based on their hash code. NULL elements areallowed in HashSet.
HashSet also implements Searlizable and Cloneable interfaces.
28. Define tree.
Trees are non-liner data structure, which is used to store data items in a shorted sequence.It represents any
hierarchical relationship between any data Item. It is a collection of nodes,which has a distinguish node called
the root and zero or more non-empty sub trees T1,T2,….Tk. each of which are connected by a directed
edgefrom the root.
29. Define Height of tree?
The height of n is the length of the longest path from root to a leaf. Thus all leaveshave height zero. The
height of a tree is equal to a height of a root.
30. Define Depth of tree?
For any node n, the depth of n is the length of the unique path from the root to noden.Thus for aroot the depth is
always zero.
31. What is the length of the path in a tree?
The length of the path is the number of edges on the path. In a tree there is exactlyone path form theroot to each node.
32. Define sibling?
Nodes with the same parent are called siblings. The nodes with common parents are calledsiblings.
33. Define binary tree?
A Binary tree is a finite set of data items which is either empty or consists of a singleitem calledroot and two
disjoin binary trees called left sub tree max degree of any node is two.
34. What are the two methods of binary tree implementation?
Two methods to implement a binary tree are,
a. Linear representation.
b. Linked representation
35. What are the applications of binary tree?
Binary tree is used in data processing.
a. File index schemes
b. Hierarchical database management system
36.
36.List out few of the Application of tree data-structure?
Ø The manipulation of Arithmetic expression Ø Usedfor Searching
Operation
Ø Used to implement the file system of several popular operating systems Ø SymbolTable construction
Ø Syntax analysis
37. Define expression tree?
Expression tree is also a binary tree in which the leafs terminal nodes or operands and non-terminal
intermediate nodes are operators used for traversal.
38. Define tree– traversal and mention the type of traversals?
Visiting of each and every node in the tree exactly is called as tree traversal.Three types oftree traversal
1. Inorder traversal
2. Preoder traversal
3. Postorder traversal.
41. Define in -order traversal?
In-order traversal entails the following steps;
a. Traverse the left subtree
b. Visit the root node
c. Traverse the right subtree
42. Define threaded binary tree.
A binary tree is threaded by making all right child pointers that would normally be null point to the in order
successor of the node, and all left child pointers that wouldnormally be null point to the in order predecessor of
the node.
43. What are the types of threaded binary tree?
i. Right-in threaded binary tree
ii. Left-in threaded binary tree
iii. Fully-in threaded binary tree
44. Define Binary Search Tree.
Binary search tree is a binary tree in which for every node X in the tree, the values ofall the keys in its left subtree
are smaller than the key value in X and the values of allthe keys in its right subtree are larger than the key value in
X.
45. What is AVL Tree?
AVL stands for Adelson-Velskii and Landis. An AVL tree is a binary search treewhich has the following
properties:The sub-trees of every node differ in height by at most one.Every sub-tree is an AVL tree.
Search time is O(logn). Addition and deletion operations also take O(logn) time.
46. List out the steps involved in deleting a node from a binary search tree.
▪ Deleting a node is a leaf node (ie) No children
▪ Deleting a node with one child.
▪ Deleting a node with two Childs.
47. What is ‘B’ Tree?
A B-tree is a tree data structure that keeps data sorted and allows searches, insertions,and deletions in logarithmic
amortized time. Unlike self-balancing binary search trees, it is optimized for systems that read and write large
blocks of data. It is most commonly used in database and file systems.
48. Define complete binary tree.
If all its levels, possible except the last, have maximum number of nodes and if allthe nodes in the last level
appear as far left as possible

What is Tree Data Structure?

We read the linear data structures like an array, linked list, stack and queue in which
all the elements are arranged in a sequential manner. The different data structures are
used for different kinds of data.

Some factors are considered for choosing the data structure:

What type of data needs to be stored?: It might be a possibility that a certain data
structure can be the best fit for some kind of data.
Cost of operations: If we want to minimize the cost for the operations for the most
frequently performed operations. For example, we have a simple list on which we
have to perform the search operation; then, we can create an array in which elements
are stored in sorted order to perform the binary search. The binary search works
very fast for the simple list as it divides the search space into half.
Memory usage: Sometimes, we want a data structure that utilizes less memory.

A tree is also one of the data structures that represent hierarchical data.
Suppose we want to show the employees and their positions in the
hierarchical form thenit can be represented as shown below:3

Let's understand some key points of the Tree data structure.

o A tree data structure is defined as a collection of objects or entities


known as nodes that are linked together to represent or simulate
hierarchy.
o A tree data structure is a non-linear data structure because it does not
store in a sequential manner. It is a hierarchical structure as elements
in aTree are arranged in multiple levels.
o In the Tree data structure, the topmost node is known as a root node.
Each node contains some data, and data can be of any type. In the
above tree structure, the node contains the name of the employee, so
the type of data would be a string.
o Each node contains some data and the link or reference of other nodes that can be
called children.

Some basic terms used in Tree data structure.

Let's consider the tree structure, which is shown below:

In the above structure, each node is labeled with some number. Each arrowshown in the
above figure is known as a link between the two nodes.

o Root: The root node is the topmost node in the tree hierarchy. In other words, the
root node is the one that doesn't have any parent. In the above structure, node
numbered 1 is the root node of the tree. If a node is directly linked to some other
node, it would be called a parent-child relationship.
o Child node: If the node is a descendant of any node, then the node is known as a
child node.
o Parent: If the node contains any sub-node, then that node is said to be the parent of
thatsub-node.
o Sibling: The nodes that have the same parent are known as siblings.
o Leaf Node:- The node of the tree, which doesn't have any child node, is called a
leaf node. A leaf node is the bottom-most node of the tree. There
can be any number of leaf nodes present in a general tree. Leaf nodes can also be
calledexternal nodes.
o Internal nodes: A node has atleast one child node known as an internal
o Ancestor node:- An ancestor of a node is any predecessor node on a path from the
root to that node. The root node doesn't have any ancestors. In thetree shown in the
above image, nodes 1, 2, and 5 are the ancestors of node 10.
o Descendant: The immediate successor of the given node is known as a descendant
of anode. In the above figure, 10 is the descendant of node 5.

Properties of Tree data structure

o Recursive data structure: The tree is also known as a recursive data structure. A
tree can be defined as recursively because the distinguished node in a tree data
structure is known as a root node. The root node of the tree contains a link to all the
roots of its subtrees. The left subtree is shown in the yellow color in the below
figure, and the right subtree is shown inthe red color. The left subtree can be
further split into subtrees shown in three different colors. Recursion means
o Number of edges: If there are n nodes, then there would n-1 edges. Each arrow in
the structure represents the link or path. Each node, except the root node, will
have atleast one incoming link known as an edge. There would be one link for the
parent- child relationship.
o Depth of node x: The depth of node x can be defined as the length of the path from
the root to the node x. One edge contributes one-unit length in the path. So, the
depth of node x can also be defined as the number of edges between the root
node and the node x. The root node has 0 depth.
o Height of node x: The height of node x can be defined as the longest path from the
node x to the leaf node.

Based on the properties of the Tree data structure, trees are classified into various categories.
Implementation of Tree

The tree data structure can be created by creating the nodes dynamically with the help of
the pointers. The tree in the memory can be represented as shown below:
The above figure shows the representation of the tree data structure in the memory. In the
above structure, the node contains three fields. The second field stores the data; the first
field stores the address of the left child, and the third field stores the address of the right
child.

In programming, the structure of a node can be defined as:struct node


{
int data;
struct node
*left;struct
node *right;
}

The above structure can only be defined for the binary trees because the binary tree can
have utmost two children, and generic trees can have more than two children. The
structure of the node for generic trees would be different as compared to the binary tree.

Applications of trees

The following are the applications of trees:


o Storing naturally hierarchical data: Trees are used to store the data in the
hierarchical structure. For example, the file system. The file system stored on the
disc drive, the file and folder are in the form of the naturally hierarchical data and
stored in the form of trees.
o Organize data: It is used to organize data for efficient insertion, deletion and
searching. For example, a binary tree has a logN time for searching an element.
o Trie: It is a special kind of tree that is used to store the dictionary. It is a fast and
efficient way for dynamic spell checking.
o Heap: It is also a tree data structure implemented using arrays. It is used
toimplement priority queues.
o B-Tree and B+Tree: B-Tree and B+Tree are the tree data structures used to
implement indexing in databases.
o Routing table: The tree data structure is also used to store the data in routing
tables in the routers.

2.Expalin different types of tree data structure.

The following are the types of a tree data structure:General tree: The general tree is one of the types of
tree data structure. In the general tree, a node can have either 0 or maximum n number of nodes. There is
no restriction imposed on the degree of the node (the number of nodes that a node can contain). The
topmost node in a general tree is known as a root node. The children of the parent node are
knownas subtrees.
There can be n number of subtrees in a general tree. In the general tree, the subtrees
are unordered as the nodes in the subtree cannot be ordered. Every non-empty tree
has a downward edge, and these edges are connected to the nodes known as child
nodes. The root node is labeled with level 0. The nodes that have the same parent
are known as siblings.
o Binary tree

Here, binary name itself suggests two numbers, i.e., 0 and 1. In a binary tree, each
node in a tree can have utmost two child nodes. Here, utmost means whether the
node has 0 nodes, 1 node or 2 nodes.

To know more about the binary tree, click on the link given below:
https://www.javatpoint.com/binary-tree

Binary Search tree

Binary search tree is a non-linear data structure in which one node is connected to n number of
nodes. It is a node-based data structure. A node can be represented in a binary search tree with
three fields, i.e., data part, left-child, and right-child. A node can be connected to the utmost two
child nodes in a binary search tree, so the node contains two pointers (left child and right
child
pointer).Every node in the left sub tree must contain a value
less than the value of the root node, and the value of each node in the right sub tree
must be bigger than the value of the root node.
A node can be created with the help of a user-defined data type knownas struct, as
shown below:

struct node
{
int data;
struct node
*left;struct
node *right;
}

The above is the node structure with three fields: data field, the second field is the left
pointer of the node type, and the third field is the right pointer of the node type.

What are the Types of Binary Tree?

To know more about the binary search tree, click on the link given below:

o AVL tree

It is one of the types of the binary tree, or we can say that it is a variant of the binary
search tree. AVL tree satisfies the property of the binary tree as well as of the binary
search tree. It is a self-balancing binary search tree that was invented by Adelson Velsky
Lindas. Here, self- balancing means that balancing the heights of left subtree and right
subtree. This balancing is measured in terms of the balancing factor.

We can consider a tree as an AVL tree if the tree obeys the binary search tree as well as a
balancing factor. The balancing factor can be defined as the difference between the height
of the left subtree and the height of the right subtree. The balancing factor's value must be
either 0, -1, or 1; therefore, each node in the AVL tree should have the value of the
balancing factor either as 0, -1, or 1.

To know more about the AVL tree, click on the link given below:

o Red-Black Tree

The red-Black tree is the binary search tree. The prerequisite of the Red-Black tree is that
we should know about the binary search tree. In a binary search tree, the value of the left-
subtree should be less than the value of that node, and the value of the right-subtree should
be greater than the value of that node. As we know that the time complexity of biary
search in the average case is log 2n, the best case is O(1), and the worst case is O(n).

When any operation is performed on the tree, we want our tree to be balanced so that all
the operations like searching, insertion, deletion, etc., take less time, and all these
operations will have the time complexity of log2n.

The red-black tree is a self-balancing binary search tree. AVL tree is also a height
balancing binary search tree then why do we require a Red-Black tree.In the AVL tree,
we do not know how many rotations would be required to balance the tree, but in the Red-
black tree, a maximum of 2 rotations are required to balance the tree. It contains one extra
bit that representseither the red or black color of a node to ensure the balancing of the tree.

o Splay tree

The splay tree data structure is also binary search tree in which recently accessed element
is placed at the root position of tree by performing some rotation operations. Here,
splaying means the recently accessed node. It is a self- balancing binary search tree
having no explicit balance condition like AVL tree.

It might be a possibility that height of the splay tree is not balanced, i.e., height ofboth left
and right subtrees may differ, but the operations in splay tree takes order of logN time
where n is the number of nodes.

Splay tree is a balanced tree but it cannot be considered as a height balanced tree because
aftereach operation, rotation is performed which leads to a balanced tree.

o Treap

Treap data structure came from the Tree and Heap data structure. So, it comprises the
properties of both Tree and Heap data structures. In Binary search tree, each node on the
left subtree must be equal or less than the value of the root node and each node on the right
subtree must be equal or greater than the value of the root node. In heap data structure,
both right and left subtrees contain larger keys than the root; therefore, we can say that the
root node contains thelowest value.

In treap data structure, each node has both key and priority where key is derived from the
Binary search tree and priority is derived from the heap data structure.
The Treap data structure follows two properties which are given below:

o Right child of a node>=current node and left child of a node <=current node
(binarytree)
o Children of any subtree must be greater than the node (heap)

B-tree

B-tree is a balanced m-way tree where m defines the order of the tree. Till now, we
read that the node contains only one key but b-tree can have more than one key,
and more than 2 children. It always maintains the sorted data. In binary tree, it is
possible that leaf nodes can be at different levels, but in b-tree, all the leaf nodes
must be at the same level.

If order is m then node has the following properties:

o Each node in a b-tree can have maximum m children


o For minimum children, a leaf node has 0 children, root node has minimum 2
childrenand internal node has minimum ceiling of m/2 children. For example, the
value of m is
5 which means that a node can have 5 children and internal nodes can contain
maximum 3 children.
o Each node has maximum (m-1) keys.

The root node must contain minimum 1 key and all other nodes must containatleast
ceilingof m/2 minus 1 keys.
Binary Tree

The Binary tree means that the node can have maximum two children. Here, binary name
itselfsuggests that 'two'; therefore, each node can have either 0, 1 or 2 children.

Properties of Binary Tree

o At each level of i, the maximum number of nodes is 2i.


o The height of the tree is defined as the longest path from the root node to the
leafnode. The tree which is shown above has a height equal to 3.
Therefore, the maximum number of nodes at height 3 is equal to (1+2+4+8) = 15.
In general, the maximum number of nodes possible at height h is (2 0 + 21 +
22+….2h) = 2h+1 -1.
o The minimum number of nodes possible at height h is equal to h+1.
o If the number of nodes is minimum, then the height of the tree would be maximum.
Conversely, if the number of nodes is maximum, then theheight of the tree would
be minimum.

If there are 'n' number of nodes in the binary tree.

What is complete binary tree?

The minimum height can be computed as:

As we know that, n

= 2h+1 -1

n+1 =

2h+1

Taking log on both the sides,

log2(n+1) = log2(2h+1 )

log2(n+1)

= h+1

h = log2(n+1) - 1

The maximum height can be computed as:

As we know that, n

= h+1
h= n-1

Types of Binary Tree

4.Explain different types of Binary tree with an example.

o Full/ proper/ strict Binary tree


o Complete Binary tree
o Perfect Binary tree
o Degenerate Binary tree
o Balanced Binary tree

1. Full/ proper/ strict Binary tree


The full binary tree is also known as a strict binary tree. The tree can only be considered as
the full binary tree if each node must contain either 0 or 2 children. The full binary tree can
also be defined as the tree in which each node must contain 2 children except the leaf
nodes.

Let's look at the simple example of the Full Binary tree.

In the above tree, we can observe that each node is either containing zero or two children;
therefore, it is a Full Binary tree.

Properties of Full Binary Tree

o The number of leaf nodes is equal to the number of internal nodes plus 1.In the
above example, the number of internal nodes is 5; therefore, the number of leaf
nodes is equal to 6.
o The maximum number of nodes is the same as the number of nodes in the binary
tree, i.e., 2h+1 -1.
o The minimum number of nodes in the full binary tree is 2*h-1.
o The minimum height of the full binary tree is log2(n+1) - 1.
o The maximum height of the full binary tree can be computed as:

o n= 2*h - 1
n+1 = 2*h

h = n+1/2

Complete Binary

Tree

The complete binary tree is a tree in which all the nodes are completely filled except the
last level. In the last level, all the nodes must be as left as possible. Ina complete binary
tree, the nodes should be added from the left.

Let's create a complete binary tree.

The above tree is a complete binary tree because all the nodes are completely filled, and all
thenodes in the last level are added at the left first.

Properties of Complete Binary Tree

o The maximum number of nodes in complete binary tree is 2h+1 - 1.


o The minimum number of nodes in complete binary tree is 2h.
o The minimum height of a complete binary tree is log2(n+1) - 1.
o The maximum height of a complete binary tree is

Perfect Binary Tree

A tree is a perfect binary tree if all the internal nodes have 2 children, and all the leaf nodes
areat the same level.
Let's look at a simple example of a perfect binary tree.

The below tree is not a perfect binary tree because all the leaf nodes are not at thesame level.

Degenerate Binary Tree

The degenerate binary tree is a tree in which all the internal nodes have only one children.

Let's understand the Degenerate binary tree through examples.

The above tree is a degenerate binary tree because all the nodes have only one child. It is
alsoknown as a right-skewed tree as all the nodes have a right child only.
The above tree is also a degenerate binary tree because all the nodes have only one child. It
isalso known as a left-skewed tree as all the nodes have a left child only.

Balanced Binary Tree

The balanced binary tree is a tree in which both the left and right trees differ byatmost 1.
Forexample, AVL and Red-Black trees are balanced binary tree.

Let's understand the balanced binary tree through examples.

The above tree is a balanced binary tree because the difference between the left subtree
andright subtree is zero.

The above tree is not a balanced binary tree because the difference between the left subtree
andthe right subtree is greater than 1.

Binary Search tree

In this article, we will discuss the Binary search tree. This article will be very helpful
and informative to the students with technical background as it is an important topic of their
course.
Before moving directly to the binary search tree, let's first see a brief description of the tree.

What is a tree?

A tree is a kind of data structure that is used to represent the data in hierarchical form. It
can be defined as a collection of objects or entities called as nodes that are linked together
to simulate a hierarchy. Tree is a non-linear data structure as the data in a tree is not stored
linearly or sequentially.
What is a Binary Search tree?

A binary search tree follows some order to arrange the elements. In a Binary search tree,
the value of left node must be smaller than the parent node, and the value of right node
must be greater than the parent node. This rule is applied recursively to the left and right
subtrees of theroot.

Let's understand the concept of Binary search tree with an example.

In the above figure, we can observe that the root node is 40, and all the nodes of the left
subtreeare smaller than the root node, and all the nodes of the right subtree are greater than
the root node.

Similarly, we can see the left child of root node is greater than its left child and smaller
than its right child. So, it also satisfies the property of binary search tree. Therefore, we can
say that thetree in the above image is a binary search tree.

Suppose if we change the value of node 35 to 55 in the above tree, check whether the tree
will be binary search tree or not.

In the above tree, the value of root node is 40, which is greater than its left child 30 but
smaller than right child of 30, i.e., 55. So, the above tree does not satisfy the property of
Binary searchtree. Therefore, the above tree is not a binary searchtree.

Advantages of Binary search tree


o Searching an element in the Binary search tree is easy as we always have a hint
that which subtree has the desired element.
o As compared to array and linked lists, insertion and deletion operations are faster in
BST.

Write a C++ Program for the binary tree traversals and creation of binary
searchtree given an example.

45, 15, 79, 90, 10, 55, 12, 20, 50

Example of creating a binary search tree

Now, let's see the creation of binary search tree using an example.

Suppose the data elements are - 45, 15, 79, 90, 10, 55, 12, 20, 50

o First, we have to insert 45 into the tree as the root of the tree.
o Then, read the next element; if it is smaller than the root node, insert it asthe
root ofthe left subtree, and move to the next element.
o Otherwise, if the element is larger than the root node, then insert it as theroot of
theright subtree.

Now, let's see the process of creating the Binary search tree using the given dataelement.
Theprocess of creating the BST is shown below -

Binary Tree Implementation

A Binary tree is implemented with the help of pointers. The first node in the tree is
represented by the root pointer. Each node in the tree consists of three parts, i.e., data, left
pointer and right pointer. To create a binary tree, we first need to create the node. We will
create the node of user-defined as shown below:

struct node
{
int data,
struct node *left, *right;
}

In the above structure, data is the value, left pointer contains the address ofthe left
node, and right pointer contains the address of the right node.

Binary Tree program in C

#include<stdio.h>
struct node
{
int data;
struct node *left, *right;
}
void main()
{
struct node
*root;root =
create();
}
struct node *create()
{
struct node *temp;
int data;
temp = (struct node *)malloc(sizeof(struct node));
printf("Press 0 to exit");
printf("\nPress 1 for new node");
printf("Enter your choice : ");
scanf("%d", &choice);
if(choice==0)
{
return 0;
}
else
{
printf("Enter the
data:");
scanf("%d", &data); temp-
>data = data;
printf("Enter the left child of %d", data); temp-
>left = create();
printf("Enter the right child of %d", data); temp-
>right = create();
return temp;
}
}

The above code is calling the create() function recursively and creating new node on
each recursive call. When all the nodes are created, then it forms a binary tree
structure. The process of visiting the nodes is known as treetraversal. There are three
types traversals usedto visit a node:

o Inorder traversal
o Preorder traversal
o Postorder traversal

Inorder Traversal

Algorithm

Until all nodes are traversed −


Step 1 − Recursively traverse left subtree.
Step 2 − Visit root node.
Step 3 − Recursively traverse right subtree.
Preorder traversal

Algorithm

Until all nodes are traversed −


Step 1 − Visit root node.
Step 2 − Recursively traverse left subtree.
Step 3 − Recursively traverse right subtree.

Postorder traversal

Algorithm

Until all nodes are traversed −


Step 1 − Recursively traverse left subtree. Step 2
− Recursively traverse right subtree.Step 3 −
Visit root node.

void preorder(struct st *temp)

if(temp!=NULL)

cout<<" "<<temp->data;

preorder(temp->left);

preorder(temp->right);

}
void inorder(struct st *temp)
{

if(temp!=NULL)

inorder(temp->left);

cout<<" "<<temp->data;

inorder(temp->right);

void postorder(struct st *temp)

if(temp!=NULL)

postorder(temp->left);

postorder(temp-

>right); cout<<"

"<<temp->data;

Searching in Binary search tree

Searching means to find or locate a specific element or node in a data structure. In Binary
search tree, searching a node is easy because elements in BST are stored in a specific
order. The steps of searching a node in Binary Search tree are listed as follows -

1. First, compare the element to be searched with the root element of the tree.
2. If root is matched with the target element, then return the node's location.
3. If it is not matched, then check whether the item is less than the
rootelement, ifit is smaller than the root element, then move to the left subtree.
4. If it is larger than the root element, then move to the right subtree.
5. Repeat the above procedure recursively until the match is found.
6. If the element is not found or not present in the tree, then return NULL.

Now, let's understand the searching in binary tree using an example. We are taking the
binary search tree formed above. Suppose we have to find node 20 from the below tree.

Step1:

Step2:

Step3:
Now, let's see the algorithm to search an element in the Binary search tree.

Algorithm to search an element in Binary search tree

Search (root, item)


if (item = root → data) or (root =
NULL)returnroot
else if (item < root → data)
return Search(root → left, item)else
return Search(root → right, item)END
if
EN
D
Now let's understand how the deletion is performed on a binary search tree. We will also
see an example to delete an element from the given tree.

Deletion in Binary Search tree

In a binary search tree, we must delete a node from the tree by keeping in mind that the
property of BST is not violated. To delete a node from BST, there are three possible
situations occur -

o The node to be deleted is the leaf node, or,


o The node to be deleted has only one child, and,
o The node to be deleted has two children

We will understand the situations listed above in detail.

When the node to be deleted is the leaf node

It is the simplest case to delete a node in BST. Here, we have to replace the leaf node with
NULL and simply free the allocated space.

We can see the process to delete a leaf node from BST in the below image. In below
image, suppose we have to delete node 90, as the node to be deleted is a leaf node, so it
will be replaced with NULL, and the allocated space will free.

When the node to be deleted has only one child

In this case, we have to replace the target node with its child, and then delete the child
node. It means that after replacing the target node with its child node, the child node will
now contain the value to be deleted. So, we simply have to replace the child node with
NULL and free up the allocated space.
We can see the process of deleting a node with one child from BST in the below image. In
the below image, suppose we have to delete the node 79, as the node to be deleted has only
one child, so it will be replaced with its child 55.

So, the replaced node 79 will now be a leaf node that can be easily deleted.

When the node to be deleted has two children

This case of deleting a node in BST is a bit complex among other two cases. In such a case,
thesteps to be followed are listed as follows -

o First, find the inorder successor of the node to be deleted.


o After that, replace that node with the inorder successor until the target node is
placed at the leaf of tree.
o And at last, replace the node with NULL and free up the allocated space.

The inorder successor is required when the right child of the node is not empty. We can
obtain the inorder successor by finding the minimum element in the right child of the node.

We can see the process of deleting a node with two children from BST in the below image.
In the below image, suppose we have to delete node 45 that is the root node, as the node to
be deleted has two children, so it will be replaced with its inorder successor. Now, node 45
will beat the leaf of the tree so that it can be deleted easily.

Now let's understand how insertion is performed on a binary search tree.

Insertion in Binary Search tree

A new key in BST is always inserted at the leaf. To insert an element in BST, we have to
start searching from the root node; if the node to be inserted is less than the root node, then
search for an empty location in the left subtree. Else, search for the empty location in the
right subtree and insert the data. Insert in BST is similar to searching, as we always have to
maintain the rule that the left subtree issmaller than the root, and right subtree is larger than
the root.

Now, let's see the process of inserting a node into BST using an example.
The complexity of the Binary Search tree

Let's see the time and space complexity of the Binary search tree. We will see the time
complexity for insertion, deletion, and searching operations in best case, average case, and
worst case.

1. Time Complexity

Operations Best case Average case Worst


time casetime
Time complexity complexity
complexity

Insertion O(log n) O(log n) O(n)

Deletion O(log n) O(log n) O(n)

Search O(log n) O(log n) O(n)

Where 'n' is the number of nodes in the given tree.


2. Space Complexity

Operations Space complexity

Insertion O(n)

Deletion O(n)
Search O(n)

o The space complexity of all operations of Binary search tree is O(n).

3.To Construct an AVL tree having the following


elements

H, I, J, B, A, E, C, F, D, G, K, L

AVL Tree

AVL Tree is invented by GM Adelson - Velsky and EM Landis in 1962. The tree is named
AVL in honour of its inventors.

AVL Tree can be defined as height balanced binary search tree in which each node is
associated with a balance factor which is calculated by subtracting the height of its right
sub- tree from that of its left sub-tree.

Tree is said to be balanced if balance factor of each node is in between -1 to 1, otherwise,


the tree will be unbalanced and need to be balanced.

Balance Factor (k) = height (left(k)) - height (right(k))

If balance factor of any node is 1, it means that the left sub-tree is one levelhigher
than the right sub-tree.

If balance factor of any node is 0, it means that the left sub-tree and right sub-treecontain
equal height.

If balance factor of any node is -1, it means that the left sub-tree is one level lower than the
right sub-tree.

An AVL tree is given in the following figure. We can see that, balance factor associated
with each node is in between -1 and +1. therefore, it is an example of AVL tree.
Complexity

Algorithm Average case Worst case

Space o(n) o(n)

Search o(log n) o(log n)

Insert o(log n) o(log n)

Delete o(log n) o(log n)

Operations on AVL tree

Due to the fact that, AVL tree is also a binary search tree therefore, all the operations are
performed in the same way as they are performed in a binarysearch tree. Searching and
traversing do not lead to the violation in property of AVL tree.

SN Operation Description

1 Insertion Insertion in AVL tree is performed in the same way as it isperformed in a


binary search tree. However, it may lead to violation in the AVL tree
property and therefore the tree may need balancing. The tree can be
balanced by applying rotations.
2 Deletion Deletion can also be performed in the same way as it is performedin a
binary search tree. Deletion may also disturb the balance of the tree
therefore, various types of rotations are used to rebalance the tree.

Why AVL Tree?

AVL tree controls the height of the binary search tree by not letting it to be skewed. The
time taken for all operations in a binary search tree of height his O(h). However, it can
be extended to O(n) if the BST becomes skewed (i.e. worst case). By limiting this height to
log n, AVL tree imposes an upper bound on each operation to be O(log n) where n is the
number of nodes.

AVL Rotations

We perform rotation in AVL tree only in case if Balance Factor is other than -1, 0, and 1.
There are basically four types of rotations which are as follows:

1. L L rotation: Inserted node is in the left subtree of left subtree of A


2. R R rotation : Inserted node is in the right subtree of right subtree of A
3. L R rotation : Inserted node is in the right subtree of left subtree of A
4. R L rotation : Inserted node is in the left subtree of right subtree of A Where
node A is the node whose balance Factor is other than -1, 0, 1.

The first two rotations LL and RR are single rotations and the next two rotations LR and
RL are double rotations. For a tree to be unbalanced, minimum height must be at least 2,
Let us understand each rotation
1. RR Rotation

When BST becomes unbalanced, due to a node is inserted into the right subtreeof the right
subtree of A, then we perform RR rotation, RR rotation is an anticlockwise rotation, which
is applied on the edge below a node having balancefactor -2
In above example, node A has balance factor -2 because a node C is inserted in the right
subtree of A right subtree. We perform the RR rotation on the edge below A.

2. LL Rotation
When BST becomes unbalanced, due to a node is inserted into the left subtree of the left
subtree of C, then we perform LL rotation, LL rotation is clockwise rotation, which is
applied on the edge below a node having balance factor 2.

In above example, node C has balance factor 2 because a node A is inserted in the left
subtree of C left subtree. We perform the LL rotation on the edge below A.
3. LR Rotation

Double rotations are bit tougher than single rotation which has already explained above.
LR rotation = RR rotation + LL rotation, i.e., first RR rotation is performed on subtree and
then LL rotation is performed on full tree, by full tree we mean the first node from the path
of inserted node whose balance factor is other than -1, 0, or 1.

Let us understand each and every step very clearly:

State Action

A node B has been inserted into the right subtree of A the leftsubtree
of C, because of which C has become an unbalanced node having balance
factor 2. This case is L R rotation where: Inserted node is in the right subtree
of left subtree of C
As LR rotation = RR + LL rotation, hence RR (anticlockwise) on subtree
rooted at A is performed first. By doing RR rotation, node A, has become the
left subtree of B.

After performing RR rotation, node C is still unbalanced, i.e., having


balance factor 2, as inserted node A is in the left of left of C

Now we perform LL clockwise rotation on full tree, i.e. on node C. node C


hasnow become the right subtree of node B, A is left subtreeof B

Balance factor of each node is now either -1, 0, or 1, i.e. BST isbalanced
now.

4. RL Rotation

As already discussed, that double rotations are bit tougher than single rotation which has
already explained above. R L rotation = LL rotation + RR rotation, i.e., first LL rotation is
performed on subtree and then RR rotation is performed onfull tree, by full tree we mean
the first node from the path of inserted node whose balance factor is other than -1, 0, or 1.

State Action
A node B has been inserted into the left subtree of C the right subtree of A,
because of which A has become an unbalanced node having balance factor -
2. This case is RL rotation where: Inserted node is in the left subtree of
right subtree of A

As RL rotation = LL rotation + RR rotation, hence, LL (clockwise) on


subtree rooted at C is performed first. By doing RR rotation, node C
has become the right subtree of B.

After performing LL rotation, node A is still unbalanced, i.e. having balance


factor -2, which is because of the right-subtree of the right- subtree node A.

Now we perform RR rotation (anticlockwise rotation) on full tree,


i.e. on node A. node C has now become the right subtree of node B,and
node Ahas become the left subtree of B.

Balance factor of each node is now either -1, 0, or 1, i.e., BST


isbalanced now.
5. Construct an AVL tree having the following elements

H, I, J, B, A, E, C, F, D, G, K, L

1. Insert H, I, J

On inserting the above elements, especially in the case of H, the BST becomes unbalanced
as the Balance Factor of H is -2. Since the BST is right-skewed, we will perform RR
Rotation on node H.

The resultant balance tree is:

2. Insert B, A

On inserting the above elements, especially in case of A, the BST becomes unbalanced as
the Balance Factor of H and I is 2, we consider the first node from the last inserted node
i.e. H. Since the BST from H is left-skewed, we will perform LL Rotation on node H.

The resultant balance tree is:

3. Insert E

On inserting E, BST becomes unbalanced as the Balance Factor of I is 2, since if we travel


from E to I we find that it is inserted in the left subtree of right subtree of I, we will
perform LR Rotation on node I. LR = RR + LL rotation

3 a) We first perform RR rotation on node B The

resultant tree after RR rotation is:


3b) We first perform LL rotation on the node I

The resultant balanced tree after LL rotation is:

4. Insert C, F, D

On inserting C, F, D, BST becomes unbalanced as the Balance Factor of B and H is -2,


since if we travel from D to B we find that it is inserted in the right subtree of left subtree
of B, we will perform RL Rotation on node I. RL = LL + RR rotation.

4a) We first perform LL rotation on node E

The resultant tree after LL rotation is:


4b) We then perform RR rotation on node B The

resultant balanced tree after RR rotation is:

5. Insert G

On inserting G, BST become unbalanced as the Balance Factor of H is 2, since if we travel


from G to H, we find that it is inserted in the left subtree of rightsubtree of H, we will
perform LR Rotation on node I. LR = RR + LL rotation.

5 a) We first perform RR rotation on node C The

resultant tree after RR rotation is:

5 b) We then perform LL rotation on node H The

resultant balanced tree after LL rotation is:

6. Insert K
On inserting K, BST becomes unbalanced as the Balance Factor of I is -2. Since the
BST isright-skewed from I to K, hence we will perform RR Rotation on the node I.

The resultant balanced tree after RR rotation is:

7. Insert L
On inserting the L tree is still balanced as the Balance Factor of each node is noweither, -1, 0,
+1.Hence the tree is a Balanced AVL tree

Red Black Tree

A Red Black Tree is a category of the self-balancing binary search tree. It wascreated in
1972 by Rudolf Bayer who termed them "symmetric binary B-trees."

A red-black tree is a Binary tree where a particular node has color as an extra attribute,
either red or black. By check the node colors on any simple path from the root to a leaf,
red-black trees secure that no such path is higher than twice as long as any other so that the
tree is generally balanced.

Properties of Red-Black Trees

A red-black tree must satisfy these properties:


1. The root is always black.
2. A nil is recognized to be black. This factor that every non-NIL node has two
children.
3. Black Children Rule: The children of any red node are black.
4. Black Height Rule: For particular node v, there exists an integer bh (v) such that
specific downward path from v to a nil has correctly bh (v) black real (i.e. non-nil)
nodes. Call this portion the black height of v. We determine the black height of an
RB tree to be the black height of its root.

A tree T is an almost red-black tree (ARB tree) if the root is red, but otherconditions
above hold.

Operations on RB Trees:

The search-tree operations TREE-INSERT and TREE-DELETE, when runs on a red-black


tree with n keys, take O (log n) time. Because they customize the tree, the conclusion may
violate the red-black properties. To restore these properties, we must change the color of
some of the nodes in the tree and also change the pointer structure.
1. Rotation:
Restructuring operations on red-black trees can generally be expressed more clearly in
details of the rotation operation.

Clearly, the order (Ax By C) is preserved by the rotation operation. Therefore, if we start
with a BST and only restructure using rotation, then we will still have a BST i.e. rotation
do not break the BST-Property.
LEFT ROTATE (T, x)
y ← right [x] y
← right [x]
right [x] ← left [y]
p [left[y]] ← x
p[y] ← p[x]
If p[x] = nil [T] then
root [T] ← y else if x
=left [p[x]] then
left [p[x]] ← y
else right [p[x]] ←
yleft [y] ← x. p [x] ←
y.
2. Insertion:
o Insert the new node the way it is done in Binary Search Trees.
o Color the node red
o If an inconsistency arises for the red-black tree, fix the tree according tothe
type of discrepancy.

A discrepancy can decision from a parent and a child both having a red color. This type of
discrepancy is determined by the location of the node concerning grandparent, and the
color ofthe sibling of the parent.

RB-INSERT (T, z)
y ← nil [T] x
← root [T]
while x ≠ NIL
[T]do y ← x
if key [z] < key [x]
then x ← left [x]
else x ← right [x] p
[z] ← y
if y = nil [T]
then root [T] ← z
else if key [z] < key [y]
then left [y] ← z
else right [y] ← z
left [z] ← nil [T]
right [z] ← nil [T]
color [z] ← RED
RB-INSERT-FIXUP (T, z)

After the insert new node, Coloring this new node into black may violate the black-height
conditions and coloring this new node into red may violate coloring conditions i.e. root is
black and red node has no red children. We know the black-height violations are hard. So
we color the node red. After this, if there is any color violation, then we have to correct
them by an RB- INSERT-FIXUP procedure.

RB-INSERT-FIXUP (T, z)
while color [p[z]] = RED
do if p [z] = left [p[p[z]]]
then y
← right [p[p[z]]]
If color [y] = RED
then color [p[z]] ← BLACK //Case 1 color
[y] ← BLACK //Case 1
color [p[z]] ← RED //Case
1z ← p[p[z]] //Case 1
else if z= right [p[z]]
then z ← p [z] //Case 2 LEFT-
ROTATE (T, z) //Case 2
color [p[z]] ← BLACK //Case 3
color [p [p[z]]] ← RED //Case 3
RIGHT-ROTATE (T,p [p[z]]) //Case 3
else (same as then clause)
With "right" and "left"
exchangedcolor [root[T]] ← BLACK

5. To Create the Red Black tree in the order of 41,38,31,12,19,8 and


deletion of thekeys in the order 8, 12, 19,31,38,41.

Example: Show the red-black trees that result after successively inserting the
keys41,38,31,12,19,8 into an initially empty red-black tree.

Solution:

Insert 41
Insert 19

Thus the final tree is

8. Deletion:
First, search for an element to be deleted

1. If the element to be deleted is in a node with only left child, swap this node with
one containing the largest element in the left subtree. (This node has no right child).
2. If the element to be deleted is in a node with only right child, swap this node with
the one containing the smallest element in the right subtree (Thisnode has no left
child).
3. If the element to be deleted is in a node with both a left child and a right child, then
swap in any of the above two ways. While swapping, swap only the keys but not the
colors.
4. The item to be deleted is now having only a left child or only a right child. Replace
thisnode with its sole child. This may violate red constraints or black constraint.
Violation of red constraints can be easily fixed.
5. If the deleted node is black, the black constraint is violated. The elimination of a
blacknode y causes any path that contained y to have one fewer black node.
6. Two cases arise:
• The replacing node is red, in which case we merely color it black to
make up forthe loss of one black node.
• The replacing node is black.

Example: In a previous example, we found that the red-black tree that results from
successively inserting the keys 41,38,31,12,19,8 into an initially empty tree. Now show the
red- black trees that result from the successful deletion of the keys in the order 8, 12,
19,31,38,41.
Delete 38

6. Define B tree and explain the inserting and deletion operation of B tree with an example.

B Tree

B Tree is a specialized m-way tree that can be widely used for disk access. A B- Tree of
order m can have at most m-1 keys and m children. One of the main reason of using B tree
is its capability to store large number of keys in a single node and large key values by
keeping the height of the tree relatively small.
A B tree of order m contains all the properties of an M way tree. In addition, it contains
the following properties.

1. Every node in a B-Tree contains at most m children.


2. Every node in a B-Tree except the root node and the leaf node contain atleast m/2
children.
3. The root nodes must have at least 2 nodes.
4. All leaf nodes must be at the same level.
It is not necessary that, all the nodes contain the same number of children but, each node
must have m/2 number of nodes.

A B tree of order 4 is shown in the following image.

While performing some operations on B Tree, any property of B Tree may violate such as
number of minimum children a node can have. To maintain the properties of B Tree, the
tree may split or join.

Operations

Searching :

Searching in B Trees is similar to that in Binary search tree. For example, if we search for
anitem 49 in the following B Tree. The process will something like following :

1. Compare item 49 with root node 78. since 49 < 78 hence, move to its leftsub-tree.
2. Since, 40<49<56, traverse right sub-tree of 40.
3. 49>45, move to right. Compare 49.
4. match found, return.
Searching in a B tree depends upon the height of the tree. The search algorithm takes O(log
n)time to search any element in a B tree.
Inserting

Insertions are done at the leaf node level. The following algorithm needs to be followed in
order to insert an item into B Tree.

1. Traverse the B Tree in order to find the appropriate leaf node at which thenode
can beinserted.
2. If the leaf node contain less than m-1 keys then insert the element in
theincreasingorder.
3. Else, if the leaf node contains m-1 keys, then follow the following steps.
o Insert the new element in the increasing order of elements.
o Split the node into the two nodes at the median.
o Push the median element upto its parent node.
o If the parent node also contain m-1 number of keys, then split it too by
following the same steps.

Deletion

Deletion is also performed at the leaf nodes. The node which is to be deleted can either be
a leaf node or an internal node. Following algorithm needs to be followed in order to delete
a node from a B tree.

1. Locate the leaf node.


2. If there are more than m/2 keys in the leaf node then delete the desired key from
the node.
3. If the leaf node doesn't contain m/2 keys then complete the keys by taking the
element from eight or left sibling.
o If the left sibling contains more than m/2 elements then push its largest
element up to its parent and move the intervening element down to the node
where the key is deleted.
o If the right sibling contains more than m/2 elements then push its smallest
element up to the parent and move intervening element down to the node
wherethe key is deleted.
4. If neither of the sibling contain more than m/2 elements then create a new leaf node
by joining two leaf nodes and the intervening element of the parent node.
5. If parent is left with less than m/2 nodes then, apply the above process on the parent
too.

If the the node which is to be deleted is an internal node, then replace the node with its in-
order successor or predecessor. Since, successor or predecessor will always be on the leaf
node hence, the process will be similar as the node is being deleted from the leaf node.

Example 1

Delete the node 53 from the B Tree of order 5 shown in the following figure.

53 is present in the right child of element 49. Delete it.

Now, 57 is the only element which is left in the node, the minimum number of elements
that must be present in a B tree of order 5, is 2. it is less than that, the elements in its left
and right sub-tree are also not sufficient therefore, merge it with the left sibling and
intervening element of parent i.e. 49.

Application of B tree

B tree is used to index the data and provides fast access to the actual data stored on the
disks since, the access to value stored in a large database that is stored on a disk is a very
time consuming process.

Searching an un-indexed and unsorted database containing n key values needs O(n)
running time in worst case. However, if we use B Tree to index this database, it will be
searched in O(log n) time in worst case.

6. Explain Disjoint set data Structures for the following sets.


s1 = {1, 2, 3, 4} s2 = {5, 6, 7, 8}

Disjoint set data structure

The disjoint set data structure is also known as union-find data structure and merge-find
set. It is a data structure that contains a collection of disjoint or non- overlapping sets. The
disjoint set means that when the set is partitioned into the disjoint subsets. The various
operations can be performed on the disjoint subsets. In this case, we can add new sets, we
can merge the sets, and we can also find there presentative member of a set. It also allows
to find out whether the two elements are in the same set or not efficiently.

The disjoint set can be defined as the subsets where there is no common element between
thetwo sets. Let's understand the disjoint sets through an example.

s1 = {1, 2, 3, 4}

s2 = {5, 6, 7, 8}

We have two subsets named s1 and s2. The s1 subset contains the elements 1, 2, 3, 4,
while s2 contains the elements 5, 6, 7, 8. Since there is no common element between these
two sets, we will not get anything if we consider the intersection between these two sets.
This is also known as a disjoint set where no elements arecommon. Now the question
arises how we can perform the operations on them. We can perform only two operations,
i.e., find and union.
In the case of find operation, we have to check that the element is present in which set.
There are two sets named s1 and s2 shown below:

Suppose we want to perform the union operation on these two sets. First, we have to check
whether the elements on which we are performing the union operation belong to different
or same sets. If they belong to the different sets, then we can perform the union operation;
otherwise, not. For example, we want to performthe union operation between 4 and 8.
Since 4 and 8 belong to different sets, so weapply the union operation. Once the union
operation is performed, the edge will be added between the 4 and 8 shown as below:

When the union operation is applied, the set would be represented as:

s1Us2 = {1, 2, 3, 4, 5, 6, 7, 8}

Suppose we add one more edge between 1 and 5. Now the final set can
berepresented as:

s3 = {1, 2, 3, 4, 5, 6, 7, 8}

If we consider any element from the above set, then all the elements belong to thesame set;
itmeans that the cycle exists in a graph.

How can we detect a cycle in a graph?

We will understand this concept through an example. Consider the belowexample to


detect a cycle with the help of using disjoint
sets.
U = {1, 2, 3, 4, 5, 6, 7, 8}
Each vertex is labelled with some weight. There is a universal set with 8 vertices. We will
consider each edge one by one and form the sets.

First, we consider vertices 1 and 2. Both belong to the universal set; we perform the union
operation between elements 1 and 2. We will add the elements 1 and 2 in a set s1 and
remove these two elements from the universal set shown below:

s1 = {1, 2}

The vertices that we consider now are 3 and 4. Both the vertices belong to the universal
set; we perform the union operation between elements 3 and 4. We will form the set s3
having elements 3 and 4 and remove the elements from the universal set shown as below:

s2 = {3, 4}

The vertices that we consider now are 5 and 6. Both the vertices belong to the universal
set, so we perform the union operation between elements 5 and 6. We will form the set s3
having elements 5 and 6 and will remove these elements fromthe universal set shown as
below:

s3 = {5, 6}

The vertices that we consider now are 7 and 8. Both the vertices belong to the universal
set, so we perform the union operation between elements 7 and 8. We will form the set s4
having elements 7 and 8 and will remove these elements fromthe universal set shown as
below:

s4 = {7, 8}

The next edge that we take is (2, 4). The vertex 2 is in set 1, and vertex 4 is in set 2, so both
the vertices are in different sets. When we apply the union operation, then it will form the
new set shown as below:

s5 = {1, 2, 3, 4}

The next edge that we consider is (2, 5). The vertex 2 is in set 5, and the vertex 5 is in set
s3, so both the vertices are in different sets. When we apply the union operation, then it
will form thenew set shown as below:
s6 = {1, 2, 3, 4, 5, 6}

Now, two sets are left which are given below:


s4 = {7, 8}

s6 = {1, 2, 3, 4, 5, 6}

The next edge is (1, 3). Since both the vertices, i.e.,1 and 3 belong to the sameset, so it
forms a cycle. We will not consider this vertex.

The next edge is (6, 8). Since both vertices 6 and 8 belong to the different vertices s4 and
s6, we will perform the union operation. The union operation will form the new set shown
as below:

s7 = {1, 2, 3, 4, 5, 6, 7, 8}

The last edge is left, which is (5, 7). Since both the vertices belong to the sameset named
s7, a cycle is formed.

function Union(x, y) is

// Replace nodes by rootsx

:=Find(x)

y := Find(y)

ifx = y then

return // x and y are already in the same setend if

// If necessary, rename variables to ensure that

// x has at least as many descendants as y if

x.size < y.size then

(x, y) := (y, x)
end if

6.Explain Fibonacci Mergeable heap operations and decreasing a key and deleting a node.

// Make x the new root

y.parent := x

// Update the size of x

x.size := x.size + y.size

end function
FIBONACCI HEAPS

Structure of Fibonacci heaps

Like a binomial heap, a Fibonacci heap is a collection of heap-ordered trees. Thetrees in a


Fibonacci heap are not constrained to be binomial trees, however. Figure 21.1 (a)
shows an example of a Fibonacci heap.

Unlike trees within binomial heaps, which are ordered, trees within Fibonacci heaps are
rooted but unordered. As Figure 21.1(b) shows, each node x contains a pointer p[x] to its
parent and a pointer child[x] to any one of its children. The children of x are linked
together in a circular, doubly linked list , which we call the child list of x. Each child y in a
child list has pointers left[y] and right[y] that point to y's left and right siblings,
respectively. If node y is an only child, then left[y] = right[y] = y. The order in which
siblings appear in a child list is arbitrary.

Mergeable-heap operations

In this section, we describe and analyze the mergeable-heap operations as implemented


forFibonacci heaps. If only these operations--MAKE- HEAP, INSERT,
MINIMUM,
EXTRACT-MIN, and UNION--are to be
supported, each Fibonacci heap is simply a collection of "unordered" binomial trees.

Creating a new Fibonacci heap

To make an empty Fibonacci heap, the MAKE-FIB-HEAP procedure allocates and returns
theFibonacci heap object H, where n[H] = 0 and min[H] = NIL; thereare no trees in H.
Because t
(H) = 0 and m(H) = 0, the potential of the empty Fibonacci heap is (H) = 0. The
amortizedcost of MAKE-FIB-HEAP is thus equal to its O(1) actual cost.
Inserting a node

The following procedure inserts node x into Fibonacci heap H, assuming of course that the
node has already been allocated and that key[x] has already been filled in.

FIB-HEAP-INSERT(H, x)
degree[x] 0
p[x] NIL
child[x] NIL
left[x] x
right[x] x
mark[x] FALSE
concatenate the root list containing x with root list H
if min[H] = NIL or key[x] < key[min[H]]
then min[H] x
n[H] n[H] + 1
Finding the minimum node

The minimum node of a Fibonacci heap H is given by the pointer min[H], so we can find
the minimum node in O(1) actual time. Because the potential of H does not change, the
amortized cost of this operation is equal to its O(1) actual cost.
Extracting the minimum node

The process of extracting the minimum node is the most complicated of the operations
presented in this section. It is also where the delayed work of consolidating trees in the root
list finally occurs. The following pseudocode extracts the minimum node. The code
assumes for convenience that when a node is removed from a linked list, pointers
remaining in the list are updated, but pointers in the extracted node are left unchanged. It
also uses the auxiliary procedure CONSOLIDATE, which will be presented shortly.

Decrease Key and Delete Node Operations on a Fibonacci Heap

Decreasing a Key

1. In decreasing
Select the nodea key
to beoperation,
decreased,thex ,value of a keyitsisvalue
and change decreased
to thetonew
a lower
valuevalue.
k.
2. If the parent of x, y, is not null and the key of parent is greater than that of
Following functions are used for decreasing the key.
the k then call Cut(x) and Cascading-Cut(y) subsequently.
3. If the key of x is smaller than the key of min, then mark x as min.
Decrease-Key
1. Remove x from the current position and add it to the root list.

2. If x is marked, then mark it as false.

Cascading-Cut

1. If the parent of is not null ythen follow the following steps.


2. If y is unmarked, then mark y.
3. Else, call Cut(y) and Cascading-Cut(parent of y) .

Decrease Key Example

The above operations can be understood in the examples below.

Example: Decreasing 46 to 15.

1. Decrease
Decreasethe
46 value
to 15 46 to 15.

2. Cut part: Since 24 ≠ nill and 15 < its parent, cut it and add it to the root
list. Cascading-Cut part: mark 24.

Add 15 to root list


and mark 24
Example: Decreasing 35 to 5

1. Decrease the value 35 to 5.

Decrease 35 to 5

2. Cut part: Since and 5<its parent, cut it and add it to the root list.
26 ≠ nill

3. Cut 5 and add it to root list


4. Cascading-Cut part: Since 26 is marked, the flow goes to Cut and
Cut.
Cascading-
Cut(26): Cut 26 and add it to the root list and mark it as false.

Cut 26 and add it to


root list
Cascading-Cut(24):
Since the 24 is also marked, again call Cut(24) and Cascading-Cut(7). These
operations result in the tree below.

5. Cut 24 and add it to root list


6. Since 5 < 7, mark 5 as min.

7. Mark 5 as min
UNIT III GRAPHS

Elementary Graph Algorithms: Representations of Graphs – Breadth-First Search – Depth-First


Search – Topological Sort – Strongly Connected Components- Minimum Spanning Trees: Growing
a Minimum Spanning Tree – Kruskal and Prim- Single-Source Shortest Paths: The Bellman-Ford
algorithm – Single- Source Shortest paths in Directed Acyclic Graphs – Dijkstra‘s Algorithm;
Dynamic Programming - All-Pairs Shortest Paths: Shortest Paths and Matrix Multiplication – The
Floyd-Warshall Algorithm.

1.Write the definition of weighted graph?


A graph in which weights are assigned to every edge is called a weighted graph.
2.Define Graph?
A graph G consist of a nonempty set V which is a set of nodes of the graph, a set E which is the set of edges of
the graph, and a mapping from the set of edges E to set of pairs of elements of V. It can also be represented as
G=(V, E).
1. Define adjacency matrix?
The adjacency –matrix is an n x n matrix A whose elements aij are given byaij = 1 if (vi, vj) Exists =0
otherwise
2. Define adjacent nodes?
Any two nodes, which are connected by an edge in a graph, are called adjacent nodes.For example, if an edge x E
is associated with a pair of nodes
(u,v) where u, v V, then we say that the edge x connects the nodes u and v.
3. What is a directed graph?
A graph in which every edge is directed is called a directed graph.
4. What is an undirected graph?
A graph in which every edge is undirected is called an undirected graph.
5. What is a loop?
An edge of a graph, which connects to itself, is called a loop or sling.
6. What is a simple graph?
A simple graph is a graph, which has not more than one edge between a pair ofnodes.
7. What is a weighted graph?
A graph in which weights are assigned to every edge is called a weighted graph.
8. Define indegree and out degree of a graph?
In a directed graph, for any node v, the number of edges, which have v as their initialnode, is called the out degree
of the node v.Outdegree: Number of edges having the node v as root node is the outdegree of the node v.
9. Define path in a graph?
The path in a graph is the route taken to reach terminal node from a starting node.
10. What is a simple path?
i. A path in a diagram in which the edges are distinct is called a simple path.
ii. It is also called as edge simple.
11. What is a cycle or a circuit?
A path which originates and ends in the same node is called a cycle or circuit.
14.What is an acyclicgraph?A simple diagram, which does not have any cycles, is called an acyclic graph.
15. What is meant by strongly connected in a graph?
An undirected graph is connected, if there is a path from every vertex to every other vertex.Adirected graph with
this property is called strongly connected.
16. When a graph said to be weakly connected?
aij = 1 if (vi, vj) Exists =0 otherwise
When a directed graph is not strongly connected but the underlying graph is connected, then thegraph is said to
be weakly connected.

17. Name the different ways of representing a graph? Give examples


a. Adjacency matrix
b. Adjacency list
18. What is an undirected acyclic graph?
When every edge in an acyclic graph is undirected, it is called an undirected acyclicgraph. It is also called as
undirected forest.
19. What is meant by depth?
The depth of a list is the maximum level attributed to any element with in the list orwith in any sub list in the list.
20. What is the use of BFS?
BFS can be used to find the shortest distance between some starting node and the remaining nodes of the graph.
The shortest distance is the minimum number of edges traversed in order to travel from the start node the specific
node being examined.
21. What is topological sort?
It is an ordering of the vertices in a directed acyclic graph, such that: If there is a pathfrom u to v, then vappears after u
in the ordering.
22. Write BFS algorithm
1. Initialize the first node’s dist number and place in queue
2. Repeat until all nodes have been examined
3. Remove current node to be examined from queue
4. Find all unlabeled nodes adjacent to current node
5. If this is an unvisited node label it and add it to the queue
6. Finished.
23. Define biconnected graph?
A graph is called biconnected if there is no single node whose removal causes the graph to break into two or more
pieces. A node whose removal causes the graph to become disconnected is called a cut vertex.
24. What are the two traversal strategies used in traversing a graph?
a. Breadth first search
b. Depth first search
25. Articulation Points (or Cut Vertices) in a Graph
A vertex in an undirected connected graph is an articulation point (or cut vertex) if removing it (and edges
through it) disconnects the graph. Articulation points represent vulnerabilities in a connected network – single
points whose failure would split the network into 2 or more disconnected components. They are useful for
designing reliable networks.
26. Write the concept of Prim’s spanning tree.
Prim’s algorithm constructs a minimum spanning tree through a sequence of expanding sub trees. The initial sub
tree in such a sequence consists of a single vertex selected arbitrarily from the set V of the
graph’s vertices. On each iteration, we expand the current tree in the greedy manner by simply attaching toit the
nearest vertex not in that tree. The algorithm stops after all the graph’s vertices have been included in the tree
being constructed
27. What is the purpose of Dijikstra’s Algorithm?
Dijikstra’s algorithm is used to find the shortest path between sources to every vertex. This algorithm is
applicable to undirected and directed graphs with nonnegative weights only.
28. How efficient is prim’s algorithm?
It depends on the data structures chosen for the graph itself and for the priority queue of the set V-VT whose
vertex priorities are the distances to the nearest tree vertices.
Mention the two classic algorithms for the minimum spanning tree problem.
 Prim’s algorithm
 Kruskal’s algorithm
29. What is the Purpose of the Floyd algorithm?
The Floyd’s algorithm is used to find the shortest distance between every pair ofvertices in agraph.
30. What are the conditions involved in the Floyd’s algorithm?
 Construct the adjacency matrix.
 Set the diagonal elements to zero
 Ak[i,j]= min Ak-1[i,j] Ak-
1[i,k]and Ak-1[k,j]
31. Write the concept of kruskal’s algorithm.
Kruskal’s algorithm looks at a minimum spanning tree for a weighted connected graph G=(V,E) as an acyclic
sub graph with |V|-1 edges for which the sum of the edge weights is the smallest. Consequently, the algorithm
constructs a minimum spanning tree as an expanding sequence of sub graphs, which are always acyclic but
are not necessarily connected on the intermediate stages of the algorithm. The algorithm begins by sorting the
graph’s edges in non decreasing order of their weights. Then, starting with the empty sub graph, it scans this
sorted list, adding the next edge on the list to the current sub graphif such an inclusion does not createa cycle
and simply skipping the edge otherwise.
32. What is the difference between dynamic programming with divide and conquer method?
Divide and conquer divides an instance into smaller instances with no intersectionswhereas dynamic
programming deals with problems in which smaller instances overlap. Consequently divide and conquer
algorithm do not explicitly store solutions to smaller instances and dynamic programming algorithms do.
33. State two obstacles for constructing minimum spanning tree using exhaustive- search approach.
The number spanning tree grows exponentially with the graph size
Generating all spanning trees for a given graph is not easy; in fact, it is more difficult than finding a minimum
spanning tree for a weighted graph by using one of several efficient algorithms available for this problem
34. Define spanning tree and minimum spanning tree problem.
A spanning tree of a connected graph is its connected acyclic sub graph that contains all the vertices of the
graph. A minimum spanning tree problem is the problem of finding a minimum spanning tree for a given
weighted connected graph.
35. Define the single source shortest paths problem.
Dijkstra’s algorithm solves the single-source shortest-path problem of finding shortest paths from a given
vertex (the source) to all the other vertices of a weighted graph or digraph. It works as Prim’s algorithm but
compares path lengthsrather than edge lengths. Dijkstra’s algorithm always yields a correct solution for a graph
with nonnegative weights
36. Mention the methods for generating transitive closure of digraph.
Depth First Search (DFS)Breadth First
Search (BFS)
37. What do you meant by graph traversals?
Graph traversal (also known asgraph search) refers to the process of visiting (checking and/or updating) each
vertex in a graph. Such traversalsare classified by the order in which the vertices are visited. Tree traversal is a
special case of graph traversal
38. Define Depth First Search DFS
Depth First Search (DFS) algorithm traverses a graph in a depthward motion and uses a stackto remember
to get the next vertex to start a search, when a dead end occurs in any iteration.
39. Write down the steps involved in DFS
Rule 1 − Visit the adjacent unvisited vertex. Mark it as visited. Display it. Push itin astack. Rule 2 − If no
adjacent vertex is found, pop up a vertex from the stack. (It will pop up allthe vertices from the stack, which
do not have adjacent vertices.)
Rule 3 − Repeat Rule 1 and Rule 2 until the stack is empty
40. Define Breadth First Search (BFS)
Breadth First Search (BFS) algorithm traverses a graph in a breadthward motion and usesaqueue to
remember to get the next vertex to start a search, when a dead
end occurs in any iteration.
41. Write down the steps involved in Breadth First Search (BFS)
Rule 1 − Vist the adjacent unvisited vertex. Mark it as visited. Display it. Insert it in a queue.
Rule 2 − If no adjacent vertex is found, remove the first vertex from the queue.
Rule 3 − Repeat Rule 1 and Rule 2 until the queue is empty
42. Define graph data structure
A graph is a pictorial representation of a set of objects where some pairs of objects are connected by links.
The interconnected objects are represented by points termed as vertices, and the links that connect the vertices
are called edges. Formally, a graph is apair of sets (V, E), where V is the set of vertices and Eis the set of
edges, connecting the pairs of vertices.
43. Define topological sorting
Topological sorting of vertices of a Directed Acyclic Graph is an ordering of the vertices
v1,v2,...vn in such a way, that if there is an edge directed towards vertex vjfrom vertex vi, then vi comes before
vj.
44. Define Memory function techniques
The memory function technique seeks to combine strengths of the top-down and bottom-up approaches to
solving problems with overlapping sub problems. It does this by solving, in the top-down fashion but only
once, just necessary sub problemsof a given problem and recording their solutions in a table.

1.Define Graph.
Graph

A graph can be defined as group of vertices and edges that are used to connect
these vertices. A graph can be seen as a cyclic tree, where the vertices
(Nodes) maintain any complex relationship among them instead of having
parent child relationship.

Definition

A graph G can be defined as an ordered set G(V, E) where V(G) represents the
set of vertices and E(G) represents the set of edges which are used to connect
these vertices.
how to represent a graph?

A Graph G(V, E) with 5 vertices (A, B, C, D, E) and six edges ((A,B),


(B,C),(C,E), (E,D), (D,B), (D,A)) is shown in the following figure.
Directed and Undirected Graph

A graph can be directed or undirected. However, in an undirected graph, edges are not
associated with the directions with them. An undirected graph is shown in the above figure
since its edges are not attached with any of the directions. If an edge exists between vertex A
and B then the vertices can be traversed from B to A as well as A to B.

In a directed graph, edges form an ordered pair. Edges represent a specific path from some
vertex A to another vertex B. Node A is called initial node while node B is called terminal
node.
A directed graph is shown in the following figure.

Graph terminology

Path

A path can be defined as the sequence of nodes that are followed in order to reach
someterminal node V from the initial node U.

Closed Path

A path will be called as closed path if the initial node is same as terminal node. Apath will
beclosed path if V0=VN.

Simple Path

If all the nodes of the graph are distinct with an exception V0=VN, then such path Pis called
asclosed simple path.

Cycle

A cycle can be defined as the path which has no repeated edges or vertices exceptthe first
andlast vertices.
Connected Graph

A connected graph is the one in which some path exists between every two vertices(u, v)
in V.There are no isolated nodes in connected graph.

Complete Graph

A complete graph is the one in which every node is connected with all other nodes. A
complete graph contain n(n-1)/2 edges where n is the number of nodes in the graph.
Weighted Graph

In a weighted graph, each edge is assigned with some data such as length or weight. The
weight of an edge e can be given as w(e) which must be a positive (+) value indicating the
cost of traversing the edge.

Digraph

A digraph is a directed graph in which each edge of the graph is associated with some
direction and the traversing can be done only in the specified direction.

Loop

An edge that is associated with the similar end points can be called as Loop.

Adjacent Nodes

If two nodes u and v are connected via an edge e, then the nodes u and v are calledas
neighbours or adjacent nodes.

Degree of the Node

A degree of a node is the number of edges that are connected with that node. Anode with
degree 0 is called as isolated node.

Graph Representation

By Graph representation, we simply mean the technique which is to be used inorder to store
some graph into the computer's memory.

There are two ways to store Graph into the computer's memory. In this part of thistutorial,
we discuss each one of them in detail.

1. Sequential Representation
In sequential representation, we use adjacency matrix to store the mapping represented by
vertices and edges. In adjacency matrix, the rows and columns are represented by the graph
vertices. A graph having n vertices, will have a dimension n x n.

An entry M ij in the adjacency matrix representation of an undirected graph G willbe 1 if


there exists an edge between Vi and Vj.Hello Java Program for Beginners
An undirected graph and its adjacency matrix representation is shown in thefollowing figure.

in the above figure, we can see the mapping among the vertices (A, B, C, D, E) is represented
by using the adjacency matrix which is also shown in the figure.

There exists different adjacency matrices for the directed and undirected graph. In
directedgraph, an entry Aij will be 1 only when there is an edge directed from Vi toVj.

A directed graph and its adjacency matrix representation is shown in the following figure.

Representation of weighted directed graph is different. Instead of filling the entry by 1, the
Non-zero entries of the adjacency matrix are represented by the weight of respective edges.

Linked Representation

In the linked representation, an adjacency list is used to store the Graph into the
computer'smemory.

Consider the undirected graph shown in the following figure and check the adjacency list
representation.
An adjacency list is maintained for each node present in the graph which stores thenode
value and a pointer to the next adjacent node to the respective node. If all the adjacent
nodes are traversed then store the NULL in the pointer field of last node of the list. The
sum of t he lengths of adjacency lists is equal to the twice of the number of edges
present in an undirected graph.

Consider the directed graph shown in the following figure and check the adjacency list
representation of the graph.

In a directed graph, the sum of lengths of all the adjacency lists is equal to the number of
edges present in the graph.
In the case of weighted directed graph, each node contains an extra field that is called the
weight of the node. The adjacency list representation of a directed graph is shown in the
following figure.
1. Explain topological sort with an example.
Problem-01:

Find the number of different topological orderings possible for the given graph-

Solution-

The topological orderings of the above graph are found in the following steps-

Step-01:

Write in-degree of each vertex-

Step-02:
 Vertex-A has the least in-degree.
 So, remove vertex-A and its associated edges.
 Now, update the in-degree of other vertices.

Step-03:

 Vertex-B has the least in-degree.


 So, remove vertex-B and its associated edges.
 Now, update the in-degree of other vertices.

Step-04:

There are two vertices with the least in-degree. So, following 2 cases are possible-

In case-01,
Remove vertex-C and its associated edges.
Then, update the in-degree of other vertices.

In case-02,
 Remove vertex-D and its associated edges.
 Then, update the in-degree of other vertices.
Step-05:

Now, the above two cases are continued separately in the similar
manner. In case-01,
Remove vertex-D since it has the least in-degree. Then,
remove the remaining vertex-E.
In case-02,
Remove vertex-C since it has the least in-degree. Then,
remove the remaining vertex-E.

Conclusion-
For the given graph, following 2 different topological orderings are possible-

 ABCDE
 ABDCE

The topological sorting for a directed acyclic graph is the linear ordering of vertices. For
every edge U-V of a directed graph, the vertex u will come before vertex v in the ordering.
As we know that the source vertex will come after the destination vertex, so we need to use
a stack to store previous elements. After completing all nodes, we can simply display them
from the stack.

Output:

Nodes after topological sorted order: 5 4 2 3 1 0

Algorithm

topoSort(u, visited, stack)

Input − The start vertex u, An array to keep track of which node is visited or not. Astack to
store nodes.

Output − Sorting the vertices in topological sequence in the stack.

Begin

mark u as visited

for all vertices v which is adjacent with u, doif v is

not visited, then

topoSort(c, visited,

stack)done

push u into a stack

End

performTopologicalSorting(Graph)
Input − The given directed acyclic graph.Output

− Sequence of nodes.

Begin
initially mark all nodes as unvisitedfor all

nodes v of the graph, do

if v is not visited,

thentopoSort(i, visited,

stack)

done

pop and print all elements from the stackEnd.

Program:

#include<iostream>

#include<stack>

#define NODE 6

int graph[NODE][NODE] = {

{0, 0, 0, 0, 0, 0},

{0, 0, 0, 0, 0, 0},

{0, 0, 0, 1, 0, 0},

{0, 1, 0, 0, 0, 0},

{1, 1, 0, 0, 0, 0},

{1, 0, 1, 0, 0, 0}
};

void topoSort(int u, bool visited[], stack<int> &stk)


{

visited[u] = true; //set as the node v is

visitedfor(int v = 0; v<NODE; v++)

if(graph[u][v])

{ //for all vertices v adjacent to

uif(!visited[v])
topoSort(v, visited, stk);

stk.push(u); //push starting vertex into the stack

void performTopologicalSort()

stack<int> stk;

bool vis[NODE];

for(int i = 0; i<NODE; i++)

vis[i] = false; //initially all nodes are unvisited

for(int i = 0; i<NODE; i++)

if(!vis[i]) //when node is not

visitedtopoSort(i, vis, stk);

while(!stk.empty())

{
cout << stk.pop() << " ";

}
main()

cout << "Nodes after topological sorted order: ";

performTopologicalSort()

Output

Nodes after topological sorted order: 5 4 2 3 1 0


2.Difference between BFS and DFS traversal algorithms. Consider a graph of minimum height of 4,
and do BFS and DFS traversals.

What is MST? Explain major difference between Prims and Kruskals MST.
2.Explain different types of Graph Traversal Algorithm.

In this part of the tutorial we will discuss the techniques by using which, we cantraverse
all the vertices of the graph.

Traversing the graph means examining all the nodes and vertices of the graph. There are two
standard methods by using which, we can traverse the graphs. Lets discuss each one of them
in detail.

o Breadth First Search


o Depth First Search

Breadth First Search (BFS) Algorithm

What is BFSA?
Breadth first search is a graph traversal algorithm that starts traversing the graph from root
node and explores all the neighbouring nodes. Then, it selects the nearest node and explore
all the unexplored nodes. The algorithm follows the same process for each of the nearest node
until it finds the goal.

The algorithm of breadth first search is given below. The algorithm starts with examining
the node A and all of its neighbours. In the next step, the neighbours of the nearest node of
A are explored and process continues in the further steps. The
algorithm explores all neighbours of all the nodes and ensures that each node is visited
exactly once and no node is visited twice.
Solution:

Minimum Path P can be found by applying breadth first search algorithm that will begin at
node A and will end at E. the algorithm uses two queues, namely QUEUE1 and
QUEUE2. QUEUE1 holds all the nodes that are to be processed while QUEUE2 holds all
the nodes that are processed and deleted from QUEUE1.

Lets start examining the graph from Node A.

1. Add A to QUEUE1 and NULL to QUEUE2.

1. QUEUE1 = {A}
2. QUEUE2 = {NULL}
2. Delete the Node A from QUEUE1 and insert all its neighbours. Insert Node A into
QUEUE2

1. QUEUE1 = {B, D}
2. QUEUE2 = {A}

3. Delete the node B from QUEUE1 and insert all its neighbours. Insert node B into
QUEUE2.

1. QUEUE1 = {D, C, F}
2. QUEUE2 = {A, B}
4. Delete the node D from QUEUE1 and insert all its neighbours. Since F is the only
neighbour of it which has been inserted, we will not insert it again. Insert node D into
QUEUE2.

1. QUEUE1 = {C, F}
2. QUEUE2 = { A, B, D}

5. Delete the node C from QUEUE1 and insert all its neighbours. Add node C to QUEUE2.

1. QUEUE1 = {F, E, G}
2. QUEUE2 = {A, B, D, C}

6. Remove F from QUEUE1 and add all its neighbours. Since all of its neighbours has
already been added, we will not add them again. Add node F to QUEUE2.

1. QUEUE1 = {E, G}
2. QUEUE2 = {A, B, D, C, F}

7. Remove E from QUEUE1, all of E's neighbours has already been added to QUEUE1
therefore we will not add them again. All the nodes are visited and the target node i.e. E is
encountered into QUEUE2.
1. QUEUE1 = {G}
2. QUEUE2 = {A, B, D, C, F, E}
Now, backtrack from E to A, using the nodes available in QUEUE2.The

minimumpath will be A → B → C → E.

Algorithm

1. Start putting anyone vertices from the graph at the back of the queue.
2. First, move the front queue item and add it to the list of the visited node.
3. Next, create nodes of the adjacent vertex of that list and add them which have
notbeen visited yet.
4. Keep repeating steps two and three until the queue is found to be empty.
Pseudocode

Set all nodes to "not visited";

q = new Queue();
q.enqueue(initial node);
while ( q ? empty ) do
{
x = q.dequeue();
if ( x has not been visited )
{
visited[x] = true; // Visit node x !

for ( every edge (x, y) /* we are using all edges ! */ )if ( y has
not been visited )
q.enqueue(y); // Use the edge (x,y) !!!
}
}

Complexity: 0(V+E) where V is vertices and E is edges.

What is DFSA?

Depth First Search (DFS) Algorithm

Depth first search (DFS) algorithm starts with the initial node of the graph G, and then goes
to deeper and deeper until we find the goal node or the node which has no children. The
algorithm, then backtracks from the dead end towards the most recent node that is yet to be
completely unexplored.
The data structure which is being used in DFS is stack. The process is similar to BFS
algorithm. In DFS, the edges that leads to an unvisited node are called discovery edges
while the edges that leads to an already visited node are called block edges.

Example :

Consider the graph G along with its adjacency list, given in the figure below. Calculate the
order to print all the nodes of the graph starting from node H, by using depth first search
(DFS) algorithm.

Solution :

Push H onto the stack

Ho Java Program for Beginners

1. STACK : H

POP the top element of the stack i.e. H, print it and push all the neighbours of H onto the
stack that are is ready state.

1. Print H
2. STACK : A

Pop the top element of the stack i.e. A, print it and push all the neighbours of A onto the
stack that are in ready state.

1. Print A
2. Stack : B, D

Pop the top element of the stack i.e. D, print it and push all the neighbours of D onto the
stack that are in ready state.

1. Print D
2. Stack : B, F
Pop the top element of the stack i.e. F, print it and push all the neighbours of Fonto the
stack that are in ready state.

1. Print F
2. Stack : B

Pop the top of the stack i.e. B and push all the neighbours

1. Print B
2. Stack : C

Pop the top of the stack i.e. C and push all the neighbours.

1. Print C
2. Stack : E, G

Pop the top of the stack i.e. G and push all its neighbours.

1. Print G
2. Stack : E

Pop the top of the stack i.e. E and push all its neighbours.

1. Print E
2. Stack :

Hence, the stack now becomes empty and all the nodes of the graph have

beentraversed. The printing sequence of the graph will be :

1. H → A → D → F → B → C → G → E

Algorithm

1. Start by putting one of the vertexes of the graph on the stack's top.
2. Put the top item of the stack and add it to the visited vertex list.
3. Create a list of all the adjacent nodes of the vertex and then add those nodes to the
unvisited at the top of the stack.
4. Keep repeating steps 2 and 3, and the stack becomes empty.

Pseudocode

DFS(G,v) ( v is the vertex where the search starts


)Stack S := {};( start with an empty stack ) for each
vertex u, set visited[u] := false;
push S, v;
while (S is not empty) dou
:=pop S;
if (not visited[u]) then
visited[u] := true;
for each unvisited neighbour w of
upush S, w;
end if end
while
END DFS()
Spanning tree

In this article, we will discuss the spanning tree and the minimum spanning tree. But before
moving directly towards the spanning tree, let's first see a brief description of the graph and
its types.

Graph

A graph can be defined as a group of vertices and edges to connect these vertices. The types
of graphs are given as follows -

o Undirected graph: An undirected graph is a graph in which all the edges donot
point to any particular direction, i.e., they are not unidirectional; they
arebidirectional. It can also be defined as a graph with a set of V vertices and a set of
E edges, each edge connecting two different vertices.
o Connected graph: A connected graph is a graph in which a path always exists from
a vertex to any other vertex. A graph is connected if we can reach any vertex from
any other vertex by following edges in either direction.
o Directed graph: Directed graphs are also known as digraphs. A graph is a directed
graph (or digraph) if all the edges present between any vertices or nodes of the graph
are directed or have a defined direction.

Now, let's move towards the topic spanning tree.

What is a spanning tree?

A spanning tree can be defined as the subgraph of an undirected connected graph.It includes
all the vertices along with the least possible number of edges. If any vertex is missed, it is
not a spanning tree. A spanning tree is a subset of the graph that does not have cycles, and it
also cannot be disconnected.

A spanning tree consists of (n-1) edges, where 'n' is the number of vertices (or nodes). Edges
of the spanning tree may or may not have weights assigned to them. All the possible
spanning trees created from the given graph G would have the same number of vertices, but
the number of edges in the spanning tree would be equal to the number of vertices in the
given graph minus 1.

A complete undirected graph can have nn-2 number of spanning trees where n is thenumber
of vertices in the graph. Suppose, if n = 5, the number of maximum possible spanning trees
would be 55-2 = 125.

Applications of the spanning tree

Basically, a spanning tree is used to find a minimum path to connect all nodes of the graph.
Some of the common applications of the spanning tree are listed as follows -

o Cluster Analysis
o Civil network planning
o Computer network routing protocol

Now, let's understand the spanning tree with the help of an example. Example of

Spanning tree

As discussed above, a spanning tree contains the same number of vertices as the graph, the
number of vertices in the above graph is 5; therefore, the spanning tree will contain 5
vertices. The edges in the spanning tree will be equal to the number of vertices in the graph
minus 1. So, there will be 4 edges in the spanning tree.

Some of the possible spanning trees that will be created from the above graph are given as
follows -
Properties of spanning-tree

Some of the properties of the spanning tree are given as follows -

o There can be more than one spanning tree of a connected graph G.


o A spanning tree does not have any cycles or loop.
o A spanning tree is minimally connected, so removing one edge from thetree will
makethe graph disconnected.
o A spanning tree is maximally acyclic, so adding one edge to the tree will create a
loop.
o There can be a maximum nn-2 number of spanning trees that can be created from a
complete graph.
o A spanning tree has n-1 edges, where 'n' is the number of nodes.
o If the graph is a complete graph, then the spanning tree can be constructed by
removing maximum (e-n+1) edges, where 'e' is the number of edges and 'n' is the
number of vertices.

So, a spanning tree is a subset of connected graph G, and there is no spanning tree of a
disconnected graph.

Minimum Spanning tree

A minimum spanning tree can be defined as the spanning tree in which the sum of the
weights of the edge is minimum. The weight of the spanning tree is the sum of the weights
given to the edges of the spanning tree. In the real world, this weight can be considered as
the distance, trafficload, congestion, or any random value.

Example of minimum spanning tree

Let's understand the minimum spanning tree with the help of an example.
The sum of the edges of the above graph is 16. Now, some of the possible spanning trees
created from the above graph are -

So, the minimum spanning tree that is selected from the above spanning trees for the given
weighted graph is -

Applications of minimum spanning tree

The applications of the minimum spanning tree are given as follows -

o Minimum spanning tree can be used to design water-supply


networks,telecommunication networks, and electrical grids.
o It can be used to find paths in the map.

Algorithms for Minimum spanning tree

A minimum spanning tree can be found from a weighted graph by using the algorithms
givenbelow -

o Prim's Algorithm
o Kruskal's Algorithm

Let's see a brief description of both of the algorithms listed above.

Prim's algorithm - It is a greedy algorithm that starts with an empty spanning tree. It is used
to find the minimum spanning tree from the graph. This algorithm finds the subset of edges
that includes every vertex of the graph such that the sum of the weights of the edges can be
minimized.

Kruskal's algorithm - This algorithm is also used to find the minimum spanning tree for a
connected weighted graph. Kruskal's algorithm also follows greedy approach, which finds
an optimum solution at every stage instead of focusing on a global optimum.
So, that's all about the article. Hope the article will be helpful and informative to you. Here,
we have discussed spanning tree and minimum spanning tree along withtheir properties,
examples, and applications.

Prim's Algorithm

In this article, we will discuss the prim's algorithm. Along with the algorithm, we will also
see the complexity, working, example, and implementation of prim's algorithm.

Before starting the main topic, we should discuss the basic and important terms such as
spanning tree and minimum spanning tree.

Spanning tree - A spanning tree is the sub graph of an undirected connected graph.

Minimum Spanning tree - Minimum spanning tree can be defined as the spanning tree in
which the sum of the weights of the edge is minimum. The weight of the spanning tree is the
sumof the weights given to the edges of the spanning tree.

Prim's Algorithm is a greedy algorithm that is used to find the minimum spanning tree
from a graph. Prim's algorithm finds the subset of edges that includes every vertex of the graph
such that the sum of the weights of the edges can be minimized.

Prim's algorithm starts with the single node and explores all the adjacent nodes with all the
connecting edges at every step. The edges with the minimal weights causing no cycles in the
graph got selected.

How does the prim's algorithm work?

Prim's algorithm is a greedy algorithm that starts from one vertex and continue to add the
edges with the smallest weight until the goal is reached. The steps to implement the prim's
algorithm are given as follows -

o First, we have to initialize an MST with the randomly chosen vertex.


o Now, we have to find all the edges that connect the tree in the above step with the
newvertices. From the edges found, select the minimum edge and add it to the tree.
o Repeat step 2 until the minimum spanning tree is formed.
The applications of prim's algorithm are -

o Prim's algorithm can be used in network designing.


o It can be used to make network cycles.
o It can also be used to lay down electrical wiring cables.

Algorithm
Step 1: Select a starting vertex
Step 2: Repeat Steps 3 and 4 until there are fringe vertices
Step 3: Select an edge 'e' connecting the tree vertex and fringe vertex that has
minimumweight
Step 4: Add the selected edge and the vertex to the minimum spanning tree T [END
OFLOOP]
Step 5: EXIT

Prim’s Algorithm:
Step 1: Assume all nodes weigh infinity except the source node. And we need to select the
sourcenode first.
Step 2: Select the node with minimum weight and assign its value equal to 0.

Step 3: Make a set MST to keep track of all the processed nodes which are included in the
MST. Step 4: Include the selected node in the set MST.
Step 5: Either relax or Compute all the adjacent edges.
Step 5.1: Whenever the cost of the adjacent edge is lower than infinity, update thecost of the
edge. Step 5.2: Select the adjacent edge which costs comparatively lower than the other.
Step 6: Till the set MST includes all the vertices, repeat the above steps from 2 to 6.
Prim’s Algorithm

Prim’s algorithm is a greedy approach to find the minimum spanning tree. In thisalgorithm,
to form a MST we can start from an arbitrary vertex.

Algorithm: MST-Prim’s (G, w, r)


for each u є G.V
u.key = ∞
u.∏ = NIL
r.key = 0
Q = G.V
while Q ≠ Ф
u = Extract-Min (Q)
for each v є G.adj[u]
if each v є Q and w(u, v) < v.keyv.∏
=u
v.key = w(u, v)
The function Extract-Min returns the vertex with minimum edge cost. Thisfunction
works onmin-heap.

Example

Using Prim’s algorithm, we can start from any vertex, let us start from vertex 1.
Vertex 3 is connected to vertex 1 with minimum edge cost, hence edge (1, 2) isadded to the
spanning tree.

Next, edge (2, 3) is considered as this is the minimum among edges {(1, 2), (2, 3),
(3, 4), (3, 7)}.
In the next step, we get edge (3, 4) and (2, 4) with minimum cost. Edge (3, 4) isselected at
random.
In a similar way, edges (4, 5), (5, 7), (7, 8), (6, 8) and (6, 9) are selected. As all thevertices
arevisited, now the algorithm stops.
The cost of the spanning tree is (2 + 2 + 3 + 2 + 5 + 2 + 3 + 4) = 23. There is no more
spanning tree in this graph with cost less than 23.

Prim’s Algorithm:
Step 1: Assume all nodes weigh infinity except the source node. And we need toselect the
sourcenode first.
Step 2: Select the node with minimum weight and assign its value equal to 0.

Step 3: Make a setMST to keep track of all the processed nodes which are includedin the
MST. Step 4: Include the selected node in the setMST.
Step 5: Either relax or Compute all the adjacent edges.
Step 5.1: Whenever the cost of the adjacent edge is lower than infinity, update thecost of the
edge. Sep 5.2: Select the adjacent edge which costs comparatively lower than the other. Step 6:
Till the setMST includes all the vertices, repeat the above steps from 2 to 6.
An Example of Prim’s Algorithm
Now that we know the approach behind Prim’s Algorithm,

Kruskal’s Algorithm.

As we have discussed before, Greedy algorithms have a huge base of functionalityin day to
daylife problems.
For Example, Kruskal comes to play a huge role in solving problems that involve sparse
graphs.Practical life Applications where Kruskal’s Algorithm can be used,

include LAN Networks, A Network of pipes for the supply of Water or Gas for domestic or
Industrial use, An Electric grid, TV Networks as well as Landing Cables among many
others. Kruskal’s Algorithm is relatively easier to implementthan Prim’s when applied to
sparse graphs.
In Kruskal’s Algorithm, unlike Prim’s we do not have to keep a connected component or a single
vertex. You look at the overall smallest edge which does notcreate a cycle. It takes N-1 steps
to form a Minimum Spanning Tree.
Kruskal’s Algorithm:
Algorithm:
Step 1: Sort the edges in increasing order according to their given weights and makea list of
thesame.
Step 2: Take an edge from the list and check if it is creating a cycle in the MST.

Step 3: If it does not create a cycle in the MST, we consider the edge and if it doescreate a
cycle,then we skip the current edge and move on to the nest edge in the sorted list.

Step 4: Repeat the above steps from 1 to 3 till we have V-1 edges in the final MST.
An Example of Kruskal’s Algorithm

Now that we have discussed both the Algorithms, let us go through a few of the
keydifferencesbetween these 2 greedy algorithms.

Although Prim’s and Kruskal are very similar in approach and easy to understandas well
havethe same worst-case scenarios but differ in a few major aspects.

Single Source Shortest Paths


Introduction:

In a shortest- paths problem, we are given a weighted, directed graphs G = (V,E), with
weight function w: E → R mapping edges to real-valued weights. The weight of path p =
(v0,v1, vk) is the total of the weights of its constituent edges:

We define the shortest - path weight from u to v by δ(u,v) = min (w (p): u→v), if there is a
path from u to v, and δ(u,v)= ∞, otherwise.

The shortest path from vertex s to vertex t is then defined as any path p with weight w (p) =
δ(s,t).

The breadth-first- search algorithm is the shortest path algorithm that works on
unweighted graphs, that is, graphs in which each edge can be considered to have unit weight.

In a Single Source Shortest Paths Problem, we are given a Graph G = (V, E), we want to
find the shortest path from a given source vertex s ∈ V to every vertex v ∈ V.

There are some variants of the shortest path problem.

o Single- destination shortest - paths problem: Find the shortest path to a given
destination vertex t from every vertex v. By shift the direction of each edge in the
graph, we can shorten this problem to a single - source problem.
o Single - pair shortest - path problem: Find the shortest path from u to v for given
vertices u and v. If we determine the single - source problem with source vertex u,
we clarify this problem also. Furthermore, no algorithms forthis problem are known
that run asymptotically faster than the best single - source algorithms in the worst case.
o All - pairs shortest - paths problem: Find the shortest path from u to v for every
pair of vertices u and v. Running a single - source algorithm once fromeach vertex
can clarify this problem; but it can generally be solved faster, and its structure is of
interest in the own right.
Negative Weight Edges

It is a weighted graph in which the total weight of an edge is negative. If a graph has a
negative edge, then it produces a chain. After executing the chain if the output is negative
then it will give - ∞ weight and condition get discarded. If weight is less than negative and -
∞ then we can'thave the shortest path in it.

Briefly, if the output is -ve, then both condition get discarded.

1. - ∞
2. Not less than 0.

And we cannot have the shortest Path.


Example:

1. Beginning from s
2. Adj [s] = [a, c, e]
3. Weight from s to a is 3
Suppose we want to calculate a path from s→c. So We have 2 paths
/weight 10 Sect
1. s to c = 5, s→c→d→c=8
2. But s→c is minimum
3. So s→c = 5

Suppose we want to calculate a path from s→e. So we have two paths again

1. s→e = 2, s→e→f→e=-1
2. As -1 < 0 ∴ Condition gets discarded. If we execute this chain, we will get -
∞. So we can't get the shortest path ∴ e = ∞.
This figure illustrates the effects of negative weights and negative weight cycle onthe
shortest path

Relaxation

The single - source shortest paths are based on a technique known as


relaxation, a method that repeatedly decreases an upper bound on the actual
shortest path weight of each vertex until the upper bound equivalent the
shortest - path weight. For each vertex v ∈ V, we maintain an attribute d [v],
which is an upper bound on the weight of the shortest path from source s to
v. We call d [v] the shortest path estimate.

Dijkstra Algorithm

Dijkstra algorithm is a single-source shortest path algorithm. Here, single-source means that
only one source is given, and we have to find the shortest path from the source to all the
nodes.

Let's understand the working of Dijkstra's algorithm. Consider the below graph.
First, we have to consider any vertex as a source vertex. Suppose we consider vertex 0 as a
source vertex.

Here we assume that 0 as a source vertex, and distance to all the other vertices is infinity.
Initially, we do not know the distances. First, we will find out the vertices which are directly
connected to the vertex 0. As we can observe in the above graphthat two vertices are directly
connected to vertex 0.

Let's assume that the vertex 0 is represented by 'x' and the vertex 1 is represented by 'y'. The
distance between the vertices can be calculated by using the below formula:

d(x, y) = d(x) + c(x, y) < d(y)

= (0 + 4) < ∞

=4<∞

Since 4<∞ so we will update d(v) from ∞ to 4.

Therefore, we come to the conclusion that the formula for calculating the distance between
thevertices:

{if( d(u) + c(u, v) < d(v))


d(v) = d(u) +c(u, v) }

Now we consider vertex 0 same as 'x' and vertex 4 as 'y'.

d(x, y) = d(x) + c(x, y) < d(y)

= (0 + 8) < ∞

=8<∞

Algorithm

1. Set all the vertices to infinity, excluding the source vertex.


2. Push the source in the form (distance, vertex) and put it in the min-priority queue.
3. From the priority, queue pop out the minimum distant vertex from the sourcevertex.
4. Update the distance after popping out the minimum distant vertex and calculate
thevertex distance using (vertex distance + weight < following vertex distance).
5. If you find that the visited vertex is popped, move ahead without using it.
6. Apply the steps until the priority queue is found to be empty.
Pseudocode

function dijkstra(G, S) for


each vertex V in G
distance[V] <-
infinite
previous[V] <- NULL
If V != S, add V to Priority Queue Q
distance[S] <- 0
while Q IS NOT EMPTY
U <- Extract MIN from Q
for each unvisited neighbour V of U
tempDistance <- distance[U] + edge_weight(U, V) if
tempDistance < distance[V]
distance[V] <- tempDistance
previous[V] <- U
return distance[], previous[]

3. Explain Bellman Ford Algorithm with an example.

Bellman ford algorithm is a single-source shortest path algorithm. This algorithm is used to
find the shortest distance from the single vertex to all the other verticesof a weighted graph.
There are various other algorithms used to find the shortest path like Dijkstra algorithm, etc.
If the weighted graph contains the negative weight values, then the Dijkstra algorithm does
not confirm whether it produces the correct answer or not. In contrast to Dijkstra algorithm,
bellmanford algorithm guarantees the correct answer even if the weighted graph contains the
negative weight values.

Consider the below graph:

As we can observe in the above graph that some of the weights are negative. The above
graph contains 6 vertices so we will go on relaxing till the 5 vertices. Here, we will relax all
the edges 5 times. The loop will iterate 5 times to get the correct answer. If the loop is
iterated more than 5 times then also the answer will be the same, i.e., there would be no
change in the distance between the vertices.

To find the shortest path of the above graph, the first step is note down all theedges which
are given below:

(A, B), (A, C), (A, D), (B, E), (C, E), (D, C), (D, F), (E, F), (C, B)

Let's consider the source vertex as 'A'; therefore, the distance value at vertex A is 0 and the
distance value at all the other vertices as infinity shown as below:

Since the graph has six vertices so it will have five iterations.

First iteration

Consider the edge (A, B). Denote vertex 'A' as 'u' and vertex 'B' as 'v'. Now use the
relaxing formula:
d(u) = 0

d(v) = ∞ c(u

, v) = 6

Since (0 + 6) is less than ∞, so updated(v)

=0 + 6 = 6

Therefore, the distance of vertex B is 6.

Consider the edge (A, C). Denote vertex 'A' as 'u' and vertex 'C' as 'v'. Now use the relaxing
formula:

d(u) = 0

d(v) = ∞ c(u

, v) = 4

Since (0 + 4) is less than ∞, so updated(v)

=0 + 4 = 4

Therefore, the distance of vertex C is 4. Algorithm:

Bellman-Ford-Algorithm (G, w, s)for each vertex v

ЄG.V
v.d := ∞ v.∏

:= NIL

s.d := 0

for i = 1 to |G.V| - 1

for each edge (u, v) Є G.E if

v.d > u.d + w(u, v)

v.d := u.d +w(u, v)

v.∏ := u

for each edge (u, v) Є G.E if

v.d > u.d + w(u,


v)return

FALSE
return TRUE
5. Explain DAG WITH AN EXAMPLE.

DAG - SHORTEST - PATHS (G, w, s)


1. Topologically sort the vertices of G.
2. INITIALIZE - SINGLE- SOURCE (G, s)
3. for each vertex u taken in topologically sorted order
4. do for each vertex v ∈ Adj [u]
5. do RELAX (u, v, w)

The running time of this data is determined by line 1 and by the for loop of lines 3
- 5. The topological sort can be implemented in ∅ (V + E) time. In the for loop of lines 3 - 5,
as in Dijkstra's algorithm, there is one repetition per vertex. For each vertex, the edges that
leave the vertex are each examined exactly once. Unlike Dijkstra's algorithm, we use only O
(1) time per edge. The running time is thus ∅ (V + E), which is linear in the size of an
adjacency list depictionof the graph.

Example:

Step1: To topologically sort vertices apply DFS (Depth First Search) and then arrange
vertices in linear order by decreasing order of finish time.
Now, take each vertex in topologically sorted order and relax each edge.

1. adj [s] →t, x2. 0


+3<∞
3. d [t] ← 3 4.
0+2<∞
5. d [x] ← 2

1. adj [t] → r, x
2. 3 + 1 < ∞
3. d [r] ← 44.
3+5≤2

1. adj [x] → y 2.
2-3 <∞
3. d [y] ← -1
1. adj [y] → r 2.
-1 + 4 < 4 3. 3
<4
4. d [r] ← 3

Thus the Shortest Path is:

1. s to x is 2
2. s to y is -1
3. s to t is 3
4. s to r is 3
All-Pairs Shortest Paths

Introduction

It aims to figure out the shortest path from each vertex v to every other u. Storing all the
paths explicitly can be very memory expensive indeed, as we need one spanning tree for
each vertex. This is often impractical regarding memory consumption, so these are generally
considered as all pairs-shortest distance problems, which aim to find just the distance from
each to each node to another. We usually want the output in tabular form: the entry in u's
row and v's column should be the weight of the shortest path from u to v.

Unlike the single-source algorithms, which assume an adjacency list representation of the graph,
most of the algorithm uses an adjacency matrix representation. (Johnson's Algorithm for
sparse graphs uses adjacency lists.) The input is a n x n matrix W representing the edge
weights of an n- vertex directed graph G = (V, E). That is, W = (wij), where
Floyd-Warshall Algorithm

Let the vertices of G be V = {1, 2........n} and consider a subset {1, 2........k} of vertices for some
k. For any pair of vertices i, j ∈ V, considered all paths from i to jwhose intermediate
vertices are all drawn from {1, 2 k}, and let p be a minimum weight path from amongst
them. The Floyd-
Warshall algorithm exploits a link between path p and shortest paths from i to j with all
intermediate vertices in the set {1, 2.......k-1}. The link depends on whether or not k is an
intermediate vertex of path p.

If k is not an intermediate vertex of path p, then all intermediate vertices of path p are in the set
{1, 2........k-1}. Thus, the shortest path from vertex i to vertex j with
all intermediate vertices in the set {1, 2.......k-1} is also the shortest path i to j with all
intermediate vertices in the set {1, 2 .................... k}.

If k is an intermediate vertex of path p, then we break p down into i → k → j.

Let d ij(k) be the weight of the shortest path from vertex i to vertex j with allintermediate
vertices in the set {1, 2. .................................. k}.

A recursive definition is given by

FLOYD - WARSHALL (W)

Algorithms
Begin
for k := 0 to n, do
fori := 0 to n, do
for j := 0 to n, do
if cost[i,k] + cost[k,j] < cost[i,j], then cost[i,j]
:=cost[i,k] + cost[k,j]
done
done
done
display the current cost matrixEnd
Program:
#include<iostream>
#include<iomanip>
#define NODE 7
#define INF 999
usingnamespace std;
//Cost matrix of the graph
int costMat[NODE][NODE] = {{0,
3,6, INF, INF, INF, INF},
{3, 0, 2, 1, INF, INF, INF},
{6, 2, 0, 1, 4, 2, INF},
{INF, 1, 1, 0, 2, INF, 4},
{INF, INF, 4, 2, 0, 2, 1},
{INF, INF, 2, INF, 2, 0, 1},
{INF, INF, INF, 4, 1, 1, 0}
};
void floydWarshal() {
int cost[NODE][NODE]; //defind to store shortest distance from any node to any
node
for(int i = 0; i<NODE; i++)
for(int j = 0; j<NODE; j++)
cost[i][j] = costMat[i][j]; //copy costMatrix to new matrix
for(int k = 0; k<NODE; k++) {
for(int i = 0; i<NODE; i++) for(int j
=0; j<NODE; j++)
if(cost[i][k]+cost[k][j]< cost[i][j])
cost[i][j] = cost[i][k]+cost[k][j];
}
cout << "The matrix:" << endl;
for(int i = 0; i<NODE; i++) {
for(int j = 0; j<NODE; j++) cout
<<setw(3) << cost[i][j];
cout << endl;
}
}
int main() {
floydWarshal();
}

The strategy adopted by the Floyd-Warshall algorithm is Dynamic Programming.The


running time of the Floyd-Warshall algorithm is determined by the triply nested for loops of
lines 3-6. Each execution of line 6 takes O (1) time. The algorithm thus runs in time θ(n3 ).

6.Explain to Apply Floyd-Warshall algorithm for constructing the shortest path.

Example: Apply Floyd-Warshall algorithm for constructing the shortest path. Show that
matrices D(k) and π(k) computed by the Floyd-Warshall algorithm for the graph.

Solution:

Warshall's Algorithm(TRANSITIVE- CLOSURE)

Warshall's algorithm is used to determine the transitive closure of a directed graph or all
paths in a directed graph by using the adjacency matrix. For this, it generates a sequence of n
matrices. Where, n is used to describe the number of vertices.
R(0), ..., R(k-1), R(k), ... , R(n)

A sequence of vertices is used to define a path in a simple graph. In the kth matrix (R(k)), (r (k)),
the element's definition at the ith row and jth column will be one if it contains a path from v i to vj.
For all intermediate vertices, wq is among the first k vertices that mean 1 ≤ q ≤ k.

The R(0) matrix is used to describe the path without any intermediate vertices. So we can say
that it is an adjacency matrix. The R (n) matrix will contain ones if it contains a path between
vertices with intermediate vertices from any of the n vertices of a graph. So we can say that
it is a transitive closure.

Now we will assume a case in which rij(k) is 1 and r (k-1) is 0. ij This condition will be
true only if it contains a path from vi to vj using the vk. More specifically, the list ofvertices is in
the following form

vi, wq (where 1 ≤ q < k), vk. wq (where 1 ≤ q < k), vj


(k-1) (k-1)
The above case will be occur only if r = ikr = 1k.j Here, k is subscript.The rij (k)
will be one if and only if rij(k-1) = 1.
So in summary, we can say that

rij(k) = rij(k-1) or (rik(k-1) and rkj(k-1))

Now we will describe the algorithm of Warshall's Algorithm for computingtransitive

closure

Warshall(A[1...n, 1...n]) // A is the adjacency matrix


R(0) ← A
for k ← 1 to n do
for i ← 1 to n do
for j ← to n do
R(k)[i, j] ← R(k-1)[i, j] or (R(k-1)[i, k] and R(k-1)[k, j])
return R(n)
Here,

o Time efficiency of this algorithm is (n3)


o In the Space efficiency of this algorithm, the matrices can be written over their
predecessors.
o Θ(n3 ) is the worst-case cost. We should know that the brute force algorithm is better
thanWarshall's algorithm. In fact, the brute force algorithm is also faster for a space
graph.

Example of Transitive closure


In this example, we will consider two graphs. The first graph is described asfollows:
The matrix of this graph is described as follows:

TRANSITIVE- CLOSURE (G)


n ← |V[G]|
fori ← 1 to n
do for j ← 1 to n
do if i = j or (i, j) ∈ E [G]

the ←1

else ←0
for k ← 1 to n
do for i ← 1 to ndo
for j ← 1 to n

do (k
) ij ←
Return T(n).
UNIT IV ALGORITHM DESIGN TECHNIQUES

Dynamic Programming: Matrix-Chain Multiplication – Elements of Dynamic Programming –


Longest Common Subsequence- Greedy Algorithms: – Elements of the Greedy Strategy- An
Activity-Selection Problem - Huffman Coding.

1.define divide and conquer design technique


a. A problem’s instance is divided into several smaller instances of the same problem,ideally of
about the same size
b. The smaller instances are solved
c. If necessary, the solutions obtained for the smaller instances are combined to get asolution
to the original instance.
2. List out some of the stable and unstable sorting techniques.
Stable sorting techniques includes Bubble sort, Insertion sort, Selection sort, Merge sortand
Unstable sorting techniques includes Shell sort, Quick sort, Radix sort, Heap sort
3. Define Knapsack problem.
iven n items of known weights w1…wn and values v1…vn and knapsack of capacity
W. The aims is to find the most valuable subset if the items that fit into the knapsack. The exhaustive
search approach to knapsack problem leads to generating all the subsets of the set of n items given,
computing the total weight of each subset to identify feasible subsets and finding a subset of the largest
value among them.
4. Define merge sort.
The merge sort algorithm divides a given array A[0..n-1] by dividing it into two halves A[0.. n/2-1]
and A[ n/2-..n-1], sorting each of them recursively, and then merging the twosmaller sorted arrays into
a single sorted one.
1. Define quick sort
Quick sort employs a divide-and-conquer strategy. It starts by picking an element from the list to be
the "pivot." It then reorders the list so that all elements with values less than the pivot come before the
pivot, and all elements with values greater than the pivot come after it (a process often called
"partitioning"). It then sorts the sub-lists to the left and the right of the pivot using the same strategy,
continuing this process recursively until the whole list is sorted
2. What is a pivot element?
The pivot element is the chosen number which is used to divide the unsorted data intotwo halves. The
lower half contains less than value of the chosen number i.e. pivot element. The upper half contains
greater than value of the chosen number i.e. pivot element. So the chosen number is now sorted.
3. Define Binary Search
Binary search is a efficient algorithm for searching in a sorted array. It works by comparing a search
key K with the array‟s middle element A[m]. If they match, the algorithm stops; otherwise, the same
operation is repeated recursively for the first half of the array if K<a[m] and for the second half if
K>A[m]
4. Define dynamic programming.
Dynamic programming is a technique for solving problems with overlapping sub problems. Rather
than solving overlapping sub problems again and again, dynamic programming suggests solving each
of the smaller sub problems only once and recording the results in a table from which a solution to the
original problem can then be obtained.
5. Define Optimal Binary Search Tree (OBST). (June 06)
Dynamic programming can be used for constructing an optimal binary search tree for a given set of
keys and known probabilities of searching for them.
6. State greedy technique.The greedy approach suggests constructing a solution through a sequence of
steps, each expanding a partially constructed solution obtained so far, until a complete solution to
theproblem is reached.
7. Write down the optimization technique used for Warshall’s algorithm. State the rules and
assumptions which are implied behind that. Optimization technique
used in Warshall’s algorithm is Dynamic programming.
Dynamic programming is a technique for solving problems withoverlapping
sub problems. Typically, these sub problems arise from a recurrencerelating a solution to a
givenproblem with solutions to itssmaller sub problems of thesame type. Dynamic programming
suggests solving each smaller sub problem onceand recording the results in a tablefrom which a
solution to the original problem can bethen obtained.
8. Define objective function and optimal solution
To find a feasible solution that either maximizes or minimizes a given objective function. It has to be
the best choice among all feasible solution available on that step.
9. Define knapsack problem using dynamic programming.
Designing a dynamic programming algorithm for the knapsack problem: given n items of known
weights w1. . . wn and values v1, . . . , vn and a knapsack of capacity W, find the most valuable subset
of the items that fit into the knapsack. We assume here that all the weights and the knapsack capacity
are positive integers; the item values do not have to beintegers
10. Mention different algorithm design techniques
 Methods of specifying an algorithm
 Proving an algorithms correctness
 Analyzing an algorithm
 Coding an algorithm
11. Mention the two properties of sorting algorithms
 A sorting algorithm is called stable if it preserves the relative order of anytwoequal elements
in its input.
 An algorithm is said to be in place if it does not require extra memory
1. Define backtracking
The principal idea is to construct solutions one component at a time and evaluate such partially
constructed candidates as follows. If a partially constructed solution can bedeveloped further
without violating the problem’s constraints, it is done by taking the first remaining legitimate option
for the next component. If there is no legitimate optionfor the next component, no alternatives for
any remaining component need to beconsidered. In this case, the algorithm backtracks to replace
the last component of thepartially constructed.
2. Define state space tree
It is convenient to implement this kind of processing by constructing a tree of choices being made, called
the state-space tree. Its root represents an initial state before the search for a solution begins. The nodes of the
first level in the tree represent the choices made for the first component of a solution; the nodes of the second
level represent the choices for the second component, and so on
3. When a node in a state space tree is said to promising and non promising?
A node in a state-space tree is said to be promising if it corresponds to a
partially constructed solution that may still lead to a complete solution; otherwise, it is called non promising.
Leaves represent either non promising dead ends or complete solutions found by the algorithm
4. Define branch and bound method
Branch and bound is an algorithm that enhances the idea of generating a state space tree
with idea of estimating the best value obtainable from a current node of the decision tree
If such an estimate is not superior to the best solution seen up to that point in the
processing, the node is eliminated from further consideration

Dynamic Programming

Dynamic programming is a technique that breaks the problems into sub-problems, and saves
the result for future purposes so that we do not need to compute the result again. The sub
problems are optimized to optimize the overall solution is known as optimal substructure
property. The main use of dynamic programming is to solve optimization problems. Here,
optimization problems mean that when we are trying to find out the minimum or the
maximum solution of a problem. The dynamic programming guarantees to find the optimal
solution of a problem if the solution exists.

The definition of dynamic programming says that it is a technique for solving a complex
problem by first breaking into a collection of simpler sub problems, solving each
subproblem just once, and then storing their solutions to avoid repetitive computations.

Let's understand this approach through an example.

Consider an example of the Fibonacci series. The following series is theFibonacci

series:20, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ,…

The numbers in the above series are not randomly calculated. Mathematically, we could write
each of the terms using the below formula:

F(n) = F(n-1) + F(n-2),

With the base values F(0) = 0, and F(1) = 1. To calculate the other numbers, we follow the
above relationship. For example, F(2) is the sum f(0) and f(1), which is equal to 1.

How can we calculate F(20)?

The F(20) term will be calculated using the nth formula of the Fibonacci series. The below
figure shows that how F(20) is calculated.
As we can observe in the above figure that F(20) is calculated as the sum of F(19) and F(18).
In the dynamic programming approach, we try to divide the problem into the similar
subproblems. We are following this approach in the above case where F(20) into the similar
subproblems, i.e., F(19) and F(18). If we recap the definition of dynamic programming that
it says the similar subproblem should not be computed more than once. Still, in the above
case, the subproblem is calculated twice. In the above example, F(18) is calculated two
times; similarly, F(17) is also calculated twice. However, this technique is quite useful as it
solves the similar subproblems, but we need to be cautious while storing the results because
we are not particular about storing the result that we have computed once, then it can lead to
a wastage of resources.

How does the dynamic programming approach work?

The following are the steps that the dynamic programming follows:

o It breaks down the complex problem into simpler subproblems.


o It finds the optimal solution to these sub-problems.
o It stores the results of sub problems (memoization). The process of storingthe results
ofsub problems is known as memorization.
o It reuses them so that same sub-problem is calculated more than once.
o Finally, calculate the result of the complex problem.

The above five steps are the basic steps for dynamic programming. The dynamic
programming is applicable that are having properties such as:
Those problems that are having overlapping sub problems and optimal substructures. Here,
optimal substructure means that the solution of optimization problems can be obtained by
simplycombining the optimal solution of all the sub problems.

In the case of dynamic programming, the space complexity would be increased as we are
storingthe intermediate results, but the time complexity would be decreased.

1.Explain Top down and bottom up approaches in Dynamic programming with an example.
Approaches of dynamic programming

There are two approaches to dynamic programming:

o Top-down approach
o Bottom-up approach

Top-down approach

The top-down approach follows the memorization technique, while bottom-up approach
follows the tabulation method. Here memorization is equal to the sum of recursion and
caching. Recursion means calling the function itself, while caching means storing the
intermediate results.

Advantages
o It is very easy to understand and implement.
o It solves the sub problems only when it is required.
o It is easy to debug.

Disadvantages

It uses the recursion technique that occupies more memory in the call stack. Sometimes
when the recursion is too deep, the stack overflow condition will occur.

It occupies more memory that degrades the overall performance.

Let's understand dynamic programming through an example.


int fib(int n)
{
if(n<0)
error;
if(n==0)
return
0
;if(n==1)
return 1;
sum = fib(n-1) + fib(n-2);
}

In the above code, we have used the recursive approach to find out the Fibonacci series.
When the value of 'n' increases, the function calls will also increase, and computations will
also increase. In this case, the time complexity increases exponentially, and it becomes 2n.

One solution to this problem is to use the dynamic programming approach. Rather than
generating the recursive tree again and again, we can reuse the previously calculated value.
If we use the dynamic programming approach, then the time complexity would be O(n).

Bottom-Up approach

The bottom-up approach is also one of the techniques which can be used to implement the
dynamic programming. It uses the tabulation technique to implement the dynamic
programming approach. It solves the same kind of problems but it removes the recursion. If
we remove the recursion, there is no stack overflow issue and no overhead of the recursive
functions. In this tabulationtechnique, we solve the problems and store the results in a matrix.

There are two ways of applying dynamic programming:

o Top-Down
o Bottom-Up
The bottom-up is the approach used to avoid the recursion, thus saving the memory space. The
bottom-up is an algorithm that starts from the beginning, whereas the recursive algorithm
starts from the end and works backward. In the bottom-up approach, we start from the base
case to find the answer for the end. As we know, the base cases in the Fibonacci series are 0
and 1. Since the bottom approach starts from the base cases, so we will start from 0 and
1.Key points

o We solve all the smaller sub-problems that will be needed to solve the larger
sub- problems then move to the larger problems using smaller sub-problems.
o We use for loop to iterate over the sub-problems.
o The bottom-up approach is also known as the tabulation or table fillingmethod.

Let's understand through an example.

Suppose we have an array that has 0 and 1 values at a[0] and a[1] positions,respectively
shown as below:

Since the bottom-up approach starts from the lower values, so the values at a[0]and a[1]
are added to find the value of a[2] shown as below:

The value of a[3] will be calculated by adding a[1] and a[2], and it becomes 2 shown as
below:

The value of a[4] will be calculated by adding a[2] and a[3], and it becomes 3 shown as
below:

The value of a[5] will be calculated by adding the values of a[4] and a[3], and
itbecomes 5 shown as below:

The code for implementing the Fibonacci series using the bottom-up approach isgiven below:

int fib(int n)
{
int A[];
A[0] = 0, A[1] = 1;
for( i=2; i<=n; i++)
{
A[i] = A[i-1] + A[i-2]
}
return A[n];
}

In the above code, base cases are 0 and 1 and then we have used for loop to findother
values of Fibonacci series.

Fibonacci sequence

Fibonacci sequence is the sequence of numbers in which every next item is the total of
theprevious two items. And each number of the Fibonacci sequence is called Fibonacci
number.

Example: 0 ,1,1,2,3,5,8,13,21, ................................ is a Fibonacci sequence.

The Fibonacci numbers F_nare defined as follows:


F0 =
0Fn=1
Fn=F(n-1)+ F(n-2)
FIB (n)
1. If (n < 2)
2. then return n
3. else return FIB (n - 1) + FIB (n - 2)

Figure: shows four levels of recursion for the call fib (8):

Figure: Recursive calls during computation of Fibonacci number

A single recursive call to fib (n) results in one recursive call to fib (n - 1), two recursive calls
to fib (n - 2), three recursive calls to fib (n - 3), five recursive callsto fib (n - 4) and, in
general, Fk-1 recursive calls to fib (n - k) We can avoid this unneeded repetition by writing
down the conclusion of recursive calls and looking them up again if we need them later.
This process is called memorization.

Here is the algorithm with memorization


MEMOFIB (n)
1 if (n < 2)
2 then return n
3 if (F[n] is undefined)
4 then F[n] ← MEMOFIB (n - 1) + MEMOFIB (n - 2)
5 return F[n]
If we trace through the recursive calls to MEMOFIB, we find that array F [] gets filled from
bottom up. I.e., first F [2], then F [3], and so on, up to F[n]. We can replace recursion with a
simple for-loop that just fills up the array F [] in that order

ITERFIB (n)
1 F [0] ← 0
2 F [1] ← 1
3 for i ← 2 to n4
do
5 F[i] ← F [i - 1] + F [i - 2]
6 return F[n]

This algorithm clearly takes only O (n) time to compute Fn.


Matrix Chain Multiplication

It is a Method under Dynamic Programming in which previous output is taken as input for

next.Here, Chain means one matrix's column is equal to the second matrix's row [always].

In general:

If A = ⌊aij⌋ is a p x q matrixB =
⌊bij⌋ is a q x r matrix C = ⌊cij⌋
is a p x r

matrixThen

Given following matrices {A1,A2,A3,...An} and we have to perform the matrixmultiplication, which
canbe accomplished by a series of matrix multiplications

A1 xA2 x,A3 x ....... x An


Matrix Multiplication operation is associative in nature rather commutative. By this, we
mean that we have to follow the above matrix order for multiplication but we are free to
parenthesize the above multiplication depending upon our need.

In general, for 1≤ i≤ p and 1≤ j ≤ r

It can be observed that the total entries in matrix 'C' is 'pr' as the matrix is of dimension p x r
Also each entry takes O (q) times to compute, thus the total time to compute all possible
entries for the matrix 'C' which is a multiplication of 'A' and 'B' is proportional to the product
of the dimension p q r.

It is also noticed that we can save the number of operations by reordering the parenthesis.

Number of ways for parenthesizing the matrices:

There are very large numbers of ways of parenthesizing these matrices. If there aren items,
thereare (n-1) ways in which the outer most pair of parenthesis can place.

(A1) (A2,A3,A4, .................... An)


Or (A1,A2) (A3,A4 ..................... An)
Or (A1,A2,A3) (A4 ................... An)
Or(A1,A2,A3 ............... An-1) (An)
It can be observed that after splitting the kth matrices, we are left with two parenthesized
sequence of matrices: one consist 'k' matrices and another consist 'n- k' matrices.

Now there are 'L' ways of parenthesizing the left sublist and 'R' ways ofparenthesizing the
right sublist then the Total will be L.R:

Also p (n) = c (n-1) where c (n) is the nth Catalon number

c (n) =

On applying Stirling's formula we have

c (n) = Ω

Which shows that 4n grows faster, as it is an exponential function, then n1.5 .

Development of Dynamic Programming Algorithm


1. Characterize the structure of an optimal solution.
2. Define the value of an optimal solution recursively.
3. Compute the value of an optimal solution in a bottom-up fashion.
4. Construct the optimal solution from the computed information.

Dynamic Programming Approach

Let Ai,j be the result of multiplying matrices i through j. It can be seen that thedimension
of Ai,j is pi-1 x pj matrix.

Dynamic Programming solution involves breaking up the problems into


subproblems whose solution can be combined to solve the global problem.

At the greatest level of parenthesization, we multiply two matrices

A1.....n=A1....k x Ak+1. n)

Thus we are left with two questions:

o How to split the sequence of matrices?


o How to parenthesize the subsequence A1.....k andAk+1. ............ n?
One possible answer to the first question for finding the best value of 'k' is to check all possible
choices of 'k' and consider the best among them. But that it can be observed that checking all
possibilities will lead to an exponential number of total possibilities. It can also be noticed
that there exists only O (n2 ) different sequence of matrices, in this way do not reach the
exponential growth.

Step1: Structure of an optimal parenthesization: Our first step in the dynamic paradigm
is to find the optimal substructure and then use it to construct an optimal solution to the
problem froman optimal solution to sub problems.
Let Ai ... j where i≤ j denotes the matrix that results from evaluating the product

Ai Ai+1 ...Aj.

If i < j then any parenthesization of the product Ai Ai+1 Aj must split that the product between
Ak and Ak+1 for some integer k in the range i ≤ k ≤ j. That is for some value of k, we first
compute the matrices A i.....k & Ak+1....j and then multiply them together to produce the final
product Ai....j. The cost of computing Ai k plus
the cost of computing Ak+1....j plus the cost of multiplying them together is the cost of
parenthesization.

Step 2: A Recursive Solution: Let m [i, j] be the minimum number of scalar multiplication
needed to compute the matrixAi j.

If i=j the chain consist of just one matrix A i....i=Ai so no scalar multiplication are necessary
to compute the product. Thus m [i, j] = 0 for i= 1, 2, 3 n.

If i<j we assume that to optimally parenthesize the product we split it betweenA k and Ak+1
where i≤ k ≤j. Then m [i,j] equals the minimum cost for computing the subproducts Ai....k and
Ak+1. j+ cost of multiplying them together. We know Ai has dimension pi-1 x pi, so computing
the
product Ai....k and Ak+1. jtakes pi-1 pk pj scalar multiplication, we obtain

m [i,j] = m [i, k] + m [k + 1, j] + pi-1 pk pj

There are only (j-1) possible values for 'k' namely k = i, i+1.....j-1. Since the optimal
parenthesization must use one of these values for 'k' we need only check them all to find the
best.

So the minimum cost of parenthesizing the product Ai Ai+1 Aj becomes

To construct an optimal solution, let us define s [i,j] to be the value of 'k' at which we can
split the product Ai Ai+1 .....Aj To obtain an optimal parenthesization i.e. s[i, j] = k such that
m [i,j] = m [i, k] + m [k + 1, j] + pi-1 pk pj

3.Explain for the Matrix Chain Multiplication for the following sequence.

Example: We are given the sequence {4, 10, 3, 12, 20, and 7}. The matrices have
size 4 x 10, 10 x 3, 3 x 12, 12 x 20, 20 x 7. We need to compute M [i,j], 0 ≤ i, j≤ 5.We know
M[i, i] = 0 for all i.

Let us proceed with working away from the diagonal. We compute the optimalsolution
forthe product of 2 matrices.

Here P0 to P5 are Position and M1 to M5 are matrix of size (pi to pi-1) On

the basis of sequence, we make a formula

In Dynamic Programming, initialization of every method done by '0'.So weinitialize it by


'0'.Itwill sort out diagonally.
We have to sort out all the combination but the minimum output combination istaken
intoconsideration.

Calculation of Product of 2 matrices:


1. m (1,2) = m1 x m2
= 4 x 10 x 10 x 3
= 4 x 10 x 3 = 120
2. m (2, 3) = m2 x m3
= 10 x 3 x 3 x 12
= 10 x 3 x 12 = 360
3. m (3, 4) = m3 x m4
= 3 x 12 x 12 x 20
= 3 x 12 x 20 =
7204. m (4,5) = m4 x m5
= 12 x 20 x 20 x 7
= 12 x 20 x 7 = 1680

o We initialize the diagonal element with equal i,j value with '0'.
o After that second diagonal is sorted out and we get all the valuescorresponded

to itNow the third diagonal will be solved out in the same way.

Now product of 3 matrices:

M [1, 3] = M1 M2 M3

1. There are two cases by which we can solve this multiplication: ( M1 x M2) +M3,
M1+(M2x M3)
2. After solving both cases we choose the case in which minimum output isthere.

M [1, 3] =264
As Comparing both output 264 is minimum in both cases so we insert 264 in tableand ( M 1
xM2) + M3 this combination is chosen for the output making.

M [2, 4] = M2 M3 M4

1. There are two cases by which we can solve this multiplication: (M2xM3)+M4,
M2+(M3 x M4)
2. After solving both cases we choose the case in which minimum output isthere.

M [2, 4] = 1320

As Comparing both output 1320 is minimum in both cases so we insert 1320 in table and
M2+(M3 x M4) this combination is chosen for the output making.

M [3, 5] = M3 M4 M5

1. There are two cases by which we can solve this multiplication: ( M3 x M4) +M5,
M3+ (M4xM5)
2. After solving both cases we choose the case in which minimum output is there.

M [3, 5] = 1140

As Comparing both output 1140 is minimum in both cases so we insert 1140 intable and (
M3x M4) + M5this combination is chosen for the output making.

Now Product of 4 matrices:

M [1, 4] = M1 M2 M3 M4

There are three cases by which we can solve this multiplication:

1. ( M1 x M2 x M3) M4
2. M1 x(M2 x M3 x M4) 3.
(M1 xM2) x ( M3 x M4)

After solving these cases we choose the case in which minimum output is there

M [1, 4] =1080

As comparing the output of different cases then '1080' is minimum output, so we insert 1080
inthe table and (M1 xM2) x (M3 x M4) combination is taken out in output making,

M [2, 5] = M2 M3 M4 M5

There are three cases by which we can solve this multiplication:


1. (M2 x M3 x M4)x M5
2. M2 x( M3 x M4 x M5) 3.
(M2 x M3 )x ( M4 x M5)

After solving these cases we choose the case in which minimum output is there

M [2, 5] = 1350

As comparing the output of different cases then '1350' is minimum output, so we insert
1350 inthe table and M2 x( M3 x M4 xM5)combination is taken out in output making.

Now Product of 5 matrices:

M [1, 5] = M1 M2 M3 M4 M5

There are five cases by which we can solve this multiplication:

1. (M1 x M2 xM3 x M4 )x M5
2. M1 x( M2 xM3 x M4 xM5)
3. (M1 x M2 xM3)x M4 xM5
4. M1 x M2x(M3 x M4 xM5)
After solving these cases we choose the case in which minimum output is there

M [1, 5] = 1344

As comparing the output of different cases then '1344' is minimum output, so we insert 1344
inthe table and M1 x M2 x(M3 x M4 x M5 )combination is taken out in output making.

Final Output is:

Step 3: Computing Optimal Costs: let us assume that matrix Ai has dimension pi-1x pi for
i=1,2, 3....n. The input is a sequence (p0,p1, pn) where length [p] = n+1.
The procedure uses an auxiliary table m [1....n, 1 n] for storing m [i, j] costs an auxiliary table
s [1.....n, 1 ....n] that record which index of k achieved the optimal costs in computing m [i, j].

The algorithm first computes m [i, j] ← 0 for i=1, 2, 3.....n, the minimum costs for the
chain oflength 1.

MATRIX-CHAIN-ORDER (p)

n ← length[p]-1
for i ← 1 to n
do m [i, i] ← 0
for l ← 2 to n // l is the chain lengthdo
for i ← 1 to n-l + 1do j ← i+ l -1m[i,j] ←
∞ for k ← i to j-1
do q ← m [i, k] + m [k + 1, j] + pi-1 pk pjIf q
< m [i,j]
then m [i,j] ← q
s[i,j] ← k return
mand s.

PRINT-OPTIMAL-PARENS (s, i, j)
if i=j
then print "A"
else print "("
PRINT-OPTIMAL-PARENS (s, i, s [i, j]) PRINT-
OPTIMAL-PARENS (s, s [i, j] + 1, j)
print ")"
.Define Greedy Algorithms.

Greedy Algorithm

The greedy method is one of the strategies like Divide and conquer used to solve the
problems. This method is used for solving optimization problems. An optimization problem
is a problem that demands either maximum or minimum results. Let's understand through
some terms.

Components of Greedy Algorithm

The components that can be used in the greedy algorithm are:


o Candidate set: A solution that is created from the set is known as acandidate set.
o Selection function: This function is used to choose the candidate or subsetwhich
canbe added in the solution.
o Feasibility function: A function that is used to determine whether the candidate
orsubset can be used to contribute to the solution or not.
o Objective function: A function is used to assign the value to the solution or the
partialsolution.
o Solution function: This function is used to intimate whether the completefunction
hasbeen reached or not.

What are the applications of Greedy Algorithms?

Applications of Greedy Algorithm

o It is used in finding the shortest path.


o It is used to find the minimum spanning tree using the prim's algorithm orthe
Kruskal'salgorithm.
o It is used in a job sequencing with a deadline.
o This algorithm is also used to solve the fractional knapsack problem.

Pseudo code of Greedy Algorithm

Algorithm Greedy (a, n)

Solution : =
0;fori = 0 to n do
{
x: = select(a);

if feasible(solution, x)

Solution: = union(solution , x)
}

return solution;

}}

6.Explain Longest Common Subsequence with an example.

Longest Common Sequence (LCS)

A subsequence of a given sequence is just the given sequence with some elements left out.

Given two sequences X and Y, we say that the sequence Z is a common sequence of X and
Y if Z is a subsequence of both X and Y.

In the longest common subsequence problem, we are given two sequences X = (x1 x2 xm) and
Y = (y1 y2 yn) and wish to find a maximum length common subsequence of X and Y. LCS
Problem can be solved using dynamic programming.

Characteristics of Longest Common Sequence

A brute-force approach we find all the subsequences of X and check each subsequence to
see if it is also a subsequence of Y, this approach requires exponential time making it
impractical for the long sequence.

Given a sequence X = (x 1 x2.....xm) we define the ith prefix of X for i=0, 1, and 2...m as Xi=
(x1 x2 xi). For example: if X = (A, B, C, B, C, A, B, C) then X4= (A,
B, C, B)

Optimal Substructure of an LCS: Let X = (x1 x2....xm) and Y = (y1 y2 ) yn) be the sequences
and let Z = (z1 z2 zk ) be any LCS of X and Y.

o If x m = yn, then zk=x_m=yn and Zk-1 is an LCS of Xm-1and Yn-1


o If x m ≠ yn, then zk≠ xm implies that Z is an LCS of Xm-1and Y.
o If x m ≠ yn, then zk≠yn implies that Z is an LCS of X and Yn-1

Step 2: Recursive Solution: LCS has overlapping subproblems property because to find
LCS of X and Y, we may need to find the LCS of X m-1 and Yn-1. If xm ≠ yn, then we must
solve two subproblems finding an LCS of X and Yn-1.Whenever of
these LCS's longer is an LCS of x and y. But each of these subproblems has the
subproblems of finding the LCS of Xm-1 and Yn-1.
Let c [i,j] be the length of LCS of the sequence X iand Yj.If either i=0 and j =0, one of the
sequences has length 0, so the LCS has length 0. The optimal substructure ofthe LCS
problem given the recurrence formula

Step3: Computing the length of an LCS: let two sequences X = (x1 x2 xm) and
Y = (y1 y2..... yn) as inputs. It stores the c [i,j] values in the table c [0......m,0..........n].Table b
[1..........m, 1..........n] is maintained which help us to construct an optimal solution. c
[m, n]contains the length of an LCS of X,Y.

Algorithm of Longest Common Sequence LCS-

LENGTH (X, Y)
m ← length [X]
n← length [Y]
for i← 1 to m do
c [i,0]← 0
for j ← 0 to mdo
c [0,j] ← 0
for i← 1 to m do
for j← 1 to n do
if xi= yj
then c [i,j] ← c [i-1,j-1] + 1 b
[i,j] ← "↖ "
else if c[i-1,j] ≥ c[i,j-1]
then c [i,j] ← c [i-1,j]
b [i,j] ← "↑"
else c [i,j] ← c [i,j-1]
b [i,j] ← "←
"return c and b.
Example of Longest Common Sequence

Example: Given two sequences X [1...m] and Y [1 n]. Find the longest
Common subsequences to both.
here X = (A,B,C,B,D,A,B) and Y = (B,D,C,A,B,A)
m = length [X] and n = length [Y]m =
7 and n = 6
Here x1= x [1] = A y1= y [1] = Bx2=
By2= D
x3= C y3= C
x4= B y4= A
x5= D y5= B
x6= A y6= A
x7= B
Now fill the values of c [i, j] in m x n table
Initially, for i=1 to 7 c [i, 0] = 0
For j = 0 to 6 c [0, j] = 0

That is:
Now for i=1 and j = 1
x1 and y1 we get x1 ≠ y1 i.e. A ≠
BAndc [i-1,j] = c [0, 1] = 0
c [i, j-1] = c [1,0 ] = 0
That is, c [i-1,j]= c [i, j-1] so c [1, 1] = 0 and b [1, 1] = ' ↑ '

Now for i=1 and j = 2


x1 and y2 we get x1 ≠ y2 i.e. A ≠ Dc
[i-1,j] = c [0, 2] = 0
c [i, j-1] = c [1,1 ] = 0
That is, c [i-1,j]= c [i, j-1] and c [1, 2] = 0 b [1, 2] = ' ↑ '

Now for i=1 and j = 3


x1 and y3 we get x1 ≠ y3 i.e. A ≠ Cc
[i-1,j] = c [0, 3] = 0
c [i, j-1] = c [1,2 ] = 0
so c [1,3] = 0 b [1,3] = ' ↑ '

Now for i=1 and j = 4


x1 and y4 we get. x1=y4 i.e A = A
c [1,4] = c [1-1,4-1] + 1
= c [0, 3] + 1
=0+1=1
c [1,4] = 1
b [1,4] = ' ↖ '

Now for i=1 and j = 5


x1 and y5 we get x1 ≠ y5 c
[i-1,j] = c [0, 5] = 0
c [i, j-1] = c [1,4 ] = 1
Thus c [i, j-1] > c [i-1,j] i.e. c [1, 5] = c [i, j-1] = 1. So b [1, 5] = '←'
Now for i=1 and j = 6
x1 and y6 we get x1=y6
c [1, 6] = c [1-1,6-1] + 1
= c [0, 5] + 1 = 0 + 1 = 1
c [1,6] = 1b [1,6] = ' ↖ '
Now for i=2 and j = 1
We get x2 and y1 B = B i.e. x2= y1 c
[2,1] = c [2-1,1-1] + 1
= c [1, 0] + 1
=0+1=1
c [2, 1] = 1 and b [2, 1] = ' ↖ '
Similarly, we fill the all values of c [i, j] and we get

Example: Determine the LCS of (1,0,0,1,0,1,0,1) and (0,1,0,1,1,0,1,1,0).

Solution: let X = (1,0,0,1,0,1,0,1) and Y = (0,1,0,1,1,0,1,1,0).

We are looking for c [8, 9]. The following table is built.


From the table we can deduct that LCS = 6. There are several such sequences, for instance
(1,0,0,1,1,0) (0,1,0,1,0,1) and (0,0,1,1,0,1)

7.Explain Activity-Selection Problem with an example.


Activity Selection Problem

The activity selection problem is a mathematical optimization problem. Our first illustration
is the problem of scheduling a resource among several challenge activities. We find a greedy
algorithm provides a well designed and simple method for selecting a maximum- size set of
manually compatible activities.

Suppose S = {1, 2....n} is the set of n proposed activities. The activities share resources
which can be used by only one activity at a time, e.g., Tennis Court, Lecture Hall, etc. Each
Activity "i" has start time si and a finish time fi, where si ≤fi. If selected activity "i" take
place meanwhile the half-open time interval [si,fi).Activities i and j are compatible if the
intervals (si, fi) and [si, fi) do not overlap (i.e. i and j are compatible if s i ≥fi or si ≥fi). The
activity-selection problem chosen the maximum- size set of mutually consistent activities.

.
Algorithm Of Greedy- Activity Selector:
GREEDY- ACTIVITY SELECTOR (s, f)
1. n ← length [s]
2.A ← {1}
3. j ← 1.
4. for i ← 2 to n
5. do if si ≥ fj
6. then A ← A 𝖴 {i}
7. j←i
8. return A

Example: Given 10 activities along with their start and end time asS = (A1

A2A3 A4 A5 A6 A7 A8 A9 A10)
Si = (1,2,3,4,7,8,9,9,11,12)

fi = (3,5,4,7,10,9,11,13,12,14)

Compute a schedule where the greatest number of activities takes place.

Solution: The solution to the above Activity scheduling problem using a greedy strategy is
illustrated below:

Arranging the activities in increasing order of end time


Now, schedule A1

Next schedule A3 as A1 and A3 are non-interfering. Next

skip A2 as it is interfering.

Next, schedule A4 as A1 A3 and A4 are non-interfering, then next, schedule A6 asA1 A3 A4


andA6 are non-interfering.

Skip A5 as it is interfering.

Next, schedule A7 as A1 A3 A4 A6 and A7 are non-interfering. Next,

schedule A9 as A1 A3 A4 A6 A7 and A9 are non-interfering.Skip A8 as it

is interfering.
Next, schedule A10 as A1 A3 A4 A6 A7 A9 and A10 are non-interfering. Thus

the final Activity schedule is:

7.Explain in detail about the Huffman Coding with an example.

Huffman Codes

o (i) Data can be encoded efficiently using Huffman Codes.


o (ii) It is a widely used and beneficial technique for compressing data.
o (iii) Huffman's greedy algorithm uses a table of the frequencies of occurrences of
each character to build up an optimal way of representing each character as a binary
string.

Suppose we have 105 characters in a data file. Normal Storage: 8 bits per character (ASCII)
- 8 x 105 bits in a file. But we want to compress the file and save it compactly. Suppose only
six characters appear in the file:

How can we represent the data in a Compact way?

(i) Fixed length Code: Each letter represented by an equal number of bits. With afixed
lengthcode, at least 3 bits per character:

For example:

a 000

b 001

c 010

d 011

e 100

f 101
For a file with 105 characters, we need 3 x 105 bits.

(ii) A variable-length code: It can do considerably better than a fixed-length code, by giving
many characters short code words and infrequent character long codewords.

For example:

a 0

b 101

c 100

d 111

e 1101

f 1100
Number of bits = (45 x 1 + 13 x 3 + 12 x 3 + 16 x 3 + 9 x 4 + 5 x 4) x 1000
= 2.24 x 105bits

Thus, 224,000 bits to represent the file, a saving of approximately 25%.This is an optimal
character code for this file.

Prefix Codes:

The prefixes of an encoding of one character must not be equal to complete encoding of
another character, e.g., 1100 and 11001 are not valid codes because 1100 is a prefix of some
other code word is called prefix codes.

Prefix codes are desirable because they clarify encoding and decoding. Encoding is always
simple for any binary character code; we concatenate the code words describing each
character of the file. Decoding is also quite comfortable with a prefix code. Since no
codeword is a prefix of any other, the codeword that starts with an encoded data is
unambiguous.
Greedy Algorithm for constructing a Huffman Code:

Huffman invented a greedy algorithm that creates an optimal prefix code called a Huffman
Code.
The algorithm builds the tree T analogous to the optimal code in a bottom-up manner. It
starts with a set of |C| leaves (C is the number of characters) and performs |C| - 1 'merging'
operations to create the final tree. In the Huffman algorithm 'n' denotes the quantity of a set
of characters, z indicates the parent node, and x & y are the left & right child of z
respectively.

Algorithm of Huffman Code

Huffman (C)

z= allocate-Node ()
x= left[z]=Extract-Min(Q)

y=right[z] =Extract-Min(Q)

f [z]=f[x]+f[y]

Insert (Q, z)

return Extract-Min (Q)

Example: Find an optimal Huffman Code for the following set of frequencies:1.

a: 50 b: 25 c: 15 d: 40 e: 75Solution:
i.e.

Similarly, we apply the same process we get


Thus, the final output is:

UNIT V NP COMPLETE AND NP HARD

NP-Completeness: Polynomial Time – Polynomial-Time Verification – NP- Completeness and


Reducibility – NP-Completeness Proofs – NP-Complete Problems

5. On what basis problems are classified?


Problems are classified into two types based on time complexity. They arePolynomial (P) Problem
Non-Polynomial (NP) Problem
6. Define Polynomial (P) problem
Class P is a class of decision problems that can be solved in polynomial time by(deterministic)
algorithms. This class of problems is called polynomial.
7. Define Non Polynomial (NP) problem
Class NP is the class of decision problems that can be solved by nondeterministicpolynomial
algorithms. This class of problems is called nondeterministic polynomial
8. Give some examples of Polynomial problem
Selection sort Bubble Sort
String Editing Factorial
GraphColoring
9. Give some examples of Non-Polynomial problem
Travelling Salesman ProblemKnapsack
Problem.
10. How NP-hard problems are different from NP-Complete?
NP-hard : If an NP-hard problem can be solved in polynomial time, then all NP-complete problems can
be solved in polynomial time.
NP-Complete: A problem that is NP-complete has the property that it can be solved in polynomial time
if all other NP-complete problems can also be solved in polynomial time
11. Define decision problem
Any problem for which the answer is either zero or one is called a decisionproblem. An algorithm for a
decision problem is termed a Decision algorithm 13.Define optimization Problem
Any problem that involves the identification of an optimal (maximum or minimum) value of a given
cost function is known as an optimization problem. An Optimization algorithm is used to solve an
optimization problem

1.Explain P,NP,NP-Hard,NP-Complete with an example.


P, NP, NP-Hard, NP-Complete –DefinitionsThe Class P
P: the class of problems that have polynomial-time
deterministic algorithms.– That is, they are solvable in O(p(n)), where p(n) is apolynomial on n
– A deterministic algorithm is (essentially) one that always computes the correctanswer.
Sample Problems in P
• Fractional Knapsack
• MST
• Sorting
• Others?
The class NP
NP: the class of decision problems that are solvable in
polynomial time on a nondeterministic machine (or with a
nondeterministic algorithm)
(A determinstic computer is what we know)
A nondeterministic computer is one that can “guess” the right answer or solution. Note that
NP stands for “Nondeterministic Polynomial-time”
Sample Problems in NP
• Fractional Knapsack
• MST
• Others?
– Traveling Salesman
– Graph Coloring
– Satisfiability (SAT)
• the problem of deciding whether a given Boolean formula is satisfiableNP-hard
• What does NP-hard mean?
– A lot of times you can solve a problem by reducing it to
a different problem. I can reduce Problem B to Problem A if, given a solution toProblem
A, I can easily construct a solution to Problem B. (In this case,"easily" means "in
polynomial time.“).
• A problem is NP-hard if all problems in NP are polynomial time reducible to it,
Everyproblem in NP is reducible to HC in polynomial time. Ex:- TSP is reducibleto HC.
Example: lcm(m, n) = m * n / gcd(m, n)
NP-complete problems
• A problem is NP-complete if the problem is both
– NP-hard, and – NP.
Reduction
• A problem R can be reduced to another problem Q if any instance of R can be
rephrased to an instance of Q, the solution to which provides a solution to the instance of
R
– This rephrasing is called a transformation
• Intuitively: If R reduces in polynomial time to Q, R is “no harder to solve” than Q
• Example: lcm(m, n) = m * n / gcd(m, n), lcm(m,n)
problem is reduced to gcd(m, n) problem.

Summary
• P is set of problems that can be solved by a deterministic Turing machine
inPolynomial time.
• NP is set of problems that can be solved by a Non-deterministic Turing Machine in
Polynomial time. P is subset of NP (any problem that can be solved by deterministic
machine in polynomial time can also be solved by non-deterministic machine in polynomial
time) but P≠NP.
Classification

 Easy
 Medium
 Hard
 Hardest

And we can visualize their relationship, too:


Problem
Definition
sO(1) – constant-time
O(log_2(n)) – logarithmic-time
O(n)-linear-time
O(n^2) – quadratic-time
O(n^k) – polynomial-time
O(k^n) – exponential-time
O(n!) – factorial-time

Polynomial Algorithms

 All basic mathematical operations; addition, subtraction,


division,multiplication
 Testing for primacy
 Hashtable lookup, string operations, sorting problems
 Shortest Path Algorithms; Djikstra, Bellman-Ford, Floyd-Warshall
 Linear and Binary Search Algorithms for a given set of numbers

NP
Algorithm
sAmong them are:

 Integer Factorization and


 Graph
IsomorphismNP-Complete
Algorithms

As stated already, there are numerous problems proven to be complete.


Among them are:

 Traveling Salesman
 Knapsack, and
 Graph Coloring

NP-Hard Algorithms

 K-means Clustering
 Traveling Salesman Problem, and
 Graph
Coloring So, Does
P=NP?

A question that’s fascinated many computer scientists is whether or not allalgorithms

in NP belong to P:

Conclusion
P problems are quick to solve
NP problems are quick to verify but slow to solve
NP Complete problems are also quick to verify, slow to solve and can be reducedto any
otherNP Complete problem
NP Hard problems are slow to verify, slow to solve and can be reduced to any other NP
problem.
Difference between NP-Hard and NP-Complete:
NP-hard NP-Complete

NP-Hard problems(say X) can be


solved if and only if there is a NP- NP-Complete problems can be solved by a non-
Complete problem(say Y) that can be deterministic Algorithm/Turing Machine in
reducible into X in polynomial time. polynomial time.

To solve this problem, it do not haveto To solve this problem, it must be both NP
bein NP . and NP-hard problems.

Do not have to be a Decision


problem. It is exclusively a Decision problem.

Example: Halting problem, Example: Determine whether a graph has a


Vertexcover problem, etc. Hamiltonian cycle, Determine whether a

NP-hard NP-Complete

Boolean formula is satisfiable or not,


Circuit-satisfiability problem, etc.

Objectives
• P, NP, NP-Hard and NP-Complete
• Solving 3-CNF Sat problem
Trackable : Problems that can be solvable in a reasonable(polynomial) time. Intrackable :
Someproblems are intractable, as they grow large, we are unable tosolve them in reasonable
time.
Tractability
• What constitutes reasonable time?
– Standard working definition: polynomial time – On an input of size n the worst-case
runningtime is O(nk) for some constant k– O(n2), O(n3), O(1), O(n lg n), O(2n), O(nn),
O(n!)
– Polynomial time: O(n2), O(n3), O(1), O(n lg n)
– Not in polynomial time: O(2n), O(nn), O(n!)
• Are all problems solvable in polynomial time?
– No: Turing’s “Halting Problem” is not solvable by any computer, no matter howmuch
time isgiven.
Definition of NP class Problem: - The set of all decision-based problems came into the
division of NP Problems who can't be solved or produced an output within polynomial time
but verified in the polynomial time. NP class contains P class asa subset. NP problems
being hard to solve.

Note: - The term "NP" does not mean "not polynomial." Originally, the term meant "non-
deterministic polynomial. It means according to the one input number of output will be
produced

Definition of P class Problem: - The set of decision-based problems come into thedivision
of P Problems who can be solved or produced an output within polynomial time. P problems
being easy to solve

Define polynomial time.

Definition of Polynomial time: - If we produce an output according to the given input


within a specific amount of time such as within a minute, hours. This is known as
Polynomial time.
Define non polynomial time.

Definition of Non-Polynomial time: - If we produce an output according to the given input


but there are no time constraints is known as Non-Polynomial time. But yes output will
produce but time is not fixed yet.

Definition of Decision Based Problem: - A problem is called a decision problemif its


output is a simple "yes" or "no" (or you may need this of this as true/false, 0/1,
accept/reject.) We will phrase many optimization problems as decision problems. For
example, Greedy method, D.P., given a graph G= (V, E) if there exists any Hamiltonian
cycle.

Definition of NP-hard class: - Here you to satisfy the following points to come into the
divisionof NP-hard.

1. If we can solve this problem in polynomial time, then we can solve all
NPproblems inpolynomial time
2. If you convert the issue into one form to another form within the polynomialtime
Definition of NP-complete class: - A problem is in NP-complete, if

1. It is in NP
2. It is NP-hard

Pictorial representation of all NP classes which includes NP, NP-hard, and NP-
complete
Write short notes on:
i) Hamiltonian cycle with an example.
ii) Traveling Salesman problem.

Hamiltonian cycle problem:-

Consider the Hamiltonian cycle problem. Given an undirected graph G, does G have a cycle
that visits each vertex exactly once? There is no known polynomialtime algorithm for this
dispute.

Fig: Hamiltonian Cycle

Let us understand that a graph did have a Hamiltonian cycle. It would be easy
for someone to convince of this. They would similarly say: "the period is hv3,v7, v1. v13i.

We could then inspect the graph and check that this is indeed a legal cycle and that it visits
all ofthe vertices of the graph exactly once. Thus, even though we know of no efficient way to
solve the Hamiltonian cycle problem, there is a beneficial way to verify that a given cycle is
indeed a Hamiltonian cycle.

Definition of Certificate: - A piece of information which contains in the given path of a


vertex is known as certificate

Relation of P and NP classes

1. P contains in NP
2. P=NP
1. Observe that P contains in NP. In other words, if we can solve a problem in
polynomial time, we can indeed verify the solution in polynomial time. More
formally, we do not need to see a certificate (there is no need to specify the
vertex/intermediate of the specific path) to solve the problem; wecan explain it in
polynomial time anyway.
2. However, it is not known whether P = NP. It seems you can verify and produce an
output of the set of decision-based problems in NP classes in a polynomial time
which is impossible because according to the definition of NP classes you can verify
the solution within the polynomial time. So this relation can never be held.

Reductions:

The class NP-complete (NPC) problems consist of a set of decision problems (a subset of
class NP) that no one knows how to solve efficiently. But if there were a polynomial
solution for even a single NP-complete problem, then every problem inNPC will be solvable
in polynomial time. For this, we need the concept of reductions.

Suppose there are two problems, A and B. You know that it is impossible to solve
problem A in polynomial time. You want to prove that B cannot be explained in
polynomial time. We want to show that (A ∉ P) => (B ∉ P)

Consider an example to illustrate reduction: The following problem is well-knownto be NPC:

3- color: Given a graph G, can each of its vertices be labeled with one of 3 differentcolors
such that two adjacent vertices do not have the same label (color).

Coloring arises in various partitioning issues where there is a constraint that two objects
cannot be assigned to the same set of partitions. The phrase "coloring" comes from the
original application which was in map drawing. Two countries thatcontribute a common
border should becolored with different colors.
Fig: Example of 3-colorable and non-3-colorable graphs.

Polynomial Time Reduction:

We say that Decision Problem L1 is Polynomial time Reducible to decision Problem L2 (L1≤p
L2) if there is a polynomial time computation function f such thatof all x, xϵL1 if and only if
xϵL2.

Reductions

Concept: - If the solution of NPC problem does not exist then the conversion from one NPC
problem to another NPC problem within the polynomial time. For this, you need the concept
of reduction. If a solution of the one NPC problem exists within the polynomial time, then
the rest of the problem can also give the solution in polynomial time (but it's hard to
believe). For this, you need the concept of reduction.

Example: - Suppose there are two problems, A and B. You know that it is impossible to
solve problem A in polynomial time. You want to prove that B cannot be solved in
polynomial time. So you can convert the problem A into problem B in polynomial time.

3.Explain t h e N P-Complete problem with an example.

NP problem: - Suppose a DECISION-BASED problem is provided in which a setof


inputs/high inputs you can get high output.
Criteria to come either in NP-hard or NP-complete.

1. The point to be noted here, the output is already given, and you can verify the
output/solution within the polynomial time but can't produce an output/solution in
polynomial time.
2. Here we need the concept of reduction because when you can't produce an output of
the problem according to the given input then in case you have to use an emphasis on
the concept of reduction in which you can convert one problem into another problem.

So according to the given decision-based NP problem, you can decide in the form of yes or
no. If, yes then you have to do verify and convert into another problem via reduction
concept. If you are being performed, both then decision-based NP problems are in NP
compete.

Here we will emphasize NPC.

CIRCUIT SAT

According to given decision-based NP problem, you can design the CIRCUIT andverify a
givenmentioned output also within the P time. The CIRCUIT is providedbelow:-

SAT (Satisfiability):-

A Boolean function is said to be SAT if the output for the given value of the inputis

true/high/1 F=X+YZ (Created a Boolean function by CIRCUIT SAT)

These points you have to be performed for NPC

Keep Watching

1. CONCEPTS OF SAT
2. CIRCUIT SAT≤ρ SAT
3. SAT≤ρ CIRCUIT SAT
4. SAT ϵ NPC

1. CONCEPT: - A Boolean function is said to be SAT if the output for the given value
of the input is true/high/1.
2. CIRCUIT SAT≤ρ SAT: - In this conversion, you have to convert CIRCUIT SAT into
SAT within the polynomial time as we did it
3. SAT≤ρ CIRCUIT SAT: - For the sake of verification of an output you haveto
convert SAT into CIRCUIT SAT within the polynomial time, and throughthe
CIRCUIT SAT you can get the verification of an output successfully.SAT ϵ NPC: -
As you know very well, you can get the SAT throughCIRCUIT SAT that comes
from NP.

Proof of NPC: - Reduction has been successfully made within the polynomial time from
CIRCUIT SAT TO SAT. Output has also been verified within the polynomial time as you
did inthe above conversation.

So concluded that SAT ϵ NPC.

Describe in detail about

3CNF SAT diagram.

3CNF SAT

Concept: - In 3CNF SAT, you have at least 3 clauses, and in clauses, you willhave
almost 3 literals or constants

Such as (X+Y+Z) (X+Y+Z)


(X+Y+Z)
You can define as (XvYvZ) ᶺ ) ᶺ (XvYvZ) V=OR
(XvYvZoperator
^ =AND operator
These all the following points need to be considered in 3CNF SAT.To prove: -
1. Concept of 3CNF SAT
2. SAT≤ρ 3CNF SAT
3. 3CNF≤ρ SAT
4. 3CNF ϵ NPC

1. CONCEPT: - In 3CNF SAT, you have at least 3 clauses, and in clauses, you
willhave almost 3 literals or constants.
2. SAT ≤ρ 3CNF SAT:- In which firstly you need to convert a Boolean
functioncreated in SAT into 3CNF either in POS or SOP form within
thepolynomial time
F=X+YZ
= (X+Y) (X+Z)
= (X+Y+ZZ') (X+YY'+Z)
= (X+Y+Z) (X+Y+Z') (X+Y+Z) (X+Y'+Z)
= (X+Y+Z) (X+Y+Z') (X+Y'+Z)
3. 3CNF ≤p SAT: - From the Boolean Function having three literals we canreduce
thewhole function into a shorter one.
F= (X+Y+Z) (X+Y+Z') (X+Y'+Z)
= (X+Y+Z) (X+Y+Z') (X+Y+Z) (X+Y'+Z)
= (X+Y+ZZ') (X+YY'+Z)
= (X+Y) (X+Z)
= X+YZ
4. 3CNF ϵ NPC: - As you know very well, you can get the 3CNF through SATand
SATthrough CIRCUIT SAT that comes from NP.

Proof of NPC:-
1. It shows that you can easily convert a Boolean function of SAT into 3CNFSAT
and satisfied the concept of 3CNF SAT also within polynomial time through
Reduction concept.
2. If you want to verify the output in 3CNF SAT then perform the Reductionand
convertinto SAT and CIRCUIT also to check the output

If you can achieve these two points that means 3CNF SAT also in NPCClique

To Prove: - Clique is an NPC or not?

For this you have to satisfy the following below-mentioned points: -

1. Clique
2. 3CNF ≤ρ Clique
3. Clique ≤ρ 3CNF≤SAT
4. Clique ϵ NP

1) Clique
Definition: - In Clique, every vertex is directly connected to another vertex, andthe
number of vertices in the Clique represents the Size of Clique.
CLIQUE COVER: - Given a graph G and an integer k, can we find k subsets ofverticesV 1,
V2...VK, such that UiVi = V, and that each Vi is a clique of G.The following figure shows a
graph that has a clique cover of size 3.

2) 3CNF ≤ρ Clique
Proof:-For the successful conversion from 3CNF to Clique, you have to follow thetwo

steps:- Draw the clause in the form of vertices, and each vertex represents the literals

ofthe clauses.

1. They do not complement each other


2. They don't belong to the same clause In the conversion, the size of the Clique and
size of 3CNF must be the same, and you successfully converted3CNF into Clique
within the polynomial time

Clique ≤ρ 3CNF

Proof: - As you know that a function of K clause, there must exist a Clique of size
k. It means that P variables which are from the different clauses can assign the same value
(say it is 1). By using these values of all the variables of the CLIQUES,you can make the
value of eachclause in the function is equal to 1

Example: - You have a Boolean function in 3CNF:-


(X+Y+Z) (X+Y+Z') (X+Y'+Z)

After Reduction/Conversion from 3CNF to CLIQUE, you will get P variables suchas: - x
+y=1,x +z=1 and x=1 Put the value of P variables in equation (i)(1+1+0)(1+0+0)(1+0+1)
(1)(1)(1)=1 output verified

4) Clique ϵ NP:-

Proof: - As you know very well, you can get the Clique through 3CNF and to convert the
decision-based NP problem into 3CNF you have to first convert into SAT and SAT comes
from NP.

So, concluded that CLIQUE belongs to NP.

Proof of NPC:-

1. Reduction achieved within the polynomial time from 3CNF to Clique


2. And verified the output after Reduction from Clique To 3CNF above So, concluded
that, if both Reduction and verification can be done within the polynomial time that
means Clique also in NPC.

Explain the Vertex Cover problem and Subset Cover problem.

Vertex Cover
1. Vertex Cover Definition
2. Vertex Cover ≤ρ Clique
3. Clique ≤ρ Vertex Cover
4. Vertex Cover ϵ NP

1) Vertex Cover:

Definition: - It represents a set of vertex or node in a graph G (V, E), which gives the
connectivity of a complete graph

According to the graph G of vertex cover which you have created, the size of Vertex
Cover =2

2) Vertex Cover ≤ρ Clique


In a graph G of Vertex Cover, you have N vertices which contain a Vertex Cover
K. There must exist of Clique Size of size N-K in its complement.

According to the graph G, you have


Number of vertices=6
Size of Clique=N-K=4

You can also create the Clique by complimenting the graph G of Vertex Cover means in
simpler form connect the vertices in Vertex Cover graph G through edges where edges don?t
exist and remove all the existed edges

You will get the graph G with Clique Size=4

3) Clique ≤ρ Vertex Cover


Here through the Reduction process, you can get the Vertex Cover form Clique by just
complimenting the Clique graph G within the polynomial time.

4) Vertex Cover ϵ NP

As you know very well, you can get the Vertex Cover through Clique and to convert the
decision-based NP problem into Clique firstly you have to convert into 3CNF and 3CNF
into SAT and SAT into CIRCUIT SAT that comes from NP.

Proof of NPC:-Reduction from Clique to Vertex Cover has been made within the
polynomial time. In the simpler form, you can convert into Vertex Cover from
Cliquewithin the polynomial time
1. And verification has also been done when you convert Vertex Cover to Clique and
Clique to 3CNF and satisfy/verified the output within a polynomial time also, so it
concluded that Reduction and Verification had been done in the polynomial time that
means Vertex Cover also comes in NPC

Subset Cover

To Prove:-

1. Subset Cover
2. Vertex Cover ≤ρ Subset Cover
3. Subset Cover≤ρ Vertex Cover
4. Subset Cover ϵ NP
1) Subset Cover

Definition: - Number of a subset of edges after making the union for a get all theedges of
thecomplete graph G, and that is called Subset Cover.

According to the graph G, which you have created the size of Subset Cover=2

1. v1{e1,e6} v2{e5,e2} v3{e2,e4,e6} v4{e1,e3,e5} v5{e4} v6{e3}


2. v3Uv4= {e1, e2, e3, e4, e5, e6} complete set of edges after the union of vertices.

2) Vertex Cover ≤ρ Subset Cover

In a graph G of vertices N, if there exists a Vertex Cover of size k, then there must also exist
a Subset Cover of size k even. If you can achieve after the Reduction from Vertex Cover to
Subset Cover within a polynomial time, which means you did right.

3) Subset Cover ≤ρ Vertex Cover

Just for verification of the output perform the Reduction and create Clique and via an
equation, N-K verifies the Clique also and through Clique you can quickly generate 3CNF
and after solving the Boolean function of 3CNF in the polynomial time. You will get output.
It means the output has been verified.

4) Subset Cover ϵ NP:-

Proof: - As you know very well, you can get the Subset-Cover through Vertex Cover and
Vertex Cover through Clique and to convert the decision-based NP problem into Clique
firstly you have to convert into3CNF and 3CNF into SAT and SAT into CIRCUIT SAT that
comes from NP.

Proof of NPC:-

The Reduction has been successfully made within the polynomial time form Vertex Cover
to Subset Cover

Output has also been verified within the polynomial time as you did in the above
conversation so, concluded that SUBSET COVER also comes in NPC.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy