Skip to content

Instantly share code, notes, and snippets.

@sukria
Created June 5, 2015 15:44
Show Gist options
  • Save sukria/de94c91dfb25ac9ee55a to your computer and use it in GitHub Desktop.
Save sukria/de94c91dfb25ac9ee55a to your computer and use it in GitHub Desktop.
<script>
// returns the node, given its name, null if not found
function get_node_by_name(name, bigfish) {
for (var lookup in bigfish["nodes"]) {
if (lookup["name"] == name) {
return lookup;
}
}
return null;
}
// a local cache to avoid searching for the same node over and over
var nodeCache = {};
// returns the node given its id, null if not found
function get_node_by_id(id, bigfish) {
var nodes = bigfish["nodes"];
if (nodeCache.hasOwnProperty(id)) {
return nodeCache[id];
}
for (i=0; i<nodes.length; i++) {
var lookup = nodes[i];
if (lookup["id"] == id) {
nodeCache[id] = lookup;
return lookup;
}
}
return null;
}
// return the array of neighbors of all nodes linked to the given node
function get_neighbors(node, bigfish) {
console.log("get_neighbors...");
var neighbors = new Array();
var links = bigfish["links"];
for (i=0; i<links.length; i++) {
var lookup = links[i];
// the graph is not directed, eg: if our node is source or target, then we have a neighbor!
if (lookup["source"] == node["id"]) {
neighbor = get_node_by_id(lookup["target"], bigfish);
// we copy the similarity of the node, for that link with our main node, for the sort
neighbor["similarity"] = lookup["similarity"];
console.log("adding sim: "+neighbor["similarity"] );
neighbors.push(neighbor);
}
else if (lookup["target"] == node["id"]) {
neighbor = get_node_by_id(lookup["source"], bigfish);
// we copy the similarity of the node, for that link with our main node, for the sort
neighbor["similarity"] = lookup["similarity"];
neighbors.push(neighbor);
}
}
return neighbors;
}
/*
* This returns the array of neighbors of the given node, sorted by their similarity value, in the links structure.
*/
function sort_by_sim(node, bigfish) {
var neighbors = get_neighbors(node, bigfish);
neighbors.sort(function(a,b) { a["similarity"] - b["similarity"] });
return neighbors;
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment