Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 114 additions & 0 deletions __tests__/unit/kCore.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import { Edge, Graph, Node } from "@antv/graphlib";
import { kCore } from "../../packages/graph/src";

const graph = new Graph<any, any>({
nodes: [
{
id: 'A',
data: {}
},
{
id: 'B',
data: {}
},
{
id: 'C',
data: {}
},
{
id: 'D',
data: {}
},
{
id: 'E',
data: {}
},
{
id: 'F',
data: {}
},
{
id: 'G',
data: {}
},
{
id: 'H',
data: {}
},
],
edges: [
{
id: 'e1',
source: 'A',
target: 'B',
data: {}
},
{
id: 'e2',
source: 'B',
target: 'C',
data: {}
},
{
id: 'e3',
source: 'C',
target: 'G',
data: {}
},
{
id: 'e4',
source: 'A',
target: 'D',
data: {}
},
{
id: 'e5',
source: 'A',
target: 'E',
data: {}
},
{
id: 'e6',
source: 'E',
target: 'F',
data: {}
},
{
id: 'e7',
source: 'F',
target: 'D',
data: {}
},
{
id: 'e8',
source: 'D',
target: 'E',
data: {}
},
],
});

const validateEdge = (edge: Edge<any>, nodes: Node<any>[]) => {
return nodes.findIndex(n => edge.source === n.id) >= 0 && nodes.findIndex(n => edge.target === n.id) >= 0;
}
describe('k-core algorithm unit test', () => {
const nodes = graph.getAllNodes();
const edges = graph.getAllEdges();
it('k=1', () => {
const { nodes: kNodes, edges: kEdges } = kCore(graph, 1);
expect(kNodes.length).toBe(nodes.filter(n => graph.getDegree(n.id) >= 1).length);
expect(kEdges).toStrictEqual(edges.filter(e => validateEdge(e, nodes)));
});

it('k=2', () => {
const { nodes: kNodes, edges: kEdges } = kCore(graph, 2);
expect(kNodes.length).toBe(nodes.filter(n => graph.getDegree(n.id) >= 2).length);
expect(kEdges).toStrictEqual(edges.filter(e => validateEdge(e, nodes)));
});

it('k=3', () => {
const { nodes: kNodes, edges: kEdges } = kCore(graph, 3);
expect(kNodes.length).toBe(nodes.filter(n => graph.getDegree(n.id) >= 3).length);
expect(kEdges).toStrictEqual(edges.filter(e => validateEdge(e, nodes)));
});
});
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"build:ci": "pnpm -r run build:ci",
"prepare": "husky install",
"test": "jest",
"test_one": "jest ./__tests__/unit/kCore.spec.ts",
"coverage": "jest --coverage",
"build:site": "vite build",
"deploy": "gh-pages -d site/dist",
Expand Down Expand Up @@ -73,4 +74,4 @@
"access": "public",
"registry": "https://registry.npmjs.org"
}
}
}
1 change: 1 addition & 0 deletions packages/graph/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from "./pageRank";
export * from "./findPath";
export * from "./louvain";
export * from "./iLouvain";
export * from "./k-core";
23 changes: 23 additions & 0 deletions packages/graph/src/k-core.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Graph } from "./types";

/**
Finds the k-core of a given graph.
@param graph - The input graph.
@param k - The minimum degree required for a node to be considered part of the k-core. Default is 1.
@returns An object containing the nodes and edges of the k-core.
*/
export function kCore(
graph: Graph,
k: number = 1,) {
const nodes = graph.getAllNodes();
let edges = graph.getAllEdges();
nodes.sort((a, b) => graph.getDegree(a.id, 'both') - graph.getDegree(b.id, 'both'));
let i = 0;
while (true) {
const curNode = nodes[i];
if (graph.getDegree(curNode.id, 'both') >= k) break;
nodes.splice(i, 1);//remove node
edges = edges.filter(e => !(e.source === i || e.target === i));
}
return { nodes, edges };
}