Skip to content

Commit

Permalink
test(graph): add tests for cycle detection
Browse files Browse the repository at this point in the history
  • Loading branch information
HugoMendes98 committed Aug 3, 2023
1 parent 1577641 commit 590204d
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 1 deletion.
87 changes: 87 additions & 0 deletions libs/common/src/app/graph/algorithms/graph.has-cycle.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { graphHasCycle } from "./graph.has-cycle";
import { AdjacencyListTransformationParams, getAdjacencyList } from "../transformations";

describe("graphHasCycle", () => {
it("should not have a cycle", () => {
const graphs: AdjacencyListTransformationParams[] = [
// "Empty" graphs
{ arcs: [], nodes: [] },
{
arcs: [],
nodes: [
{ inputs: [], outputs: [] },
{ inputs: [], outputs: [] }
]
},
{
arcs: [],
nodes: [
{ inputs: [{ _id: 1 }], outputs: [{ _id: 2 }] },
{ inputs: [{ _id: 3 }], outputs: [{ _id: 4 }] }
]
},

{
arcs: [{ __from: 1, __to: 2 }],
nodes: [
{ inputs: [], outputs: [{ _id: 1 }] },
{ inputs: [{ _id: 2 }], outputs: [] }
]
},
{
arcs: [
{ __from: 11, __to: 20 },
{ __from: 11, __to: 21 },
{ __from: 11, __to: 30 },
{ __from: 22, __to: 31 }
],
nodes: [
{ inputs: [{ _id: 10 }], outputs: [{ _id: 11 }] },
{ inputs: [{ _id: 20 }, { _id: 21 }], outputs: [{ _id: 22 }] },
{ inputs: [{ _id: 30 }, { _id: 31 }], outputs: [] }
]
}
];

for (const graph of graphs) {
expect(graphHasCycle(getAdjacencyList(graph))).toBeFalse();
}
});

it("should have a cycle", () => {
const graphs: AdjacencyListTransformationParams[] = [
{
arcs: [{ __from: 2, __to: 1 }],
nodes: [{ inputs: [{ _id: 1 }], outputs: [{ _id: 2 }] }]
},
{
arcs: [
{ __from: 11, __to: 20 },
{ __from: 21, __to: 10 }
],
nodes: [
{ inputs: [{ _id: 10 }], outputs: [{ _id: 11 }] },
{ inputs: [{ _id: 20 }], outputs: [{ _id: 21 }] }
]
},
{
arcs: [
{ __from: 11, __to: 20 },
{ __from: 11, __to: 30 },
{ __from: 22, __to: 30 },
{ __from: 22, __to: 31 },
{ __from: 32, __to: 10 }
],
nodes: [
{ inputs: [{ _id: 10 }], outputs: [{ _id: 11 }] },
{ inputs: [{ _id: 20 }], outputs: [{ _id: 22 }] },
{ inputs: [{ _id: 30 }, { _id: 31 }], outputs: [{ _id: 32 }] }
]
}
];

for (const graph of graphs) {
expect(graphHasCycle(getAdjacencyList(graph))).toBeTrue();
}
});
});
13 changes: 13 additions & 0 deletions libs/common/src/app/graph/algorithms/graph.has-cycle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { AdjacencyList } from "../transformations";

/**
* Detects if the given graph has a cycle.
*
* The cycle is determined by the explored nodes.
*
* @param graph the graph by its adjacency list
* @returns true if the graphs contains at least one cycle
*/
export function graphHasCycle(graph: AdjacencyList): boolean {
throw new Error("TODO");
}
1 change: 1 addition & 0 deletions libs/common/src/app/graph/algorithms/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./graph.has-cycle";
12 changes: 11 additions & 1 deletion libs/common/src/app/graph/transformations/get-adjacency.list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,16 @@ export interface AdjacencyListItem<Arc extends AdjacencyListArc, Node extends Ad
node: Node;
}

/**
* The final adjacency list.
*
* On a Map to access more simply the nodes.
*/
export type AdjacencyList<
Arc extends AdjacencyListArc = AdjacencyListArc,
Node extends AdjacencyListNode = AdjacencyListNode
> = ReadonlyMap<Node, AdjacencyListItem<Arc, Node>>;

/**
* Gets the adjacency list from the arcs and the nodes of a graph
*
Expand All @@ -83,7 +93,7 @@ export interface AdjacencyListItem<Arc extends AdjacencyListArc, Node extends Ad
*/
export function getAdjacencyList<Arc extends AdjacencyListArc, Node extends AdjacencyListNode>(
params: AdjacencyListTransformationParams<Arc, Node>
): ReadonlyMap<Node, AdjacencyListItem<Arc, Node>> {
): AdjacencyList<Arc, Node> {
const { arcs, nodes } = params;

// TODO: If the data becomes too big, transform to a non-functional approach
Expand Down

0 comments on commit 590204d

Please sign in to comment.