This repository was archived by the owner on Nov 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraph.java
88 lines (68 loc) · 2.47 KB
/
Graph.java
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
* Name: Adam Kaplan
*
* NetID: akaplan6
*
* Project: #4
*
* Lab Section: TR 4:50PM - 6:05PM (I switched my lab section)
*
* TA: Charlie Kelman
*
* I affirm that I have not given or received any unauthorized help on this assignment, and that this work is my own.
*/
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
public class Graph {
int vertexCount, edgeCount;
HashMap<String, Vertex> vertexList;
double maxX, maxY;
double minX, minY;
LinkedList<Vertex> shortestPathVertices;
LinkedList<Edge> shortestPathEdges;
LinkedList<Edge> meridianPathEdges;
public Graph(){
vertexList = new HashMap<String, Vertex>();
shortestPathVertices = new LinkedList<Vertex>();
}
public void addVertex(Vertex v){
vertexList.put(v.id, v);
vertexCount++;
}
public void addEdge(Edge e){
Vertex v = e.from;
Vertex w = e.to;
Edge ePrime = new Edge(e.id, e.to, e.from);
vertexList.get(v.id).adjacentEdges.add(e);
vertexList.get(w.id).adjacentEdges.add(ePrime);
edgeCount++;
}
public void getShortestPath(String from, String to){
Vertex vfrom = vertexList.get(from);
Vertex vto = vertexList.get(to);
Dijkstra d = new Dijkstra(this);
shortestPathVertices = d.getShortestPathVertices(vfrom, vto);
shortestPathEdges = d.getShortestPathEdges(vfrom, vto);
double traveled = d.getTraveled();
Vertex[] pathV = shortestPathVertices.toArray(new Vertex[shortestPathVertices.size()]);
Edge[] pathE = shortestPathEdges.toArray(new Edge[shortestPathEdges.size()]);
if(pathV.length == 1)
System.out.printf("The intersections %s and %s are not connected.", from, to);
else{
System.out.printf("The directions to get from %s to %s are:%n", from, to);
for(int i = 0; i < pathE.length; i++){
System.out.printf("Take %s to get from %s to %s.%n", pathE[i], pathV[i], pathV[i+1]);
}
System.out.println("Total miles traveled: " + (traveled ) + " miles.");
System.out.printf("In summary you want the following intersections: %n%s%n", Arrays.toString(pathV));
}
}
public void getMeridianMap(){
Prim p = new Prim(this);
Vertex start = vertexList.get(vertexList.keySet().toArray(new String[vertexList.keySet().size()])[0]);
meridianPathEdges = p.getMinimumWeightTree(start);
Edge[] meridianPath = meridianPathEdges.toArray(new Edge[meridianPathEdges.size()]);
System.out.printf("The roads that need to be covered to see all intersections are: %n%s%n", Arrays.toString(meridianPath));
}
}