-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChain.java
More file actions
171 lines (158 loc) · 5.27 KB
/
Chain.java
File metadata and controls
171 lines (158 loc) · 5.27 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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package vrpheurstics;
/**
*
* @author Gindullin
*/
public class Chain {
private int n;
private int m;
private int[][] links;
private int[] linklength;
public Chain(int x) {
this.n = x;
this.Reset();
}
private void Reset() {
this.links = new int[this.n+1][this.n+1];
this.linklength = new int[this.n+1];
this.m = 0;
for (int i = 1; i <= this.n; i++) {
for (int j = 1; j <= this.n; j++) {
this.links[i][j] = 0;
}
this.linklength[i] = 0;
}
}
public void Chain(int[] path, int n, int i, int j, int in, int jn) {
if ((i>=in)||(j>=jn)||(i==j)||(in==jn)||(i==jn)||(j==in)) {
System.out.println("Error #3");
}
else {
this.Reset();
int k = 0;
int s = 0;
int start = 0;
while (k < n) {
if ((s==0)&&((path[k]==i)||(path[k]==in)||(path[k]==j)||(path[k]==jn)/*||(k == n - 1)*/)) {
s = 0;
start = k + 1;
}
else if ((path[k]==i)||(path[k]==in)||(path[k]==j)||(path[k]==jn)||(k == n - 1)) {
if ((k == n - 1)&&((path[k]!=i)&&(path[k]!=in)&&(path[k]!=j)&&(path[k]!=jn))) {
s++;
}
int[] templink = new int[s];
for (int p = 0; p < s; p++) {
templink[p]=path[start + p];
}
this.AddLink(templink, s);
start = k + 1;
s = 0;
}
else {
s++;
}
k++;
}
}
}
public int[] Path() {
int[] temppath = new int[2*n];
int k = 0;
for (int i = 1; i <= this.m; i++) {
for (int j = 1; j <= this.linklength[i]; j++) {
temppath[k] = this.links[i][j];
k++;
}
}
return temppath;
}
public void Split() {
if (this.m<5) {
do {
int maxl=this.linklength[1];
int maxi=1;
for (int i = 1; i <= m; i++) {
if (maxl<this.linklength[i]) {
maxl=this.linklength[i];
maxi=i;
}
}
int cutoffpoint = (int) this.linklength[maxi] / 2;
int[] cut = new int[this.linklength[maxi] - cutoffpoint];
for (int i = cutoffpoint + 1; i <= this.linklength[maxi]; i++) {
cut[i - (cutoffpoint + 1)] = this.links[maxi][i];
this.links[maxi][i] = 0;
}
this.InsertLink(cut, this.linklength[maxi] - cutoffpoint, maxi);
this.linklength[maxi] = cutoffpoint;
} while (this.m<5);
}
}
public int getM() {
return m;
}
public void PrintChainsFullTest() {
System.out.println(this.n + " " + this.m);
for (int i = 1; i <= this.n; i++) {
for (int j = 1; j <= this.n; j++) {
System.out.print(this.links[i][j] + " ");
}
System.out.println(" " + this.linklength[i]);
}
System.out.println("");
}
public void PrintChains() {
System.out.println(this.n + " " + this.m);
for (int i = 1; i <= m; i++) {
System.out.print(this.linklength[i] + " ");
for (int j = 1; j <= linklength[i]; j++) {
System.out.print(this.links[i][j] + " ");
}
System.out.println("");
}
System.out.println("");
}
public void AddLink(int[] link, int length) {
this.m++;
this.linklength[this.m] = length;
for (int i = 1; i <= length; i++) {
this.links[this.m][i] = link[i-1];
}
for (int i = length+1; i <= this.n; i++) {
this.links[this.m][i] = 0;
}
}
public void InsertLink(int[] link, int length, int row) {
if (row>this.m) {
System.out.println("Error #1 " + this.m + " " + row);
}
else if (row<0) {
System.out.println("Error #2 " + this.m + " " + row);
}
else if (row==this.m) {
this.AddLink(link, length);
}
else {
for (int i=this.m; i>=row+1; i--) {
for (int j=0; j<=this.n; j++) {
this.links[i+1][j]=this.links[i][j];
}
this.linklength[i+1]=this.linklength[i];
}
this.linklength[row+1] = length;
for (int i = 1; i <= length; i++) {
this.links[row+1][i] = link[i-1];
}
for (int i = length+1; i <= this.n; i++) {
this.links[row+1][i] = 0;
}
this.m++;
}
}
}