Professional Documents
Culture Documents
Graph - Data Structure
Graph - Data Structure
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.
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 V0=VN, then such
path P is called as closed simple path.
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.
Types of Graphs
1. Null Graph
Here,
• This graph consists only of the vertices and there are no edges in it.
• Since the edge set is empty, therefore it is a null graph.
Example:
Here,
• This graph consists of only one vertex and there are no edges in it.
• Since only one vertex is present, therefore it is a trivial graph.
3. Non-Directed Graph
Example:
Here,
• This graph consists of four vertices and four undirected edges.
• Since all the edges are undirected, therefore it is a non-directed graph.
•A graph in which all the edges are directed is called as a directed graph.
• In other words, all the edges of a directed graph contain some direction.
• Directed graphs are also called as Digraphs.
Example:
Here,
• This graph consists of four vertices and four directed edges.
• Since all the edges are directed, therefore it is a directed graph.
5. Connected Graph
•A graph in which we can visit from any one vertex to any other vertex
is called as a connected graph.
• In connected graph, at least one path exists between every pair of
vertices.
Example:
6. Disconnected Graph
•A graph in which there does not exist any path between at least one pair
of vertices is called as a disconnected graph.
Example:
Here,
• This graph consists of two independent components which are
disconnected.
• It is not possible to visit from the vertices of one component to the
vertices of another component.
• Therefore, it is a disconnected graph.
7. Regular Graph
In these graphs,
• Allthe vertices have degree-2.
• Therefore, they are 2-Regular graphs.
8. Complete Graph
Examples:
In these graphs,
• Each vertex is connected with all the remaining vertices through exactly
one edge.
• Therefore, they are complete graphs.
Examples:
In these graphs,
• Each vertex is having degree 2.
• Therefore, they are cycle graphs.
Here,
• Thisgraph consists of finite number of vertices and edges.
• Therefore, it is a finite graph.
•A
graph consisting of infinite number of vertices and edges is called as
an infinite graph.
Example:
Here,
• Thisgraph consists of infinite number of vertices and edges.
• Therefore, it is an infinite graph.
•A planar graph is a graph that we can draw in a plane such that no two
edges of it cross each other.
Example:
Here,
• Thisgraph can be drawn in a plane without crossing any edges.
• Therefore, it is a planar graph.
Here,
• Thisgraph consists of three vertices and three edges.
• There are neither self-loops nor parallel edges.
• Therefore, it is a simple graph.
•A
graph having no self-loops but having parallel edge(s) in it is called as
a multi graph.
Example:
Here,
• Thisgraph consists of three vertices and four edges out of which one
edge is a parallel edge.
• There are no self-loops but a parallel edge is present.
• Therefore, it is a multi-graph.
•A
graph having no parallel edges but having self-loop(s) in it is called as
a pseudo graph.
Example:
Here,
• Thisgraph consists of three vertices and four edges out of which one
edge is a self-loop.
• There are no parallel edges but a self-loop is present.
• Therefore, it is a pseudo graph.
• Euler
Graph is a connected graph in which all the vertices are even
degree.
Example:
Here,
• This graph is a connected graph and the degree of all the vertices is even.
• Therefore, it is a Euler graph.
• If
there exists a closed walk in the connected graph that visits every
vertex of the graph exactly once (except starting vertex) without
repeating the edges, then such a graph is called as a Hamiltonian graph.
Example:
Here,
• This graph contains a closed walk ABCDEFG that visits all the vertices
(except starting vertex) exactly once.
• All the vertices are visited without repeating the edges.
• Therefore, it is a Hamiltonian Graph.
i) 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.
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.
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.
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.
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 figure, we can see that there is a linked list or adjacency list for
every node of the graph. From vertex A, there are paths to vertex B and vertex
D. These nodes are linked to nodes A in the given adjacency list.
The sum of the lengths of adjacency lists is equal to twice the number of edges
present in an undirected graph.
For a directed graph, the sum of the lengths of adjacency lists is equal to the
number of edges present in the graph.
In the case of a weighted directed graph, each node contains an extra field that
is called the weight of the node.