module5ds
module5ds
What is a Graph?
A graph is an abstract data type (ADT) which consists of a set of objects that are connected
to each other via links. The interconnected objects are represented by points termed
as vertices, and the links that connect the vertices are called edges.
Formally, a graph is a pair of sets (V, E), where V is the set of vertices and E is the set of
edges, connecting the pairs of vertices. Take a look at the following graph −
V = {a, b, c, d, e}
A graph can be represented as a collection of vertices with edges joining them. The most
popular types of graphs:
Undirected Graph: An undirected graph is one where the edges do not indicate in the same
direction, making it bidirectional instead of unidirectional. It’s also feasible to consider it as
a graph with V vertices and E edges, each uniting two separate vertices.
Connected Graph: A connected graph is one when there is invariably a path from one
vertex to another. Also, we can say that a graph is connected if we can go to any vertex
from any different vertex by pursuing edges in either direction.
Directed Graph: A graph is considered a directed graph if all the edges present between
any nodes or vertices have a defined direction.
Operations of Graphs
The primary operations of a graph include creating a graph with vertices and edges, and
displaying the said graph. However, one of the most common and popular operation
performed using graphs are Traversal, i.e. visiting every vertex of the graph in a specific
order.
The DFS traversal uses the stack data structure to keep track of the unvisited nodes.
Depth First Search (DFS) algorithm is a recursive algorithm for searching all the vertices of
a graph or tree data structure. This algorithm traverses a graph in a depth ward motion and
uses a stack to remember to get the next vertex to start a search, when a dead end occurs in
any iteration.
Rule 1 − Visit the adjacent unvisited vertex. Mark it as visited. Display it. Push it in a stack.
Rule 2 − If no adjacent vertex is found, pop up a vertex from the stack. (It will pop up all
the vertices from the stack, which do not have adjacent vertices.)
Rule 3 − Repeat Rule 1 and Rule 2 until the stack is empty.
St
Traversal Description
ep
Initialize the
1
stack.
Mark S as visited
and put it onto
the stack. Explore
any unvisited
adjacent node
from S. We have
2 three nodes and
we can pick any
of them. For this
example, we shall
take the node in
an alphabetical
order.
Mark A as visited
and put it onto
the stack. Explore
any unvisited
adjacent node
3 from A.
Both S and D are
adjacent to A but
we are concerned
for unvisited
nodes only.
We choose B,
mark it as visited
and put onto the
stack.
Here B does not
5
have any
unvisited adjacent
node. So, we
pop B from the
stack.
We check the
stack top for
return to the
previous node
and check if it has
6
any unvisited
nodes. Here, we
find D to be on
the top of the
stack.
Only unvisited
adjacent node is
from D is C now.
7 So we visit C,
mark it as visited
and put it onto
the stack.
As C does not have any unvisited adjacent node so we keep popping the stack until we find
a node that has an unvisited adjacent node. In this case, there's none and we keep popping
until the stack is empty.
Breadth First Search is a traversal algorithm that visits all the vertices of a graph present at
one level of the depth before moving to the next level of depth. In this algorithm, an
arbitrary node is chosen as the starting point and the graph is traversed by visiting the
adjacent vertices on the same depth level and marking them until there is no vertex left.
The BFS traversal uses the queue data structure to keep track of the unvisited nodes.
Breadth First Search (BFS) algorithm traverses a graph in a breadthward motion to search a
graph data structure for a node that meets a set of criteria. Breadth First Search (BFS)
algorithm starts at the tree root and explores all nodes at the present depth prior to moving
on to the nodes at the next depth level.
As in the example given below, BFS algorithm traverses from A to B to E to F first then to
C and G lastly to D. It employs the following rules.
Rule 1 − Visit the adjacent unvisited vertex. Mark it as visited. Display it. Insert it in a
queue.
Rule 2 − If no adjacent vertex is found, remove the first vertex from the queue.
Rule 3 − Repeat Rule 1 and Rule 2 until the queue is empty.
St
Traversal Description
ep
Initialize the
1
queue.
We start from
visiting S (starti
2 ng node), and
mark it as
visited.
We then see an
unvisited
adjacent node
from S. In this
example, we
have three
3
nodes but
alphabetically
we choose A,
mark it as
visited and
enqueue it.
Next, the
unvisited
adjacent node
4 from S is B. We
mark it as
visited and
enqueue it.
Next, the
unvisited
adjacent node
5 from S is C. We
mark it as
visited and
enqueue it.
Now, S is left
with no unvisited
6 adjacent nodes.
So, we dequeue
and find A.
From A we
have D as
unvisited
7 adjacent node.
We mark it as
visited and
enqueue it.
At this stage, we are left with no unmarked (unvisited) nodes. But as per the algorithm we
keep on dequeuing in order to get all unvisited nodes. When the queue gets emptied, the
program is over.
The weight of a spanning tree is determined by the sum of weight of all the edge involved
in it.
A minimum spanning tree (MST) is defined as a spanning tree that has the minimum weight
among all the possible spanning trees.
A minimum spanning tree connects all the vertices in the graph, ensuring that there is a
path between any pair of nodes.
An MST is acyclic, meaning it contains no cycles. This property ensures that it remains a
tree and not a graph with loops.
An MST with V vertices (where V is the number of vertices in the original graph) will
have exactly V – 1 edges, where V is the number of vertices.
An MST is optimal for minimizing the total edge weight, but it may not necessarily be
unique.
The cut property states that if you take any cut (a partition of the vertices into two sets) in
the original graph and consider the minimum-weight edge that crosses the cut, that edge is
part of the MST.
Like a spanning tree, there can also be many possible MSTs for a graph as shown in the
below image:
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.
The graph contains 9 vertices and 14 edges. So, the minimum spanning tree formed will
be having (9 – 1) = 8 edges.
After sorting:
1 7 6
2 8 2
2 6 5
4 0 1
4 2 5
6 8 6
7 2 3
7 7 8
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.
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.
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.
The algorithm starts with an empty spanning tree. The idea is to maintain two sets of
vertices. The first set contains the vertices already included in the MST, and the other set
contains the vertices not yet included. At every step, it considers all the edges that connect
the two sets and picks the minimum weight edge from these edges. After picking the edge,
it moves the other endpoint of the edge to the set containing MST.
A group of edges that connects two sets of vertices in a graph is called cut in graph theory.
So, at every step of Prim’s algorithm, find a cut, pick the minimum weight edge from the
cut, and include this vertex in MST Set (the set that contains already included vertices).
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.
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.
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].
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.
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.
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.
Step 9: Only the vertex 4 remains to be included. The minimum weighted edge from the
incomplete MST to 4 is {3, 4}.
Note: If we had selected the edge {1, 2} in the third step then the MST would look like the
following.