-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConveyorGraph.java
204 lines (162 loc) · 4.36 KB
/
ConveyorGraph.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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package org.barclays.assignment;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Set;
/**
*
* @author Administrator
*
*/
public class ConveyorGraph implements IConveyorGraph{
private HashMap<String, LinkedList<Node>> graph = null;
private Integer shortestDistance = null;
public ConveyorGraph() {
graph = new HashMap<String, LinkedList<Node>>();
}
/*
* Initializes the Conveyor System
*/
public boolean initialize(ArrayList<String> input) {
boolean status = false;
LinkedList<Node> temp1;
LinkedList<Node> temp2;
String [] strArr;
for(String str : input) {
strArr = str.split(" ");
String s1 = strArr[0];
String s2 = strArr[1];
Node n1 = new Node(s1, strArr[2]);
Node n2 = new Node(s2, strArr[2]);
temp1 = graph.get(s1);
if(temp1 == null)
temp1 = new LinkedList<Node>();
temp1.add(n2);
temp2 = graph.get(s2);
if(temp2 == null)
temp2 = new LinkedList<Node>();
temp2.add(n1);
graph.put(s1, temp1);
graph.put(s2, temp2);
}
status = true;
return status;
}
public void printGraph() {
Set<String> str = graph.keySet();
for(String s : str) {
LinkedList<Node> l;
l = graph.get(s);
System.out.print(s + "->");
Iterator<Node> i = l.iterator();
while(i.hasNext()) {
Node node = (Node) i.next();
System.out.print(node.getValue() + " ");
System.out.print(node.getWeight() + " ->");
}
System.out.println("null");
}
}
public ArrayList<ArrayList<String>> getShortestDistance(String node1, String node2) {
Set<String> keys = graph.keySet();
ArrayList<ArrayList<String>> paths = new ArrayList<ArrayList<String>>();
ArrayList<String> temp;
ArrayList<String> covered = new ArrayList<String>();
int noOfKeys = keys.size();
ArrayList<ShortestDistance> list = new ArrayList<ShortestDistance>();
ShortestDistance sd;
LinkedList<Node> ll;
Integer min;
Iterator<ShortestDistance> its;
String cur = null;
sd = new ShortestDistance(node1, 0);
list.add(sd);
for(int i = 1; i < noOfKeys; i++) {
String sourceNode = null;
Integer sourceWeight = null;
String destNode = null;
Integer destWeight = null;
min = Integer.MAX_VALUE;
its = list.iterator();
while(its.hasNext()) {
sd = its.next();
ll = graph.get(sd.getNode());
Iterator<Node> itl = ll.iterator();
while(itl.hasNext()) {
Node n = itl.next();
String s = sd.getNode() + " " + n.getValue();
if(!covered.contains(s)) {
if(Integer.parseInt(n.getWeight()) <= min) {
min = Integer.parseInt(n.getWeight());
sourceNode = sd.getNode();
sourceWeight = sd.getDistance();
destNode = n.getValue();
destWeight = Integer.parseInt(n.getWeight());
}
}
}
}
covered.add(sourceNode + " " + destNode);
covered.add(destNode + " " + sourceNode);
destWeight += sourceWeight;
boolean f = false;
Iterator<ArrayList<String>> it = paths.iterator();
if(it.hasNext()) {
while(it.hasNext()) {
ArrayList<String> t = it.next();
if(t.contains(sourceNode) && cur.equalsIgnoreCase(sourceNode)) {
t.add(destNode);
cur = destNode;
f = true;
}
}
if(!f) {
ArrayList<String> n = new ArrayList<String>();
if(!sourceNode.equalsIgnoreCase(node1))
n.add(sourceNode);
n.add(destNode);
cur = destNode;
paths.add(n);
}
} else {
ArrayList<String> n = new ArrayList<String>();
//n.add(sourceNode);
n.add(destNode);
paths.add(n);
cur = destNode;
}
if(destNode.equalsIgnoreCase(node2)) {
shortestDistance = destWeight;
return paths;
}
sd = new ShortestDistance(destNode, destWeight);
list.add(sd);
}
return null;
}
/**
* @return the graph
*/
public HashMap<String, LinkedList<Node>> getGraph() {
return graph;
}
/**
* @param graph the graph to set
*/
public void setGraph(HashMap<String, LinkedList<Node>> graph) {
this.graph = graph;
}
/**
* @return the shortestDistance
*/
public Integer getShortestDistance() {
return shortestDistance;
}
/**
* @param shortestDistance the shortestDistance to set
*/
public void setShortestDistance(Integer shortestDistance) {
this.shortestDistance = shortestDistance;
}
}