-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.java
More file actions
29 lines (22 loc) · 721 Bytes
/
Graph.java
File metadata and controls
29 lines (22 loc) · 721 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
import java.util.HashMap;
// a data structure representing a Graph
public class Graph {
HashMap<String,Vertex> vertices;
public Graph() {
vertices = new HashMap<String,Vertex>();
}
public Vertex addVertex(String info, int xcor, int ycor) {
Vertex v = new Vertex(info, xcor, ycor);
vertices.put(info,v);
return v;
}
public void addEdge(String v1name, String v2name, int edgedistance) {
if (vertices.containsKey(v1name) && vertices.containsKey(v2name)) {
Edge e = new Edge(vertices.get(v1name), vertices.get(v2name), edgedistance);
vertices.get(v1name).addEdge(e);
vertices.get(v2name).addEdge(e);
}
else
System.out.println("Graph does not contain the inputted vertices.");
}
}