-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphClass.java
More file actions
235 lines (168 loc) · 6.27 KB
/
Copy pathGraphClass.java
File metadata and controls
235 lines (168 loc) · 6.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
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
/*
Graph Class
Bruk Mulatu
COSC 2203
Assignment : Can It Be Done?
Due Date: November 18, 2020
Approved by: Robel Tadele
*/
public class GraphClass {
// Two dimensional array for matrix
int [] [] matrix;
BrukListStack <Integer> stacker = new BrukListStack<>(); // Stack object
int [] edgesIn; //Store the number of edges going into a stage
boolean feasibility;
int temp;
//Constructor that receives the adjacency matrix
GraphClass(int [][] adjacencyMatrix, int [] edgesInCount){
this.matrix = adjacencyMatrix;
this.edgesIn = edgesInCount;
feasibility = false;
}
/*--------------------------Make methods return int [][] arrays ------------------------*/
//Method to get top Order
public int[] topOrder(int[] stages) {
int topVal = 0;
int counter = 0;
int[] topo = new int[stages.length];
for(int i = 0; i < edgesIn.length; i++) {
if(edgesIn[i] == 0) {
stacker.push(i);
}
}
while(!stacker.isEmpty()) {
topVal = stacker.pop();
topo[counter] = stages[topVal];
for(int j = 0; j < matrix[topVal].length; j++) {
if(matrix[topVal][j] != -1) {
edgesIn[j]--;
if(edgesIn[j] == 0) {
stacker.push(j);
}
}
}
counter++;
if(topo.length == stages.length) {
feasibility = true;
}
}
return topo;
}
//Method to check if it is feasible
public boolean isFeasible (){
return feasibility;
}
//The EST is the max of the sum of the EST of pre-req stages and the cost of
//the connecting activity.
public int [] setEarlyStage(int [] stage, int [] topOrder){
int [] earlyStage = new int[stage.length];
int [] edges = new int [stage.length];
int count = 0;
int maxTime = 0;
//For loop that runs from 1 to the end of the topological order
for (int i = 0; i <stage.length; i++) {
//Store topological order value into int loc
int loc = topOrder[i];
//for length of topological array
for (int j = 0; j < stage.length; j++) {
//If matrix location is not empty
if (!(matrix[j][loc-1] == -1)) {
//Store time value in temp variable
edges[count] = earlyStage[j] + matrix[j][loc - 1];
//Increment index
count++;
}
}
for (int j = 0; j < count; j++) {
if (edges[j] > maxTime)
maxTime = edges[j];
}
earlyStage[loc-1] = maxTime;
count = 0;
maxTime =0;
}
return earlyStage;
}
//The LST of other stages is the minimum of the
//difference between the LST of outgoing activities end stage and the cost of the activity.
public int [] setLateStage(int totalWeight, int [] topOrder, int [] stage){
int[] lateStage = new int[stage.length];
//calling the reverse method on the topological order:
int [] topological = backwards(topOrder);
//array for holding all possible paths to the next node:
int[] edge = new int[stage.length];
int index = 0;
int min = totalWeight;
//For loop for rows:
for(int i = 0; i < stage.length; i++) {
//For loop for columns:
for(int j = 0; j < stage.length; j++) {
//if there is a path:
if(matrix[topological[i] - 1][j] != -1) {
edge[index] = lateStage[i] - matrix[topological[i] - 1][j];
index++;
}
}
//Find the weight
for(int j= 0; j < index; j++) {
if(edge[j] < min && edge[j] >= 0) {
min = edge[j];
}
}
//Store late stage value into an array
lateStage[topological[i] - 1] = min;
//Set back to 0
index = 0;
min = totalWeight;
}
return lateStage;
}
// Method to get early times
public int [] setEarlyActivity(int activityCount, int stageCount, int [] earlStageTime){
int [] earlyActivities = new int[activityCount];
int count =0;
for (int i = 0; i < stageCount; i++) {
for (int j = 0; j <stageCount; j++) {
if (!(matrix[i][j] == -1)){
earlyActivities[count] = earlStageTime[i];
count++;
}
}
}
return earlyActivities;
}
//Method to get late times
public int [] setLateActivity(int [] lateStageTime, int stageCount, int activityCount){
int [] lateActivities = new int [activityCount];
int count = 0;
for (int i = 0; i <stageCount ; i++) {
for (int j = 0; j <stageCount ; j++) {
if (!(matrix[i][j]==-1)){
lateActivities[count] = lateStageTime[j] - matrix[i][j];
count++;
}
}
}
return lateActivities;
}
//Method to reverse an array
public int [] backwards(int [] array){
int [] backwards = array.clone();
for (int i = 0; i <array.length/2 ; i++) {
int temp = backwards[i];
backwards[i] = backwards[array.length-1-i];
backwards[array.length-1-i] = temp;
}
return backwards;
}
public String criticalActivities (int [] lateActivities, int [] earlyActivities){
String activities = "";
for (int i = 0; i <earlyActivities.length; i++) {
if (lateActivities[i] - earlyActivities[i] == 0){
//Covert the int to string and append it to the original string
activities += Integer.toString(i+1) + " ";
}
}
return activities;
}
}