Graph
Graph
Graph
A graph can be defined as group of vertices and edges that are used to connect
these vertices. A graph can be seen as a cyclic tree, where the vertices (Nodes)
maintain any complex relationship among them instead of having parent child
relationship.
Definition
A graph G can be defined as an ordered set G(V, E) where V(G) represents the set of
vertices and E(G) represents the set of edges which are used to connect these
vertices.
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.
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 V 0=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.
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.
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
neighbours or adjacent nodes.
Graph representation
In this article, we will discuss the ways to represent the graph. By Graph
representation, we simply mean the technique to be used to store some graph into
the computer's memory.
A graph is a data structure that consist a sets of vertices (called nodes) and edges.
There are two ways to store Graphs into the computer's memory:
o Sequential representation (or, Adjacency matrix representation)
o Linked list representation (or, Adjacency list representation)
Now, let's start discussing the ways of representing a graph in the data structure.
Sequential representation
In sequential representation, there is a use of an adjacency matrix to represent the
mapping between vertices and edges of the graph. 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.
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.
There exist different adjacency matrices for the directed and undirected graph. In a
directed graph, an entry A ij will be 1 only when there is an edge directed from V i to
Vj.
Consider the below-directed graph and try to construct the adjacency matrix of it.
In the above figure, an image shows the mapping among the vertices (A, B, C, D, E),
and this mapping is represented by using the adjacency matrix.
There exist different adjacency matrices for the directed and undirected graph. In a
directed graph, an entry A ij will be 1 only when there is an edge directed from V i to
Vj.
Consider the below-directed graph and try to construct the adjacency matrix of it.
In the above graph, we can see there is no self-loop, so the diagonal entries of the
adjacent matrix are 0.
In the above image, we can see that the adjacency matrix representation of the
weighted directed graph is different from other representations. It is because, in this
representation, the non-zero values are replaced by the actual weight assigned to
the edges.
Adjacency matrix is easier to implement and follow. An adjacency matrix can be used
when the graph is dense and a number of edges are large.
Spanning tree
In this article, we will discuss the spanning tree and the minimum spanning tree. But
before moving directly towards the spanning tree, let's first see a brief description of
the graph and its types.
Graph
A graph can be defined as a group of vertices and edges to connect these vertices.
The types of graphs are given as follows -
A spanning tree consists of (n-1) edges, where 'n' is the number of vertices (or nodes). Edges
of the spanning tree may or may not have weights assigned to them. All the possible
spanning trees created from the given graph G would have the same number of vertices, but
the number of edges in the spanning tree would be equal to the number of vertices in the
given graph minus 1.
o Cluster Analysis
o Civil network planning
o Computer network routing protocol
Now, let's understand the spanning tree with the help of an example.
As discussed above, a spanning tree contains the same number of vertices as the graph, the
number of vertices in the above graph is 5; therefore, the spanning tree will contain 5
vertices. The edges in the spanning tree will be equal to the number of vertices in the graph
minus 1. So, there will be 4 edges in the spanning tree.
Some of the possible spanning trees that will be created from the above graph are given as
follows –
Properties of spanning-tree
Some of the properties of the spanning tree are given as follows -
So, a spanning tree is a subset of connected graph G, and there is no spanning tree
of a disconnected graph.
Minimum Spanning tree
A minimum spanning tree can be defined as the spanning tree in which the sum of
the weights of the edge is minimum. The weight of the spanning tree is the sum of
the weights given to the edges of the spanning tree. In the real world, this weight
can be considered as the distance, traffic load, congestion, or any random value.
The sum of the edges of the above graph is 16. Now, some of the possible spanning
trees created from the above graph are -
So, the minimum spanning tree that is selected from the above spanning trees for the given
weighted graph is –
Applications of minimum spanning tree
The applications of the minimum spanning tree are given as follows -
o Prim's Algorithm
o Kruskal's Algorithm
Prim's algorithm - It is a greedy algorithm that starts with an empty spanning tree. It is used
to find the minimum spanning tree from the graph. This algorithm finds the subset of edges
that includes every vertex of the graph such that the sum of the weights of the edges can be
minimized.
Kruskal's algorithm - This algorithm is also used to find the minimum spanning tree for a
connected weighted graph. Kruskal's algorithm also follows greedy approach, which finds an
optimum solution at every stage instead of focusing on a global optimum.
Step 2 - Add the edge DE with weight 2 to the MST as it is not creating the cycle.
Step 3 - Add the edge BC with weight 3 to the MST, as it is not creating any cycle or
loop.
Step 4 - Now, pick the edge CD with weight 4 to the MST, as it is not forming the
cycle.
Step 5 - After that, pick the edge AE with weight 5. Including this edge will create
the cycle, so discard it.
Step 6 - Pick the edge AC with weight 7. Including this edge will create the cycle, so
discard it.
Step 7 - Pick the edge AD with weight 10. Including this edge will also create the
cycle, so discard it.
So, the final minimum spanning tree obtained from the given weighted graph by
using Kruskal's algorithm is -
Algorithm
1. Step 1: Create a forest F in such a way that every vertex of the graph is a separ
ate tree.
2. Step 2: Create a set E that contains all the edges of the graph.
3. Step 3: Repeat Steps 4 and 5 while E is NOT EMPTY and F is not spanning
4. Step 4: Remove an edge from E with minimum weight
5. Step 5: IF the edge obtained in Step 4 connects two different trees, then add it
to the forest F
6. (for combining two trees into one tree).
7. ELSE
8. Discard the edge
9. Step 6: END
Chromatic Number of graphs
Graph coloring
Graph coloring can be described as a process of assigning colors to the vertices of a
graph. In this, the same color should not be used to fill the two adjacent vertices. We
can also call graph coloring as Vertex Coloring. In graph coloring, we have to take
care that a graph must not contain any edge whose end vertices are colored by the
same color. This type of graph is known as the Properly colored graph.
In this graph, we are showing the properly colored graph, which is described as
follows:
The above graph contains some points, which are described as follows
o The same color cannot be used to color the two adjacent vertices.
o Hence, we can call it as a properly colored graph.
o Assignment
o Map coloring
o Scheduling the tasks
o Sudoku
o Prepare time table
o Conflict resolution
Chromatic Number
The chromatic number can be described as the minimum number of colors required
to properly color any graph. In other words, the chromatic number can be described
as a minimum number of colors that are needed to color any graph in such a way
that no two adjacent vertices of a graph will be assigned the same color.
The above graph contains some points, which are described as follows:
o The same color is not used to color the two adjacent vertices.
o The minimum number of colors of this graph is 3, which is needed to properly color
the vertices.
o Hence, in this graph, the chromatic number = 3
o If we want to properly color this graph, in this case, we are required at least 3 colors.