# How Do You Implement a Graph in Data Structure?

//

Larry Thompson

Implementing a graph in a data structure is an essential concept in computer science and has numerous applications in various domains. In this article, we will explore the steps involved in implementing a graph and discuss some popular graph representation techniques.

## Understanding Graphs

A graph is a collection of vertices or nodes connected by edges. It is used to represent relationships between objects or entities. Graphs can be classified into various types based on their properties, such as directed or undirected, weighted or unweighted, cyclic or acyclic.

## Graph Representation Techniques

When it comes to implementing a graph, there are two popular representation techniques:

An adjacency matrix is a 2-dimensional array that represents the connections between vertices in a graph. Each cell of the matrix holds information about whether an edge exists between two vertices. If there is an edge, the cell value can be set to 1 or the weight of the edge if it’s a weighted graph.

1. Create a square matrix with size VxV, where V is the number of vertices.
2. Initialize all cells of the matrix with 0 (or infinity for weighted graphs).
3. If there is an edge between vertex i and vertex j, set matrix[i][j] = 1 (or weight).

This representation allows for efficient lookup of edges but requires O(V^2) space even if the graph has only a few connections.

The adjacency list representation uses an array of linked lists or arrays to store information about edges in a graph. Each vertex has its own list containing all the vertices it is connected to.

1. Create an array of size V, where V is the number of vertices.
2. For each vertex, create a linked list or array to store its adjacent vertices.
3. Add the connected vertices to the respective lists or arrays.

This representation is memory-efficient as it requires space proportional to the number of edges in the graph. However, lookup operations may take longer compared to an adjacency matrix.

## Graph Operations

Once we have implemented a graph using either adjacency matrix or adjacency list, we can perform various operations on it:

To add a new vertex to a graph:

1. In an adjacency matrix, increase the size of the matrix and initialize all new cells with 0 (or infinity).
2. In an adjacency list, create a new array element or linked list for the new vertex.

To add an edge between two vertices:

1. In an adjacency matrix, set matrix[i][j] = 1 (or weight) and matrix[j][i] = 1 (or weight) for undirected graphs.
2. In an adjacency list, add j to the linked list or array of vertex i and vice versa for undirected graphs.

### 3. Removing a Vertex

To remove a vertex from a graph:

1. In an adjacency matrix, delete the row and column corresponding to the vertex from the matrix and shift remaining elements accordingly.
2. In an adjacency list, remove the linked list or array element for the vertex and update the connections of other vertices accordingly.

### 4. Removing an Edge

To remove an edge between two vertices:

1. In an adjacency matrix, set matrix[i][j] = 0 (or infinity) and matrix[j][i] = 0 (or infinity) for undirected graphs.
2. In an adjacency list, remove j from the linked list or array of vertex i and vice versa for undirected graphs.

## Conclusion

Implementing a graph in a data structure is crucial for solving graph-related problems efficiently. The choice between using an adjacency matrix or adjacency list depends on the specific requirements of the problem at hand. Both representations have their pros and cons in terms of space complexity and lookup time.

By understanding graph representation techniques and performing various operations on graphs, you can unlock the power of this versatile data structure in your programming endeavors.