Skip to content

Commit 47767fc

Browse files
committed
Fixed duplicate edges on opening.
1 parent 2488995 commit 47767fc

File tree

2 files changed

+95
-1
lines changed

2 files changed

+95
-1
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "graph-creator",
3-
"version": "0.7.4",
3+
"version": "0.7.5",
44
"main": "index.html",
55
"window": {
66
"height": 1024,

src/fileWrite.js

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,97 @@ function openPositions(){
300300
//***************************************************************************
301301
//OPEN GRAPHS
302302
//***************************************************************************
303+
function enumEdges(){
304+
/*
305+
Goes through and enumerates the number the number undirected edges between
306+
each pairs of vertices that have edges between them.
307+
308+
OUTPUT
309+
edgeCount: An object of the form {source: {target: {count: 1, ids: [edgeIdHere]}}}
310+
*/
311+
var edges = cy.edges();
312+
var edgeCount = {};
313+
314+
//Enumerate each type of edge
315+
for(var i=0; i<edges.length; i++){
316+
var source = edges[i].data('source'),
317+
target = edges[i].data('target'),
318+
id = edges[i].data('id');
319+
320+
//Check if our edgeCount dict has an entry for the source or target
321+
var sourceEntry = source in edgeCount,
322+
targetEntry = target in edgeCount;
323+
324+
//If neither entries exist make one
325+
if(!sourceEntry && !targetEntry){
326+
edgeCount[source] = {};
327+
edgeCount[source][target] = {count: 1, ids: [id]};
328+
}
329+
//Otherwise use the existing one already
330+
else if(sourceEntry && !targetEntry){
331+
//Check if our target is in the sourceEntry
332+
var targetInner = target in edgeCount[source];
333+
334+
if(!targetInner){
335+
edgeCount[source][target] = {count: 0, ids: []};
336+
}
337+
338+
edgeCount[source][target].count++;
339+
edgeCount[source][target].ids.push(id);
340+
}
341+
else if(targetEntry && !sourceEntry){
342+
//Check if our source is in the targetEntry
343+
var sourceInner = source in edgeCount[target];
344+
345+
if(!sourceInner){
346+
edgeCount[target][source] = {count: 0, ids: []};
347+
}
348+
349+
edgeCount[target][source].count++;
350+
edgeCount[target][source].ids.push(id);
351+
}
352+
else if(targetEntry && sourceEntry){
353+
//Check if one already has a entry
354+
var targetInner = target in edgeCount[source],
355+
sourceInner = source in edgeCount[target];
356+
357+
if(targetInner){
358+
edgeCount[source][target].count++;
359+
edgeCount[source][target].ids.push(id);
360+
}
361+
else if(sourceInner){
362+
edgeCount[target][source].count++;
363+
edgeCount[target][source].ids.push(id);
364+
}
365+
else{
366+
edgeCount[source][target] = {count: 1, ids: id};
367+
}
368+
}
369+
}
370+
371+
return edgeCount;
372+
}
373+
374+
function removeDuplicateEdges(){
375+
/*
376+
Counts the number of undirected edges of each type and removes half of them.
377+
*/
378+
379+
var edgeCount = enumEdges();
380+
381+
//When the number of a type of edge is divisible by two, we have duplicates
382+
for(var source in edgeCount){
383+
for(var target in edgeCount[source]){
384+
385+
//Check for duplicates and remove
386+
if(edgeCount[source][target].count % 2 === 0){
387+
for(var i=0; i<edgeCount[source][target].ids.length/2; i++){
388+
cy.remove(cy.$id(edgeCount[source][target].ids[i]))
389+
}
390+
}
391+
}
392+
}
393+
}
303394

304395
function parseGraph(graph){
305396
/*
@@ -384,6 +475,9 @@ function parseGraph(graph){
384475

385476
currentLine+= degree;
386477
}
478+
479+
//The way in which we add adds each edge twice
480+
removeDuplicateEdges();
387481
}
388482

389483
function openGraph(){

0 commit comments

Comments
 (0)