This repository was archived by the owner on Dec 16, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiGraphProgram.java
More file actions
175 lines (151 loc) · 5.92 KB
/
DiGraphProgram.java
File metadata and controls
175 lines (151 loc) · 5.92 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
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
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
public class DiGraphProgram {
private static String dir = System.getProperty("user.dir");
private static String fileSep = System.getProperty("file.separator");
/**
* @param args
* @throws IOException
*/
class VertexTest<K> implements Comparable<VertexTest<K>>{
private IVertex<K> source;
private HashMap<IVertex<K>, Integer> targets ;
Comparator<Map.Entry<IVertex<K>, Integer>> resComp = new Comparator<Map.Entry<IVertex<K>,Integer>>() {
public int compare(Map.Entry<IVertex<K>, Integer> o1,
Map.Entry<IVertex<K>, Integer> o2) {
return o1.getValue().compareTo(o2.getValue());
}
};
public VertexTest(IVertex<K> s) {
this.source = s;
targets = new HashMap<IVertex<K>, Integer>();
}
public void addTarget(IVertex<K> t) {
targets.put(t,-1);
}
public int compareTo(VertexTest<K> o) {
return this.source.compareTo(o.source);
}
public void addResult(IVertex<K> v, int dist) {
targets.put(v, dist);
}
public Collection<IVertex<K>> getTargets() {
return targets.keySet();
}
public List<Entry<IVertex<K>, Integer>> getResults() {
List<Entry<IVertex<K>, Integer>> list = new ArrayList<Map.Entry<IVertex<K>,Integer>>( targets.entrySet() );
Collections.sort(list, resComp);
return list;
}
}
Comparator<VertexTest<String>> testComp= new Comparator<VertexTest<String>>() {
public int compare(VertexTest<String> o1,
VertexTest<String> o2) {
return o1.source.compareTo(o2.source);
}
};
public DiGraphProgram(HashMap<String, String> args) throws IOException {
// Init new digraph
System.out.println(args);
Stopwatch w = new Stopwatch();
double elapsed = 0.0;
IDiGraph<String> dg = null;
HashSet<String> boolVals = new HashSet<String>();
boolVals.add("1");
boolVals.add("T");
boolVals.add("TRUE");
boolean multicore = args.containsKey("mc") && boolVals.contains(args.get("mc").toUpperCase());
for(int t = 0; t < 3; t++){
if (multicore)
dg = new DiGraphThreadedCarrot<String>();
else dg = new DiGraph<String>();
dg.buildGraph(args.get("df"));
System.out.println("Buildtime: " + (w.elapsedTime()- elapsed));
elapsed = w.elapsedTime();
System.out.println("Edges: " + dg.E());
}
System.out.println("Mean Buildtime: " + w.elapsedTime() / 3);
// if (args.containsKey("tf")) SearchGraph(dg, args.get("tf"));
// System.out.println("Search time: " +(w.elapsedTime()- elapsed));
// System.out.println("Total runtime: " +w.elapsedTime());
// System.out.println("Multicore: " + multicore);
}
private void closeThreads() {
ThreadGroup root = Thread.currentThread().getThreadGroup().getParent();
root.destroy();
}
private void SearchGraph(IDiGraph<String> dg, String filename) throws IOException{
HashMap<String, VertexTest<String>> tests = new HashMap<String, VertexTest<String>>();
BufferedReader r = new BufferedReader(new InputStreamReader(new FileInputStream(filename), "UTF-8"));
// Read test file
while(true) {
String str = r.readLine();
if (str == null || str.length() == 0) break;
String[] sa = str.split(" ");
IVertex<String> source = dg.getByValue(sa[0]);
IVertex<String> target = dg.getByValue(sa[1]);
// Add source and target to the collection of tests
// It ensures that BFS is only performed once for tests with the same source
if (!tests.containsKey(source.getValue())) {
tests.put(source.getValue(), new VertexTest<String>(source));
}
tests.get(source.getValue()).addTarget(target);
}
for (VertexTest<String> test : tests.values()) {
BFS<String> bfs = new BFS<String>(dg, test.source);
for (IVertex<String> t : test.getTargets()) {
test.addResult(t, bfs.distTo(t));
}
}
ArrayList<VertexTest<String>> printlist = new ArrayList<VertexTest<String>>(tests.values());
Collections.sort(printlist, testComp);
for (VertexTest<String> test : printlist) {
for (Entry<IVertex<String>, Integer> result: test.getResults()) {
//System.out.printf("Dist from %s to %s : %s\n", test.source.getValue(), result.getKey().getValue(), result.getValue());
}
}
}
public static void main(String[] args) throws IOException {
HashMap<String, String> cmdargs = new HashMap<String, String>();
for (String arg : args) {
String[] a = arg.split(":");
cmdargs.put(a[0], a[1]);
}
if (cmdargs.containsKey("df")) {
new DiGraphProgram(cmdargs);
} else {
System.out.println(gethelp());
}
}
private static String gethelp() {
StringBuilder sb = new StringBuilder();
sb.append("*************************************************************\n");
sb.append("* *\n");
sb.append("* USAGE *\n");
sb.append("* Source *\n");
sb.append("* javac *.java *\n");
sb.append("* java DiGraphProgram df:[datafilename] tf:[testfilename] *\n");
sb.append("* mc:[1/0,T/F,TRUE/FALSE] *\n");
sb.append("* *\n");
sb.append("* Wordladder.jar *\n");
sb.append("* java -cp Wordladder.jar DiGraphProgram df:[datafilename] *\n");
sb.append("* tf:[testfilename] mc:[1/0,T/F,TRUE/FALSE] *\n");
sb.append("* *\n");
sb.append("* DataGen tool *\n");
sb.append("* java -cp Wordladder.jar DataGen *\n");
sb.append("* *\n");
sb.append("*************************************************************\n");
return sb.toString();
}
}