# How Do You Represent a Graph in Data Structure Explain With Example?

//

Heather Bennett

Representing a graph in a data structure is an essential concept in computer science and is widely used in various applications. A graph is a collection of nodes or vertices connected by edges.

It is used to represent relationships between objects or entities. There are several ways to represent a graph, each with its own advantages and disadvantages.

An adjacency matrix is one of the most common ways to represent a graph. It uses a two-dimensional array to store whether there is an edge between each pair of vertices. The rows and columns of the matrix represent the vertices, and the value at position (i, j) indicates whether there is an edge between vertex i and vertex j.

To illustrate this, let’s consider a simple example of a graph with 4 vertices:

```   A---B
|  / \
| /   \
|/     \
C-------D
```

In this example, we can represent the graph using an adjacency matrix as follows:

```    A  B  C  D
A   0  1  1  0
B   1  0  1  1
C   1  1  0  1
D   0  1  1  0
```

The value 1 at position (i, j) indicates that there is an edge between vertex i and vertex j. The value 0 indicates that there is no edge between them.

An adjacency list is another popular way to represent a graph. In this representation, each vertex has a list of adjacent vertices.

Let’s use the same example to illustrate how an adjacency list can be used to represent the graph:

```A: B, C
B: A, C, D
C: A, B, D
D: B, C
```

In this representation, each vertex is followed by a colon and then a comma-separated list of its adjacent vertices. For example, vertex A is adjacent to vertices B and C.

## Comparison

Both the adjacency matrix and adjacency list have their own advantages and disadvantages. The choice between them depends on the specific requirements of the application.

• Space complexity: The adjacency matrix requires space proportional to the square of the number of vertices. In contrast, the adjacency list requires space proportional to the number of edges.
• Edge operations: The adjacency matrix allows for efficient edge operations such as checking if there is an edge between two vertices or adding/removing edges.

On the other hand, the adjacency list allows for efficient iteration over all adjacent vertices of a given vertex.

• Sparse graphs vs. dense graphs: The adjacency matrix is more suitable for dense graphs where most pairs of vertices are connected by edges. In contrast, the adjacency list is more efficient for sparse graphs where only a few pairs of vertices are connected by edges.

## Conclusion

In this tutorial, we explored two common ways to represent a graph in data structures – the adjacency matrix and the adjacency list. We saw how they can be used to represent relationships between nodes or vertices in a graph. Each representation has its own advantages and disadvantages, so it’s important to choose the appropriate one based on your specific requirements.

To summarize:

• The adjacency matrix is efficient for dense graphs and allows for efficient edge operations.
• The adjacency list is efficient for sparse graphs and allows for efficient iteration over adjacent vertices.

By understanding and implementing these representations, you will be equipped with the knowledge to work with graphs in various applications.