0% found this document useful (0 votes)
19 views87 pages

Module 5

hashing
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views87 pages

Module 5

hashing
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 87

MODULE 5

GRAPHS
Abstract data type

• ADT is a set of operations


• ADT specifies what is to be done but how it is done is not specified i.e
all implementation details are hidden
Graphs

• A Graph is a non-linear data structure consisting of vertices and edges.

• The vertices are sometimes also referred to as nodes and the edges are
lines or arcs that connect any two nodes in the graph.

• More formally a Graph is composed of a set of vertices( V ) and a set


of edges( E ). The graph is denoted by G(E, V).
Directed and Undirected Graph

• A graph can be directed or undirected.


• However, in an undirected graph, edges are not associated with the
directions with them.
• An undirected graph is shown in the above figure since its edges are
not attached with any of the directions.
• If an edge exists between vertex A and B then the vertices can be
traversed from B to A as well as A to B.
• In a directed graph, edges form an ordered pair. Edges represent a
specific path from some vertex A to another vertex B. Node A is called
initial node while node B is called terminal node.
Graph Terminology
Path
• A path can be defined as the sequence of nodes that are
followed in order to reach some terminal 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. A path will be closed path if V0=VN.
Simple Path
• If all the nodes of the graph are distinct with an exception
V0=VN, then such path P is called as closed simple path.
Cycle
• A cycle can be defined as the path which has no repeated
edges or vertices except the first and last vertices.
Graph Terminology
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.
Graph Terminology
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 neighbors 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.
Degree of a Vertex
Degree of vertex is the number of lines associated with a vertex. For example, let us consider the above
graph. Degree of a vertex A is 1. Degree of a vertex B is 4. Degree of a vertex C is 2.

Indegree of a Vertex
It is the number of arcs entering the vertex. For example, let us consider the above graph. Indegree of
vertex B is 1.

Outdegree of Vertex
It is the number of arcs leaving the vertex. For example, let us consider the above graph. Outdegree of
vertex B is 3.
Graph representation

• Adjacency matrix representation(sequential representation)

• Adjacency list representation(adjacency list representation)


Adjacency matrix representation(sequential representation)
• We can use an adjacency matrix to represent the undirected graph, directed
graph, weighted directed graph, and weighted undirected graph.
• If adj[i][j] = w, it means that there is an edge exists from vertex i to vertex j with
weight w.
• An entry Aij in the adjacency matrix representation of an undirected graph G will
be 1 if an edge exists between Vi and Vj.
• If an Undirected Graph G consists of n vertices, then the adjacency matrix for
that graph is n x n, and the matrix A = [aij] can be defined as -
• aij = 1 {if there is a path exists from Vi to Vj}
• aij = 0 {Otherwise}
• If there is no self loop present in the graph ,it means that the diagonal entries of
the adjacency matrix will be 0.
Adjacency matrix for a directed graph

• In a directed graph, edges represent a specific path from one vertex to


another vertex. Suppose a path exists from vertex A to another vertex
B; it means that node A is the initial node, while node B is the terminal
node.
• In the below graph, we can see there is no self-loop, so the diagonal
entries of the adjacent matrix are 0.
Adjacency matrix for a weighted directed graph
• It is similar to an adjacency matrix representation of a
directed graph except that instead of using the '1' for
the existence of a path, here we have to use the weight
associated with the edge.
• The weights on the graph edges will be represented as
the entries of the adjacency matrix.
Linked list representation
• An adjacency list is used in the linked representation to store the
Graph in the computer's memory. It is efficient in terms of storage as
we only have to store the values for edges.
• 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 the last node of the list.
• The sum of the lengths of adjacency lists is equal to twice the number
of edges present in an undirected graph.
For a directed graph, the sum of the lengths of adjacency lists is equal to the number
of edges present in the graph.
Elementary Graph Operations

• DFS(Depth First Search)

• BFS(Breadth First Search)

• Connected components

• Spanning trees

• Biconnected components
DFS(Depth First Search)
• Visit all the vertices up to the deepest level and continue.
• DFS uses stack for its implementation.
Procedure
1.Select any vertex as starting vertex and insert it on to the top of stack and
make status as visited.
2.While stack is not empty we have to perform the following 2 steps
i)select any unvisited adjacent vertex of the corresponding top vertex and
push into on the stack &make the status as visited.
ii)if there is no unvisited vertex then perform backtracking and then pop
element from the stack.
void dfs(int source) {
int v, top = -1;
s[++top] = 1;
b[source] = 1;
for (v = 1; v <= n; v++)
{ if (a[source][v] == 1 && b[v] ==
0)
{
printf("\n%d -> %d", source, v);
dfs(v); }
DFS
Algorithm
1.Push starting vertex & print
2.while(stack not empty)
{
i.P=TOP()
ii.push(print)only one adjacent vertex of P
If no valid vertex available,POP()
}
Breadth first search(BFS)

• Level by level searching

• Level at 0 are visited first then level 1,2….n

• Queue data structures are used to implement BFS


Breadth first search(BFS)

• Breadth-first search is a graph traversal algorithm that starts traversing


the graph from the root node and explores all the neighboring nodes.
Then, it selects the nearest node and explores all the unexplored nodes.
While using BFS for traversal, any node in the graph can be
considered as the root node.
BFS Algorithm
1.Select any vertex has the starting vertex, insert that vertex into the
queue &make the status of that vertex has visited.

2.While the queue is not empty

a.delete a vertex from the queue & display it(we are visiting that vertex).
b. Insert all the unvisited adjacent vertices of that vertex(deleted vertex) into the

queue &make the status has visited.


BFS
• ALGORITHM
1.Enqueue starting vertex & print
2.while(Q is not empty)
i.P=Dequeue();
ii.Enqueue(print) all adjacent unvisited vertices of P
Following is Breadth First Traversal (starting from vertex 2)
2031
void bfs()
{ int q[10], u, front = 0, rear = -1;
printf("\nEnter the source vertex to find other nodes reachable or not: ");
scanf("%d", &source);
q[++rear] = source;
visited[source] = 1;
printf("\nThe reachable vertices are: ");
while (front <= rear) {
u = q[front++];
for (i = 1; i <= n; i++)
{
if (a[u][i] == 1 && visited[i] == 0)
{
q[++rear] = i;
visited[i] = 1;

printf("\n%d", i); } } }}
AVL Trees
• AVL trees was developed to overcome the drawbacks of binary search
trees.
• In case of left skewed BST and right skewed BST to search an element
it takes more amount of time.
• If there are 5 nodes than 5 comparisons needed.
• AVL tree is known as high balanced tree.
• Height of AVL tree is O(logn).
• Balance Factor (k) = height (left(k)) - height (right(k))
AVL TREES
• AVL Tree is invented by GM Adelson - Velsky and EM
Landis in 1962. The tree is named AVL in honor of its
inventors.
• AVL Tree can be defined as height balanced binary
search tree in which each node is associated with a
balance factor which is calculated by subtracting the
height of its right sub-tree from that of its left sub-tree.
• Tree is said to be balanced if balance factor of each
node is in between -1 to 1, otherwise, the tree will be
unbalanced and need to be balanced.
• Balance Factor (k) = height (left(k)) - height (right(k))
AVL TREES(Contd..)
• If balance factor of any node is 1, it means that the left
sub-tree is one level higher than the right sub-tree.
• If balance factor of any node is 0, it means that the left
sub-tree and right sub-tree contain 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.
• We have to calculate balance factor of each node.
• Balance factor=height of LST-height of RST.
• If a balance factor of a node is -1,0,+1 that that node is balanced.
• If we get other than these than it is unbalanced.
• In order to make unbalanced to balanced we need to perform some
rotations on the tree
• Balance Factor (k) = height (left(k)) - height (right(k))
AVL Rotations

• We perform rotation in AVL tree only in case if Balance Factor is other


than -1, 0, and 1. There are basically four types of rotations which are as
follows:
1.L L rotation: Inserted node is in the left subtree of left subtree of A
2.R R rotation : Inserted node is in the right subtree of right subtree of A
3.L R rotation : Inserted node is in the right subtree of left subtree of A
4.R L rotation : Inserted node is in the left subtree of right subtree of A
• 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 subtree of 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 balance factor -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.
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 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.
Q: Construct an AVL tree having the following elements
H, I, J, B, A, E, C, F, D, G, K, L
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.
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.
On inserting E, BST becomes unbalanced as the Balance Factor
of I is 2, since if we travel from E to I we find that it is inserted
in the left subtree of right subtree of I, we will perform LR
Rotation on node I. LR = RR + LL rotation
On inserting C, F, D, BST becomes unbalanced as the Balance Factor of B and H is -2, since if we travel from D to B
we find that it is inserted in the right subtree of left subtree of B, we will perform RL Rotation on node I. RL = LL +
RR rotation.
Red-Black trees

What are Red Black Trees?

• Red-black tree is another variant of Binary search tree in which every


node is coloured either RED or BLACK.

• In Red Black Tree, the colour of a node is decided based on the


properties of Red Black Tree.
Properties of Red-Black trees
• Red-Black tree must be a binary search tree.

• The ROOT node must be coloured BLACK.

• The children of red coloured node must be coloured BLACK.(There


should not be two consecutive RED nodes).

• In all the paths of the tree ,there should be same number of BLACK
coloured nodes.

• Every new node must be inserted with RED colour.

• Every leaf(i.e NULL node)must be coloured BLACK.


Insertion into RED BLACK Tree

• In a Red-Black Tree, every new node must be inserted with the color
RED. The insertion operation in Red Black Tree is similar to insertion
operation in Binary Search Tree. But it is inserted with a color
property. After every insertion operation, we need to check all the
properties of Red-Black Tree. If all the properties are satisfied then we
go to next operation otherwise we perform the following operation to
make it Red Black Tree.
• 1. Recolor
• 2. Rotation
• 3. Rotation followed by Recolor
The insertion operation in Red Black tree

• Step 1 - Check whether tree is Empty.


• Step 2 - If tree is Empty then insert the newNode as Root node with color Black and exit
from the operation.
• Step 3 - If tree is not Empty then insert the newNode as leaf node with color Red.
• Step 4 - If the parent of newNode is Black then exit from the operation.
• Step 5 - If the parent of newNode is Red then check the color of parentnode's sibling of
newNode.
• Step 6 - If it is colored Black or NULL then make suitable Rotation and Recolor it.
• Step 7 - If it is colored Red then perform Recolor. Repeat the same until tree becomes
Red Black Tree.
why do we require a Red-Black tree if AVL is also a height-
balanced tree

• The Red-Black tree is used because the AVL tree requires many rotations
when the tree is large, whereas the Red-Black tree requires a maximum of
two rotations to balance the tree.

• The main difference between the AVL tree and the Red-Black tree is that
the AVL tree is strictly balanced, while the Red-Black tree is not
completely height-balanced. So, the AVL tree is more balanced than the
Red-Black tree.
B-Trees
• What are B-trees?
• B-tree is a self balancing search tree(like AVL and Red-Black trees),it
• is assumed that everything is in memory.
• B trees are used when the entire data cannot be stored in memory.
• Disk access time is very high compared to main memory access time.
• The main idea of using B-tree is to reduce the number of disk
accesses.
• B-tree is a special type of self-balancing search tree in
which each node can contain more than one key and
can have more than two children. It is a generalized
form of the binary search tree.
Properties of B trees

• All leaves of B-tree are at the same level.

• All B-tree of order m can have at most m-1 keys and m children.

• Every node in B-tree has at most m children.

• Root node must have at least two nodes


Splay trees
• Self adjusted BST.
• A splay tree is a self-balancing tree, but AVL and Red-Black trees are
also self-balancing trees then. What makes the splay tree unique two
trees. It has one extra property that makes it unique is splaying.
• A splay tree contains the same operations as a Binary search tree, i.e.,
Insertion, deletion and searching, but it also contains one more
operation, i.e., splaying. So. all the operations in the splay tree are
followed by splaying.
• Rotations
• There are six types of rotations used for splaying:
1.Zig rotation (Right rotation)
2.Zag rotation (Left rotation)
3.Zig zag (Zig followed by zag)
4.Zag zig (Zag followed by zig)
5.Zig zig (two right rotations)
6.Zag zag (two left rotations)

You might also like