-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTreeBuilder.java
154 lines (136 loc) · 4.79 KB
/
TreeBuilder.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
import java.io.*;
import java.nio.*;
import java.nio.channels.*;
import java.util.Scanner;
import java.util.Arrays;
import java.util.Comparator;
public class TreeBuilder {
Vertex[] vertices;
public TreeBuilder(String filename) throws IOException {
parseVertexFile(filename + ".co");
parseArcFile(filename + ".gr");
treeify();
remapIdsToAddresses(vertices);
}
void treeify() {
treeify(true, 0, vertices.length);
}
void treeify(boolean useXaxis, int left, int right) {
MedianSplitter splitter = new MedianSplitter();
if (right - left > 1) {
splitter.split(vertices, left, right, useXaxis);
int midpt = (right+left)/2;
treeify(!useXaxis, left, midpt);
treeify(!useXaxis, midpt, right);
}
}
static void remapIdsToAddresses(Vertex[] vertices) {
int[] map = new int[vertices.length];
for (int i=0; i < vertices.length; i++) {
map[vertices[i].id] = i;
}
for (int i=0; i < vertices.length; i++)
for (int j=0; j<vertices[i].edgeCount; j++)
vertices[i].edges[j].vertexIdx = map[vertices[i].edges[j].vertexIdx];
}
void parseVertexFile(String filename) throws IOException {
Scanner s = new Scanner(new BufferedInputStream(new FileInputStream(filename), 1<<24));
//Get vertex count
while (s.hasNext()) {
if (s.next().equals("p") ) {
int size;
s.next();
s.next();
s.next();
size = s.nextInt();
vertices = new Vertex[size];
break;
}
s.nextLine();
}
while (s.hasNext()) {
String t = s.next();
if (!t.equals("v")) {
s.nextLine();
continue;
}
int id = s.nextInt() - 1;
int x = s.nextInt();
int y = s.nextInt();
vertices[id] = new Vertex(id, x, y);
}
}
void parseArcFile(String filename) throws IOException {
Scanner s = new Scanner(new BufferedInputStream(new FileInputStream(filename), 1<<24));
while (s.hasNext()) {
String t = s.next();
if (!t.equals("a")) {
s.nextLine();
continue;
}
int v1 = s.nextInt() - 1;
int v2 = s.nextInt() - 1;
int weight = s.nextInt();
vertices[v1].addEdge(v2, weight);
}
}
public void write(DataOutputStream out) throws IOException {
for (int i=0; i < vertices.length; i++) {
vertices[i].write(out);
}
}
}
class MedianSplitter {
static VertexComparator comparator;
static int targetpos;
public static void split(Vertex[] arr, boolean splitOnX) {
split(arr, 0, arr.length, splitOnX);
}
public static void split(Vertex[] arr, int leftpos, int rightpos, boolean splitOnX) {
comparator = new VertexComparator(splitOnX);
targetpos = (leftpos+rightpos)/2;
split(arr, leftpos, rightpos);
}
static void split(Vertex[] arr, int leftpos, int rightpos) {
int midpt = (leftpos+rightpos)/2;
Vertex pivot = arr[midpt];
arr[midpt] = arr[rightpos-1];
int i = leftpos;
int j = rightpos - 1;
// ensure all values in the array are placed in position < i are <= pivot,
// and those that are placed in position > j are > pivot.
for (; i < j; i++) {
if (comparator.compare(arr[i], pivot) > 0) {
boolean terminate = false;
while (comparator.compare(arr[j-1], pivot) > 0) {
j--;
if (i == j) {
terminate = true;
break;
}
}
if (terminate)
break;
Vertex tmp = arr[j-1];
arr[j-1] = arr[i];
arr[i] = tmp;
}
}
arr[rightpos-1] = arr[i];
arr[i] = pivot;
if (i != targetpos) { // have we found the median?
if (i > targetpos) { // if not, exclude it and recursively search the rest
split(arr, leftpos, i);
} else {
split(arr, i+1, rightpos);
}
}
}
public static void naiveSplit(Vertex[] vertices, boolean splitOnX) {
naiveSplit(vertices, 0, vertices.length, splitOnX);
}
public static void naiveSplit(Vertex[] vertices, int left, int right, boolean splitOnX) {
Comparator<Vertex> vertexComparator = new VertexComparator(splitOnX);
Arrays.sort(vertices, left, right, vertexComparator);
}
}