Skip to content

Instantly share code, notes, and snippets.

@tbblack
Created November 28, 2019 13:09
Show Gist options
  • Save tbblack/9270721755e73d35a03b5d94ba5d2d06 to your computer and use it in GitHub Desktop.
Save tbblack/9270721755e73d35a03b5d94ba5d2d06 to your computer and use it in GitHub Desktop.
Cytoscape.js shortest path exmaple
<div id="cy"></div>
var cy = cytoscape({
container: document.getElementById('cy'), // container to render in
layout: {
name: 'random'
},
elements: [{
group: 'nodes',
data: {
id: 'n1'
}
}, {
group: 'nodes',
data: {
id: 'n2'
}
}, {
group: 'nodes',
data: {
id: 'n3'
}
}, {
group: 'nodes',
data: {
id: 'n4'
}
}, {
data: {
id: 'e1',
source: 'n1',
target: 'n4'
}
},
{
data: {
id: 'e2',
source: 'n1',
target: 'n2'
}
}, {
data: {
id: 'e3',
source: 'n2',
target: 'n3'
}
}, {
data: {
id: 'e4',
source: 'n3',
target: 'n4'
}
}]
});
var dfs = cy.elements().aStar({
root: '#n1',
goal: '#n4',
directed: true
})
dfs.path.select()
console.log(dfs.distance)
<script src="https://rawgit.com/cytoscape/cytoscape.js/master/dist/cytoscape.min.js"></script>
#cy {
width: 300px;
height: 300px;
display: block;
background: #000;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment