# Graph Representation and Traversal Techniques

Graphs are an essential data structure in many computer science applications. They can efficiently represent a wide range of problems, allowing us to analyze and solve complex scenarios in various domains. In this article, we will explore graph representation and traversal techniques using Java for competitive programming.

## Graph Representation

A graph can be represented in different ways, each with its advantages and use cases. Here are two commonly used graph representations:

### 1. Adjacency Matrix

The adjacency matrix is a 2-dimensional array that stores the connections between vertices. In this matrix, `matrix[i][j]` will be true if there is an edge between vertices `i` and `j`, and false otherwise. The matrix size is usually `N x N`, where `N` is the number of vertices in the graph.

``````// Example adjacency matrix representation
boolean[][] matrix = new boolean[N][N];
matrix[0][1] = true; // Edge between vertex 0 and vertex 1
matrix[1][2] = true; // Edge between vertex 1 and vertex 2
// ...``````

The adjacency matrix is efficient for dense graphs (i.e., graphs with many edges) and provides constant time lookup for checking if an edge exists between two vertices. However, it requires `O(N^2)` space, which may be inefficient for sparse graphs.

### 2. Adjacency List

The adjacency list representation maintains a list of adjacent vertices for each vertex in the graph. It is usually implemented using an array of linked lists, where each index in the array corresponds to a vertex.

``````// Example adjacency list representation
adjList[0].add(1); // Edge between vertex 0 and vertex 1
adjList[1].add(2); // Edge between vertex 1 and vertex 2
// ...``````

The adjacency list representation is memory-efficient for sparse graphs, as it only uses `O(N + E)` space, where `E` is the number of edges. Traversing the adjacent vertices of a vertex is straightforward as well. However, it takes `O(N)` time to check if an edge exists between two vertices.

## Graph Traversal Techniques

Graph traversal refers to the process of visiting all the vertices or edges of a graph. There are two commonly used techniques for graph traversal:

### 1. Depth-First Search (DFS)

Depth-First Search explores the graph by visiting as far as possible along each branch before backtracking. It uses a stack data structure to keep track of visited vertices.

``````// Depth-First Search traversal using recursion
boolean[] visited = new boolean[N];
dfs(0, visited);

void dfs(int vertex, boolean[] visited) {
visited[vertex] = true; // Mark current vertex as visited
// Process vertex data or perform required operations

// Traverse all adjacent vertices if not visited
}
}
}``````

DFS can be used to find connected components, detect cycles, and solve problems involving depth-based exploration.

### 2. Breadth-First Search (BFS)

Breadth-First Search explores the graph by visiting all the vertices of a particular depth before moving to the next level. It uses a queue data structure to keep track of visited vertices.

``````// Breadth-First Search traversal using queue
boolean[] visited = new boolean[N];
bfs(0, visited);

void bfs(int startVertex, boolean[] visited) {
Queue<Integer> queue = new LinkedList<>();
visited[startVertex] = true;

while (!queue.isEmpty()) {
int vertex = queue.poll();
// Process vertex data or perform required operations

// Traverse all adjacent vertices if not visited