Unit 5
Unit 5
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.
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.
Edge − Edge represents a path between two vertices or a line between two vertices.
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.
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.
A degree of a node is the number of edges that are connected with that 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.
The adjacency list is a list of the vertices directly connected to the other vertices in the graph.
The most common and popular operation performed using graphs are Traversal, i.e. visiting
every vertex of the graph in a specific order.
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.
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 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 6 – Remove the unused edges and form the spanning tree only after the Queue is
empty.
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.
Example 2:
Example-3
Example-4
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 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.
Back tracking is coming back to the vertex from which we reached the current vertex.
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
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.
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.
Kruskal ’s algorithm
Below are the steps for finding MST using Kruskal’s algorithm:
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.
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.
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
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].
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.
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.
Include vertex 3 in 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.
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
Exapmle-3