Skip to content

Commit

Permalink
feat(adjacency): add bitmatrix edge counting, add/fix toDot() impls, …
Browse files Browse the repository at this point in the history
…add tests
  • Loading branch information
postspectacular committed Feb 17, 2019
1 parent f227107 commit dae97ff
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 16 deletions.
24 changes: 19 additions & 5 deletions packages/adjacency/src/binary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ export class AdjacencyBitMatrix implements
}

mat: BitMatrix;
undirected: boolean;
protected undirected: boolean;
protected numE: number;

constructor(n: number, undirected = false) {
this.mat = new BitMatrix(n);
this.undirected = undirected;
this.numE = 0;
}

*edges() {
Expand All @@ -44,11 +46,11 @@ export class AdjacencyBitMatrix implements
}

numEdges(): number {
throw new Error("Method not implemented.");
return this.numE;
}

numVertices(): number {
throw new Error("Method not implemented.");
return this.mat.n;
}

/**
Expand All @@ -62,13 +64,13 @@ export class AdjacencyBitMatrix implements
}

addEdge(from: number, to: number) {
this.mat.setAt(to, from, true);
!this.mat.setAt(to, from, true) && this.numE++;
this.undirected && this.mat.setAt(from, to, true);
return this;
}

removeEdge(from: number, to: number) {
this.mat.setAt(to, from, false);
this.mat.setAt(to, from, false) && this.numE--;
this.undirected && this.mat.setAt(from, to, false);
return this;
}
Expand Down Expand Up @@ -107,4 +109,16 @@ export class AdjacencyBitMatrix implements
toString() {
return this.mat.toString();
}

toDot() {
const [type, sep] = this.undirected ?
["graph", "--"] :
["digraph", "->"];
const res = [`${type} g {`];
for (let e of this.edges()) {
res.push(`"${e[0]}"${sep}"${e[1]}";`);
}
res.push(`}`);
return res.join("\n");
}
}
2 changes: 1 addition & 1 deletion packages/adjacency/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from "./api";
export * from "./adjacency";
export * from "./binary";
export * from "./sparse";
6 changes: 2 additions & 4 deletions packages/adjacency/src/sparse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,8 @@ export class AdjacencyMatrix extends CSR implements
["graph", "--"] :
["digraph", "->"];
const res = [`${type} g {`];
for (let i = 0; i < this.m; i++) {
for (let j of this.nzRowCols(i)) {
res.push(`"${j}"${sep}"${i}";`);
}
for (let e of this.edges()) {
res.push(`"${e[0]}"${sep}"${e[1]}";`);
}
res.push(`}`);
return res.join("\n");
Expand Down
35 changes: 35 additions & 0 deletions packages/adjacency/test/binary.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { Pair } from "@thi.ng/api";
import { AdjacencyBitMatrix } from "../src/index";
import * as assert from "assert";

const edges: Pair<number, number>[] = [
[2, 3],
[0, 1],
[5, 4],
[2, 0],
];

describe("adjacency (bitmatrix)", () => {
it("fromEdges, undirected", () => {
const m = AdjacencyBitMatrix.fromEdges(6, edges, true);
console.log([...m.edges()]);
assert.deepEqual(
m.mat.data.slice(0, 6),
[
1610612736,
2147483648,
2415919104,
536870912,
67108864,
134217728
],
"data"
);
assert.equal(m.numEdges(), 4, "numEdges");
assert.deepEqual(
[...m.edges()],
[[4, 5], [2, 3], [0, 1], [0, 2]],
"edges"
);
});
});
6 changes: 0 additions & 6 deletions packages/adjacency/test/index.ts

This file was deleted.

32 changes: 32 additions & 0 deletions packages/adjacency/test/sparse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Pair } from "@thi.ng/api";
import { AdjacencyMatrix } from "../src/index";
import * as assert from "assert";

const edges: Pair<number, number>[] = [
[2, 3],
[0, 1],
[5, 4],
[2, 0],
];

describe("adjacency (sparse)", () => {
it("fromEdges, undirected", () => {
const m = AdjacencyMatrix.fromEdges(6, edges, true);
assert.deepEqual(
m.rows,
[0, 2, 3, 5, 6, 7, 8],
"rows"
);
assert.deepEqual(
m.cols,
[1, 2, 0, 0, 3, 2, 5, 4],
"cols"
);
assert.equal(m.numEdges(), 4, "numEdges");
assert.deepEqual(
[...m.edges()],
[[0, 1], [0, 2], [2, 3], [4, 5]],
"edges"
);
});
});

0 comments on commit dae97ff

Please sign in to comment.