Java Program to Implement the graph data structure

Java Program to Implement the graph data structure

What are Graphs?

In this Article, We will write a program to Implement the graph data structure in Java.
A graph is a data structure that consists of a collection of vertices (also called nodes) and edges that connect these vertices. It is used to model complex relationships between objects, such as networks, social structures, or data flow.

Graphs:

There are several ways to represent a graph in Java Which includes:

  • Adjacency Matrix: This representation uses a two-dimensional array to store the edges between vertices. If the element at position (i, j) is 1, it means that there is an edge between vertex i and vertex j.
  • Adjacency List: This representation uses a list of lists to store the edges between vertices. Each vertex in the graph has an associated list that contains its neighbors (i.e., vertices that are directly connected to it).
  • Edge List: This representation stores all the edges in the graph in a list, where each edge is represented as a pair of vertices.

Java provides several libraries for implementing graphs, such as JGraphT and GraphStream. These libraries offer a wide range of functionalities for creating, manipulating, and visualizing graphs.

Java Program to Implement the graph data structure

Java Program to Implement the graph data structure:

Run
// Importing all the required packages
import java.util.*;

// implementation graph class
class Graph{
    private int V;
    private LinkedList[] adjList;

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

    void addEdge(int src, int dest) {
        adjList[src].add(dest);
        adjList[dest].add(src);
    }

    void printGraph() {
        for (int i = 0; i < V; i++) {
            System.out.print("Vertex " + i + " is connected to: ");
            for (int j = 0; j < adjList[i].size(); j++) {
                System.out.print(adjList[i].get(j) + " ");
            }
            System.out.println();
        }
    }
}

class Main {
    public static void main(String[] args){
        // Object for graph class
        Graph g = new Graph(5);
        
        // Adding elements in the graph
        g.addEdge(0, 1);
        g.addEdge(0, 4);
        g.addEdge(1, 2);
        g.addEdge(1, 3);
        g.addEdge(1, 4);
        g.addEdge(2, 3);
        g.addEdge(3, 4);

        g.printGraph();
    }
}

Output:

Vertex 0 is connected to: 1 4 
Vertex 1 is connected to: 0 2 3 4 
Vertex 2 is connected to: 1 3 
Vertex 3 is connected to: 1 2 4 
Vertex 4 is connected to: 0 1 3 

In the above program, the Graph class represents a graph with V vertices, where each vertex is represented by an adjacency list. The addEdge method adds an undirected edge between two vertices, and the printGraph method prints out the adjacency list for each vertex. The Main class creates a new graph with 5 vertices and adds edges between them, and then prints out the resulting adjacency list.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription