Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save codewithrajranjan/5352d3da66421c997c28bf174f90555b to your computer and use it in GitHub Desktop.
Save codewithrajranjan/5352d3da66421c997c28bf174f90555b to your computer and use it in GitHub Desktop.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
class Edge {
int src;
int dest;
public Edge(int src,int dest) {
this.src = src;
this.dest = dest;
}
}
class Graph {
private ArrayList<ArrayList<Integer>> graph;
public Graph() {
// List of graph edges as per above diagram
List<Edge> edges = Arrays.asList(
new Edge(1, 0),
new Edge(2,1),
new Edge(3,4),
new Edge(4,0)
);
graph = new ArrayList<ArrayList<Integer>>();
for(int i=0;i<4;i++) {
graph.add(i,new ArrayList<Integer>());
}
// adding the vertex to the graph
for(int i=0;i<edges.size();i++) {
Edge e = edges.get(i);
graph.get(e.src).add(e.dest);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment