-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
129 lines (115 loc) · 3.92 KB
/
index.js
File metadata and controls
129 lines (115 loc) · 3.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import FloydWarshall from "floyd-warshall";
import graphInterfaceDefault from "./graphInterfaces";
import "./graphInterfaces";
export * from "./graphInterfaces";
import curry from "curry";
function buildUnweightedAdjacencyMatrix(
nodes = [],
edges = [],
graphInterface = {}
) {
graphInterface = { ...graphInterfaceDefault, ...graphInterface };
const { getNodeId, getEdgeSource, getEdgeTarget } = graphInterface;
var adj = [];
for (var i = 0; i < nodes.length; i++) {
adj[i] = new Array(nodes.length);
}
edges.forEach((edge) => {
adj[
nodes.indexOf(
nodes.find((node) => getNodeId(node) == getEdgeSource(edge))
)
][
nodes.indexOf(
nodes.find((node) => getNodeId(node) == getEdgeTarget(edge))
)
] = 1;
});
return adj;
}
const iterateUndirected = (adjacencyMatrix, iterFn) => {
// only iterate over the upper triangular part of the adjacency matrix
for (let i = 0; i < adjacencyMatrix.length; i++) {
const row = adjacencyMatrix[i];
for (let j = i; j < row.length; j++) {
const entryValue = row[j];
iterFn(i, j, entryValue);
}
}
};
const iterateDirected = (adjacencyMatrix, iterFn) => {
// use the whole adjacency matrix to build edges
adjacencyMatrix.forEach((row, i) => {
row.forEach((entryValue, j) => {
iterFn(i, j, entryValue);
});
});
};
/**
* @typedef {Object} Options
* @property {boolean} [directed=true] - Whether or not to treat this as a directed graph.
* @property {GraphInterface} [graphInterface] - An object containing graph interface functions.
* @property {getNodeIdFunction} [getNodeId] - A function that returns a node's ID.
* @property {getEdgeSourceFunction} [getEdgeSource] - A function that gets an edge's source node.
* @property {getEdgeTargetFunction} [getEdgeTarget] - A function that gets an edge's target node.
* @property {makeHopFunction} [makeHop] - A function that returns information about a geodesic relationship.
*/
const defaultOptions = {
directed: true,
graphInterface: {},
};
/**
* Generates n-hop graph edges from the given graph's geodesics (auto-curried interface).
* @function
* @param {Options} options
* @param {Array.<NodeObject>|Array.<NodeValue>} nodes
* @param {Array.<EdgeObject>} edges
* @return {object|function} An object containing lists of n-hop graph edges keyed by the lengths of their associated geodesics.
*/
export const hopsFinder = curry((options, nodes, edges) => {
options = { ...defaultOptions, ...options };
let { directed, graphInterface, makeHop } = options;
// fallback to defaults
graphInterface = { ...graphInterfaceDefault, ...graphInterface, ...options };
// if a `makeHop` function is specified in the options use that one
makeHop = makeHop || graphInterface.makeHop;
// make the graph's adjacency matrix and find all shortest paths
const adjacencyMatrix = buildUnweightedAdjacencyMatrix(
nodes,
edges,
graphInterface
);
const hopsAdjacencyMatrix = new FloydWarshall(adjacencyMatrix).shortestPaths;
const hops = {};
// make info objects for each hop
const addHop = (rowIndex, colIndex, distance) => {
if (distance > 0) {
if (!hops[distance]) {
hops[distance] = [];
}
let hop = makeHop(
nodes[rowIndex],
nodes[colIndex],
distance,
graphInterface.getNodeId
);
hops[distance].push(hop);
}
};
if (directed) {
iterateDirected(hopsAdjacencyMatrix, addHop);
} else {
iterateUndirected(hopsAdjacencyMatrix, addHop);
}
return hops;
});
/**
* Generates n-hop graph edges from the given graph's geodesics.
* @function
* @param {Array.<NodeObject>|Array.<NodeValue>} nodes
* @param {Array.<EdgeObject>} edges
* @param {Options} [options]
* @return {object} An object containing lists of n-hop graph edges keyed by the lengths of their associated geodesics.
*/
export const graphHops = (nodes, edges, options) =>
hopsFinder(options, nodes, edges);