mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-12-25 14:36:10 +08:00
Add detect cycle.
This commit is contained in:
parent
20bc442dcf
commit
d0504fdbb0
@ -9,6 +9,7 @@
|
||||
"no-bitwise": "off",
|
||||
"no-lonely-if": "off",
|
||||
"class-methods-use-this": "off",
|
||||
"arrow-body-style": "off"
|
||||
"arrow-body-style": "off",
|
||||
"no-loop-func": "off"
|
||||
}
|
||||
}
|
||||
|
@ -54,8 +54,39 @@ describe('breadthFirstSearch', () => {
|
||||
expect(enterVertexCallback).toHaveBeenCalledTimes(8);
|
||||
expect(leaveVertexCallback).toHaveBeenCalledTimes(8);
|
||||
|
||||
expect(enterVertexCallback.mock.calls.toString()).toBe('A,B,D,E,C,H,F,G');
|
||||
expect(leaveVertexCallback.mock.calls.toString()).toBe('A,B,D,E,C,H,F,G');
|
||||
const enterVertexParamsMap = [
|
||||
{ currentVertex: vertexA, previousVertex: null },
|
||||
{ currentVertex: vertexB, previousVertex: vertexA },
|
||||
{ currentVertex: vertexD, previousVertex: vertexB },
|
||||
{ currentVertex: vertexE, previousVertex: vertexD },
|
||||
{ currentVertex: vertexC, previousVertex: vertexE },
|
||||
{ currentVertex: vertexH, previousVertex: vertexC },
|
||||
{ currentVertex: vertexF, previousVertex: vertexH },
|
||||
{ currentVertex: vertexG, previousVertex: vertexF },
|
||||
];
|
||||
|
||||
for (let callIndex = 0; callIndex < graph.getAllVertices().length; callIndex += 1) {
|
||||
const params = enterVertexCallback.mock.calls[callIndex][0];
|
||||
expect(params.currentVertex).toEqual(enterVertexParamsMap[callIndex].currentVertex);
|
||||
expect(params.previousVertex).toEqual(enterVertexParamsMap[callIndex].previousVertex);
|
||||
}
|
||||
|
||||
const leaveVertexParamsMap = [
|
||||
{ currentVertex: vertexA, previousVertex: null },
|
||||
{ currentVertex: vertexB, previousVertex: vertexA },
|
||||
{ currentVertex: vertexD, previousVertex: vertexB },
|
||||
{ currentVertex: vertexE, previousVertex: vertexD },
|
||||
{ currentVertex: vertexC, previousVertex: vertexE },
|
||||
{ currentVertex: vertexH, previousVertex: vertexC },
|
||||
{ currentVertex: vertexF, previousVertex: vertexH },
|
||||
{ currentVertex: vertexG, previousVertex: vertexF },
|
||||
];
|
||||
|
||||
for (let callIndex = 0; callIndex < graph.getAllVertices().length; callIndex += 1) {
|
||||
const params = leaveVertexCallback.mock.calls[callIndex][0];
|
||||
expect(params.currentVertex).toEqual(leaveVertexParamsMap[callIndex].currentVertex);
|
||||
expect(params.previousVertex).toEqual(leaveVertexParamsMap[callIndex].previousVertex);
|
||||
}
|
||||
});
|
||||
|
||||
it('should allow to create custom vertex visiting logic', () => {
|
||||
@ -100,15 +131,44 @@ describe('breadthFirstSearch', () => {
|
||||
breadthFirstSearch(graph, vertexA, {
|
||||
enterVertex: enterVertexCallback,
|
||||
leaveVertex: leaveVertexCallback,
|
||||
allowTraversal: (vertex, neighbor) => {
|
||||
return !(vertex === vertexA && neighbor === vertexB);
|
||||
allowTraversal: ({ currentVertex, nextVertex }) => {
|
||||
return !(currentVertex === vertexA && nextVertex === vertexB);
|
||||
},
|
||||
});
|
||||
|
||||
expect(enterVertexCallback).toHaveBeenCalledTimes(7);
|
||||
expect(leaveVertexCallback).toHaveBeenCalledTimes(7);
|
||||
|
||||
expect(enterVertexCallback.mock.calls.toString()).toBe('A,D,E,H,F,D,H');
|
||||
expect(leaveVertexCallback.mock.calls.toString()).toBe('A,D,E,H,F,D,H');
|
||||
const enterVertexParamsMap = [
|
||||
{ currentVertex: vertexA, previousVertex: null },
|
||||
{ currentVertex: vertexD, previousVertex: vertexA },
|
||||
{ currentVertex: vertexE, previousVertex: vertexD },
|
||||
{ currentVertex: vertexH, previousVertex: vertexE },
|
||||
{ currentVertex: vertexF, previousVertex: vertexH },
|
||||
{ currentVertex: vertexD, previousVertex: vertexF },
|
||||
{ currentVertex: vertexH, previousVertex: vertexD },
|
||||
];
|
||||
|
||||
for (let callIndex = 0; callIndex < 7; callIndex += 1) {
|
||||
const params = enterVertexCallback.mock.calls[callIndex][0];
|
||||
expect(params.currentVertex).toEqual(enterVertexParamsMap[callIndex].currentVertex);
|
||||
expect(params.previousVertex).toEqual(enterVertexParamsMap[callIndex].previousVertex);
|
||||
}
|
||||
|
||||
const leaveVertexParamsMap = [
|
||||
{ currentVertex: vertexA, previousVertex: null },
|
||||
{ currentVertex: vertexD, previousVertex: vertexA },
|
||||
{ currentVertex: vertexE, previousVertex: vertexD },
|
||||
{ currentVertex: vertexH, previousVertex: vertexE },
|
||||
{ currentVertex: vertexF, previousVertex: vertexH },
|
||||
{ currentVertex: vertexD, previousVertex: vertexF },
|
||||
{ currentVertex: vertexH, previousVertex: vertexD },
|
||||
];
|
||||
|
||||
for (let callIndex = 0; callIndex < 7; callIndex += 1) {
|
||||
const params = leaveVertexCallback.mock.calls[callIndex][0];
|
||||
expect(params.currentVertex).toEqual(leaveVertexParamsMap[callIndex].currentVertex);
|
||||
expect(params.previousVertex).toEqual(leaveVertexParamsMap[callIndex].previousVertex);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
@ -2,11 +2,14 @@ import Queue from '../../../data-structures/queue/Queue';
|
||||
|
||||
/**
|
||||
* @typedef {Object} Callbacks
|
||||
* @property {function(vertex: GraphVertex, neighbor: GraphVertex): boolean} allowTraversal -
|
||||
*
|
||||
* @property {function(vertices: Object): boolean} [allowTraversal] -
|
||||
* Determines whether DFS should traverse from the vertex to its neighbor
|
||||
* (along the edge). By default prohibits visiting the same vertex again.
|
||||
* @property {function(vertex: GraphVertex)} enterVertex - Called when DFS enters the vertex.
|
||||
* @property {function(vertex: GraphVertex)} leaveVertex - Called when DFS leaves the vertex.
|
||||
*
|
||||
* @property {function(vertices: Object)} [enterVertex] - Called when BFS enters the vertex.
|
||||
*
|
||||
* @property {function(vertices: Object)} [leaveVertex] - Called when BFS leaves the vertex.
|
||||
*/
|
||||
|
||||
/**
|
||||
@ -21,9 +24,9 @@ function initCallbacks(callbacks = {}) {
|
||||
const allowTraversalCallback = (
|
||||
() => {
|
||||
const seen = {};
|
||||
return (vertex, neighbor) => {
|
||||
if (!seen[neighbor.getKey()]) {
|
||||
seen[neighbor.getKey()] = true;
|
||||
return ({ nextVertex }) => {
|
||||
if (!seen[nextVertex.getKey()]) {
|
||||
seen[nextVertex.getKey()] = true;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
@ -50,18 +53,23 @@ export default function breadthFirstSearch(graph, startVertex, originalCallbacks
|
||||
// Do initial queue setup.
|
||||
vertexQueue.enqueue(startVertex);
|
||||
|
||||
let previousVertex = null;
|
||||
|
||||
// Traverse all vertices from the queue.
|
||||
while (!vertexQueue.isEmpty()) {
|
||||
const currentVertex = vertexQueue.dequeue();
|
||||
callbacks.enterVertex(currentVertex);
|
||||
callbacks.enterVertex({ currentVertex, previousVertex });
|
||||
|
||||
// Add all neighbors to the queue for future traversals.
|
||||
graph.getNeighbors(currentVertex).forEach((neighbor) => {
|
||||
if (callbacks.allowTraversal(currentVertex, neighbor)) {
|
||||
vertexQueue.enqueue(neighbor);
|
||||
graph.getNeighbors(currentVertex).forEach((nextVertex) => {
|
||||
if (callbacks.allowTraversal({ previousVertex, currentVertex, nextVertex })) {
|
||||
vertexQueue.enqueue(nextVertex);
|
||||
}
|
||||
});
|
||||
|
||||
callbacks.leaveVertex(currentVertex);
|
||||
callbacks.leaveVertex({ currentVertex, previousVertex });
|
||||
|
||||
// Memorize current vertex before next loop.
|
||||
previousVertex = currentVertex;
|
||||
}
|
||||
}
|
||||
|
@ -67,7 +67,7 @@ describe('depthFirstSearch', () => {
|
||||
expect(params.previousVertex).toEqual(enterVertexParamsMap[callIndex].previousVertex);
|
||||
}
|
||||
|
||||
const leavingVertexParamsMap = [
|
||||
const leaveVertexParamsMap = [
|
||||
{ currentVertex: vertexG, previousVertex: vertexC },
|
||||
{ currentVertex: vertexC, previousVertex: vertexB },
|
||||
{ currentVertex: vertexB, previousVertex: vertexA },
|
||||
@ -79,8 +79,8 @@ describe('depthFirstSearch', () => {
|
||||
|
||||
for (let callIndex = 0; callIndex < graph.getAllVertices().length; callIndex += 1) {
|
||||
const params = leaveVertexCallback.mock.calls[callIndex][0];
|
||||
expect(params.currentVertex).toEqual(leavingVertexParamsMap[callIndex].currentVertex);
|
||||
expect(params.previousVertex).toEqual(leavingVertexParamsMap[callIndex].previousVertex);
|
||||
expect(params.currentVertex).toEqual(leaveVertexParamsMap[callIndex].currentVertex);
|
||||
expect(params.previousVertex).toEqual(leaveVertexParamsMap[callIndex].previousVertex);
|
||||
}
|
||||
});
|
||||
|
||||
@ -146,7 +146,7 @@ describe('depthFirstSearch', () => {
|
||||
expect(params.previousVertex).toEqual(enterVertexParamsMap[callIndex].previousVertex);
|
||||
}
|
||||
|
||||
const leavingVertexParamsMap = [
|
||||
const leaveVertexParamsMap = [
|
||||
{ currentVertex: vertexG, previousVertex: vertexD },
|
||||
{ currentVertex: vertexD, previousVertex: vertexA },
|
||||
{ currentVertex: vertexG, previousVertex: vertexD },
|
||||
@ -158,8 +158,8 @@ describe('depthFirstSearch', () => {
|
||||
|
||||
for (let callIndex = 0; callIndex < graph.getAllVertices().length; callIndex += 1) {
|
||||
const params = leaveVertexCallback.mock.calls[callIndex][0];
|
||||
expect(params.currentVertex).toEqual(leavingVertexParamsMap[callIndex].currentVertex);
|
||||
expect(params.previousVertex).toEqual(leavingVertexParamsMap[callIndex].previousVertex);
|
||||
expect(params.currentVertex).toEqual(leaveVertexParamsMap[callIndex].currentVertex);
|
||||
expect(params.previousVertex).toEqual(leaveVertexParamsMap[callIndex].previousVertex);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user