Programming & Development / April 19, 2025

Implementing Directed and Undirected Graphs in Java Using Adjacency Lists

Java Graph Directed Graph Undirected Graph Adjacency List Data Structures Algorithms Graph Implementation Java Graph Example Interview Prep

✅ Directed Graph in Java

💡 Characteristics:

  • Edges have direction.
  • If there's an edge from A → B, it's not implied that B → A exists.

🔧 Implementation

java

import java.util.*;

public class DirectedGraph {
    private Map<Integer, List<Integer>> adjList = new HashMap<>();

    public void addVertex(int vertex) {
        adjList.putIfAbsent(vertex, new ArrayList<>());
    }

    public void addEdge(int from, int to) {
        adjList.putIfAbsent(from, new ArrayList<>());
        adjList.putIfAbsent(to, new ArrayList<>());
        adjList.get(from).add(to); // One-way edge
    }

    public void printGraph() {
        for (var entry : adjList.entrySet()) {
            System.out.print("Vertex " + entry.getKey() + " → ");
            System.out.println(entry.getValue());
        }
    }

    public static void main(String[] args) {
        DirectedGraph graph = new DirectedGraph();
        graph.addVertex(1);
        graph.addVertex(2);
        graph.addVertex(3);

        graph.addEdge(1, 2);
        graph.addEdge(2, 3);
        graph.addEdge(1, 3);

        graph.printGraph();
    }
}

✅ Undirected Graph in Java

💡 Characteristics:

  • Edges are bidirectional.
  • If A is connected to B, then B is also connected to A.

🔧 Implementation

java

import java.util.*;

public class UndirectedGraph {
    private Map<Integer, List<Integer>> adjList = new HashMap<>();

    public void addVertex(int vertex) {
        adjList.putIfAbsent(vertex, new ArrayList<>());
    }

    public void addEdge(int v1, int v2) {
        adjList.putIfAbsent(v1, new ArrayList<>());
        adjList.putIfAbsent(v2, new ArrayList<>());

        adjList.get(v1).add(v2);
        adjList.get(v2).add(v1); // Reverse edge for undirected connection
    }

    public void printGraph() {
        for (var entry : adjList.entrySet()) {
            System.out.print("Vertex " + entry.getKey() + " ↔ ");
            System.out.println(entry.getValue());
        }
    }

    public static void main(String[] args) {
        UndirectedGraph graph = new UndirectedGraph();
        graph.addVertex(1);
        graph.addVertex(2);
        graph.addVertex(3);

        graph.addEdge(1, 2);
        graph.addEdge(2, 3);
        graph.addEdge(1, 3);

        graph.printGraph();
    }
}

🔁 Summary Table

FeatureDirected GraphUndirected GraphEdge DirectionOne-way (A → B)Two-way (A ↔ B)addEdge BehaviorAdds edge from from → to onlyAdds both v1 → v2 and v2 → v1Use CasesWeb Crawlers, Dependency GraphsSocial Networks, Road Maps


Comments

No comments yet

Add a new Comment

NUHMAN.COM

Information Technology website for Programming & Development, Web Design & UX/UI, Startups & Innovation, Gadgets & Consumer Tech, Cloud Computing & Enterprise Tech, Cybersecurity, Artificial Intelligence (AI) & Machine Learning (ML), Gaming Technology, Mobile Development, Tech News & Trends, Open Source & Linux, Data Science & Analytics

Categories

Tags

©{" "} Nuhmans.com . All Rights Reserved. Designed by{" "} HTML Codex