DSC++ Unit-V
DSC++ Unit-V
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.
A Graph G(V, E) with 5 vertices (A, B, C, D, E) and six edges ((A,B), (B,C),
(C,E), (E,D), (D,B), (D,A)) is shown in the following figure.
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 called as neighbours or adjacent nodes.
Degree of the Node
A degree of a node is the number of edges that are connected with that
node. A node with degree 0 is called as isolated node.
Graph Representation
By Graph representation, we simply mean the technique which is to be
used in order 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
this tutorial, we discuss each one of them in detail.
1. Sequential Representation
In sequential representation, we use adjacency matrix to store the
mapping represented by vertices and edges. In adjacency matrix, the
rows and columns are represented by the graph vertices. A graph having
n vertices, will have a dimension n x n.
An entry Mij in the adjacency matrix representation of an undirected graph G
will be 1 if there exists an edge between Vi and Vj.
An undirected graph and its adjacency matrix representation is shown in the
following figure.
in the above figure, we can see the mapping among the vertices (A, B, C,
D, E) is represented by using the adjacency matrix which is also shown in
the figure.
There exists different adjacency matrices for the directed and undirected
graph. In directed graph, an entry Aij will be 1 only when there is an edge
directed from Vi to Vj.
Linked Representation
In the linked representation, an adjacency list is used to store the Graph into
the computer's memory.
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 the node 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 the 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.
In this part of the tutorial we will discuss the techniques by using which, we can
traverse 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.
Algorithm
Step 1: SET STATUS = 1 (ready
state) for each node in G
Step 2: Enqueue the starting
node A and set its STATUS = 2
(waiting state)
Step 3: Repeat Steps 4
and 5 until QUEUE is empty
Step 4: Dequeue a node N.
Process it and set its STATUS =
3
(processed state).
Step 5: Enqueue all the
neighbours of N that are in the
ready state
(whose STATUS = 1) and
set their STATUS = 2
(waiting state)
[END OF LOOP]
Step 6: EXIT
Depth First Search (DFS) Algorithm
Depth first search (DFS) algorithm starts with the initial node of the graph G,
and then goes to deeper and deeper until we find the goal node or the node
which has no children. The algorithm, then backtracks from the dead end
towards the most recent node that is yet to be completely unexplored.
The data structure which is being used in DFS is stack. The process is
similar to BFS algorithm. In DFS, the edges that leads to an unvisited node
are called discovery edges while the edges that leads to an already visited
node are called block edges.
Algorithm
Step 1: SET STATUS = 1 (ready state) for each node in G
Step 2: Push the starting node A on the stack and set its STATUS = 2 (waiting
state)
Step 3: Repeat Steps 4 and 5 until STACK is empty
Step 4: Pop the top node N. Process it and set its STATUS = 3 (processed
state)
Step 5: Push on the stack all the neighbours of N that are in the ready state
(whose STATUS
= 1) and set their
STATUS = 2 (waiting
state) [END OF LOOP]
Step 6: EXIT
Binary Search Trees:
In a binary tree, every node can have a maximum of two children but there
is no need to maintain the order of nodes basing on their values. In binary
tree, the elements are arranged in the order they arrive to the tree from
top to bottom and left to right.
A binary tree has the following time complexities...
To enhance the performance of binary tree, we use special type of binary tree
known as Binary Search Tree
In this tree, left subtreeyof ever node contains nodes with smaller values and
right subtree of
every node contains larger values.
Example
Construct a Binary Search Tree by inserting the followings
...
sequence of number
10,12,5,4,20,8,7,15 and 13
Above elements are inserted into a Binary Search Tree as
follows...
Deletion Operation in BST
We use the following steps to delete a node with two children from BST...
Step 1 - Find the node to be deleted using search operation
Step 2 - If it has two children, then find the largest node in its
left subtree (OR) the smallest node in its right subtree.
Step 3 - Swap both deleting node and node which is found in the above
step.
Step 4 - Then check whether deleting node came to case 1 or case 2or else
goto step 2
Step 5 - If it comes to case 1, then delete using case 1 logic.
Step 6- If it comes to case 2, then delete using case 2 logic.
Step 7 - Repeat the same process until the node is deleted from the tree.
AVL tree
In early 60’s of 19th century E.M. Landis and G.M. Adelson- Velsky formed a
self - balancing BST (binary search tree) data structure. This data structure is
known by AVL tree.
An AVL tree is a binary search tree with self – balancing condition. The
condition assures that the difference between the height of left and right
sub tree cannot be greater than one. This difference between left sub tree
and right sub tree is known as Balance Factor
Balance Factor= Height of Left Subtree - Height of
Right Subtree
If the value of balance factor is greater than one then the tree is balanced
using some rotational techniques and these rotational techniques are known
as AVL rotation.
In above techniques the first two are single rotations and next two are double
rotations.
Left Rotation
If the node is inserted into the right subtree of the right subtree then we can
Right Rotation
If the node is inserted into the left subtree of the leftsubtree then we can
perform the right rotation to balance the unbalanced tree.
In above figure we can see the right rotation of the tree. Using this technique
we balance an unbalance tree of height 2.
The children of a red node are black. It can be possible that parent
of red node is black node.
All the leaves have the same black depth.
Every simple path from the root node to the (downward) leaf node
contains the same number of black nodes.
While representing the red black tree color of each node should be shown.
In this tree leaf nodes are simply termed as null nodes which means they
are not physical nodes. It can be checked easily in the above-given tree
there are two types of node in which one of them is red and another one is
black in color. The above-given tree follows all the properties of a red black
tree that are
It is a binary search tree.
The root node is black.
The children’s of red node are black.
All the root to external node paths contain same number of black nodes.
Example: Consider path 75-90-80-88-null and 75-40-30-null in both these
paths 3 black nodes are there.
Binary
O(log O(log O(log n) O(n) O(n) O(n)
Search Tree n) n)