ProCodebaseProCodebase
  • ModusA Growth OS for your business, on WhatsAppKiosqSell more. Chase less.AI InterviewerAutomated screening & AI-led interviewsXperto AIAI prep companion for candidatesAI Tools HubResume builder, learning paths & more
  • Pre-Vetted DevelopersScreened, scored and ready to interviewAI-Native DevelopersSenior engineers on an hourly basis
  • Services
  • Features
  • Jobs
  • FAQs
Sign inBook a demo
  • Services
  • Features
  • Jobs
  • FAQs
Sign inBook a demo
ProCodebaseProCodebase

ProCodebase Technologies builds AI products for hiring and growth, and ships software for clients as a technical consultancy. We source, screen and deliver pre-vetted developers — so you only interview high-signal candidates.

Products

  • Modus
  • Kiosq
  • AI Interviewer
  • Xperto AI
  • AI Tools Hub

Hire & build

  • Pre-Vetted Developers
  • AI-Native Developers
  • Technical Consultancy
  • MVP Development
  • Features

Resources

  • Articles
  • Topics
  • Certifications
  • Collections
  • Jobs

Company

  • About Us
  • Contact Us
  • Book a Demo
  • FAQs

© 2026 ProCodebase Technologies. All rights reserved.

  • Privacy Policy
  • Terms & Conditions
  • Refund & Cancellation

Level Up Your Skills with Xperto-AI

A multi-AI agent platform that helps you level up your development skills and ace your interview preparation to secure your dream job.

Launch Xperto-AI

Implementation of Adjacency Matrix and Adjacency List in Graph Theory

author
Generated by
ProCodebase AI

16/11/2024

DSA

Sign in to read full article

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.

Adjacency Matrix

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.

Characteristics

  • Space Complexity: O(V^2), where V is the number of vertices. This means that the space used grows rapidly as the number of vertices increases.
  • Time Complexity for Edge Check: O(1). Checking for the existence of an edge between two vertices is efficient.
  • Best Used For: Dense graphs where the number of edges is close to the maximum number of edges.

Implementation in Java

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(); } } }

Example Usage

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(); }

Adjacency List

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.

Characteristics

  • Space Complexity: O(V + E), where E is the number of edges, making it more efficient for larger, sparser graphs.
  • Time Complexity for Edge Check: O(V) in the worst-case. Check speed can decrease, depending on the structure of the linked list.
  • Best Used For: Sparse graphs where the number of edges is significantly lower than V^2.

Implementation in Java

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"); } } }

Example Usage

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(); }

Conclusion

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.

Popular tags

DSAGraph TheoryJava

Share now!

Like & bookmark

Related collections

  • Top 20 DSA Interview Questions Mastery

    23/09/2024 · DSA

  • Advanced String-based Interview Techniques

    15/11/2024 · DSA

  • Advanced Recursion and Backtracking Problems Using Java

    13/10/2024 · DSA

  • DSA Mastery for Interviews

    23/09/2024 · DSA

  • Advanced Graph Interview Questions in Java

    16/11/2024 · DSA

Related articles

  • Custom Comparator for Priority Queue in Java

    16/11/2024 · DSA

  • Finding the Maximum Product Subarray

    15/11/2024 · DSA

  • Exploring Palindromic Substrings Count

    15/11/2024 · DSA

  • Understanding Bridges and Articulation Points in Graphs

    16/11/2024 · DSA

  • Counting Set Bits in a Number

    08/12/2024 · DSA

  • Array Partitioning

    06/12/2024 · DSA

  • Path with Maximum Sum

    15/11/2024 · DSA

Popular category

  • Python
  • Generative AI
  • Machine Learning
  • ReactJS
  • System Design