logologo
  • AI Tools

    DB Query GeneratorMock InterviewResume BuilderLearning Path GeneratorCheatsheet GeneratorAgentic Prompt GeneratorCompany ResearchCover Letter Generator
  • XpertoAI
  • MVP Ready
  • Resources

    CertificationsTopicsExpertsCollectionsArticlesQuestionsVideosJobs
logologo

Elevate Your Coding with our comprehensive articles and niche collections.

Useful Links

  • Contact Us
  • Privacy Policy
  • Terms & Conditions
  • Refund & Cancellation
  • About Us

Resources

  • Xperto-AI
  • Certifications
  • Python
  • GenAI
  • Machine Learning

Interviews

  • DSA
  • System Design
  • Design Patterns
  • Frontend System Design
  • ReactJS

Procodebase © 2024. All rights reserved.

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 Dynamic Programming Interview Questions

    15/11/2024 | DSA

  • Advanced Priority Queue and Heap Interview Questions in Java

    16/11/2024 | DSA

  • Mastering Arrays : The Basic Data Structure

    06/12/2024 | DSA

Related Articles

  • Understanding Priority Queue Implementation with Java Collections Framework

    16/11/2024 | DSA

  • Unraveling the Mystery of Finding Duplicates in Arrays

    06/12/2024 | DSA

  • Understanding Bipartite Graphs and Matching Problems in DSA

    16/11/2024 | DSA

  • Merge K Sorted Arrays Using Priority Queue

    16/11/2024 | DSA

  • Sliding Window Maximum Using Priority Queue

    16/11/2024 | DSA

  • Minimum Window Substring

    15/11/2024 | DSA

  • Palindrome Partitioning

    13/10/2024 | DSA

Popular Category

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