Implement iterative graph compression
This commit is contained in:
parent
a51a80a4b3
commit
e696761f8e
|
@ -340,7 +340,7 @@ different sequence of nodes in two or more lines.`);
|
||||||
* Create a graph for navigating between stops.
|
* Create a graph for navigating between stops.
|
||||||
* @param {Array.<Object>} elementsList List of nodes retrieved from OSM.
|
* @param {Array.<Object>} elementsList List of nodes retrieved from OSM.
|
||||||
* @param {Object.<string,Object>} elementsById OSM nodes indexed by their ID.
|
* @param {Object.<string,Object>} elementsById OSM nodes indexed by their ID.
|
||||||
* @return {Object.<string,Object.<string,NavigationEdge>} Resulting graph.
|
* @return {Object} Resulting graph and reverse arcs.
|
||||||
*/
|
*/
|
||||||
const createNavigationGraph = (elementsList, elementsById) => {
|
const createNavigationGraph = (elementsList, elementsById) => {
|
||||||
const navigation = {};
|
const navigation = {};
|
||||||
|
@ -350,7 +350,7 @@ const createNavigationGraph = (elementsList, elementsById) => {
|
||||||
for (const obj of elementsList) {
|
for (const obj of elementsList) {
|
||||||
if (obj.type === "node") {
|
if (obj.type === "node") {
|
||||||
navigation[obj.id] = {};
|
navigation[obj.id] = {};
|
||||||
navigationReverse[obj.id] = {};
|
navigationReverse[obj.id] = new Set();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -360,27 +360,27 @@ const createNavigationGraph = (elementsList, elementsById) => {
|
||||||
const oneWay = osm.isOneWay(obj);
|
const oneWay = osm.isOneWay(obj);
|
||||||
|
|
||||||
for (let i = 0; i + 1 < obj.nodes.length; ++i) {
|
for (let i = 0; i + 1 < obj.nodes.length; ++i) {
|
||||||
const from = obj.nodes[i];
|
const from = obj.nodes[i].toString();
|
||||||
let to = obj.nodes[i + 1];
|
let to = obj.nodes[i + 1].toString();
|
||||||
let path = [from.toString(), to.toString()];
|
let path = [from, to];
|
||||||
|
|
||||||
// Make sure we can’t jump between rails at railway crossings
|
// Make sure we can’t jump between rails at railway crossings
|
||||||
if (i + 2 < obj.nodes.length
|
if (i + 2 < obj.nodes.length
|
||||||
&& osm.isRailwayCrossing(elementsById[to])) {
|
&& osm.isRailwayCrossing(elementsById[to])) {
|
||||||
const next = obj.nodes[i + 2];
|
const next = obj.nodes[i + 2].toString();
|
||||||
path = [from.toString(), to.toString(), next.toString()];
|
path = [from, to, next];
|
||||||
to = next;
|
to = next;
|
||||||
i += 1;
|
i += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
navigation[from][to] = path;
|
navigation[from][to] = path;
|
||||||
navigationReverse[to][from] = true;
|
navigationReverse[to].add(from);
|
||||||
|
|
||||||
if (!oneWay) {
|
if (!oneWay) {
|
||||||
const reversePath = [...path];
|
const reversePath = [...path];
|
||||||
reversePath.reverse();
|
reversePath.reverse();
|
||||||
navigation[to][from] = reversePath;
|
navigation[to][from] = reversePath;
|
||||||
navigationReverse[from][to] = true;
|
navigationReverse[from].add(to);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -390,29 +390,22 @@ const createNavigationGraph = (elementsList, elementsById) => {
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Remove and relink nodes that connect only two nodes or less.
|
* Identify intermediate nodes of the navigation graph that can be simplified.
|
||||||
* @param {Object.<string,Stop>} stops List of stops.
|
* @param {Set.<string>} stopsSet OSM IDs of stop nodes.
|
||||||
* @param {Navigation} navigation Input navigation graph.
|
* @param {Navigation} navigation Input navigation graph.
|
||||||
* @param {Object.<string,Object.<string,boolean>>} navigationReverse
|
* @param {Object.<string,Set.<string>>} navigationReverse Reverse arcs.
|
||||||
* Backward edges of the navigation graph.
|
* @return {Set.<string>} Set of compressible nodes.
|
||||||
*/
|
*/
|
||||||
const compressNavigationGraph = (stops, navigation, navigationReverse) => {
|
const findCompressibleNodes = (stopsSet, navigation, navigationReverse) => {
|
||||||
const stopsReverse = Object.fromEntries(
|
const compressible = new Set();
|
||||||
Object.entries(stops).map(([id, stop]) => [stop.properties.node, id])
|
|
||||||
);
|
|
||||||
|
|
||||||
let removedDeadEnds = true;
|
|
||||||
const nodesToCompress = {};
|
|
||||||
|
|
||||||
while (removedDeadEnds) {
|
|
||||||
// Identify nodes to be compressed
|
|
||||||
for (const nodeId in navigation) {
|
for (const nodeId in navigation) {
|
||||||
if (nodeId in stopsReverse) {
|
if (stopsSet.has(nodeId)) {
|
||||||
// Keep stop nodes
|
// Keep stop nodes
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
const entries = new Set(Object.keys(navigationReverse[nodeId]));
|
const entries = navigationReverse[nodeId];
|
||||||
const exits = new Set(Object.keys(navigation[nodeId]));
|
const exits = new Set(Object.keys(navigation[nodeId]));
|
||||||
|
|
||||||
// Keep split nodes, i.e. nodes with at least two exit nodes
|
// Keep split nodes, i.e. nodes with at least two exit nodes
|
||||||
|
@ -462,26 +455,34 @@ const compressNavigationGraph = (stops, navigation, navigationReverse) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Compress all other nodes
|
// Compress all other nodes
|
||||||
nodesToCompress[nodeId] = true;
|
compressible.add(nodeId);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Find nodes that cannot be used to directly link up two kept nodes
|
return compressible;
|
||||||
const usedNodes = {};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove nodes that are not used to link up two kept nodes.
|
||||||
|
* @param {Navigation} navigation Input navigation graph.
|
||||||
|
* @param {Object.<string,Set.<string>>} navigationReverse Reverse arcs.
|
||||||
|
* @param {Set.<string>} compressible Set of nodes that will not be kept.
|
||||||
|
* @return {boolean} True if some dead-ends were removed.
|
||||||
|
*/
|
||||||
|
const removeDeadEnds = (navigation, navigationReverse, compressible) => {
|
||||||
|
let didRemove = false;
|
||||||
|
|
||||||
|
// Find dead-ends starting from kept nodes
|
||||||
for (const beginId in navigation) {
|
for (const beginId in navigation) {
|
||||||
if (beginId in nodesToCompress) {
|
if (compressible.has(beginId)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
usedNodes[beginId] = true;
|
|
||||||
|
|
||||||
// Start a DFS from each node to be kept
|
|
||||||
const begin = navigation[beginId];
|
const begin = navigation[beginId];
|
||||||
const stack = [];
|
const stack = [];
|
||||||
const parent = {[beginId]: beginId};
|
const parent = {[beginId]: beginId};
|
||||||
|
|
||||||
for (const succId in begin) {
|
for (const succId in begin) {
|
||||||
if (succId in nodesToCompress) {
|
if (compressible.has(succId)) {
|
||||||
stack.push(succId);
|
stack.push(succId);
|
||||||
parent[succId] = beginId;
|
parent[succId] = beginId;
|
||||||
}
|
}
|
||||||
|
@ -491,57 +492,103 @@ const compressNavigationGraph = (stops, navigation, navigationReverse) => {
|
||||||
const endId = stack.pop();
|
const endId = stack.pop();
|
||||||
const end = navigation[endId];
|
const end = navigation[endId];
|
||||||
|
|
||||||
if (!(endId in nodesToCompress)) {
|
if (compressible.has(endId)) {
|
||||||
let trackback = parent[endId];
|
let hasSuccessor = false;
|
||||||
|
|
||||||
|
for (const succId in end) {
|
||||||
|
if (succId !== parent[endId]) {
|
||||||
|
parent[succId] = endId;
|
||||||
|
stack.push(succId);
|
||||||
|
hasSuccessor = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!hasSuccessor) {
|
||||||
|
// Remove the dead-end path
|
||||||
|
let trackback = endId;
|
||||||
|
|
||||||
while (trackback !== beginId) {
|
while (trackback !== beginId) {
|
||||||
usedNodes[trackback] = true;
|
navigationReverse[trackback].delete(parent[trackback]);
|
||||||
|
delete navigation[parent[trackback]][trackback];
|
||||||
trackback = parent[trackback];
|
trackback = parent[trackback];
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
|
didRemove = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Find dead-ends starting from compressible source nodes
|
||||||
|
for (const beginId in navigation) {
|
||||||
|
if (!compressible.has(beginId)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (navigationReverse[beginId].size > 0) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
const begin = navigation[beginId];
|
||||||
|
const stack = [];
|
||||||
|
const parent = {[beginId]: beginId};
|
||||||
|
|
||||||
|
for (const succId in begin) {
|
||||||
|
stack.push(succId);
|
||||||
|
parent[succId] = beginId;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (stack.length > 0) {
|
||||||
|
const endId = stack.pop();
|
||||||
|
const end = navigation[endId];
|
||||||
|
|
||||||
|
if (compressible.has(endId)) {
|
||||||
for (const succId in end) {
|
for (const succId in end) {
|
||||||
if (succId !== parent[endId]) {
|
if (succId !== parent[endId]) {
|
||||||
parent[succId] = endId;
|
parent[succId] = endId;
|
||||||
stack.push(succId);
|
stack.push(succId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
// Remove the dead-end path
|
||||||
|
let trackback = endId;
|
||||||
|
|
||||||
|
while (trackback !== beginId) {
|
||||||
|
navigationReverse[trackback].delete(parent[trackback]);
|
||||||
|
delete navigation[parent[trackback]][trackback];
|
||||||
|
trackback = parent[trackback];
|
||||||
|
}
|
||||||
|
|
||||||
|
didRemove = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove dead-end nodes
|
return didRemove;
|
||||||
removedDeadEnds = false;
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Compress the given set of nodes.
|
||||||
|
* @param {Navigation} navigation Input navigation graph.
|
||||||
|
* @param {Object.<string,Set.<string>>} navigationReverse Reverse arcs.
|
||||||
|
* @param {Set.<string>} compressible Set of nodes to compress.
|
||||||
|
* @return {boolean} True if some nodes were compressed.
|
||||||
|
*/
|
||||||
|
const removeCompressibleNodes = (navigation, navigationReverse, compressible) => {
|
||||||
|
let didCompress = false;
|
||||||
|
|
||||||
for (const beginId in navigation) {
|
for (const beginId in navigation) {
|
||||||
if (!(beginId in usedNodes)) {
|
if (compressible.has(beginId)) {
|
||||||
for (const neighborId in navigation[beginId]) {
|
|
||||||
delete navigationReverse[neighborId][beginId];
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const neighborId in navigationReverse[beginId]) {
|
|
||||||
delete navigation[neighborId][beginId];
|
|
||||||
}
|
|
||||||
|
|
||||||
delete navigation[beginId];
|
|
||||||
delete navigationReverse[beginId];
|
|
||||||
removedDeadEnds = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Perform node compression
|
|
||||||
for (const beginId in navigation) {
|
|
||||||
if (beginId in nodesToCompress) {
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start a DFS from each node to be kept
|
// Start a DFS from each kept node
|
||||||
const begin = navigation[beginId];
|
const begin = navigation[beginId];
|
||||||
const stack = [];
|
const stack = [];
|
||||||
const parent = {[beginId]: beginId};
|
const parent = {[beginId]: beginId};
|
||||||
|
|
||||||
for (const succId in begin) {
|
for (const succId in begin) {
|
||||||
if (succId in nodesToCompress) {
|
if (compressible.has(succId)) {
|
||||||
stack.push(succId);
|
stack.push(succId);
|
||||||
parent[succId] = beginId;
|
parent[succId] = beginId;
|
||||||
}
|
}
|
||||||
|
@ -551,44 +598,35 @@ const compressNavigationGraph = (stops, navigation, navigationReverse) => {
|
||||||
const endId = stack.pop();
|
const endId = stack.pop();
|
||||||
const end = navigation[endId];
|
const end = navigation[endId];
|
||||||
|
|
||||||
if (!(endId in nodesToCompress)) {
|
if (!compressible.has(endId)) {
|
||||||
// Found another kept node
|
// Found another kept node
|
||||||
// Collect and remove intermediate nodes
|
// Collect and remove intermediate path
|
||||||
const reversePath = [endId];
|
let path = [];
|
||||||
let trackback = parent[endId];
|
let trackback = endId;
|
||||||
let oneWay = !(trackback in end);
|
|
||||||
|
|
||||||
while (trackback !== beginId) {
|
do {
|
||||||
reversePath.push(trackback);
|
const segment = [...navigation[parent[trackback]][trackback]];
|
||||||
oneWay = oneWay || !(parent[trackback] in navigation[trackback]);
|
segment.reverse();
|
||||||
|
path = path.concat(segment.slice(0, -1));
|
||||||
|
|
||||||
|
navigationReverse[trackback].delete(parent[trackback]);
|
||||||
|
delete navigation[parent[trackback]][trackback];
|
||||||
|
|
||||||
delete navigation[trackback];
|
|
||||||
delete navigationReverse[trackback];
|
|
||||||
trackback = parent[trackback];
|
trackback = parent[trackback];
|
||||||
|
} while (trackback !== beginId);
|
||||||
|
|
||||||
|
// Make sure not to add loops if we’re compressing a cycle
|
||||||
|
if (endId !== beginId) {
|
||||||
|
path.push(beginId);
|
||||||
|
path.reverse();
|
||||||
|
|
||||||
|
begin[endId] = path;
|
||||||
|
navigationReverse[endId].add(beginId);
|
||||||
}
|
}
|
||||||
|
|
||||||
reversePath.push(beginId);
|
didCompress = true;
|
||||||
const forwardPath = [...reversePath];
|
|
||||||
forwardPath.reverse();
|
|
||||||
|
|
||||||
// Create edges to link both nodes directly
|
|
||||||
delete begin[forwardPath[1]];
|
|
||||||
delete navigationReverse[endId][reversePath[1]];
|
|
||||||
|
|
||||||
delete end[reversePath[1]];
|
|
||||||
delete navigationReverse[beginId][forwardPath[1]];
|
|
||||||
|
|
||||||
if (!(endId in begin)) {
|
|
||||||
begin[endId] = forwardPath;
|
|
||||||
navigationReverse[endId][beginId] = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!oneWay && !(beginId in end)) {
|
|
||||||
end[beginId] = reversePath;
|
|
||||||
navigationReverse[beginId][endId] = true;
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
// Continue the traversal down unused nodes
|
// Continue the traversal down compressible nodes
|
||||||
let isFirst = true;
|
let isFirst = true;
|
||||||
|
|
||||||
for (const succId in end) {
|
for (const succId in end) {
|
||||||
|
@ -606,6 +644,57 @@ non-junction node ${endId}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return didCompress;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Find nodes in the graph that have no exits nor entries and remove them.
|
||||||
|
* @param {Navigation} navigation Input navigation graph.
|
||||||
|
* @param {Object.<string,Set.<string>>} navigationReverse Reverse arcs.
|
||||||
|
*/
|
||||||
|
const cleanUpIsolatedNodes = (navigation, navigationReverse) => {
|
||||||
|
for (const nodeId in navigation) {
|
||||||
|
if (
|
||||||
|
Object.keys(navigation[nodeId]).length === 0
|
||||||
|
&& navigationReverse[nodeId].size === 0
|
||||||
|
) {
|
||||||
|
delete navigation[nodeId];
|
||||||
|
delete navigationReverse[nodeId];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove and relink nodes that connect only two nodes or less.
|
||||||
|
* @param {Object.<string,Stop>} stops List of stops.
|
||||||
|
* @param {Navigation} navigation Input navigation graph.
|
||||||
|
* @param {Object.<string,Set.<string>>} navigationReverse Reverse arcs.
|
||||||
|
*/
|
||||||
|
const compressNavigationGraph = (stops, navigation, navigationReverse) => {
|
||||||
|
const stopsSet = new Set(
|
||||||
|
Object.values(stops).map(stop => stop.properties.node)
|
||||||
|
);
|
||||||
|
let compressible = null;
|
||||||
|
let didCompress = true;
|
||||||
|
|
||||||
|
while (didCompress) {
|
||||||
|
let didRemove = true;
|
||||||
|
|
||||||
|
while (didRemove) {
|
||||||
|
compressible = findCompressibleNodes(
|
||||||
|
stopsSet, navigation, navigationReverse
|
||||||
|
);
|
||||||
|
didRemove = removeDeadEnds(
|
||||||
|
navigation, navigationReverse, compressible
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
didCompress = removeCompressibleNodes(
|
||||||
|
navigation, navigationReverse, compressible
|
||||||
|
);
|
||||||
|
cleanUpIsolatedNodes(navigation, navigationReverse);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
2420
src/tam/network.json
2420
src/tam/network.json
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue