-
Notifications
You must be signed in to change notification settings - Fork 275
Expand file tree
/
Copy pathComputeShortValue.java
More file actions
83 lines (64 loc) · 3.45 KB
/
Copy pathComputeShortValue.java
File metadata and controls
83 lines (64 loc) · 3.45 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
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
package subway.Controller;
import org.jgrapht.alg.shortestpath.DijkstraShortestPath;
import org.jgrapht.graph.DefaultWeightedEdge;
import org.jgrapht.graph.WeightedMultigraph;
import subway.View.OutputView;
import subway.domain.Line;
import subway.domain.LineRepository;
import subway.domain.Station;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class ComputeShortValue {
private static List<Line> allLine=LineRepository.lines();
List<String> checkDuplicatedStation=new ArrayList<String>();
LineRepository lineRepository=new LineRepository();
WeightedMultigraph<String, DefaultWeightedEdge> distanceGraph = new WeightedMultigraph(DefaultWeightedEdge.class);
WeightedMultigraph<String, DefaultWeightedEdge> timeGraph = new WeightedMultigraph(DefaultWeightedEdge.class);
public void setDistanceGraph(String start,String end) {
setAllLine(distanceGraph);
setDistanceEdge(distanceGraph);
DijkstraShortestPath dijkstraShortestPath = new DijkstraShortestPath(distanceGraph);
List<String> shortestPath = dijkstraShortestPath.getPath(start, end).getVertexList();
OutputView outputView =new OutputView();
outputView.printStation(shortestPath);
}
public void setTimeGraph(String start,String end) {
setAllLine(timeGraph);
setTimeEdge(timeGraph);
DijkstraShortestPath dijkstraShortestPath = new DijkstraShortestPath(distanceGraph);
List<String> shortestPath = dijkstraShortestPath.getPath(start, end).getVertexList();
OutputView outputView =new OutputView();
outputView.printStation(shortestPath);
}
public void setDistanceEdge(WeightedMultigraph<String, DefaultWeightedEdge> tmpGraph) {
for(int i=0;i<allLine.size();i++) {
Line tmpSaveLine=allLine.get(i);
for (int j = 0; j < tmpSaveLine.getLineStation().size()-1; j++) {
tmpGraph.setEdgeWeight(tmpGraph.addEdge(tmpSaveLine.getLineStation().get(j).getName(),tmpSaveLine.getLineStation().get(j+1).getName()),lineRepository.getLine(tmpSaveLine.getName()).getDistance(tmpSaveLine.getLineStation().get(j),tmpSaveLine.getLineStation().get(j+1)));
}
}
}
public void setTimeEdge(WeightedMultigraph<String, DefaultWeightedEdge> tmpGraph) {
for(int i=0;i<allLine.size();i++) {
Line tmpSaveLine=allLine.get(i);
for (int j = 0; j < tmpSaveLine.getLineStation().size()-1; j++) {
tmpGraph.setEdgeWeight(tmpGraph.addEdge(tmpSaveLine.getLineStation().get(j).getName(),tmpSaveLine.getLineStation().get(j+1).getName()),lineRepository.getLine(tmpSaveLine.getName()).getDistance(tmpSaveLine.getLineStation().get(j),tmpSaveLine.getLineStation().get(j+1)));
}
}
}
public void setAllLine(WeightedMultigraph<String, DefaultWeightedEdge> tmpGraph) {
for(int i=0;i<allLine.size();i++) {
Line tmpSaveLine=allLine.get(i);
for(int j=0;j<tmpSaveLine.getLineStation().size();j++) {
setCheckDuplicatedStation(allLine.get(i).getLineStation().get(j),tmpGraph);
}
}
}
public void setCheckDuplicatedStation(Station tmpSaveStation,WeightedMultigraph<String, DefaultWeightedEdge> tmpGraph) {
if(!checkDuplicatedStation.contains(tmpSaveStation.getName())){
checkDuplicatedStation.add(tmpSaveStation.getName());
tmpGraph.addVertex(tmpSaveStation.getName());
}
}
}