When navigating the vast world of graph theory, understanding how to represent graphs efficiently is crucial. Among various representations, the adjacency matrix and adjacency list are the most commonly used. Both have their unique advantages and are suited for different scenarios. Let's break down each representation, focusing on the Java implementation that allows us to work with graphs seamlessly.
An adjacency matrix is a 2D array used to represent a finite graph. The rows and columns of the matrix correspond to the vertices of the graph. If there is an edge connecting vertex i
and vertex j
, the cell at row i
and column j
is set to 1 (or the weight of the edge, in the case of weighted graphs), and 0 otherwise.
public class AdjacencyMatrix { private int[][] matrix; private int vertices; public AdjacencyMatrix(int v) { vertices = v; matrix = new int[v][v]; } public void addEdge(int src, int dest) { matrix[src][dest] = 1; // for unweighted graph matrix[dest][src] = 1; // for undirected graph } public void display() { for (int i = 0; i < vertices; i++) { for (int j = 0; j < vertices; j++) { System.out.print(matrix[i][j] + " "); } System.out.println(); } } }
public static void main(String[] args) { AdjacencyMatrix graph = new AdjacencyMatrix(5); graph.addEdge(0, 1); graph.addEdge(0, 4); graph.addEdge(1, 2); graph.addEdge(1, 3); graph.addEdge(1, 4); graph.addEdge(2, 3); graph.addEdge(3, 4); graph.display(); }
An adjacency list is a collection of lists or arrays. Each list corresponds to a vertex in the graph and contains a list of all adjacent vertices (the vertices it is connected to). This method is more space-efficient compared to the adjacency matrix, particularly for sparse graphs.
import java.util.LinkedList; public class AdjacencyList { private LinkedList<Integer>[] adjList; private int vertices; public AdjacencyList(int v) { vertices = v; adjList = new LinkedList[v]; for (int i = 0; i < v; i++) { adjList[i] = new LinkedList<>(); } } public void addEdge(int src, int dest) { adjList[src].add(dest); // for directed graph // For undirected graph, uncomment the line below: // adjList[dest].add(src); } public void display() { for (int i = 0; i < vertices; i++) { System.out.print(i + ": "); for (Integer edge : adjList[i]) { System.out.print(edge + " -> "); } System.out.println("null"); } } }
public static void main(String[] args) { AdjacencyList graph = new AdjacencyList(5); graph.addEdge(0, 1); graph.addEdge(0, 4); graph.addEdge(1, 2); graph.addEdge(1, 3); graph.addEdge(1, 4); graph.addEdge(2, 3); graph.addEdge(3, 4); graph.display(); }
Understanding these two representations of graphs is essential in computer science and software engineering for algorithm design and optimization. Each has its advantages and use cases, emphasizing the need for familiarity with both when dealing with graph-related problems in data structures and algorithms.
08/12/2024 | DSA
15/11/2024 | DSA
16/11/2024 | DSA
06/12/2024 | DSA
23/09/2024 | DSA
13/10/2024 | DSA
16/11/2024 | DSA
16/11/2024 | DSA
06/12/2024 | DSA
15/11/2024 | DSA
16/11/2024 | DSA
15/11/2024 | DSA