-
Notifications
You must be signed in to change notification settings - Fork 1
/
DuallyPairedness.java
258 lines (225 loc) · 6.11 KB
/
DuallyPairedness.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
import java.util.*;
public class DuallyPairedness
{
public static void main(String[] args)
{
// james' test case that is paired
// int[] thing = new int[] {1, 1, 2, 2, 3, 4, 5, 3, 4, 6, 6, 5};
//something that isn't paired
// int[] thing = new int[] {1, 3, 4, 2, 1, 5, 2, 3, 5, 4};
int[] thing = new int[] {1, 2, 3, 1, 4, 3, 2, 4};
int[] wStar = gcStar(thing);
if (isDuallyPaired(wStar))
{
System.out.println("dually paired");
}
else
{
System.out.println("not dualy paired");
}
}
public static int[] gcStar(int[] gc)
{
int[] star = gc;
// for each letter in gc, change the order of all other letters inbetween its occurences
// for example, 1, 2, 3, 1, 4, 3, 2, 4 becomes 1, 3, 2, 1, 4, 3, 2, 4 becomes
// 1, 3, 2, 3, 4, 1, 2, 4 becomes 1, 3, 4, 1, 2, 3, 2, 4 becomes 1, 3, 4, 2, 3, 2, 1, 4
int first = -1;
int second = -1;
Stack<Integer> stack = new Stack<Integer>();
// for each letter
for (int i = 1; i < star.length/2 + 1 ; i++)
{
// System.out.println("i = " + i);
//find the first index
first = -1;
second = -1;
for (int j = 0; j < star.length; j++)
{
if (star[j] == i && first == -1)
{
// System.out.println("got in if");
first = j;
// System.out.println("first = " + first);
}
else if (star[j] == i && first != -1) //star[j] = i after we've set first
{
// System.out.println("got in else");
second = j;
// System.out.println("second = " + second);
}
}
// int blah = first + 1;
// System.out.println("BLAH = " + blah);
// pass over the array pushing everything between first and second onto the stack
if (second - first == 1)
{
// do nothing
}
else
{
int start = first + 1;
for (int j = start; j < second; j++)
{
// System.out.println("first for j = " + j);
// System.out.println("filling stack with " + star[j]);
stack.push(star[j]);
}
// System.out.println("stack size = " + stack.size() + stack.peek());
// pass over the array again, poping entries from the stack into the array
for (int j = start; j < second; j++)
{
// System.out.println("second " + second);
// System.out.println("j = " + j);
star[j] = stack.pop();
}
for (int j = 0; j < star.length; j++)
{
System.out.print(star[j] + ", ");
}
System.out.println();
}
stack.clear();
}
return star;
}
//
public static boolean isDuallyPaired(int[] star)
{
// set up the adj matrix
int size = star.length/2;
// if there's an arc between nodes i and j then graph[i][j] = 1, 0 otherwise
int[][] graph = new int[size][size];
int PINK = 1;
int BLUE = 0;
// colour[i] = 0 means blue, colour[i] = 1 means pink
int[] colour = new int[size];
//add the nodes of the graph to a list of not yet visited nodes, to be popped when visited
ArrayDeque<Integer> notVisited = new ArrayDeque<Integer>(size);
for (int i = 0 ; i < size; i++ )
{
//add i to the ith index, since we can't remove an int from an array list because java thinks we're giving it an index
notVisited.push(i);
}
System.out.println("Initial list size = " + notVisited.size());
int first;
int second;
// clear all the colours
for (int i = 0; i < size; i++)
{
colour[i] = -1;
}
for (int i = 1; i < size + 1 ; i++)
{
System.out.print("i = " + i + " ... ");
first = -1;
second = -1;
for (int j = 0; j < size*2; j++)
{
if (star[j] == i && first == -1)
{
// System.out.println("got in if");
first = j;
// System.out.println("first = " + first);
}
else if (star[j] == i && first != -1) //star[j] = i after we've set first
{
// System.out.println("got in else");
second = j;
// System.out.println("second = " + second);
}
}
// int blah = first + 1;
// System.out.println("BLAH = " + blah);
// pass over the array pushing everything between first and second onto the stack
if (second - first == 1)
{
System.out.println("do nothing");
}
else
{
int start = first + 1;
int appearancesBetweenLetter[] = new int[size];
// System.out.println("start " + start + " second "+ second);
for (int j = start; j < second; j++)
{
// System.out.println(j +"");
// System.out.println(star[j] + " is between " + j);
appearancesBetweenLetter[star[j] -1]++;
}
for (int j = 0; j < size; j++)
{
System.out.print(appearancesBetweenLetter[j] + " ");
}
// finally we get round to setting up the adj matrix
for (int j = 0; j < size; j++)
{
if (appearancesBetweenLetter[j] == 1)
{
graph[i-1][j] = 1;
graph[j][i-1] = 1;
}
}
}
System.out.println();
}
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
System.out.print(graph[i][j] + " ");
}
System.out.println();
}
// just start from zero
colour[0] = 0;
ArrayDeque<Integer> queue = new ArrayDeque<Integer>();
queue.add(0);
for (int i = 0; i < size ; i++)
{
System.out.print(" " + colour[i]);
}
System.out.println();
int index = 0;
while (queue.peek() != null || !notVisited.isEmpty())
{
//take things from notVisited if the queue becomes empty before all nodes have been visited
if (queue.peek() == null)
{
queue.add(notVisited.removeFirst());
}
int u = queue.removeFirst();
notVisited.remove(u);
System.out.println("u = " + u);
for (int j = 0; j < size; j++)
{
if (graph[u][j] == 1 && colour[j] == -1)
{
// System.out.println("got in big if");
// System.out.println("colour u = " + colour[u]);
if (colour[u] == 0)
{
colour[j] = 1;
}
else if (colour[u] == 1)
{
colour[j] = 0;
queue.add(j);
}
else //we get in here if the opening choice isn't connected
{
colour[j] = 0;
// System.out.println("Something has gone terribly wrong");
}
}
else if (graph[u][j] == 1 && colour[j] == colour[u])
{
System.out.println(" j = " + j + " colour " + colour[j]);
return false;
}
}
}
// if we got through all that then things are cooooooel
return true;
}
}