JSFiddle - React, Tailwind, and code Playground
by jrab227
JavaScript
var graph = {
nodes: [0,1,2,3,4,5,6].map(function(d){return {id:d} }),
edges: [
{source: 0, target:1, value:44},
{source: 0, target:2, value:43},
{source: 2, target:1, value:50},
{source: 2, target:3, value:47},
{source: 5, target:1, value:49},
{source: 3, target:5, value:48},
{source: 3, target:4, value:45},
{source: 4, target:6, value:46},
]
};
function strongestLink(edges, comparator){
//Function to find the strongest link in edges
var winner = undefined;
for (edge in edges) {
if (winner === undefined
|| comparator(edges[edge].value, winner.value) ){
winner = edges[edge];
}
};
console.log(winner)
return [winner.source.toString(), winner.target.toString()];
};
function greaterThan(first,second){
return (first > second);
};
function singleLinkage(edge1,edge2){
//single linkage binary maximum strategy
return Math.max([edge1,edge2]);
};
function randomId(){
return Math.floor(Math.random()*16777215).toString(16);
}
function hierarchicalCluster(graph, comparator, linkageStrategy, debug){
var nodeRoot = {};
graph.nodes.map(function(node){
nodeRoot[node.id.toString()] = node;
});
var edgesList = Object.create(graph.edges);
while (Object.keys(nodeRoot).length > 1){
//for (var i = 0; i < 1; i++) { //Here for small run checking
(debug) ? console.log("--------------------------") : null
//find greatest distance edge
var mergingNodes = strongestLink(edgesList, comparator);
if (debug) {
console.log("Merging: " + mergingNodes)
console.log(nodeRoot[mergingNodes[0]])
console.log(nodeRoot[mergingNodes[1]])
}
var edgesLinkedToMerging = edgesList.filter(function(edge){
if (debug) {
...