min-cycles
Version:
Minimal cycle basis for a planar graph
34 lines (23 loc) • 3.5 kB
JavaScript
import expect from 'expect.js';
import {a2v, addEdges} from './util';
import {extract_cycles as ec} from '../src/cycles';
suite('real-life data');
test('graph causing a stack overflow', () => {
let vertices = [[528,22],[438,22],[793,476],[932,476],[793,314],[438,314],[438,260],[239,321],[168,321],[300,260],[-430,46],[-430,426],[-734,46],[898,260],[1296,260],[1307,449],[1541,449],[1541,997],[1067,996],[898,46],[1068,748],[932,748],[793,46],[793,-71],[793,748],[793,949],[793,1174],[239,1174],[239,748],[239,949],[168,748],[-734,748],[-734,426],[-1067,426],[-1067,-220],[-389,-239],[-260,46],[-126,46],[88,-252],[-376,-379],[528,-71],[528,-132],[585,-132],[1024,-171],[718,-265],[-157,-1001],[1002,-105],[1504,-497],[1474,-114],[1471,-71],[1327,-6]].map(a2v),
edges = [{v:[[528,22],[438,22]],t:0},{v:[[793,476],[932,476]],t:1},{v:[[793,314],[793,476]],t:2},{v:[[438,314],[793,314]],t:3},{v:[[438,260],[438,22]],t:4},{v:[[438,314],[438,260]],t:5},{v:[[239,321],[168,321]],t:6},{v:[[438,260],[300,260]],t:7},{v:[[239,321],[300,260]],t:8},{v:[[-430,46],[-430,426]],t:9},{v:[[-430,46],[-734,46]],t:10},{v:[[898,260],[1296,260]],t:11},{v:[[1296,260],[1307,449]],t:12},{v:[[1307,449],[1541,449]],t:13},{v:[[1541,449],[1541,997]],t:14},{v:[[1541,997],[1067,996]],t:15},{v:[[898,46],[898,260]],t:16},{v:[[1067,996],[1068,748]],t:17},{v:[[932,748],[932,476]],t:18},{v:[[1068,748],[932,748]],t:19},{v:[[793,46],[898,46]],t:20},{v:[[793,46],[793,314]],t:21},{v:[[793,46],[793,-71]],t:22},{v:[[793,748],[932,748]],t:23},{v:[[793,748],[793,949]],t:24},{v:[[793,1174],[793,949]],t:25},{v:[[239,1174],[793,1174]],t:26},{v:[[239,748],[239,949]],t:27},{v:[[793,949],[239,949]],t:28},{v:[[239,949],[239,1174]],t:29},{v:[[168,321],[168,748]],t:30},{v:[[239,748],[168,748]],t:31},{v:[[168,748],[-734,748]],t:32},{v:[[-734,426],[-734,46]],t:33},{v:[[-430,426],[-734,426]],t:34},{v:[[-734,748],[-734,426]],t:35},{v:[[-734,426],[-1067,426]],t:36},{v:[[-1067,426],[-1067,-220]],t:37},{v:[[-1067,-220],[-389,-239]],t:38},{v:[[-389,-239],[-260,46]],t:39},{v:[[-430,46],[-260,46]],t:40},{v:[[-126,46],[-260,46]],t:41},{v:[[-126,46],[88,-252]],t:42},{v:[[88,-252],[-376,-379]],t:43},{v:[[793,-71],[528,-71]],t:44},{v:[[528,-71],[528,22]],t:45},{v:[[528,-71],[528,-132]],t:46},{v:[[528,-132],[585,-132]],t:47},{v:[[1024,-171],[718,-265]],t:48},{v:[[585,-132],[718,-265]],t:49},{v:[[-376,-379],[-157,-1001]],t:50},{v:[[1024,-171],[1002,-105]],t:51},{v:[[-157,-1001],[1504,-497]],t:52},{v:[[1474,-114],[1504,-497]],t:53},{v:[[1471,-71],[1474,-114]],t:54},{v:[[1002,-105],[1327,-6]],t:55}],
getv = (pt) => vertices.find(v => v.x == pt[0] && v.y == pt[1]),
ref_edges = edges.map( ({v}) => [getv(v[0]), getv(v[1])] );
addEdges(ref_edges);
expect(ec).withArgs(vertices).to.not.throwException();
});
test('the case with two connected triangles', () => {
let walls = [ [[384,559],[384,340]], [[282,228],[52,169]], [[282,12],[282,228]], [[282,12],[52,169]],
[[52,169],[127,523]], [[384,340],[127,523]], [[127,523],[384,559]] ],
findVertex = (vs, p) => vs.find(v => v.x == p[0] && v.y == p[1]),
addVertex = (vs, p) => findVertex(vs, p) ? vs : vs.concat(a2v(p)),
addVertices = (vs, [a,b]) => addVertex(addVertex(vs, a), b),
vertices = walls.reduce(addVertices, []),
edges = walls.map(([a,b]) => [findVertex(vertices, a), findVertex(vertices, b)]);
addEdges(edges);
expect(ec).withArgs(vertices).to.not.throwException();
});