-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeightedGraph.java
More file actions
51 lines (37 loc) · 1.04 KB
/
WeightedGraph.java
File metadata and controls
51 lines (37 loc) · 1.04 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
import java.util.ArrayList;
import java.util.HashMap;
public class WeightedGraph<E>{
private class Edge{
private E vertexFrom;
private E vertextTo;
private int weight;
public Edge(E _vertexFrom, E _vertexTo, int weight_){
vertexFrom= _vertexFrom;
vertexTo = _vertexTo;
weight = _weight;
}
public int getWeight(){
return weight;
}
}
private ArrayList<E> vertices;
private ArrayList<Edge> edges;
public int numVertices(){
return vertices.size();
}
public int numEdges(){
return edges.size();
}
public void insertVertex(E vertex){
if( containsVertex(vertex) ) return;
vertices.add( vertex );
}
public boolean containsVertex(E vertex){
return vertices.contains(vertex);
}
public void insertEdge(E vertexFrom, E vertexTo, int weight){
insert(vertexFrom);
insert(vertexTo);
edges.add( new Edge(vertexFrom, vertexTo, weight) );
}
}