Home » Data Structure » Graph and Types of Graph in Data Structure

Graph and Types of Graph in Data Structure

Last updated:

Support this post with a reaction:

6,151 views

A graph is a non-linear data structure that consists of a finite set of vertices (also called nodes) and a set of edges that connect these vertices.

A graph is used to represent relationships or connections between different objects. Unlike linear data structures such as arrays, linked lists, stacks and queues, graph elements are not arranged in a specific sequential order.

A vertex represents an object, while an edge represents the connection between two objects.

For example, a social networking application can use a graph to represent users and their friendships. Each user is represented by a vertex, and a friendship between two users is represented by an edge.

Graphs are widely used in computer networks, Google Maps, social networking applications, recommendation systems and many other real-world applications.

Mathematical Representation of a Graph

Mathematically, a graph is represented as:

G = (V, E)

Where:

  • G represents the graph.
  • V represents the finite set of vertices.
  • E represents the set of edges connecting the vertices.

Let’s understand this with an example.

Consider a graph containing four vertices: A, B, C and D.

Vertices:

V(G) = {A, B, C, D}

Edges:

E(G) = {(A, B), (B, C), (A, D), (C, D)}

Here, the edges represent the connections between the vertices.

Figure: Example of a graph containing four vertices and four edges.

In this example:

  • Vertex A is connected to vertices B and D.
  • Vertex B is connected to vertices A and C.
  • Vertex C is connected to vertices B and D.
  • Vertex D is connected to vertices A and C.

Note: The mathematical representation of an edge depends on the graph type. In an undirected graph, (A, B) and (B, A) represent the same edge. In a directed graph, they represent different edges.

Types of Graphs in Data Structure

Graphs can be classified into different types based on the direction of edges, weights, connectivity and other properties.

Some important types of graphs are explained below.

1. Directed Graph

A directed graph is a graph in which every edge has a specific direction. The direction indicates how one vertex is connected to another vertex.

A directed graph is also called a digraph.

In a directed graph, an edge is represented using an ordered pair (A, B). It indicates that the edge starts from vertex A and points towards vertex B.

Figure: Example of a directed graph showing the direction of edges.

For an edge (A, B):

  • A is the starting vertex or source vertex.
  • B is the ending vertex or destination vertex.
  • A is called the predecessor of B.
  • B is called the successor of A.
  • A and B are adjacent vertices.

Real-world example: Following another user on a social media platform can be represented using a directed graph. If user A follows user B, it does not necessarily mean that user B follows user A.

2. Undirected Graph

An undirected graph is a graph where the edges have no direction. This means you can move between two connected vertices in both ways. An edge in an undirected graph is written as a pair of nodes (a,b).

Lets see below example of undirected graph

Directed graph in data structure

For an edge {A, B}:

  • The edge connects vertices A and B.
  • We can move from A to B and from B to A.
  • There is no fixed starting or ending vertex.
  • A and B are called adjacent vertices.

Real-world example: A mutual friendship between two users can be represented using an undirected graph because both users are connected to each other.

3. Weighted Graph

A weighted graph is a graph in which each edge has an associated numerical value called a weight.

The weight can represent distance, cost, time, capacity or any other measurable value.

For example, in a road network:

  • Vertices represent cities.
  • Edges represent roads connecting cities.
  • Weights represent the distance between cities.

Weighted graphs are useful in shortest-path algorithms such as Dijkstra’s algorithm.

4. Unweighted Graph

An unweighted graph is a graph in which edges do not have any associated weights.

All edges are treated equally when determining the number of connections or the number of steps between vertices.

For example, a basic friendship network can be represented using an unweighted graph when we only want to know which users are connected.

5. Connected Graph

A connected graph is an undirected graph in which there is a path between every pair of vertices.

In simple words, we can reach any vertex from any other vertex by following the edges.

6. Disconnected Graph

A disconnected graph is a graph in which at least one pair of vertices does not have a path connecting them.

Such a graph contains two or more connected components.

7. Cyclic Graph

A cyclic graph is a graph that contains at least one cycle.

A cycle is a path that starts and ends at the same vertex.

For example, if vertices A, B and C are connected as A → B → C → A, the graph contains a cycle.

8. Acyclic Graph

An acyclic graph is a graph that does not contain any cycle.

A tree is an example of an undirected acyclic graph that is also connected.

A directed acyclic graph is known as a DAG (Directed Acyclic Graph).

9. Complete Graph

A complete graph is a simple undirected graph in which every pair of distinct vertices is directly connected by an edge.

For an undirected complete graph containing n vertices, the total number of edges is:

E = n(n – 1) / 2

For example, a complete graph with four vertices contains six edges.

10. Simple Graph

A simple graph is an undirected graph that does not contain self-loops or multiple edges between the same pair of vertices.

A self-loop is an edge that connects a vertex to itself.

Basic Terminology of Graph in Data Structure

To understand graph data structures and graph algorithms, you should know the following important terms.

1. Vertex (Node)

A vertex is an individual element or point in a graph. It is also called a node.

For example, in a social networking graph, each user represents a vertex.

2. Edge

An edge is a connection between two vertices in a graph.

For example, if A and B are connected, the edge can be represented as (A, B) in a directed graph or {A, B} in an undirected graph.

3. Adjacent Vertices

Two vertices are called adjacent vertices if there is a direct edge connecting them.

For example, if an edge connects A and B, then A and B are adjacent to each other.

4. Path

A path is a sequence of vertices in which each consecutive pair of vertices is connected by an edge.

For example:

A → B → C → D

This represents a path from vertex A to vertex D.

5. Degree

The degree of a vertex is the number of edges incident to that vertex.

In an undirected graph, the degree is the total number of edges connected to a vertex. A self-loop contributes two to the degree.

In a directed graph, the total degree is calculated using:

Degree(v) = In-degree(v) + Out-degree(v)

6. In-Degree

The in-degree of a vertex is the number of directed edges entering that vertex.

For example, if three edges point towards vertex A, its in-degree is 3.

7. Out-Degree

The out-degree of a vertex is the number of directed edges leaving that vertex.

For example, if two edges start from vertex A and point towards other vertices, its out-degree is 2.

8. Cycle

A cycle is a path that starts and ends at the same vertex.

For example:

A → B → C → A

This represents a cycle containing three edges.

9. Loop (Self-Loop)

A loop or self-loop is an edge that connects a vertex to itself.

For example, an edge (A, A) is a self-loop in a directed graph.

10. Connected Components

A connected component is a maximal connected subgraph of an undirected graph.

In simple words, it is a group of vertices in which every vertex can reach every other vertex in that group.

Difference Between Directed and Undirected Graph

Directed GraphUndirected Graph
Edges have a specific direction.Edges do not have a direction.
Edges are represented using ordered pairs.Edges are represented using unordered pairs.
A → B does not necessarily mean B → A.A — B means both vertices are connected.
In-degree and out-degree are used.Degree is used.
Example: Following users on social media.Example: Mutual friendship between users.

Graph Representation in Data Structure

A graph can be stored in computer memory using different representations.

The two most commonly used graph representations are:

  1. Adjacency Matrix
  2. Adjacency List

1. Adjacency Matrix

An adjacency matrix is a two-dimensional array used to represent connections between vertices.

If a graph contains V vertices, its adjacency matrix has V rows and V columns.

Each cell indicates whether an edge exists between two vertices.

For an unweighted graph:

  • 1 represents the presence of an edge.
  • 0 represents the absence of an edge.

For example, consider an undirected graph with three vertices A, B and C, where A is connected to B and B is connected to C.

Its adjacency matrix is:

VertexABC
A010
B101
C010

The adjacency matrix requires O(V²) space.

Advantages:

  • Checking whether an edge exists is fast: O(1).
  • Easy to implement using a two-dimensional array.
  • Suitable for dense graphs.

Disadvantages:

  • Requires O(V²) space even when the graph has very few edges.
  • Adding or removing a vertex can require rebuilding the matrix.

2. Adjacency List

An adjacency list is a graph representation in which each vertex stores a list of its adjacent vertices.

For example, consider the same graph containing vertices A, B and C.

Its adjacency list is:

A → B
B → A, C
C → B

Each vertex maintains a list of the vertices directly connected to it.

For a simple adjacency-list representation, the space complexity is O(V + E).

Advantages:

  • Uses less memory for sparse graphs.
  • Efficient for visiting neighboring vertices.
  • Suitable for implementing graph traversal algorithms.

Disadvantages:

  • Checking whether a particular edge exists may take O(degree of vertex) time.
  • Implementation is slightly more complex than an adjacency matrix.

Applications of Graphs in Data Structure

Graphs are used in many real-world applications where relationships and connections need to be represented.

Some common applications include:

  1. Social Networking: Users are represented as vertices, and friendships or followers are represented as edges.
  2. Computer Networks: Computers, routers and servers are represented as vertices, while network connections are represented as edges.
  3. Google Maps and Navigation: Locations are represented as vertices, roads as edges and distances or travel times as weights.
  4. Recommendation Systems: Graphs help represent relationships between users, products, movies and interests.
  5. Web Page Linking: Web pages are represented as vertices, and hyperlinks are represented as directed edges.
  6. Project Scheduling: Dependencies between tasks can be represented using a Directed Acyclic Graph (DAG).
  7. Network Routing: Graph algorithms help determine efficient paths through communication networks.

Advantages of Graph Data Structure

  • Graphs represent complex relationships between different objects.
  • They can model both directed and undirected connections.
  • They are useful for finding paths between different vertices.
  • They support efficient traversal using BFS and DFS.
  • They are widely applicable to real-world networking and optimization problems.

Disadvantages of Graph Data Structure

  • Graph implementation can be more complex than linear data structures.
  • Graph algorithms may require significant processing time for large graphs.
  • An adjacency matrix can consume a large amount of memory for sparse graphs.
  • Some graph problems are computationally expensive to solve.