Skip to content

Commit

Permalink
Fixed a bug
Browse files Browse the repository at this point in the history
Fixed a bug in the function used to find the minimal weight matching: the counter in the inner loop before the recursive calls has a incorrect starting value, causing the algorithm to visit the lower left triangle of the given weight matrix, which maybe undefined.
  • Loading branch information
hanzhi713 committed Feb 22, 2018
1 parent 0549412 commit 32125ba
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 33 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
.babelrc
/Graphs/js/node_modules/
/Doc/
/.idea/
/.idea/
dist/
2 changes: 1 addition & 1 deletion Graphs/js/MWMMT.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ var onmessage = function (message) {
var matches = new Array(len);
var tempWeight, tempEles;
for (var x = start; x < end; x++)
for (var y = 0; y < len; y++) {
for (var y = x + 1; y < len; y++) {
if (weightMatrix[x][y] === 0)
continue;
tempWeight = weightMatrix[x][y];
Expand Down
2 changes: 1 addition & 1 deletion Graphs/js/MWMMT.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 29 additions & 26 deletions Graphs/js/main-undirected.babel.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Graphs/js/main-undirected.babel.min.js

Large diffs are not rendered by default.

13 changes: 10 additions & 3 deletions Graphs/js/main-undirected.js
Original file line number Diff line number Diff line change
Expand Up @@ -1579,6 +1579,7 @@ function myDijkstra() {
animateEdge(edgeBetween, () => {
if (!animationFlag)
return;

// if the target node haven't got a permanent label
if (target.data('permanent') === undefined)

Expand Down Expand Up @@ -1607,6 +1608,7 @@ function myDijkstra() {
animateEdge(edge, () => {
if (!animationFlag)
return;

// if the target node doesn't have a temporary label,
// or the new weight is lower than the current label
// update the label
Expand Down Expand Up @@ -1706,6 +1708,7 @@ function myDijkstra() {
addNextLabel(currentNode, currentNode.connectedEdges(), 0);
});
}

// same story as the above, but without animation
// looks nicer, no awful callbacks
else {
Expand Down Expand Up @@ -2337,7 +2340,7 @@ function traceEulerianCycle(start, c) {
}
});

// break the journey (node: the next half of the journey journey will be added back later)
// break the journey (note: the next half of the journey journey will be added back later)
nextJourney = node.next;
currentNode = node.cargo;
node.next = null;
Expand Down Expand Up @@ -2372,6 +2375,7 @@ function minimalWeightMatching() {
stopAnimation();
clearCyStyle();
cy.elements().unselect();

// precondition: the graph is connected
if (!isConnected())
return alert("Graph not connected!");
Expand Down Expand Up @@ -2428,7 +2432,7 @@ function minimalWeightMatching() {
* @return void
* */
function minimalWeightMatchingMultiThread(weightMatrix, numOfThreads, callback) {
// generator is used to merge the results from each thread
// generator is used to merge the results from each worker
function*join() {
while (true) {
yield null;
Expand Down Expand Up @@ -2501,6 +2505,7 @@ function CPP() {
stopAnimation();
clearCyStyle();
cy.elements().unselect();

// get the collection of odd nodes
cy.nodes().forEach((ele) => {
if (ele.degree() % 2 !== 0)
Expand All @@ -2512,7 +2517,7 @@ function CPP() {
if (!confirm("Warning! This graph has " + n + " nodes of odd degree, at most " + (f(n) / (f(n / 2) * Math.pow(2, n / 2))) + " iterations are needed and it might take a long time!"))
return;

// get the weight matrix of these nodes (a subgraph)
// get the weight matrix of these nodes (the subgraph consisting only the nodes of odd degree)
let weightMatrix = new Array(n);
let paths = new Array(n);
for (let x = 0; x < n; x++) {
Expand All @@ -2521,9 +2526,11 @@ function CPP() {
for (let y = x + 1; y < n; y++)
weightMatrix[x][y] = paths[x].distanceTo(nodes[y]);
}

// get the minimal weight perfect matching
minimalWeightMatchingMultiThread(weightMatrix, 4, displayResult);

// the callback function used to show the result on the result canvas
function displayResult(minPairing) {
clearResult();
ca.add(cy.elements());
Expand Down

0 comments on commit 32125ba

Please sign in to comment.