0% found this document useful (0 votes)
10 views36 pages

Unit 5

Uploaded by

Sai Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
10 views36 pages

Unit 5

Uploaded by

Sai Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 36

Graph in data Structures

A Graph Data Structure is a collection of nodes or vertices connected by edges. It’s used
to represent relationships between different entities. Graph algorithms are methods used
to manipulate and analyse graphs, solving various problems like finding the shortest
path or detecting cycles.

A Graph G(V, E) with 5 vertices (A, B, C, D, E) and six edges ((A,B), (B,C), (C,E), (E,D),
(D,B), (D,A)) is shown in the following figure.

Directed and Undirected Graph

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

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

A directed graph is shown in the following figure.


Graph Terminology

Vertex − Each node of the graph is represented as a vertex

In the above graph,

Vertices or nodes = {a, b, c, d, e}

Edge − Edge represents a path between two vertices or a line between two vertices.

Edges = {ab, ac, bd, cd, de}

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.
Cycle

A cycle can be defined as the path which has no repeated edges or vertices except the first
and last 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.
Directed graph

A directed graph 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.

Undirected graph definition

An undirected graph is graph, i.e., a set of objects (called vertices or nodes) that are
connected together, where all the edges are bidirectional.. In contrast, a graph where the
edges point in a direction is called a directed graph.
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.

 Adjacency − Two node or vertices are adjacent if they are connected to each other
through an edge. In the following example, B is adjacent to A, C is adjacent to B, and
so on.

 Path − Path represents a sequence of edges between the two vertices. In the following
example, ABCD represents a path from A to D.

Types of graph
There are two basic types of graph −

 Directed Graph
 Undirected Graph

Directed graph, as the name suggests, consists of edges that possess a direction that goes
either away from a vertex or towards the vertex. Undirected graphs have edges that are not
directed at all.

Directed Graph

Undirected Graph
Representation of Graphs

While representing graphs, we must carefully depict the elements (vertices and edges) present
in the graph and the relationship between them. Pictorially, a graph is represented with a
finite set of nodes and connecting links between them. However, we can also represent the
graph in other most commonly used ways, like

 Adjacency Matrix
 Adjacency List

Adjacency Matrix

The Adjacency Matrix is a V x V matrix where the values are filled with either 0 or 1. If the
link exists between Vi and Vj, it is recorded 1; otherwise, 0.

For the given graph below, let us construct an adjacency matrix −

The adjacency matrix is −


Adjacency List

The adjacency list is a list of the vertices directly connected to the other vertices in the graph.

The adjacency list is −


Graph traversals

The most common and popular operation performed using graphs are Traversal, i.e. visiting
every vertex of the graph in a specific order.

There are two types of traversals in Graphs −

 Depth First Search Traversal


 Breadth First Search Traversal

Breadth first search:


The Breadth First Search (BFS) algorithm is used to search a graph data structure for a
node that meets a set of criteria.
It starts at the root of the graph and visits all nodes at the current depth level before moving
on to the nodes at the next depth level.

graphs may contain cycles, so we may come to the same node again. To avoid processing a
node more than once, we divide the vertices into two categories:
 Visited and
 Not visited.
A boolean visited array is used to mark the visited vertices. For simplicity, it is assumed
that all vertices are reachable from the starting vertex. BFS uses a queue data structure for
traversal.

BFS (Breadth-First Search)Algorithm:

Search is implemented using a queuing method

Breadth-First Search navigates a graph in a breadth motion and utilises based on the
Queue to jump from one node to another,
Steps followed to implement BFS search,

Step 1 – Based on the number of nodes, the Queue is defined.

Step 2 – Start from any node of the traversal. Visit that node and add it to the Queue.

Step 3 – Now check the non-visited adjacent node, which is in front of the Queue, and add
that into the Queue, not to the start.

Step 4 – Now start deleting the node that doesn’t have any edges that need to be visited
and is not in the Queue.

Step 5 – Empty the Queue by repeating steps 4 and 5.

Step 6 – Remove the unused edges and form the spanning tree only after the Queue is
empty.

How does BFS work?


Starting from the root, all the nodes at a particular level are visited first and then the nodes
of the next level are traversed till all the nodes are visited.
To do this a queue is used. All the adjacent unvisited nodes of the current level are pushed
into the queue and the nodes of the current level are marked visited and popped from the
queue.
Illustration:
Let us understand the working of the algorithm with the help of the following example.
Step1: Initially queue and visited arrays are empty.

Queue and visited arrays are empty initially.

Step2: Push node 0 into queue and mark it visited.


Push node 0 into queue and mark it visited.

Step 3: Remove node 0 from the front of queue and visit the unvisited neighbours and push
them into queue.

Remove node 0 from the front of queue and visited the unvisited neighbours and push into

queue.

Step 4: Remove node 1 from the front of queue and visit the unvisited neighbours and push
them into queue.

Remove node 1 from the front of queue and visited the unvisited neighbours and push

Step 5: Remove node 2 from the front of queue and visit the unvisited neighbours and push
them into queue.
Remove node 2 from the front of queue and visit the unvisited neighbours and push them

into queue.

Step 6: Remove node 3 from the front of queue and visit the unvisited neighbours and push
them into queue.
As we can see that every neighbours of node 3 is visited, so move to the next node that are
in the front of the queue.

Remove node 3 from the front of queue and visit the unvisited neighbours and push them

into queue.

Steps 7: Remove node 4 from the front of queue and visit the unvisited neighbours and
push them into queue.
As we can see that every neighbours of node 4 are visited, so move to the next node that is
in the front of the queue.

Remove node 4 from the front of queue and visit the unvisited neighbours and push them

into queue.
Now, Queue becomes empty, So, terminate these process of iteration.

Breadth first search traversal

Example 2:

Example-3

Example-4

BFS (Breadth First Search)


BFS traversal of a graph produces a spanning tree as final result. Spanning Tree is a graph
without loops. We use Queue data structure with maximum size of total number of vertices in
the graph to implement BFS traversal.
We use the following steps to implement BFS traversal...

 Step 1 - Define a Queue of size total number of vertices in the graph.


 Step 2 - Select any vertex as starting point for traversal. Visit that vertex and insert it
into the Queue.
 Step 3 - Visit all the non-visited adjacent vertices of the vertex which is at front of the
Queue and insert them into the Queue.
 Step 4 - When there is no new vertex to be visited from the vertex which is at front of the
Queue then delete that vertex.
 Step 5 - Repeat steps 3 and 4 until queue becomes empty.
 Step 6 - When queue becomes empty, then produce final spanning tree by removing
unused edges from the graph
Applications of BFS are:

 Peer to Peer Networks- Like in Bit torrent, it is used to find all adjacent nodes.
 Crawlers in Search Engine.
 Social Networking Websites and many more.

Depth First Search or DFS for a Graph

Depth First Traversal (or DFS) for a graph is similar to Depth First Traversal of a tree. The
only catch here is, that, unlike trees, graphs may contain cycles (a node may be visited twice).
To avoid processing a node more than once, use a Boolean visited array. A graph can have
more than one DFS traversal.

1. DFS (Depth First Search) Algorithm: In-depth search method

DFS (Depth First Search)


DFS traversal of a graph produces a spanning tree as final result. Spanning Tree is a graph
without loops. We use Stack data structure with maximum size of total number of vertices in the
graph to implement DFS traversal.

We use the following steps to implement DFS traversal...

 Step 1 - Define a Stack of size total number of vertices in the graph.


 Step 2 - Select any vertex as starting point for traversal. Visit that vertex and push it on
to the Stack.
 Step 3 - Visit any one of the non-visited adjacent vertices of a vertex which is at the top
of stack and push it on to the stack.
 Step 4 - Repeat step 3 until there is no new vertex to be visited from the vertex which is
at the top of the stack.
 Step 5 - When there is no new vertex to visit then use back tracking and pop one vertex
from the stack.
 Step 6 - Repeat steps 3, 4 and 5 until stack becomes Empty.
 Step 7 - When stack becomes Empty, then produce final spanning tree by removing
unused edges from the graph

Back tracking is coming back to the vertex from which we reached the current vertex.

Applications of DFS are:


 Solving puzzles with only one solution.
 To test if a graph is bipartite.
 Topological Sorting for scheduling the job and many others.

How does DFS work?


Depth-first search is an algorithm for traversing or searching tree or graph data structures.
The algorithm starts at the root node (selecting some arbitrary node as the root node in the
case of a graph) and explores as far as possible along each branch before backtracking.
Let us understand the working of Depth First Search with the help of the following
illustration:
Step1: Initially stack and visited arrays are empty.

Stack and visited arrays are empty initially.

Step 2: Visit 0 and put its adjacent nodes which are not visited yet into the stack.

Visit node 0 and put its adjacent nodes (1, 2, 3) into the stack

Step 3: Now, Node 1 at the top of the stack, so visit node 1 and pop it from the stack and
put all of its adjacent nodes which are not visited in the stack.

Visit node 1

Step 4: Now, Node 2 at the top of the stack, so visit node 2 and pop it from the stack and
put all of its adjacent nodes which are not visited (i.e, 3, 4) in the stack.

Visit node 2 and put its unvisited adjacent nodes (3, 4) into the stack

Step 5: Now, Node 4 at the top of the stack, so visit node 4 and pop it from the stack and
put all of its adjacent nodes which are not visited in the stack.

Visit node 4

Step 6: Now, Node 3 at the top of the stack, so visit node 3 and pop it from the stack and
put all of its adjacent nodes which are not visited in the stack.

Visit node 3

Now, Stack becomes empty, which means we have visited all the nodes and our DFS
traversal ends.
Example-2 Depth first search

Example-3 depth first search


Spanning Tree A Spanning Tree is a tree which have V vertices and V-1
edges. All nodes in a spanning tree are reachable from each other.

For a connected and undirected graph, a spanning tree of that graph is a


subgraph that is a tree and connects all the vertices together. A single graph
can have multiple spanning trees.

A Minimum Spanning Tree(MST) or minimum weight spanning tree for a


weighted, connected, undirected graph is a spanning tree having a weight
less than or equal to the weight of every other possible spanning tree. The
weight of a spanning tree is the sum of weights given to each edge of the
spanning tree. In short out of all spanning trees of a given graph, the
spanning tree having minimum weight is MST.

Necessary conditions for Minimum Spanning Tree:


1. It must not form a cycle i.e, no edge is traversed twice.
2. There must be no other spanning tree with lesser weight.

Properties of MST:
Possible Multiplicity:
If G(V, E) is a graph then every spanning tree of graph G consists of (V –
1) edges, where V is the number of vertices in the graph and E is the number
of edges in the graph
There may be several minimum spanning trees of the same weight.
If all the edge weights of a graph are the same, then every spanning tree of
that graph is minimum.
Consider a complete graph of three vertices and all the edge weights are the
same then there will be three spanning trees(which are also minimal) of the
same path length are possible. Below is the image to illustrate the same:
Each of the spanning trees has the same weight equal to 2.

Cut property:

For any cut C of the graph, if the weight of an edge E in the cut-set of C is
strictly smaller than the weights of all other edges of the cut-set of C, then
this edge belongs to all the MSTs of the graph. Below is the image to
illustrate the same:
Cycle property:
In the above figure, in cycle ABD, edge BD cannot be present in any minimal
spanning tree because it has the largest weight among all the edges in the
cycle.

Uniqueness:
If each edge has a distinct weight then there will be only one, i.e., a unique
minimum spanning tree.
Minimum Cost Sub-graph
For all the possible spanning trees, the minimum spanning tree must have
the minimum weight possible.

However, there may exist some more spanning with the same weight that
of minimum spanning tree, and those all may also be considered as
Minimum Spanning tree.

Minimum Cost Edge:


 If the minimum cost edge of a graph is unique, then this edge is included in any
MST. For example, in the above figure, the edge AB (of the least weight) is
always included in MST.

 If a new edge is added to the spanning tree then it will become cyclic because
every spanning tree is minimally acyclic. In the above figure, if
edge AD or BC is added to the resultant MST, then it will form a cycle.

 The spanning tree is minimally connected, i.e., if any edge is removed from the
spanning tree it will disconnect the graph. In the above figure, if any edge is
removed from the resultant MST, then it will disconnect the graph.

Algorithms for finding Minimum Spanning Tree(MST):-


1. Prim’s Algorithm
2. Kruskal’s Algorithm

Kruskal’s Minimum Spanning Tree (MST) Algorithm


A minimum spanning tree (MST) or minimum weight spanning tree for a weighted,
connected, undirected graph is a spanning tree with a weight less than or equal to the
weight of every other spanning tree.
In Kruskal’s algorithm, sort all edges of the given graph in increasing order.
Then it keeps on adding new edges and nodes in the MST if the newly
added edge does not form a cycle. It picks the minimum weighted edge at
first and the maximum weighted edge at last. Thus we can say that it makes
a locally optimal choice in each step in order to find the optimal solution.
Hence this is a Greedy Algorithm.

Kruskal ’s algorithm
Below are the steps for finding MST using Kruskal’s algorithm:

Step-1 Sort all the edges in ASCENDING order of their weight.

Step-2 Pick the smallest edge. Check if it forms a cycle with the spanning
tree formed so far. If the cycle is not formed, include this edge. Else, discard
it.
Step-3 Repeat step#2 until there are (V-1) edges in the spanning tree.

Kruskal’s algorithm to find the minimum cost spanning tree uses the greedy
approach. The Greedy Choice is to pick the smallest weight edge that does
not cause a cycle in the MST constructed so far. Let us understand it with an
example:

Input Graph:

The graph contains 9 vertices and 14 edges. So, the minimum spanning tree
formed will be having (9 – 1) = 8 edges.
After sorting:
Weight Source Destination

1 7 6

2 8 2

2 6 5

4 0 1

4 2 5

6 8 6

7 2 3

7 7 8
Weight Source Destination

8 0 7

8 1 2

9 3 4

10 5 4

11 1 7

14 3 5

Now pick all edges one by one from the sorted list of edges
Step 1: Pick edge 7-6. No cycle is formed, include it.

Add edge 7-6 in the MST

Step 2: Pick edge 8-2. No cycle is formed, include it.


Add edge 8-2 in the MST

Step 3: Pick edge 6-5. No cycle is formed, include it.

Add edge 6-5 in the MST

Step 4: Pick edge 0-1. No cycle is formed, include it.


Add edge 0-1 in the MST

Step 5: Pick edge 2-5. No cycle is formed, include it.

Add edge 2-5 in the MST

Step 6: Pick edge 8-6. Since including this edge results in the cycle, discard
it. Pick edge 2-3: No cycle is formed, include it.
Add edge 2-3 in the MST

Step 7: Pick edge 7-8. Since including this edge results in the cycle, discard
it. Pick edge 0-7. No cycle is formed, include it.

Add edge 0-7 in MST

Step 8: Pick edge 1-2. Since including this edge results in the cycle, discard
it. Pick edge 3-4. No cycle is formed, include it.

Add edge 3-4 in the MST

Note: Since the number of edges included in the MST equals to (V – 1), so
the algorithm stops here
Time Complexity for kruskal algorithm: O(E * logE) or O(E * logV)
Space complexity for kruskal algorithm: O(V + E), where V is the number
of vertices and E is the number of edges in the graph.
Example-2 for kruskal’s

Prim’s Algorithm for Minimum Spanning


Tree (MST)
Like Kruskal’s algorithm, Prim’s algorithm is also a Greedy algorithm. This
algorithm always starts with a single node and moves through several
adjacent nodes, in order to explore all of the connected edges along the way.

1. Step 1: Select a starting vertex


2. Step 2: Repeat Steps 3 and 4 until there are fringe vertices
3. Step 3: Select an edge 'e' connecting the tree vertex and select the
vertex that has minimum weight
4. Step 4: Add the selected edge and the vertex to the minimum spanning tree T
5. [END OF LOOP]
6. Step 5: EXIT

Illustration of Prim’s Algorithm:


Consider the following graph as an example for which we need to find the
Minimum Spanning Tree (MST).
Example of a graph

Step 1: Firstly, we select an arbitrary vertex that acts as the starting vertex of
the Minimum Spanning Tree. Here we have selected vertex 0 as the starting
vertex.

0 is selected as starting vertex

Step 2: All the edges connecting the incomplete MST and other vertices are
the edges {0, 1} and {0, 7}. Between these two the edge with minimum
weight is {0, 1}. So include the edge and vertex 1 in the MST.
1 is added to the MST

Step 3: The edges connecting the incomplete MST to other vertices are {0,
7}, {1, 7} and {1, 2}. Among these edges the minimum weight is 8 which is of
the edges {0, 7} and {1, 2}. Let us here include the edge {0, 7} and the vertex
7 in the MST. [We could have also included edge {1, 2} and vertex 2 in the
MST].

7 is added in the MST

Step 4: The edges that connect the incomplete MST with the fringe vertices
are {1, 2}, {7, 6} and {7, 8}. Add the edge {7, 6} and the vertex 6 in the MST
as it has the least weight (i.e., 1).
6 is added in the MST

Step 5: The connecting edges now are {7, 8}, {1, 2}, {6, 8} and {6, 5}. Include
edge {6, 5} and vertex 5 in the MST as the edge has the minimum weight
(i.e., 2) among them.

Include vertex 5 in the MST

Step 6: Among the current connecting edges, the edge {5, 2} has the
minimum weight. So include that edge and the vertex 2 in the MST.
Include vertex 2 in the MST

Step 7: The connecting edges between the incomplete MST and the other
edges are {2, 8}, {2, 3}, {5, 3} and {5, 4}. The edge with minimum weight is
edge {2, 8} which has weight 2. So include this edge and the vertex 8 in the
MST.

Add vertex 8 in the MST

Step 8: See here that the edges {7, 8} and {2, 3} both have same weight
which are minimum. But 7 is already part of MST. So we will consider the
edge {2, 3} and include that edge and vertex 3 in the MST.
Include vertex 3 in MST

Step 9: Only the vertex 4 remains to be included. The minimum weighted


edge from the incomplete MST to 4 is {3, 4}.

Include vertex 4 in the MST

The final structure of the MST is as follows and the weight of the edges of
the MST is (4 + 8 + 1 + 2 + 4 + 2 + 7 + 9) = 37.

The structure of the MST formed using the above method

Note: If we had selected the edge {1, 2} in the third step then the MST would
look like the following.
Structure of the alternate MST if we had selected edge {1, 2} in the MST

Example-2 for prism’s algorithm

Example-2

Exapmle-3

You might also like