Types of Graphs in Data Structure

Challenge Inside! : Find out where you stand! Try quiz, solve problems & win rewards!

Video Tutorial

Classification of Graph

Overview

A graph can be defined as group of vertices and edges that are used to connect these vertices.

Scope

  • This article tells about the different types of graphs in data structure.




Takeaways

  • Types of graphs
    • Finite Graph
    • Infinite Graph
    • Trivial Graph
    • Simple Graph
    • Multi Graph
    • Null Graph
    • Complete Graph
    • Pseudo Graph
    • Regular Graph
    • Bipartite Graph
    • Labelled Graph
    • Digraph Graph
    • Subgraph
    • Connected or Disconnected Graph
    • Cyclic Graph
    • Vertex Labeled Graph
    • Directed Acyclic Graph

Introduction

If you were asked to represent people you are connected with on social media in such a way that you can see the relations between them, how would you do it? What about the roads of the city you live in? We can use graphs for such representations. graphs in data structure types

In data structure, a graph G=(V,E) is a collection of sets V and E where V and E are the collections of vertices and edges respectively. An edge is a line or arc connecting 2 vertices and it is denoted by a pair (i,j) where i,j belong to a set of vertices V.

graphs in data structure

Different Types of Graphs in Data Structure

Graphs in data structure can be of various types and be used based on the requirements of the application.

Finite Graph

As the name suggests, a finite graph is one which has a finite (countable) number of vertices and edges, i.e. If a graph G=(V,E) consists of a finite number of vertices and edges denoted by V and E respectively, then the graph is a finite graph. finite graph

Infinite Graph

As the name suggests, contrary to a finite graph, an infinite graph is one which has infinite number of vertices and edges, i.e. If a graph G=(V,E) consists of an uncountable number of vertices or edges denoted by V and E respectively, then the graph is an infinite graph. infinite graph

Trivial Graph

A finite graph G=(V,E) is said to be trivial if it has only one vertex. Since such a graph has only one vertex, it does not have any edges. trivial graph

Simple Graph

A simple graph G=(V,E) is one which a pair of vertices V1 and V2 are connected by only one edge. A minimum cost graph mentioning the least cost of travelling by car between 2 places on its edges is an example of a simple graph. In such a graph, since least cost is a single value, there will be only one edge connecting 2 locations. simple graph

Multi Graph

A graph G=(V,E) which contains more than 1 edge or a loop (not self-loop) between 2 vertices is called a multi graph. A map of a place showing connections between places is an example of a multi graph. In such a graph, we can find more than one routes (edges) connecting 2 places (vertices), some of which could be longer, others shorter. multi graph

Null Graph

A null graph G=(V,E) is one which has only isolated vertices, ie. such a graph does not have any edges connecting the vertices. An example of such a graph would be importing an item from a foreign country to your place when there is no transport connecting the places.

If we consider the 2 locations to be vertices, then due to no route between them, there would be no edges connecting them. null graph

Complete Graph

A graph G=(V,E) is said to be complete if each vertex in the graph is adjacent to all of its vertices, i.e. there is an edge connecting any pair of vertices in the graph.

An undirected complete graph with n vertices will have n(n-1)/2 edges, while a directed complete graph with n vertices will have n(n-1) edges. The following figure shows graphs Ki where i represents the number of vertices.

We can clearly see how all the vertices in each graph have an edge connecting each other. complete graph

Pseudo Graph

A pseudo graph G=(V,E) is a graph that contains self loop and multiple edges. Consider the folllowing pseudo graph abcd. Between a and b, there exist 2 paths. b also contains a self loop. Thus, we can say that abcd is a pseudo graph. pseudo graph

Regular Graph

A graph G=(V,E) is said to be regular if every vertex V of the graph is adjacent or connected to the same number of vertices. All complete graphs are regular but it isn't the same vice versa. Consider the following example.

In a 2-regular graph, every vertex is adjacent to 2 vertices, whereas in a 3-regular, every vertex is adjacent to 3 other vertices and so on. regular graph

Bipartite Graph

A graph G=(V,E) is called a bipartite graph if its vertex set can be partitioned into two non-empty disjoint subsets A and B such that every edge (a,b) connects a vertex from A to B or vice versa. In bipartite graphs, there would be no edge that connects vertices from the same set.

A bipartite graph can be represented by colouring vertices of the different subsets in different colours. As shown in the below example, the vertices of the figure shown that can be split into 2 sets are depicted in blue and purple respectively.

Further they are added to their respective set and the connections can be shown in the image to the right. bipartite graph

Labelled Graph

A labelled graph G=(V,E) is one in which the vertices and edges of a graph are labelled with a name, data, or weight. This type of graph is also known as a weighted graph since the edges can consider some useful information.

These graphs are very useful in various purposes like calculating the minimum distance or shortest path between 2 places. labelled graph in data structure

Digraph Graph

A digraph, also known as a directed graph, is one in which the vertices are ordered in a particular sequence. Thus, the edges are usually represented by arrows that point in a particular direction.

In such a graph, changing the direction of arrows can alter the meaning of the graph. In the following figure A points to B, B to C, C to E, E to D, D to B and E to F.

Digraphs are commonly used in marking hierarchies in network administration, social media connection graphs and roadways. digraph graph in data structure

Subgraph

A graph in data structure is said to be a subgraph if it is a part of another graph. In the following example, graphs H1, H2 and H3 are subgraphs of the graph G. subgraph in data structure

2 Types of Subgraph

types of subgraph in data structure

Vertex Disjoint Subgraph

A subgraph with no common vertex is called a vertex disjoint subgraph. Any two graphs A = (V1, E1) and B = (V2, E2) are said to be vertex disjoint of a graph G = (V, E) if V1(A) intersection V2(B) = null.

Since vertices in a vertex disjoint graph cannot have a common edge, a vertex disjoint subgraph will always be an edge disjoint subgraph. In the above example, since subgraph EFG and ABCD have no vertex in common, they are said to be vertex disjoint subgraphs.

Edge Disjoint Subgraph

A subgraph with no common edge is called an edge disjoint subgraph. A subgraph is said to be edge disjoint if E1(A) intersection E2(B) = null. In the above example, since graphs ABCD and BEFG have no edge in common, they are said to be edge disjoint subgraphs.

Connected or Disconnected Graph

A unidirected graph is connected if there is a path from any vertex to any other vertex, or any vertex is reachable from any other vertex. A connected graph of n vertices has at least n-1 edges.

A disconnected graph is one that is not connected, i.e. at least one of the vertices is not connected to any other vertex. connected or disconnected graph in data structure

Cyclic Graph

A graph that has one or more cycles is called a cyclic graph. A jogging path around the city that connects various landmarks could be considered as a cyclic graph. In the following figure, BCDE represents a cyclic graph since it consists a cycle denoted by BCED. cyclic graph

Vertex Labeled Graph

A vertex labeled graph is one that holds some data in its vertices, in a way that they determine data of the edges. Consider a pair of vertices A and B, the edge between them can be thus called AB based on the label of the vertices. Such graphs are most useful in a road map where the label of vertices can be considered as the names of the landmarks. In the following figure, A, B, C, D, E represent the labels of a vertex labeled graph. vertex labeled graph

Directed Acyclic Graph

A directed acyclic graph, commonly known as DAG is a directed graph that doesn't contain any cycles. DAGs may be used to represent data flows, network of processing elements and even in project management.

Since in the following figure, the edges represent directions and there is no cycle present, the given graph is a DAG. directed acyclic graph in data structure

Conclusion

Graphs in Data structure are helpful in many applications. There are several sorts of graphs for various scenarios. It is used in computer networks, computational devices, sociology, geometry, conservation, and other fields. Most common uses of graphs are as follows:

  1. Graphs in data structure are used in the World Wide Web to link Web pages. In this format, the pages would be represented as vertices and link between them as edges.
  2. In computer networks, graphs are used to show the flow of computation between various devices and also to show how various devices are connected.
  3. Maps can be represented by graphs to show the roadways between various locations.