How Do You Create a Graph in Data Structure in Java?


Larry Thompson

Creating a graph in data structure is an essential skill for any Java programmer. In this tutorial, we will learn how to create a graph using Java and explore some common operations associated with it.

What is a Graph?

A graph is a non-linear data structure that consists of vertices (also called nodes) and edges. Vertices represent the entities, while edges represent the connections between these entities. Graphs are widely used in various applications such as social networks, maps, and computer networks.

There are two main types of graphs: directed and undirected graphs. In a directed graph, edges have a specific direction, while in an undirected graph, edges do not have any direction.

Representing a Graph

There are several ways to represent a graph in Java. One commonly used representation is the adjacency list representation. In this representation, we use an array of lists to store the vertices and their corresponding adjacency lists.


import java.util.*;

class Graph {
private int V;
private LinkedList[] adjList;

public Graph(int v) {
V = v;
adjList = new LinkedList[V];
for (int i = 0; i < V; i++) { adjList[i] = new LinkedList<>();

public void addEdge(int src, int dest) {
// For undirected graph, add this line as well
// adjList[dest].add(src);

public void printGraph() {
for (int i = 0; i < V; i++) { System.out.print("Vertex " + i + " is connected to: "); for (Integer vertex : adjList[i]) { System.print(vertex + " "); } System.println(); } } } public class Main { public static void main(String[] args) { int V = 5; // Number of vertices Graph graph = new Graph(V); 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.printGraph(); } } ```

In this example, we create a class called `Graph` that represents a directed graph. The `addEdge` method is used to add an edge between two vertices. The `printGraph` method is used to print the adjacency list representation of the graph.

We then create a `Main` class where we initialize a `Graph` object and add some edges using the `addEdge` method. Finally, we call the `printGraph` method to display the adjacency list representation of the graph.

Common Operations on Graphs

Now that we know how to create a graph in Java, let’s explore some common operations that can be performed on graphs:

  • Adding an Edge: To add an edge between two vertices in a graph, we use the `addEdge` method as shown in the previous example.
  • Removing an Edge: To remove an edge between two vertices in a graph, we can modify the `addEdge` method to remove the specified edge from the adjacency list.
  • Finding Neighbors: To find all neighbors of a vertex in a graph, we simply need to iterate over its adjacency list.
  • Checking if Two Vertices are Connected: To check if two vertices are connected in a graph, we can iterate over the adjacency list of one vertex and check if the other vertex is present in the list.
  • Traversing the Graph: To traverse a graph, we can use various graph traversal algorithms such as Depth-First Search (DFS) and Breadth-First Search (BFS).

By understanding these operations, you will be able to manipulate and analyze graphs effectively in your Java programs.


In this tutorial, we learned how to create a graph using Java and explored some common operations associated with it. We also discussed different ways to represent a graph in Java, with a focus on the adjacency list representation. With this knowledge, you can now start building powerful applications that involve graphs.

Remember to practice regularly and experiment with different graph-related algorithms to strengthen your understanding. Happy coding!

Discord Server - Web Server - Private Server - DNS Server - Object-Oriented Programming - Scripting - Data Types - Data Structures

Privacy Policy