-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.java
More file actions
54 lines (45 loc) · 1.16 KB
/
Graph.java
File metadata and controls
54 lines (45 loc) · 1.16 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package Graph;
import java.util.*;
public class Graph {
@SuppressWarnings("unchecked")
public Graph(int v) {
numberOfVertices = v;
numberOfEdges = 0;
adj = (ArrayList<Integer>[])new ArrayList[v];
for (int i = 0; i < v; i++) {
adj[i] = new ArrayList<Integer>();
}
}
public void addEdge(int v, int w) {
adj[v].add(w);
adj[w].add(v);
numberOfEdges++;
}
public Iterable<Integer> adj(int v) {
return adj[v];
}
public int V() {return numberOfVertices;}
public int E() {return numberOfEdges;}
public void printGraph() {
for (int i = 0; i < numberOfVertices; i++) {
System.out.print(i + ": ");
for (int neighbor : adj[i]) {
System.out.print(neighbor + ", ");
}
System.out.println();
}
}
private int numberOfVertices;
private int numberOfEdges;
private ArrayList<Integer>[] adj;
}
/*class Node {
public Node(int k, int v) {
key = k;
value = v;
next = null;
}
int key;
int value;
Node next;
}*/