Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
15 views18 pages

Graph

Download as docx, pdf, or txt
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 18

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.

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

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.

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)

In sequential representation, an adjacency matrix is used to store the graph. Whereas


in linked list representation, there is a use of an adjacency list to store the graph.

In this tutorial, we will discuss each one of them in detail.

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.

An entry Aij in the adjacency matrix representation of an undirected graph G will be 1


if an edge exists between Vi and Vj. If an Undirected Graph G consists of n vertices,
then the adjacency matrix for that graph is n x n, and the matrix A = [aij] can be
defined as -

aij = 1 {if there is a path exists from Vi to Vj}

aij = 0 {Otherwise}

It means that, in an adjacency matrix, 0 represents that there is no association exists


between the nodes, whereas 1 represents the existence of a path between two
edges.

If there is no self-loop present in the graph, it means that the diagonal entries of the
adjacency matrix will be 0.

Now, let's see the adjacency matrix representation of an undirected graph.


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.

Adjacency matrix for a directed graph


In a directed graph, edges represent a specific path from one vertex to another
vertex. Suppose a path exists from vertex A to another vertex B; it means that node A
is the initial node, while node B is the terminal node.

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.

Adjacency matrix for a directed graph


In a directed graph, edges represent a specific path from one vertex to another
vertex. Suppose a path exists from vertex A to another vertex B; it means that node A
is the initial node, while node B is the terminal node.

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.

Adjacency matrix for a weighted directed graph

It is similar to an adjacency matrix representation of a directed graph except that


instead of using the '1' for the existence of a path, here we have to use the weight
associated with the edge. The weights on the graph edges will be represented as the
entries of the adjacency matrix. We can understand it with the help of an example.
Consider the below graph and its adjacency matrix representation. In the
representation, we can see that the weight associated with the edges is represented
as the entries in the adjacency matrix.

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 -

o Undirected graph: An undirected graph is a graph in which all the edges do


not point to any particular direction, i.e., they are not unidirectional; they are
bidirectional. It can also be defined as a graph with a set of V vertices and a
set of E edges, each edge connecting two different vertices.
o Connected graph: A connected graph is a graph in which a path always exists
from a vertex to any other vertex. A graph is connected if we can reach any
vertex from any other vertex by following edges in either direction.
o Directed graph: Directed graphs are also known as digraphs. A graph is a
directed graph (or digraph) if all the edges present between any vertices or
nodes of the graph are directed or have a defined direction.

Now, let's move towards the topic spanning tree.

What is a spanning tree?


A spanning tree can be defined as the subgraph of an undirected connected graph. It
includes all the vertices along with the least possible number of edges. If any vertex
is missed, it is not a spanning tree. A spanning tree is a subset of the graph that does
not have cycles, and it also cannot be disconnected.

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.

Applications of the spanning tree


Basically, a spanning tree is used to find a minimum path to connect all nodes of the
graph. Some of the common applications of the spanning tree are listed as follows -

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.

Example of Spanning tree


Suppose the graph be -

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 -

o There can be more than one spanning tree of a connected graph G.


o A spanning tree does not have any cycles or loop.
o A spanning tree is minimally connected, so removing one edge from the tree will
make the graph disconnected.
o A spanning tree is maximally acyclic, so adding one edge to the tree will create a
loop.
o There can be a maximum nn-2 number of spanning trees that can be created from a
complete graph.
o A spanning tree has n-1 edges, where 'n' is the number of nodes.
o If the graph is a complete graph, then the spanning tree can be constructed by
removing maximum (e-n+1) edges, where 'e' is the number of edges and 'n' is the
number of vertices.

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.

Example of minimum spanning tree


Let's understand the minimum spanning tree with the help of an example.

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 Minimum spanning tree can be used to design water-supply networks,


telecommunication networks, and electrical grids.
o It can be used to find paths in the map.

Algorithms for Minimum spanning tree


A minimum spanning tree can be found from a weighted graph by using the algorithms given
below -

o Prim's Algorithm
o Kruskal's Algorithm

Let's see a brief description of both of the algorithms listed above.

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.

How does Kruskal's algorithm work?


In Kruskal's algorithm, we start from edges with the lowest weight and keep adding
the edges until the goal is reached. The steps to implement Kruskal's algorithm are
listed as follows -

o First, sort all the edges from low weight to high.


o Now, take the edge with the lowest weight and add it to the spanning tree. If the
edge to be added creates a cycle, then reject the edge.
o Continue to add the edges until we reach all vertices, and a minimum spanning tree
is created.

The applications of Kruskal's algorithm are -

o Kruskal's algorithm can be used to layout electrical wiring among cities.


o It can be used to lay down LAN connections.

Example of Kruskal's algorithm


Now, let's see the working of Kruskal's algorithm using an example. It will be easier
to understand Kruskal's algorithm using an example.

Suppose a weighted graph is -


Step 1 - First, add the edge AB with weight 1 to the MST.

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 -

The cost of the MST is = AB + DE + BC + CD = 1 + 2 + 3 + 4 = 10.

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.

Example of Graph coloring

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.

Applications of Graph coloring

There are various applications of graph coloring. Some of their important


applications are described as follows:

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.

Example of Chromatic number:


To understand the chromatic number, we will consider a graph, which is described as
follows:

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.

You might also like