Posts

Showing posts with the label Adjacency List

Graph Traversal Code - DFS on Graph represented using Adjacency List

Image
  import java.util.*; // A graph node  class GraphNode{     String name;     // this is a list of adjacent nodes for the current node and is  used in adjacency list     ArrayList<GraphNode> neighbours;          // constructor     GraphNode(String name){         this.name = name;         this.neighbours = new ArrayList<>();     } } public class Main {     // The node list of graph , also used to maintain adjacency list     ArrayList<GraphNode> nodeList;          // constructor     Main(){         this.nodeList = new ArrayList<>();     }            // function to add an undirected and unweighted edge between two nodes with index i and j in nodeList     public void addUndirectedEdge(int i, int j){     ...

Graph Traversal Code - BFS on Graph represented using Adjacency List

Image
  import java.util.*; // A graph node  class GraphNode{     String name;     // this is a list of adjacent nodes for the current node and is  used in adjacency list     ArrayList<GraphNode> neighbours;          // constructor     GraphNode(String name){         this.name = name;         this.neighbours = new ArrayList<>();     } } public class Main {          // The node list of graph , also used to maintain adjacency list     ArrayList<GraphNode> nodeList;          // constructor     Main(){         this.nodeList = new ArrayList<>();     }          // function to add an undirected and unweighted edge between two nodes with index i and j in nodeList     public void addUndirectedEdge(int i, int j){ ...