ADS ALL 5 UNIT NOTES Final
ADS ALL 5 UNIT NOTES Final
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.
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
Write an algorithm for Towers of Hanoi problem using recursion and analyze the complexity (No of disc-4).
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.
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.
7. The Algorithm gives a clear description of requirements and goal of the problem to
thedesigner.
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.
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.
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.
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.
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.
1. Time Complexity
Case Time Complexity
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)
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.
Techniques.
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.
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.
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
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
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
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
For Example:
3n+2= θ (n) as 3n+2≥3n and 3n+2≤ 4n, for n
k1=3,k2=4, and n0=2
4.What is recurrence relation? Explain substitution method and recursion tree methods.
Recurrence Relation
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
1.Substitution Method:
T (n) = T +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
T (n) = 2T + n n>1
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
T (n) = 4T +n
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
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?
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 2: Squeeze the lemon as much you can and take out its juice in a container.Step 3: Add
Step 5: When sugar gets dissolved, add some water and ice in it. Step 6:
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.
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:
Step 4: Add the values of a and b and store the result in the sum variable,
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:
efficiencyApproaches of Algorithm
The following are the approaches used after considering both the theoretical and
practical importance of designing an algorithm:
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
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.
Types of Algorithms
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.
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.
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.
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
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.
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.
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 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 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.
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.
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.
As we know that, n
= 2h+1 -1
n+1 =
2h+1
log2(n+1) = log2(2h+1 )
log2(n+1)
= h+1
h = log2(n+1) - 1
As we know that, n
= h+1
h= n-1
In the above tree, we can observe that each node is either containing zero or two children;
therefore, it is a 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.
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.
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.
The degenerate binary tree is a tree in which all the internal nodes have only one children.
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.
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.
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.
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.
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.
Write a C++ Program for the binary tree traversals and creation of binary
searchtree given an example.
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 -
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.
#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
Algorithm
Postorder traversal
Algorithm
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);
if(temp!=NULL)
postorder(temp->left);
postorder(temp-
>right); cout<<"
"<<temp->data;
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.
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 -
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.
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.
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 -
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.
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
Insertion O(n)
Deletion O(n)
Search O(n)
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.
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
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
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:
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.
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.
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
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.
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.
3. Insert E
4. Insert C, F, D
5. Insert G
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.
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
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.
A tree T is an almost red-black tree (ARB tree) if the root is red, but otherconditions
above hold.
Operations on RB Trees:
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
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
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.
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.
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.
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.
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.
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}
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
:=Find(x)
y := Find(y)
ifx = y then
(x, y) := (y, x)
end if
6.Explain Fibonacci Mergeable heap operations and decreasing a key and deleting a node.
y.parent := x
end function
FIBONACCI HEAPS
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
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.
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.
Cascading-Cut
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.
Decrease 35 to 5
2. Cut part: Since and 5<its parent, cut it and add it to the root list.
26 ≠ nill
7. Mark 5 as min
UNIT III GRAPHS
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 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.
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.
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:
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:
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:
Algorithm
Input − The start vertex u, An array to keep track of which node is visited or not. Astack to
store nodes.
Begin
mark u as visited
topoSort(c, visited,
stack)done
End
performTopologicalSorting(Graph)
Input − The given directed acyclic graph.Output
− Sequence of nodes.
Begin
initially mark all nodes as unvisitedfor all
if v is not visited,
thentopoSort(i, visited,
stack)
done
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}
};
if(graph[u][v])
uif(!visited[v])
topoSort(v, visited, stk);
void performTopologicalSort()
stack<int> stk;
bool vis[NODE];
while(!stk.empty())
{
cout << stk.pop() << " ";
}
main()
performTopologicalSort()
Output
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.
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.
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
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) !!!
}
}
What is DFSA?
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 :
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
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
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.
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.
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
So, a spanning tree is a subset of connected graph G, and there is no spanning tree of a
disconnected graph.
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.
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 -
A minimum spanning tree can be found from a weighted graph by using the algorithms
givenbelow -
o Prim's Algorithm
o Kruskal's Algorithm
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.
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 -
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.
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.
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.
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.
1. - ∞
2. Not less than 0.
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
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:
= (0 + 4) < ∞
=4<∞
Therefore, we come to the conclusion that the formula for calculating the distance between
thevertices:
= (0 + 8) < ∞
=8<∞
Algorithm
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.
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
=0 + 6 = 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
=0 + 4 = 4
ЄG.V
v.d := ∞ v.∏
:= NIL
s.d := 0
for i = 1 to |G.V| - 1
v.∏ := u
FALSE
return TRUE
5. Explain DAG WITH AN EXAMPLE.
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 [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
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}.
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}.
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();
}
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 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
closure
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
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.
The numbers in the above series are not randomly calculated. Mathematically, we could write
each of the terms using the below formula:
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.
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.
The following are the steps that the dynamic programming follows:
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
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.
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.
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.
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.
Figure: shows four levels of recursion for the call fib (8):
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.
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]
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
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.
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.
Now there are 'L' ways of parenthesizing the left sublist and 'R' ways ofparenthesizing the
right sublist then the Total will be L.R:
c (n) =
c (n) = Ω
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.
A1.....n=A1....k x Ak+1. n)
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
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.
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.
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.
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.
M [1, 4] = M1 M2 M3 M4
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
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.
M [1, 5] = M1 M2 M3 M4 M5
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.
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.
Solution : =
0;fori = 0 to n do
{
x: = select(a);
if feasible(solution, x)
Solution: = union(solution , x)
}
return solution;
}}
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.
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.
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.
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] = ' ↑ '
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)
Solution: The solution to the above Activity scheduling problem using a greedy strategy is
illustrated below:
skip A2 as it is interfering.
Skip A5 as it is interfering.
is interfering.
Next, schedule A10 as A1 A3 A4 A6 A7 A9 and A10 are non-interfering. Thus
Huffman Codes
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:
(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.
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)
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.
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
Polynomial Algorithms
NP
Algorithm
sAmong them are:
Traveling Salesman
Knapsack, and
Graph Coloring
NP-Hard Algorithms
K-means Clustering
Traveling Salesman Problem, and
Graph
Coloring So, Does
P=NP?
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
To solve this problem, it do not haveto To solve this problem, it must be both NP
bein NP . and NP-hard problems.
NP-hard NP-Complete
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
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.
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.
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.
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)
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.
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.
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.
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
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.
3CNF SAT
Concept: - In 3CNF SAT, you have at least 3 clauses, and in clauses, you willhave
almost 3 literals or constants
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
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.
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
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.
Proof of NPC:-
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
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
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
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.
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.
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.