Cytoscape Dijkstra Algorithm off?

795 Views Asked by At

I'm trying to use Cytoscape to test Dijkstra's algorithm on various graphs. Using the graph below

enter image description here

It's easy to see that the shortest path from A to C would be A -> D -> E -> C.

When running Cytoscape's Djikstra's algorithm though

const dijkstra = cy.elements().dijkstra('#A', function(){
  const weight = this.data('weight');
  return weight;
}, false);
const pathToC = dijkstra.pathTo( cy.$('#C') );
const distToC = dijkstra.distanceTo( cy.$('#C') );

Cytoscape gives the output

A -> B -> C

which is definitely wrong.

Am I doing something wrong? Or is Cytoscape's algorithm off?

PS Code to generate the graph in Cytoscape is

{
  "elements": {
    "nodes": [
      {
        "data": {
          "id": "A"
        },
        "position": {
          "x": 187,
          "y": 135
        },
        "group": "nodes",
        "removed": false,
        "selected": false,
        "selectable": true,
        "locked": false,
        "grabbable": true,
        "classes": ""
      },
      {
        "data": {
          "id": "B"
        },
        "position": {
          "x": 516,
          "y": 128
        },
        "group": "nodes",
        "removed": false,
        "selected": false,
        "selectable": true,
        "locked": false,
        "grabbable": true,
        "classes": ""
      },
      {
        "data": {
          "id": "C"
        },
        "position": {
          "x": 726,
          "y": 302
        },
        "group": "nodes",
        "removed": false,
        "selected": false,
        "selectable": true,
        "locked": false,
        "grabbable": true,
        "classes": ""
      },
      {
        "data": {
          "id": "D"
        },
        "position": {
          "x": 186,
          "y": 447
        },
        "group": "nodes",
        "removed": false,
        "selected": false,
        "selectable": true,
        "locked": false,
        "grabbable": true,
        "classes": ""
      },
      {
        "data": {
          "id": "E"
        },
        "position": {
          "x": 510,
          "y": 445
        },
        "group": "nodes",
        "removed": false,
        "selected": false,
        "selectable": true,
        "locked": false,
        "grabbable": true,
        "classes": ""
      }
    ],
    "edges": [
      {
        "data": {
          "id": "BA",
          "source": "B",
          "target": "A",
          "weight": "100"
        },
        "position": {
          "x": 514,
          "y": 127
        },
        "group": "edges",
        "removed": false,
        "selected": true,
        "selectable": true,
        "locked": false,
        "grabbable": true,
        "classes": ""
      },
      {
        "data": {
          "id": "DA",
          "source": "D",
          "target": "A",
          "weight": "1"
        },
        "position": {
          "x": 187,
          "y": 446
        },
        "group": "edges",
        "removed": false,
        "selected": false,
        "selectable": true,
        "locked": false,
        "grabbable": true,
        "classes": ""
      },
      {
        "data": {
          "id": "BD",
          "source": "B",
          "target": "D",
          "weight": "100"
        },
        "position": {
          "x": 516,
          "y": 127
        },
        "group": "edges",
        "removed": false,
        "selected": false,
        "selectable": true,
        "locked": false,
        "grabbable": true,
        "classes": ""
      },
      {
        "data": {
          "id": "EB",
          "source": "E",
          "target": "B",
          "weight": "100"
        },
        "position": {
          "x": 500,
          "y": 442
        },
        "group": "edges",
        "removed": false,
        "selected": false,
        "selectable": true,
        "locked": false,
        "grabbable": true,
        "classes": ""
      },
      {
        "data": {
          "id": "ED",
          "source": "E",
          "target": "D",
          "weight": "1"
        },
        "position": {
          "x": 501,
          "y": 443
        },
        "group": "edges",
        "removed": false,
        "selected": false,
        "selectable": true,
        "locked": false,
        "grabbable": true,
        "classes": ""
      },
      {
        "data": {
          "id": "CE",
          "source": "C",
          "target": "E",
          "weight": "5"
        },
        "position": {
          "x": 727,
          "y": 304
        },
        "group": "edges",
        "removed": false,
        "selected": false,
        "selectable": true,
        "locked": false,
        "grabbable": true,
        "classes": ""
      },
      {
        "data": {
          "id": "BC",
          "source": "B",
          "target": "C",
          "weight": "5"
        },
        "position": {
          "x": 521,
          "y": 132
        },
        "group": "edges",
        "removed": false,
        "selected": false,
        "selectable": true,
        "locked": false,
        "grabbable": true,
        "classes": ""
      }
    ]
  },
  "style": [
    {
      "selector": "node",
      "style": {
        "background-color": "#666",
        "label": "data(id)"
      }
    },
    {
      "selector": "edge",
      "style": {
        "width": "3px",
        "label": "data(weight)",
        "line-color": "#ccc",
        "target-arrow-color": "#ccc",
        "target-arrow-shape": "triangle"
      }
    }
  ],
  "zoomingEnabled": true,
  "userZoomingEnabled": true,
  "zoom": 1,
  "minZoom": 1.0e-50,
  "maxZoom": 1.0e+50,
  "panningEnabled": true,
  "userPanningEnabled": true,
  "pan": {
    "x": 0,
    "y": 0
  },
  "boxSelectionEnabled": true,
  "renderer": {
    "name": "canvas"
  }
}
1

There are 1 best solutions below

0
Mark-K On BEST ANSWER

I'm having the same issue, pretty much same code.

Done some debugging, looks like the dist object is concatenating strings rather than adding integers.

dist: Object
AMS11: "0474483"  -> 0, 4, 74, 483 should really be 561
BRU11: Infinity
FRA11: Infinity
GVA11: Infinity
LON11: "0474"     -> 0, 4, 74 should really be 78
LON21: 0
LON22: "04"       -> 0, 4 should really be 4
LON31: "047415"   -> 0, 4, 74, 15 should really be 93
LUX21: Infinity
LUX31: Infinity
MOW11: Infinity
MOW12: Infinity
PAR11: "049693"
PAR21: "0474805"
STO21: Infinity
STO31: Infinity
TYO11: Infinity
TYO12: Infinity

Edit: Answer is to change your weights to integers so use "weight": 1 instead of "weight": "1".