Last active
July 4, 2019 16:47
-
-
Save codewithrajranjan/5352d3da66421c997c28bf174f90555b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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