Introduction to Graph

Graphs are a fundamental data structure in computer science used to represent relationships between pairs of objects. They consist of a set of vertices (nodes) and a set of edges (connections).

Mentor

Blog

What is Graph

A graph is a set of vertices (nodes) that are connected to each other via edges in the form of a network.

Representing graphs

1- Adjacency List.

2- Adjacency Matrix.

Adjacency Matrix

The adjacency matrix is a two-dimensional matrix where each cell can contain a 0 or 1. The row and column headings represent the vertices.

Adjacency List

An array of Linked Lists is used to store edges between two vertices. The size of the array is equal to the number of vertices. Each index in this array represents a specific vertex in the graph. The entry at the index i of the array contains a linked list containing the vertices that are adjacent to vertex i.

Types of Graph Traversals 

There are two basic techniques used for graph traversal:

  1. Breadth First Search (BFS)
    1. Depth First Search (DFS)

      Implementation In Java

      import java.util.*;
      
      class Graph {
          private int V;
          private LinkedList<Integer> adj[];
      
          Graph(int v) {
              V = v;
              adj = new LinkedList[v];
              for (int i = 0; i < v; ++i)
                  adj[i] = new LinkedList();
          }
      
          void addEdge(int v, int w) {
              adj[v].add(w);
          }
      
          void BFS(int s) {
              boolean visited[] = new boolean[V];
              LinkedList<Integer> queue = new LinkedList<Integer>();
      
              visited[s] = true;
              queue.add(s);
      
              while (queue.size() != 0) {
                  s = queue.poll();
                  System.out.print(s + " ");
      
                  // Iterate over the neighbors of vertex s using a for loop
                  for (int n : adj[s]) {
                      if (!visited[n]) {
                          visited[n] = true;
                          queue.add(n);
                      }
                  }
              }
          }
      
          void DFSUtil(int v, boolean visited[]) {
              visited[v] = true;
              System.out.print(v + " ");
      
              // Iterate over the neighbors of vertex v using a for loop
              for (int n : adj[v]) {
                  if (!visited[n])
                      DFSUtil(n, visited);
              }
          }
      
          void DFS(int v) {
              boolean visited[] = new boolean[V];
              DFSUtil(v, visited);
          }
      
          public static void main(String args[]) {
              Graph g = new Graph(4);
      
              g.addEdge(0, 1);
              g.addEdge(0, 2);
              g.addEdge(1, 2);
              g.addEdge(2, 0);
              g.addEdge(2, 3);
              g.addEdge(3, 3);
      
              System.out.println("Following is Breadth First Traversal " + "(starting from vertex 2)");
              g.BFS(2);
      
              System.out.println("\nFollowing is Depth First Traversal " + "(starting from vertex 2)");
              g.DFS(2);
          }
      }
      

      That's all in this blog. Feel free to book 1:1 session with me where we can discuss thing at the length.