Skip to content

Commit cdd9a05

Browse files
committed
Finding a subgraph and extracting the subarray
1 parent f2d12df commit cdd9a05

File tree

1 file changed

+162
-98
lines changed

1 file changed

+162
-98
lines changed

alg.js

Lines changed: 162 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,5 @@
11
let array1 = [
2-
/*
3-
[[], 3], //A 0
4-
[[0], 2], //B 1
5-
[[0], 1], //C 2
6-
[[1, 2], 3], //D 3
7-
[[1, 2], 1], //E 4
8-
[[4], 1], //F 5
9-
[[3, 5], 1], //G 6
10-
*/
11-
/*
12-
[[], 0], //A 0
13-
[[0], 3], //B 1
14-
[[0], 4], //C 2
15-
[[2], 5], //D 3
16-
[[1, 3], 9], //E 4
17-
[[1, 3], 7], //F 5
18-
[[2], 8], //G 6
19-
[[2], 5], //H 7
20-
[[7], 6], //I 8
21-
[[5, 6, 8], 10], //J 9
22-
[[5, 6, 8], 3], //K 10
23-
[[5, 6, 8], 7], //L 11
24-
[[7], 4], //M 12
25-
[[11, 12], 5], //N 13
26-
[[4, 9], 3], //O 14
27-
[[10, 13, 14], 0], //P 15
28-
29-
*/
2+
//prvi zadatak unos
303
[[], 2], //A 0
314
[[0], 2], //B 1
325
[[0], 2], //C 2
@@ -77,105 +50,102 @@ let array1 = [
7750
[[45, 42, 46], 1], //M 47
7851
[[37, 47, 44, 28, 11], 2], //N 48
7952

80-
/*
81-
[[], 2], //A 0
82-
[[0], 2], //B 1
83-
[[0], 2], //C 2
84-
[[1,2], 1], //D 3
53+
/*
54+
[[], 1], //0
55+
[[0], 1], //1
56+
[[1], 1], //2
57+
[[1], 1], //3
58+
[[2], 1], //4
59+
[[3,4], 1], //5
60+
*/
61+
];
62+
63+
let array2 = [
64+
65+
//šesti zadatak -> 5 dana u tjednu
66+
/*
67+
[[], 2], //A 0
68+
[[0], 2], //B 1
69+
[[0], 2], //C 2
70+
[[1,2], 1], //D 3
8571
[[3], 3], //E 4
8672
[[4], 1], //F 5
8773
[[5], 2], //G 6
8874
[[5], 2], //H 7
8975
[[5], 3], //I 8
9076
[[5], 3], //J 9
9177
[[5], 3], //K 10
92-
[[5], 54], //M 11
93-
[[7], 6], //N 12
94-
[[7], 5], //O 13
95-
[[8], 5], //P 14
78+
[[5], 108], //M 11
79+
[[7], 13], //N 12
80+
[[7], 5], //O 13
81+
[[8], 6], //P 14
9682
[[9, 10], 5], //A 15
97-
[[13], 2], //B 16
98-
[[12], 2], //C 17
99-
[[12], 4], //D 18
83+
[[13], 4], //B 16
84+
[[12], 4], //C 17
85+
[[12], 8], //D 18
10086
[[15], 8], //E 19
10187
[[6, 15], 8], //F 20
102-
[[14], 8], //G 21
103-
[[14], 4], //H 22
104-
[[15], 4], //I 23
105-
[[15], 4], //J 24
106-
[[23], 6], //K 25
107-
[[19, 17, 18], 6], //L 26
108-
[[24], 7], //M 27
109-
[[22], 42], //N 28
110-
[[20], 4], //O 29
111-
[[21], 5], //P 30
112-
[[20], 8], //G 31
113-
[[21], 9], //H 32
114-
[[21, 25], 4], //I 33
115-
[[21, 25], 4], //J 34
116-
[[25], 12], //K 35
117-
[[30], 4], //L 36
118-
[[16, 31, 29], 30], //M 37
119-
[[36], 6], //N 38
88+
[[14], 1], //G 21
89+
[[14], 8], //H 22
90+
[[15], 5], //I 23
91+
[[15], 6], //J 24
92+
[[23], 2], //K 25
93+
[[19, 17, 18], 12], //L 26
94+
[[24], 4], //M 27
95+
[[22], 84], //N 28
96+
[[20], 8], //O 29
97+
[[21], 10], //P 30
98+
[[20], 16], //G 31
99+
[[21], 18], //H 32
100+
[[21, 25], 8], //I 33
101+
[[21, 25], 8], //J 34
102+
[[25], 24], //K 35
103+
[[30], 8], //L 36
104+
[[16, 31, 29], 60], //M 37
105+
[[36], 12], //N 38
120106
[[31, 33, 34, 35, 27], 9], //O 39
121107
[[38, 32, 33, 34], 3], //P 40
122108
[[38], 9], //G 41
123-
[[43], 6], //H 42
124-
[[26, 29, 41, 40, 39], 9], //I 43
125-
[[43], 1], //J 44
126-
[[43], 4], //K 45
127-
[[43], 7], //L 46
128-
[[45, 42, 46], 1], //M 47
129-
[[37, 47, 44, 28, 11], 2], //N 48
130-
*/
131-
132-
/*
133-
[[], 2], //A 0
134-
[[0], 3], //B 1
135-
[[0], 5], //C 2
136-
[[1], 3], //D 3
137-
[[2, 3], 4], //E 4
109+
[[43], 12], //H 42
110+
[[26, 29, 41, 40, 39], 18], //I 43
111+
[[43], 2], //J 44
112+
[[43], 8], //K 45
113+
[[43], 14], //L 46
114+
[[45, 42, 46], 2], //M 47
115+
[[37, 47, 44, 28, 11], 4], //N 48
138116
*/
139-
140-
];
141117

142-
let array2 = [
118+
119+
//šesti zadatak -> 7 dana u tjendu
143120
/*
144-
[[], 2], //A 0
145-
[[0], 2], //B 1
146-
[[0], 5], //C 2
147-
[[1], 4], //D 3
148-
[[2, 3], 4], //E 4
149-
*/
150-
151-
[[], 2], //A 0 Izrada koncepta svijeta
152-
[[0], 2], //B 1 Izrada koncepta likova
153-
[[0], 2], //C 2 Izrada koncepta mehanike
154-
[[1,2], 1], //D 3 Izrada GDD-a
155-
[[3], 3], //E 4
121+
[[], 2], //A 0
122+
[[0], 2], //B 1
123+
[[0], 2], //C 2
124+
[[1,2], 1], //D 3
125+
[[3], 3], //E 4
156126
[[4], 1], //F 5
157127
[[5], 2], //G 6
158128
[[5], 2], //H 7
159129
[[5], 3], //I 8
160130
[[5], 3], //J 9
161131
[[5], 3], //K 10
162132
[[5], 108], //M 11
163-
[[7], 6], //N 12
133+
[[7], 14], //N 12
164134
[[7], 5], //O 13
165-
[[8], 5], //P 14
135+
[[8], 6], //P 14
166136
[[9, 10], 5], //A 15
167-
[[13], 2], //B 16
137+
[[13], 4], //B 16
168138
[[12], 4], //C 17
169139
[[12], 8], //D 18
170140
[[15], 8], //E 19
171141
[[6, 15], 8], //F 20
172-
[[14], 1], //G 21
142+
[[14], 1], //G 21 // probat cemo 0 kasnije
173143
[[14], 8], //H 22
174-
[[15], 4], //I 23
175-
[[15], 4], //J 24
176-
[[23], 3], //K 25
144+
[[15], 5], //I 23
145+
[[15], 6], //J 24
146+
[[23], 2], //K 25
177147
[[19, 17, 18], 12], //L 26
178-
[[24], 5], //M 27
148+
[[24], 4], //M 27
179149
[[22], 84], //N 28
180150
[[20], 8], //O 29
181151
[[21], 10], //P 30
@@ -197,11 +167,41 @@ let array2 = [
197167
[[43], 14], //L 46
198168
[[45, 42, 46], 2], //M 47
199169
[[37, 47, 44, 28, 11], 4], //N 48
170+
*/
171+
172+
//deseti zadatak
173+
[[], 0],
174+
[[0], 2], //G 6 0
175+
[[0], 2], //H 7 1
176+
[[0], 3], //I 8 2
177+
[[0], 3], //J 9 3
178+
[[0], 3], //K 10 4
179+
[[2], 13], //N 12 5
180+
[[2], 5], //O 13 6
181+
[[3], 6], //P 14 7
182+
[[4, 5], 5], //A 15 8
183+
[[7], 4], //B 16 9
184+
[[9], 8], //E 19 10
185+
[[1, 9], 8], //F 20 12
186+
[[8], 7], //G 21 --- //nedovršena aktivnost 13
187+
[[9], 5], //I 23 14
188+
[[9], 6], //J 24 15
189+
[[15], 4], //K 25 --- //nedovršena aktivnost 16
190+
[[16], 3], //M 27 --- //nedovršena aktivnost 17
191+
192+
/*
193+
[[], 1], //0
194+
[[0], 1], //1
195+
[[1], 1], //2
196+
[[1], 1], //3
197+
[[2], 1], //4
198+
[[3,4], 1], //5
199+
*/
200200
];
201201

202202
document.getElementById("start").addEventListener('click', () => { document.getElementById("results").innerHTML = ""; start(array1);})
203203
document.getElementById("start2").addEventListener('click', () => { document.getElementById("results").innerHTML = ""; start(array1); start(array2);
204-
compareArrays(array1, array2);})
204+
findSubgraph(array1, array2);})
205205

206206
function init() {
207207
array1.forEach((item) => {
@@ -271,6 +271,7 @@ function calculateKP_All(array) {
271271
resolve(true);
272272
});
273273
}
274+
274275
function calculateST_All(array) {
275276
return new Promise((resolve, reject) => {
276277
array.forEach((item) => (item[6] = item[4] - item[3]));
@@ -295,17 +296,80 @@ function hasSuccessorInChainOrIsLast(index, array){
295296
function compareArrays(array1, array2){
296297
let out = "Usporedba grafova: <br/>"
297298
out += `<br/><span style="padding-left: 2.1em;">Tr - Poč - Kr </span>`;
299+
let old_out = out;
298300
for(let i = 0; i < array1.length; i++){
299-
if(JSON.stringify(array1[i]) !== JSON.stringify(array2[i])){
301+
if(array1[i][1] !== array2[i][1] || array1[i][2] !== array2[i][2] || array1[i][4] !== array2[i][4] ){
300302
out += `<br/>----AKTIVNOST ${i+1}----` + "<br/>";
301303
out += "<span class='padded'>"+array1[i][1] + " - " + array1[i][2] + " - " + array1[i][4] + "</span><br/>";
302304
out += "<span class='padded'>"+array2[i][1] + " - " + array2[i][2] + " - " + array2[i][4] + "</span><br/>";
303305
out += "---------------------------" + "<br/>";
304306
}
305307
}
308+
if(out === old_out)
309+
out += "<br/><br/>Grafovi se podudaraju!"
306310
document.getElementById("comparison").innerHTML = out;
307311
}
308312

313+
function DFS(array, curr_index, item){
314+
let not_checked_array1 = array[curr_index][7];
315+
316+
if(array[curr_index][7].length == item[7].length)
317+
array[curr_index][9] = 2;
318+
else if(array[curr_index][7].length > item[7].length && array[curr_index][9] != 2)
319+
array[curr_index][9] = 1;
320+
not_checked_array1.forEach(elem => {
321+
item[7].forEach(elem2 => {
322+
DFS(array, elem, array2[elem2]);
323+
})
324+
})
325+
array[curr_index][7].forEach(next_index => DFS(array, next_index, item))
326+
}
327+
328+
function findSubgraph(array1, array2){
329+
let index = 0;
330+
array1.forEach(item => {
331+
let new_arr = [];
332+
array1.filter(elem => elem[0].includes(index)).forEach(item => new_arr.push(array1.indexOf(item)));
333+
item.push(new_arr);
334+
index++;
335+
})
336+
index = 0;
337+
array2.forEach(item => {
338+
let new_arr = [];
339+
array2.filter(elem => elem[0].includes(index)).forEach(item => new_arr.push(array2.indexOf(item)));
340+
item.push(new_arr, 0);
341+
index++;
342+
})
343+
344+
index = 0;
345+
let item = array2[0];
346+
array1.forEach(item2 => {
347+
DFS(array1, index, item);
348+
let sum = 0;
349+
array1.forEach(item3 =>{
350+
sum+=item3[9];
351+
item3[9] = 0;
352+
item3[8] = 0;
353+
});
354+
item2[10] = sum;
355+
index++;
356+
})
357+
let max = 0;
358+
let max_index = -1;
359+
index = 0;
360+
array1.forEach(item => {
361+
if(item[10] > max){
362+
max = item[10]
363+
max_index = index;
364+
}
365+
index++;
366+
})
367+
let arr_to_compare = array1.slice(max_index,max_index+array2.length);
368+
compareArrays(arr_to_compare, array2);
369+
}
370+
init();
371+
findSubgraph(array1, array2);
372+
309373
let tableShown = false;
310374
function showTable(){
311375
if(tableShown)

0 commit comments

Comments
 (0)