Ispravan algoritam
This commit is contained in:
@@ -1,2 +1,2 @@
|
||||
PEOPLE_DB=https://docs.google.com/spreadsheets/d/1s4Ytz7mf-YpszVTMTTrYd5iG_dsTGjrCpDQbpIx7zkU/edit#gid=1116948122
|
||||
PAIRS_LIST=https://docs.google.com/spreadsheets/d/1gPuRhTry3YoJ_ibI2BfL1ue2_YLlhtW56OP39ADJY_4/edit
|
||||
PEOPLE_DB=https://docs.google.com/spreadsheets/d/1s4Ytz7mf-YpszVTMTTrYd5iG_dsTGjrCpDQbpIx7zkU/edit#gid=0
|
||||
PAIRS_LIST=https://docs.google.com/spreadsheets/d/1gPuRhTry3YoJ_ibI2BfL1ue2_YLlhtW56OP39ADJY_4/edit#gid=0
|
||||
|
||||
140
backend/app.js
140
backend/app.js
@@ -386,6 +386,142 @@ function MakePairsV2(callback){
|
||||
});
|
||||
}
|
||||
|
||||
var matrix = [];
|
||||
var tree = [];
|
||||
let done = false;
|
||||
|
||||
function count_pairs(row, col, cntr){
|
||||
if (done) return;
|
||||
|
||||
matrix[row][col] = 2;
|
||||
|
||||
//oznaci zauzeta polja zbog izbora para
|
||||
for(let i=0;i<row;i++) matrix[row][i] = (matrix[row][i]===0)? cntr: matrix[row][i];
|
||||
for(let i=0;i<col;i++) matrix[col][i] = (matrix[col][i]===0)? cntr: matrix[col][i];
|
||||
|
||||
for(let i=row;i<matrix.length;i++) matrix[i][row] = (matrix[i][row]===0)? cntr: matrix[i][row];
|
||||
for(let i=col;i<matrix.length;i++) matrix[i][col] = (matrix[i][col]===0)? cntr: matrix[i][col];
|
||||
|
||||
//nadi sljedece slobodno
|
||||
|
||||
let found = false;
|
||||
for (let i=0;i<matrix.length;i++){
|
||||
for (let j=0;j<=i;j++){
|
||||
if (matrix[i][j]==0){
|
||||
count_pairs(i,j,cntr+1);
|
||||
found=true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!found){
|
||||
let inner_tree = [];
|
||||
for (let i=0;i<matrix.length;i++){
|
||||
for (let j=0;j<=i;j++){
|
||||
if (matrix[i][j]==2){
|
||||
inner_tree.push({row: i, col:j});
|
||||
}
|
||||
}
|
||||
}
|
||||
if (inner_tree.length == max_pairs) {
|
||||
done=true;
|
||||
tree.push(inner_tree);
|
||||
}
|
||||
}
|
||||
|
||||
//skini oznake za zauzeta polja zbog izbora para
|
||||
for(let i=0;i<row;i++) matrix[row][i] = (matrix[row][i]===cntr)? 0: matrix[row][i];
|
||||
for(let i=0;i<col;i++) matrix[col][i] = (matrix[col][i]===cntr)? 0: matrix[col][i];
|
||||
|
||||
for(let i=row;i<matrix.length;i++) matrix[i][row] = (matrix[i][row]===cntr)? 0: matrix[i][row];
|
||||
for(let i=col;i<matrix.length;i++) matrix[i][col] = (matrix[i][col]===cntr)? 0: matrix[i][col];
|
||||
|
||||
matrix[row][col] = 0;
|
||||
}
|
||||
|
||||
function MakePairsV3(callback){
|
||||
executeAPI(getAvailableNames,function(names){
|
||||
executeAPI(getPairs, function(pairs){
|
||||
max_pairs = Math.floor(names.length/2);
|
||||
matrix=[];
|
||||
|
||||
for (let i=0;i<names.length;i++){
|
||||
let matrix_row = [];
|
||||
for (let j=0;j<=i;j++) (i==j)? matrix_row.push(1):matrix_row.push(0);
|
||||
matrix.push(matrix_row);
|
||||
}
|
||||
|
||||
//Oznaci postojece parove, stavljajuci 3 na polja u matrici
|
||||
pairs.filter((pair)=>{
|
||||
|
||||
let name1_id = names.indexOf(pair.name1);
|
||||
let name2_id = names.indexOf(pair.name2);
|
||||
|
||||
if (name1_id != -1 && name2_id != -1){
|
||||
if (name1_id > name2_id){
|
||||
matrix[name1_id][name2_id] = 3;
|
||||
}else{
|
||||
matrix[name2_id][name1_id] = 3;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
|
||||
tree = [];
|
||||
done=false;
|
||||
|
||||
for (let i=0;i<names.length;i++){
|
||||
for (let j=0;j<=i;j++){
|
||||
if (matrix[i][j] === 0){
|
||||
count_pairs(i,j,5);
|
||||
if (done) break;
|
||||
}
|
||||
}
|
||||
if (done) break;
|
||||
}
|
||||
|
||||
/*
|
||||
for (let i=0;i<tree.length;i++){
|
||||
console.log(tree[i]);
|
||||
console.log("---");
|
||||
}*/
|
||||
|
||||
let max_count_index = 0;
|
||||
|
||||
for (let i=0;i<tree.length;i++){
|
||||
if (tree[i].length > tree[max_count_index].length)
|
||||
max_count_index = i;
|
||||
}
|
||||
|
||||
let result_pairs = [];
|
||||
if (tree.length > 0){
|
||||
for (let i=0;i<tree[max_count_index].length;i++){
|
||||
result_pairs.push({name1: names[tree[max_count_index][i].row],name2: names[tree[max_count_index][i].col] });
|
||||
}
|
||||
|
||||
let indexToRemove = [];
|
||||
|
||||
for (let i=0;i<tree[max_count_index].length;i++){
|
||||
indexToRemove.push(tree[max_count_index][i].row);
|
||||
indexToRemove.push(tree[max_count_index][i].col);
|
||||
}
|
||||
|
||||
indexToRemove.sort((a,b)=>{
|
||||
if (a>b) return -1; else return 1;
|
||||
});
|
||||
|
||||
for (let i=0;i<indexToRemove.length;i++)
|
||||
names.splice(indexToRemove[i],1);
|
||||
}
|
||||
|
||||
if (names.length == 0) names=null;
|
||||
pairsForSave = result_pairs;
|
||||
callback.send({pairs: result_pairs, left: names});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function SavePairs(callback){
|
||||
if (lastRow== null) {
|
||||
callback.send({result:true});
|
||||
@@ -454,11 +590,11 @@ app.use(function(req, res, next) {
|
||||
app.get('/getPairs',(req,resp)=>{
|
||||
pairsForSave=[];
|
||||
lastRow=null;
|
||||
MakePairsV2(resp);
|
||||
MakePairsV3(resp);
|
||||
});
|
||||
|
||||
app.get('/getAllPairs',(req,resp)=>{
|
||||
MakePairsV2(resp);
|
||||
MakePairsV3(resp);
|
||||
});
|
||||
|
||||
app.get('/savePairs', (req,resp)=>{
|
||||
|
||||
Reference in New Issue
Block a user