Code for Graph
//time: O(logV), space: O(V + E)
import java.util.*;
public class Graph {
private int V;
private int E;
private Set<Integer> adj;
public int V() {return V;}
public int E() {return E;}
public Graph(int V) {
this.V = V;
this.adj = (Set<Integer>) new HashSet[V];
for(int i = 0; i < adj.length; i++) {
adj[i] = new HashSet<Integer>();
}
}
public void addEdge(int v, int w) {
adj[v].add(w);
adj[w].add(v);
E++;
}
public Iterator<Integer> adj(int v) {return adj[v];}
}
//time: O(logV), space: O(V + E)
import java.util.*;
public class Graph {
private int V;
private int E;
private Set<Integer> adj;
public int V() {return V;}
public int E() {return E;}
public Graph(int V) {
this.V = V;
this.adj = (Set<Integer>) new HashSet[V];
for(int i = 0; i < adj.length; i++) {
adj[i] = new HashSet<Integer>();
}
}
public void addEdge(int v, int w) {
adj[v].add(w);
adj[w].add(v);
E++;
}
public Iterator<Integer> adj(int v) {return adj[v];}
}