-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGraph.java
More file actions
30 lines (24 loc) · 747 Bytes
/
Copy pathGraph.java
File metadata and controls
30 lines (24 loc) · 747 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
package Graph;
import java.util.ArrayList;
import java.util.List;
public class Graph {
List<List<Edge>> adjacencyList;
int numVertices;
public Graph(List<Edge> list, int vertices) {
if (list.isEmpty()) {
throw new IllegalArgumentException("Null list of edges");
}
if (vertices < 0) {
throw new IllegalArgumentException("Number of vertices is -ve");
}
numVertices = vertices;
adjacencyList = new ArrayList<>(numVertices);
for (int i = 0; i < numVertices; ++i) {
adjacencyList.add(i, new ArrayList<>());
}
for (int i = 0; i < list.size(); ++i) {
Edge edge = list.get(i);
adjacencyList.get(edge.source).add(new Edge(edge.source, edge.destination, edge.weight));
}
}
}