-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.java
More file actions
33 lines (27 loc) · 708 Bytes
/
Graph.java
File metadata and controls
33 lines (27 loc) · 708 Bytes
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
import java.util.Arrays;
public class Graph {
private int v; // number of vertices
private int[][] graph; // graph represented using adjacency matrix
// constructor
Graph(int numVertices) {
v = numVertices;
graph = new int[v][v];
for (int row = 0; row < v; row++) {
Arrays.fill(graph[row], 0);
}
}
// get encapsulation
int[][] getGraph() {
return graph;
}
int getVertices() {
return v;
}
// add an edge to graph
void addEdge(int from, int to, int weight, boolean directed) {
graph[from][to] = weight;
if (!directed) {
graph[to][from] = weight;
}
}
}