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