Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Non numeric node IDs #13

Open
sscarduzio opened this issue Nov 12, 2022 · 1 comment
Open

Non numeric node IDs #13

sscarduzio opened this issue Nov 12, 2022 · 1 comment

Comments

@sscarduzio
Copy link

This would be infinitely more practical if I could add nodes and edges by unique string ID rather than numbers.
For example, G6 graph visualization library works exclusively with string IDs.

@blakelapierre
Copy link

Here's my attempt so far. I'll put it in a helper package if I get my project working correctly.

This seems to work ok until I try using the BellmanFord algorith on a larger graph (hundreds of nodes and edges)....

const g = createWeightedDiGraph([
    ['btc', 'usd', -Math.log(20549.38), -Math.log(1/20549.38)]
]);

function createWeightedDiGraph(edgeInfo) {
    const labels = edgeInfo.reduce((acc, [n1, n2, weight]) => {
        const indices = [acc.symbolMap[n1], acc.symbolMap[n2]];

        if (indices[0] === undefined) {
            const i = indices[0] = acc.symbolArray.length;
            acc.symbolMap[n1] = i;
            acc.symbolArray.push(n1);
        }
        if (indices[1] === undefined) {
            const i = indices[1] = acc.symbolArray.length;
            acc.symbolMap[n2] = i;
            acc.symbolArray.push(n2);
        }


        return acc;   
    }, {symbolMap: {}, symbolArray: []});

    //edgeInfo.forEach(())

    const g = new jsgraphs.WeightedDiGraph(labels.symbolArray.length);

    edgeInfo.forEach(([n1, n2, weight, rweight]) => {
        const i1 = labels.symbolMap[n1],
              i2 = labels.symbolMap[n2];

        if (weight !== undefined) {
            console.log('adding edge', n1, '->', n2, '(', weight, ')');
            g.addEdge(new jsgraphs.Edge(i1, i2, weight));
        }
        if (rweight !== undefined) {
            console.log('adding edge', n2, '->', n1, '(', rweight, ')');
            g.addEdge(new jsgraphs.Edge(i2, i1, rweight));
        }

        g.node(i1).label = n1;
        g.node(i2).label = n2;
    });

    return {g, labels};
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants