0% found this document useful (0 votes)
5 views

module5ds

A graph is an abstract data type consisting of vertices connected by edges, represented as a pair of sets (V, E). Key graph types include undirected, connected, and directed graphs, with traversal methods such as Depth First Search (DFS) and Breadth First Search (BFS). Spanning trees connect all vertices with the minimum number of edges, and Minimum Spanning Trees (MST) are defined by having the least total edge weight, with algorithms like Kruskal's and Prim's used to find them.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

module5ds

A graph is an abstract data type consisting of vertices connected by edges, represented as a pair of sets (V, E). Key graph types include undirected, connected, and directed graphs, with traversal methods such as Depth First Search (DFS) and Breadth First Search (BFS). Spanning trees connect all vertices with the minimum number of edges, and Minimum Spanning Trees (MST) are defined by having the least total edge weight, with algorithms like Kruskal's and Prim's used to find them.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Graph Data Structure

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 −

In the above graph,

V = {a, b, c, d, e}

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

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.

There are two types of traversals in Graphs −

 Depth First Search Traversal


 Breadth First Search Traversal

Depth First Search Traversal


Depth First Search is a traversal algorithm that visits all the vertices of a graph in the
decreasing order of its depth. In this algorithm, an arbitrary node is chosen as the starting
point and the graph is traversed back and forth by marking unvisited adjacent nodes until all
the vertices are marked.

The DFS traversal uses the stack data structure to keep track of the unvisited nodes.

Depth First Search (DFS) Algorithm

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.

As in the example given above, DFS algorithm traverses from S to A to D to G to E to B


first, then to F and lastly to C. It employs the following rules.

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

Visit D and mark


it as visited and
put onto the
stack. Here, we
have B and C nod
es, which are
4 adjacent to D and
both are
unvisited.
However, we shall
again choose in
an alphabetical
order.

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 Traversal

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

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.

What is a Spanning Tree ?


A spanning tree is a subset of Graph G, such that all the vertices are connected using
minimum possible number of edges. Hence, a spanning tree does not have cycles and a
graph may have more than one spanning tree.
Properties of a Spanning Tree :
 A Spanning tree does not exist for a disconnected graph.
 For a connected graph having N vertices then the number of edges in the spanning tree for
that graph will be N-1.
 A Spanning tree does not have any cycle.
 We can construct a spanning tree for a complete graph by removing E-N+1 edges,
where E is the number of Edges and N is the number of vertices.
Minimum Spanning Tree(MST):

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.

Properties of Minimum Spanning Tree :

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

Minimum Spanning Tree of a Graph may not be Unique:

Like a spanning tree, there can also be many possible MSTs for a graph as shown in the
below image:

Algorithms to Find Minimum Spanning Tree of a Graph:


There are several algorithms to find the minimum spanning tree from a given graph, some
of them are listed below:
1. Krushkal’s MST Algorithm
2. Prim’s MST Algorithm
1. Krushkal’s Minimum 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.

1. Sort all the edges in non-decreasing order of their weight.


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.
3. Repeat step#2 until there are (V-1) edges in the spanning tree.

Below is the illustration of the above approach:


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:

Weigh Sourc Destinati


t e on

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.

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.
2. Prim’s Algorithm for Minimum Spanning Tree (MST)

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

Step 1: Determine an arbitrary vertex as the starting vertex of the MST.


Step 2: Follow steps 3 to 5 till there are vertices that are not included in the MST (known
as fringe vertex).
Step 3: Find edges connecting any tree vertex with the fringe vertices.
Step 4: Find the minimum among these edges.
Step 5: Add the chosen edge to the MST if it does not form any cycle.
Step 6: Return the MST and exit

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.

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

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.

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.

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

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.

You might also like