-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathGraph.java
More file actions
39 lines (34 loc) · 1.03 KB
/
Copy pathGraph.java
File metadata and controls
39 lines (34 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
public class Graph {
// Graph parameter: Number of vertices
private int V;
// Graph parameter: Number of edges
private int E;
// Array to store adjacency list for every vertex
private Bag<Integer>[] adj;
/* Constructor */
public Graph(int V) {
this.V = V;
this.E = 0;
// empty array of V bags for V vertices
this.adj = (Bag<Integer>[]) new Bag[V];
// initialise bags
for (int i = 0; i < V; i++)
adj[i] = new Bag<Integer>();
}
/* API: Add an edge to the graph */
public void addEdge(int v, int w) {
// add w to v's adjacency list
adj[v].add(w);
// add v to w's adjacency list
adj[w].add(v);
E++;
}
/* API: Return an iterable to adjacency list of vertex v */
public Iterable<Integer> adj(int v) {
return adj[v];
}
/* API: Return number of vertices in graph */
public int V() { return V; }
/* API: Return number of edges in graph */
public int E() { return E; }
}