trassel
Version:
Graph computing in JavaScript
145 lines (142 loc) • 5.88 kB
JavaScript
import louvain from "./louvain"
describe("Community detection", () => {
const baseNodes = JSON.stringify([
{ id: "n0" },
{ id: "n1" },
{ id: "n2" },
{ id: "n3" },
{ id: "n4" },
{ id: "n5" },
{ id: "n6" },
{ id: "n7" },
{ id: "n8" },
{ id: "n9" },
{ id: "n10" },
{ id: "n11" },
{ id: "n12" },
{ id: "n13" },
{ id: "n14" },
{ id: "n15" },
{ id: "n16" },
{ id: "n17" },
{ id: "n18" },
{ id: "n19" },
{ id: "n20" },
{ id: "n21" },
{ id: "n22" },
{ id: "n23" },
{ id: "n24" },
{ id: "n25" },
{ id: "n26" },
{ id: "n27" },
{ id: "n28" },
{ id: "n29" },
{ id: "n30" },
{ id: "n31" },
{ id: "n32" },
{ id: "n33" },
{ id: "n34" },
{ id: "n35" },
{ id: "n36" },
{ id: "n37" },
{ id: "n38" },
{ id: "n39" },
{ id: "n40" },
{ id: "n41" },
{ id: "n42" },
{ id: "n43" },
{ id: "n44" },
{ id: "n45" },
{ id: "n46" },
{ id: "n47" }
])
const baseEdges = JSON.stringify([
{ id: "L0", sourceNode: "n0", targetNode: "n1", weight: 1 },
{ id: "L1", sourceNode: "n2", targetNode: "n3", weight: 1 },
{ id: "L2", sourceNode: "n2", targetNode: "n4", weight: 1 },
{ id: "L3", sourceNode: "n2", targetNode: "n5", weight: 1 },
{ id: "L4", sourceNode: "n2", targetNode: "n4", weight: 1 },
{ id: "L5", sourceNode: "n2", targetNode: "n6", weight: 1 },
{ id: "L6", sourceNode: "n2", targetNode: "n7", weight: 1 },
{ id: "L7", sourceNode: "n2", targetNode: "n8", weight: 1 },
{ id: "L8", sourceNode: "n2", targetNode: "n9", weight: 1 },
{ id: "L9", sourceNode: "n2", targetNode: "n10", weight: 1 },
{ id: "L10", sourceNode: "n10", targetNode: "n10", weight: 1 },
{ id: "L11", sourceNode: "n17", targetNode: "n11", weight: 1 },
{ id: "L12", sourceNode: "n2", targetNode: "n12", weight: 1 },
{ id: "L13", sourceNode: "n2", targetNode: "n12", weight: 1 },
{ id: "L14", sourceNode: "n2", targetNode: "n13", weight: 1 },
{ id: "L15", sourceNode: "n13", targetNode: "n13", weight: 1 },
{ id: "L16", sourceNode: "n13", targetNode: "n14", weight: 1 },
{ id: "L17", sourceNode: "n2", targetNode: "n15", weight: 1 },
{ id: "L18", sourceNode: "n2", targetNode: "n15", weight: 1 },
{ id: "L19", sourceNode: "n2", targetNode: "n15", weight: 1 },
{ id: "L20", sourceNode: "n2", targetNode: "n15", weight: 1 },
{ id: "L21", sourceNode: "n13", targetNode: "n15", weight: 1 },
{ id: "L22", sourceNode: "n16", targetNode: "n15", weight: 1 },
{ id: "L23", sourceNode: "n2", targetNode: "n2", weight: 1 },
{ id: "L24", sourceNode: "n2", targetNode: "n17", weight: 1 },
{ id: "L25", sourceNode: "n17", targetNode: "n15", weight: 1 },
{ id: "L26", sourceNode: "n17", targetNode: "n15", weight: 1 },
{ id: "L27", sourceNode: "n17", targetNode: "n15", weight: 1 },
{ id: "L28", sourceNode: "n17", targetNode: "n15", weight: 1 },
{ id: "L29", sourceNode: "n18", targetNode: "n15", weight: 1 },
{ id: "L30", sourceNode: "n18", targetNode: "n15", weight: 1 },
{ id: "L31", sourceNode: "n18", targetNode: "n15", weight: 1 },
{ id: "L32", sourceNode: "n15", targetNode: "n42", weight: 1 },
{ id: "L33", sourceNode: "n19", targetNode: "n15", weight: 1 },
{ id: "L34", sourceNode: "n20", targetNode: "n19", weight: 1 },
{ id: "L35", sourceNode: "n21", targetNode: "n19", weight: 1 },
{ id: "L36", sourceNode: "n22", targetNode: "n19", weight: 1 },
{ id: "L37", sourceNode: "n19", targetNode: "n32", weight: 1 },
{ id: "L38", sourceNode: "n30", targetNode: "n17", weight: 1 },
{ id: "L39", sourceNode: "n30", targetNode: "n17", weight: 1 },
{ id: "L40", sourceNode: "n17", targetNode: "n23", weight: 1 },
{ id: "L41", sourceNode: "n17", targetNode: "n24", weight: 1 },
{ id: "L42", sourceNode: "n17", targetNode: "n25", weight: 1 },
{ id: "L43", sourceNode: "n17", targetNode: "n26", weight: 1 },
{ id: "L44", sourceNode: "n17", targetNode: "n27", weight: 1 },
{ id: "L45", sourceNode: "n17", targetNode: "n28", weight: 1 },
{ id: "L46", sourceNode: "n17", targetNode: "n43", weight: 1 },
{ id: "L47", sourceNode: "n17", targetNode: "n44", weight: 1 },
{ id: "L48", sourceNode: "n17", targetNode: "n45", weight: 1 },
{ id: "L49", sourceNode: "n17", targetNode: "n29", weight: 1 },
{ id: "L50", sourceNode: "n17", targetNode: "n29", weight: 1 },
{ id: "L51", sourceNode: "n17", targetNode: "n29", weight: 1 },
{ id: "L52", sourceNode: "n33", targetNode: "n17", weight: 1 },
{ id: "L53", sourceNode: "n17", targetNode: "n19", weight: 1 },
{ id: "L54", sourceNode: "n35", targetNode: "n35", weight: 1 },
{ id: "L55", sourceNode: "n35", targetNode: "n35", weight: 1 },
{ id: "L56", sourceNode: "n35", targetNode: "n35", weight: 1 },
{ id: "L57", sourceNode: "n35", targetNode: "n35", weight: 1 },
{ id: "L58", sourceNode: "n35", targetNode: "n36", weight: 1 },
{ id: "L59", sourceNode: "n35", targetNode: "n37", weight: 1 },
{ id: "L60", sourceNode: "n35", targetNode: "n38", weight: 1 },
{ id: "L61", sourceNode: "n17", targetNode: "n41", weight: 1 },
{ id: "L62", sourceNode: "n35", targetNode: "n39", weight: 1 },
{ id: "L63", sourceNode: "n35", targetNode: "n40", weight: 1 },
{ id: "L64", sourceNode: "n17", targetNode: "n19", weight: 1 }
])
let nodes
let edges
beforeEach(() => {
nodes = JSON.parse(baseNodes)
edges = JSON.parse(baseEdges)
})
it("Louvain community detection", () => {
const correctAnswer = [
["n0", "n1"],
["n2", "n3", "n4", "n5", "n6", "n7", "n8", "n9", "n10", "n12"],
["n11", "n17", "n23", "n24", "n25", "n26", "n27", "n28", "n29", "n30", "n33", "n41", "n43", "n44", "n45"],
["n13", "n14"],
["n15", "n16", "n18", "n42"],
["n19", "n20", "n21", "n22", "n32"],
["n31"],
["n34"],
["n35", "n36", "n37", "n38", "n39", "n40"],
["n46"],
["n47"]
]
expect(JSON.stringify(louvain(nodes, edges).communities)).toEqual(JSON.stringify(correctAnswer))
})
})