-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShortPath.java
More file actions
124 lines (111 loc) · 3.6 KB
/
ShortPath.java
File metadata and controls
124 lines (111 loc) · 3.6 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import java.util.*;
public class ShortPath{
EdgeList thisEdgeList = new EdgeList();
PositionList thisPositionList = new PositionList();
Myset<Position> settled;
Myset<Position> unsettled;
public Map<Position, Position> deeps;
public Map<Position, Integer> timeSpan;
public ShortPath(PositionList p, EdgeList e){
thisPositionList = p;
thisEdgeList = e;
}
public boolean isSettled(Position njk){
return settled.isMember(njk);
}
public Myset<Position> getAllVertexNeibhouringThisVertex(Position c){
Myset<Position> shit = new Myset<Position>();
for(int i=0;i<thisEdgeList.bir.getSizeSet();i++){
if(thisEdgeList.bir.getElement(i).start.thisPosition.equals(c.thisPosition) && !isSettled(thisEdgeList.bir.getElement(i).end)){
shit.addElement(thisEdgeList.bir.getElement(i).end);
}
if(thisEdgeList.bir.getElement(i).end.thisPosition.equals(c.thisPosition) && !isSettled(thisEdgeList.bir.getElement(i).start)){
shit.addElement(thisEdgeList.bir.getElement(i).start);
}
}
return shit;
}
public Position getMinimum(Myset<Position> pos){
//System.out.println("getMinimum me aa gya");
Position min = null;
for(int i=0;i<pos.getSizeSet();i++){
Position node = pos.getElement(i);
if(min==null){
min = node;
}
else{
if(getShortestDistance(node)<getShortestDistance(min)){
min = node;
}
}
}
return min;
}
public int getShortestDistance(Position v){
Integer c = timeSpan.get(v);
if(c == null){
return Integer.MAX_VALUE;
}
else{
return c;
}
}
public int getDistance(Position a, Position b){
Edge c = thisEdgeList.getEdge(a, b);
return c.time;
}
public void findMinLength(Position fg){
Myset<Position> neib = getAllVertexNeibhouringThisVertex(fg);
for(int i=0;i<neib.getSizeSet();i++){
if(getShortestDistance(neib.getElement(i))>getShortestDistance(fg) + getDistance(fg, neib.getElement(i))){
timeSpan.put(neib.getElement(i), getShortestDistance(fg) + getDistance(fg, neib.getElement(i)));
deeps.put(neib.getElement(i), fg);
//System.out.println(deeps);
unsettled.addElement(neib.getElement(i));
}
}
//System.out.println(deeps);
}
public void mainFunction(Position imp){
//System.out.println("main func");
settled = new Myset<Position>();
unsettled = new Myset<Position>();
timeSpan = new HashMap<Position, Integer>();
deeps = new HashMap<Position, Position>();
unsettled.addElement(imp);
timeSpan.put(imp, 0);
while(unsettled.getSizeSet()>0){
//System.out.println("while me aa gya");
Position vgh = getMinimum(unsettled);
settled.addElement(vgh);
//System.out.println(unsettled.getSizeSet());
unsettled.removeElement(vgh);
//System.out.println(unsettled.getSizeSet());
findMinLength(vgh);
}
}
public int time(Position desti)
{
Integer l =timeSpan.get(desti);
return l;
}
public Myset<Position> getPath(Position destination){
Myset<Position> path = new Myset<Position>();
Position temp = destination;
path.addElement(destination);
//System.out.println(deeps);
if(deeps.get(temp)==null){
//System.out.println("bh");
return null;
}
while (deeps.get(temp) != null){
temp = deeps.get(temp);
path.addElement(temp);
}
Myset<Position> path1 = new Myset<Position>();
for(int i=0;i<path.getSizeSet();i++){
path1.addElement(path.getElement(path.getSizeSet()-1-i));
}
return path1;
}
}