Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.61 KB | None | 0 0
  1. class Graph {
  2.     int numberOfVertices;
  3.     ArrayList<Integer>[] adjacencyList;
  4.     public Graph(int numberOfVertices){
  5.         this.numberOfVertices=numberOfVertices;
  6.         adjacencyList=new ArrayList[numberOfVertices];
  7.         for(int i=0;i<numberOfVertices;i++)
  8.             adjacencyList[i]=new ArrayList<>();
  9.     }
  10.  
  11.     public void addEdge(int x,int y){
  12.         adjacencyList[x].add(y);
  13.     }
  14.    
  15.     public ArrayList<Integer> getAdjacentVertices(int x){
  16.         return adjacencyList[x];
  17.     }
  18.    
  19.     public boolean hasEdge(int x,int y){
  20.         return adjacencyList[x].contains(y);
  21.     }
  22.  
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement