-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraph.java
323 lines (265 loc) · 7.8 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
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*
* Chris Fahlin
* Tcss 342
* Graphs
* Graph.java
*/
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Scanner;
import java.util.Set;
import java.util.StringTokenizer;
// Graph class: evaluate shortest paths.
//
// CONSTRUCTION: with no parameters.
//
// ******************PUBLIC OPERATIONS**********************
// void addEdge( String v, String w, double cvw )
// --> Add additional edge
// void printPath( String w ) --> Print path after alg is run
// void unweighted( String s ) --> Single-source unweighted
// ******************ERRORS*********************************
// Some error checking is performed to make sure graph is ok,
// and to make sure graph satisfies properties needed by each
// algorithm. Exceptions are thrown if errors are detected.
public class Graph {
public static final double INFINITY = Double.MAX_VALUE;
public Map<String,Vertex> vertexMap = new HashMap<String,Vertex>( );
public Set<FullEdge> fullEdges = new HashSet<FullEdge>();
public String file;
private int numberOfVerticies = 0;
public Graph(String fileName) {
FileReader fin;
try {
fin = new FileReader(fileName);
Scanner graphFile = new Scanner(fin);
file = fileName;
// Read the edges and insert
String line;
while( graphFile.hasNextLine( ) ) {
line = graphFile.nextLine( );
StringTokenizer st = new StringTokenizer( line );
try {
if( st.countTokens( ) != 3 ) {
System.err.println( "Skipping ill-formatted line " + line );
continue;
}
String source = st.nextToken( );
String dest = st.nextToken( );
int cost = Integer.parseInt( st.nextToken( ) );
// System.out.println(source +" "+ dest +" " + cost);
addEdge(source, dest, cost);
fullEdges.add(new FullEdge(source, dest, cost));
addEdge(dest, source, cost);
}
catch( NumberFormatException e ) {
System.err.println( "Skipping ill-formatted line " + line );
}
}
graphFile.close();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
}
/*TODO rcSubgraph*/
public Graph rcSubgraph() {
Set<Set<FullEdge>> powerSet = SetMaker.powerSet(fullEdges);
PriorityQueue<Graph> pq = new PriorityQueue<Graph>(1, new WeightComparator());
Graph subGraph;
for(Set<FullEdge> s : powerSet) {
if(s.size() < numberOfVerticies){
s.clear();
}
subGraph = new Graph(file);
subGraph.fullEdges.clear();
for(Vertex v :subGraph.vertexMap.values()){
v.adj.clear();
}
for(FullEdge e : s){
subGraph.addEdge(e.start, e.dest, e.cost);
subGraph.fullEdges.add(e);
subGraph.addEdge(e.dest, e.start, e.cost);
}
if(subGraph.isRedConnected()){
pq.add(subGraph); //add this redundantly connected subgraph to pq
}
}
return pq.poll();
}
public double totalEdgeCost()/*TODO Total Edge*/ {
double totalCost = 0;
for(FullEdge f : fullEdges)
totalCost += f.cost;
return totalCost;
}
public String toString() {
final StringBuilder sb = new StringBuilder();
for(Vertex v : vertexMap.values()){
for(Edge e : v.adj){
if(v.name.compareTo(e.dest.name)<0){
sb.append(v.name + " " + e.dest.name+ " " + e.cost + "\n");
}
}
}
return sb.toString();
}
public boolean isConnected()/*TODO isConnected*/ {
clearAll();
/*Begin DFS to check if graph is connected*/
int traveled = 0;
List<Vertex> toExplore = new LinkedList<Vertex>();
if(vertexMap.values().toArray().length > 0){
Vertex v = (Vertex) vertexMap.values().toArray()[0];
toExplore.add(v);
v.scratch = 1;
}
while (!toExplore.isEmpty()) {
Vertex curr = toExplore.remove(0);
traveled++;
for(Edge e : curr.adj) {
if ( e != null && e.dest != null && e.dest.scratch == 0) {
toExplore.add(0,e.dest);
e.dest.scratch = 1;
}
}
}//End of DFS Algorithm
clearAll();
if(traveled == numberOfVerticies){
return true;
} else {
return false;
}
}
public boolean isRedConnected()/*TODO isRedConnected*/{
if(!isConnected() || vertexMap.values().isEmpty()) return false;
for(Vertex v : vertexMap.values()){
List<Edge> tempList = new LinkedList<Edge>();
tempList.addAll(v.adj);
if (v.adj.size() == 0 || tempList == null) return false;
for(Edge e : tempList){
Vertex destVertex = e.dest;
Edge tempEdge = null;
for(Edge edge : destVertex.adj){
if(edge.dest.equals(v))
tempEdge = edge;
}
v.adj.remove(tempEdge);
destVertex.adj.remove(tempEdge);
if(!isConnected()){
v.adj.add(tempEdge);
destVertex.adj.add(tempEdge);
return false;
}
v.adj.add(tempEdge);
destVertex.adj.add(tempEdge);
}
}
return true;
}
/**
* Add a new edge to the graph.
*/
public void addEdge( String sourceName, String destName, double cost) {
Vertex v = getVertex( sourceName );
Vertex w = getVertex( destName );
v.adj.add( new Edge( w, cost ) );
}
/**
* Driver routine to handle unreachables and print total cost.
* It calls recursive routine to print shortest path to
* destNode after a shortest path algorithm has run.
*/
public void printPath( String destName ) {
Vertex w = vertexMap.get( destName );
if( w == null )
throw new NoSuchElementException( "Destination vertex not found" );
else if( w.dist == INFINITY )
System.out.println( destName + " is unreachable" );
else {
System.out.print( "(Cost is: " + w.dist + ") " );
printPath( w );
System.out.println( );
}
}
/**
* If vertexName is not present, add it to vertexMap.
* In either case, return the Vertex.
*/
private Vertex getVertex( String vertexName ) {
Vertex v = vertexMap.get( vertexName );
if( v == null ) {
v = new Vertex( vertexName );
vertexMap.put( vertexName, v);
numberOfVerticies++;//This is my field
}
return v;
}
/**
* Recursive routine to print shortest path to dest
* after running shortest path algorithm. The path
* is known to exist.
*/
private void printPath( Vertex dest ) {
if( dest.prev != null ) {
printPath( dest.prev );
System.out.print( " to " );
}
System.out.print( dest.name );
}
/**
* Initializes the vertex output info prior to running
* any shortest path algorithm.
*/
private void clearAll( ) {
for( Vertex v : vertexMap.values( ) )
v.reset( );
}
/**
* Single-source unweighted shortest-path algorithm.
*/
public void unweighted( String startName ) {
clearAll( );
Vertex start = vertexMap.get( startName );
if( start == null )
throw new NoSuchElementException( "Start vertex not found" );
Queue<Vertex> q = new LinkedList<Vertex>( );
q.add( start ); start.dist = 0;
while( !q.isEmpty( ) ) {
Vertex v = q.remove( );
for( Edge e : v.adj ) {
Vertex w = e.dest;
if( w.dist == INFINITY ) {
w.dist = v.dist + 1;
w.prev = v;
q.add( w );
}
}
}
}
/**
* Process a request; return false if end of file.
*/
public boolean processRequest( Scanner in, Graph g ) {
try {
System.out.println( "Unweighted shortest distance:" );
System.out.print( "Enter start node:" );
String startName = in.nextLine( );
System.out.print( "Enter destination node:" );
String destName = in.nextLine( );
g.unweighted( startName );
g.printPath( destName );
}
catch( NoSuchElementException e ) {
return false;
}
return true;
}
}