-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphSearch.java
More file actions
180 lines (147 loc) · 5.92 KB
/
GraphSearch.java
File metadata and controls
180 lines (147 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
176
177
178
179
180
import java.util.Arrays;
public class GraphSearch {
private Node allCandidates[];
private int taken[];
private int nodesMatrix[][];
private boolean finished = false, jumpback = false;
private int minBandwidth;
private int minmaxBandwidth[];
private int currMax, currMaxIndex;
/**
* Initializes GraphSearch fields
*
* @param allCandidates The array of nodes of the graph
*/
public GraphSearch(Node allCandidates[], int nodesMatrix[][]) {
//this.nodes = nodes;
this.nodesMatrix = nodesMatrix;
this.allCandidates = allCandidates;
taken = new int[allCandidates.length];
minBandwidth = nodesMatrix.length;
minmaxBandwidth = new int[allCandidates.length];
currMax = 0;
currMaxIndex = -1;
Arrays.sort(allCandidates);
/*for(int i = 0; i< allCandidates.length; i++){
System.out.println("Node ID "+ allCandidates[i].getID() + " DEGREE " + allCandidates[i].getDegree());
}*/
}
void backtrack(int solution[], int currIndex) {
int candidates[] = new int[solution.length - currIndex - 1]; /* max candidates for next position */
if (currIndex != -1 && is_a_solution(solution, currIndex)) {
process_solution(solution);
//printArray(solution);
//System.out.println("^ solution Min: " + minBandwidth + " currMaxIndex:" +currMaxIndex);
} else {
currIndex = currIndex + 1;
int nc = construct_candidates(solution, candidates, currIndex);
if(currIndex == 0){
nc--;
}
//System.out.print("CURR MAX: " + currMax + " CURR MAX INDEX " +currMaxIndex + "||| ");
//printArray(solution);
for (int candIndex = 0; candIndex < nc; candIndex++) {
int prevMax = currMax;
//WIP
// If able to jumpback, but the currIndex is at the next candidate
// Then cancel the jumpback and reset the currMaxIndex
if (jumpback && currMaxIndex >= currIndex) {
currMaxIndex = currIndex;
jumpback = false;
}
if(jumpback){
return;
}
makeMove(solution, candidates, currIndex, candIndex);
//System.out.println("CurrMaxIndex: " + currMaxIndex);
if(currMax < minBandwidth){
//System.out.print("Passed:");
//printArray(solution);
backtrack(solution, currIndex);
}
//printArray(solution);
currMax = prevMax;
unmakeMove(solution, candidates, currIndex, candIndex);
if (finished) {
return; /* terminate early */
}
}
}
}
public boolean is_a_solution(int solution[], int currIndex) {
if (currIndex == solution.length - 1){
return true;
}
return false;
}
private void process_solution(int[] solution) {
//System.out.println("------------------------");
//System.out.print("Curr array: ");
//printArray(a);
jumpback = true;
if (currMax < minBandwidth) {
minBandwidth = currMax;
minmaxBandwidth = solution.clone();
}
if (minBandwidth == 1) {
finished = true;
}
//System.out.println("Min bandwidth: " + minBandwidth);
}
private int construct_candidates(int solution[], int candidates[], int currIndex) {
int index = 0;
for (int i = 0; i < allCandidates.length; i++) {
if(taken[allCandidates[i].getID() - 1] != 1){ // If node is not taken then add it to candidate
candidates[index] = allCandidates[i].getID();
index++;
}
}
return index;
}
private void makeMove(int solution[], int candidates[], int currIndex, int candIndex){
solution[currIndex] = candidates[candIndex]; // Places the current candidate into the solution at currIndex
taken[candidates[candIndex] - 1] = 1; // Marks that candidate as taken
int maxLength = findMaxLength(solution, solution[currIndex], currIndex); // Max length from the currNode to components
if(maxLength > currMax){
currMax = maxLength;
currMaxIndex = currIndex;
}
}
/**
* This finds the max length from the current node to its connected nodes that
* are in partial solution[]
*
* @param solution Partial solution
* @param currNode The current node recently inserted
* @param currIndex The index of the current node in partial solution[]
* @return The max length between current node and its connections
*/
public int findMaxLength(int solution[], int currNode, int currIndex) {
int maxLength = 0;
// Loops through each node that's connected to currNode
for (int i = 0; i < currIndex; i++) {
if(nodesMatrix[currNode -1][solution[i] - 1] == 1){
if(currIndex - i > maxLength)
maxLength = currIndex - i;
}
}
return maxLength;
}
private void unmakeMove(int solution[], int candidates[], int currIndex, int candIndex) {
solution[currIndex] = -1; // Undos the node at the currIndex
taken[candidates[candIndex] - 1] = 0; // Remove candidate from taken
}
public int[] getMinMaxBandwidth(){
System.out.println("Bandwidth: " + minBandwidth);
return minmaxBandwidth;
}
public boolean possibleWithMin2(){
return true;
}
public void printArray(int arr[]) {
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
}