mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-12-26 23:21:18 +08:00
Add DFS.
This commit is contained in:
parent
47ac5fcd70
commit
97f8bd3a9f
@ -48,11 +48,40 @@ describe('depthFirstSearch', () => {
|
|||||||
leaveVertex: leaveVertexCallback,
|
leaveVertex: leaveVertexCallback,
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(enterVertexCallback).toHaveBeenCalledTimes(7);
|
expect(enterVertexCallback).toHaveBeenCalledTimes(graph.getAllVertices().length);
|
||||||
expect(leaveVertexCallback).toHaveBeenCalledTimes(7);
|
expect(leaveVertexCallback).toHaveBeenCalledTimes(graph.getAllVertices().length);
|
||||||
|
|
||||||
expect(enterVertexCallback.mock.calls.toString()).toBe('A,B,C,G,D,E,F');
|
const enterVertexParamsMap = [
|
||||||
expect(leaveVertexCallback.mock.calls.toString()).toBe('G,C,B,D,F,E,A');
|
{ currentVertex: vertexA, previousVertex: null },
|
||||||
|
{ currentVertex: vertexB, previousVertex: vertexA },
|
||||||
|
{ currentVertex: vertexC, previousVertex: vertexB },
|
||||||
|
{ currentVertex: vertexG, previousVertex: vertexC },
|
||||||
|
{ currentVertex: vertexD, previousVertex: vertexA },
|
||||||
|
{ currentVertex: vertexE, previousVertex: vertexA },
|
||||||
|
{ currentVertex: vertexF, previousVertex: vertexE },
|
||||||
|
];
|
||||||
|
|
||||||
|
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 leavingVertexParamsMap = [
|
||||||
|
{ currentVertex: vertexG, previousVertex: vertexC },
|
||||||
|
{ currentVertex: vertexC, previousVertex: vertexB },
|
||||||
|
{ currentVertex: vertexB, previousVertex: vertexA },
|
||||||
|
{ currentVertex: vertexD, previousVertex: vertexA },
|
||||||
|
{ currentVertex: vertexF, previousVertex: vertexE },
|
||||||
|
{ currentVertex: vertexE, previousVertex: vertexA },
|
||||||
|
{ currentVertex: vertexA, previousVertex: null },
|
||||||
|
];
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
it('allow users to redefine vertex visiting logic', () => {
|
it('allow users to redefine vertex visiting logic', () => {
|
||||||
@ -93,15 +122,44 @@ describe('depthFirstSearch', () => {
|
|||||||
depthFirstSearch(graph, vertexA, {
|
depthFirstSearch(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,G,E,F,D,G');
|
const enterVertexParamsMap = [
|
||||||
expect(leaveVertexCallback.mock.calls.toString()).toBe('G,D,G,D,F,E,A');
|
{ currentVertex: vertexA, previousVertex: null },
|
||||||
|
{ currentVertex: vertexD, previousVertex: vertexA },
|
||||||
|
{ currentVertex: vertexG, previousVertex: vertexD },
|
||||||
|
{ currentVertex: vertexE, previousVertex: vertexA },
|
||||||
|
{ currentVertex: vertexF, previousVertex: vertexE },
|
||||||
|
{ currentVertex: vertexD, previousVertex: vertexF },
|
||||||
|
{ currentVertex: vertexG, previousVertex: vertexD },
|
||||||
|
];
|
||||||
|
|
||||||
|
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 leavingVertexParamsMap = [
|
||||||
|
{ currentVertex: vertexG, previousVertex: vertexD },
|
||||||
|
{ currentVertex: vertexD, previousVertex: vertexA },
|
||||||
|
{ currentVertex: vertexG, previousVertex: vertexD },
|
||||||
|
{ currentVertex: vertexD, previousVertex: vertexF },
|
||||||
|
{ currentVertex: vertexF, previousVertex: vertexE },
|
||||||
|
{ currentVertex: vertexE, previousVertex: vertexA },
|
||||||
|
{ currentVertex: vertexA, previousVertex: null },
|
||||||
|
];
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -1,10 +1,13 @@
|
|||||||
/**
|
/**
|
||||||
* @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 DFS enters the vertex.
|
||||||
|
*
|
||||||
|
* @property {function(vertices: Object)} [leaveVertex] - Called when DFS leaves the vertex.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -19,9 +22,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;
|
||||||
@ -38,19 +41,20 @@ function initCallbacks(callbacks = {}) {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {Graph} graph
|
* @param {Graph} graph
|
||||||
* @param {GraphVertex} vertex
|
* @param {GraphVertex} currentVertex
|
||||||
|
* @param {GraphVertex} previousVertex
|
||||||
* @param {Callbacks} callbacks
|
* @param {Callbacks} callbacks
|
||||||
*/
|
*/
|
||||||
function depthFirstSearchRecursive(graph, vertex, callbacks) {
|
function depthFirstSearchRecursive(graph, currentVertex, previousVertex, callbacks) {
|
||||||
callbacks.enterVertex(vertex);
|
callbacks.enterVertex({ currentVertex, previousVertex });
|
||||||
|
|
||||||
graph.getNeighbors(vertex).forEach((neighbor) => {
|
graph.getNeighbors(currentVertex).forEach((nextVertex) => {
|
||||||
if (callbacks.allowTraversal(vertex, neighbor)) {
|
if (callbacks.allowTraversal({ previousVertex, currentVertex, nextVertex })) {
|
||||||
depthFirstSearchRecursive(graph, neighbor, callbacks);
|
depthFirstSearchRecursive(graph, nextVertex, currentVertex, callbacks);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
callbacks.leaveVertex(vertex);
|
callbacks.leaveVertex({ currentVertex, previousVertex });
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -59,5 +63,6 @@ function depthFirstSearchRecursive(graph, vertex, callbacks) {
|
|||||||
* @param {Callbacks} [callbacks]
|
* @param {Callbacks} [callbacks]
|
||||||
*/
|
*/
|
||||||
export default function depthFirstSearch(graph, startVertex, callbacks) {
|
export default function depthFirstSearch(graph, startVertex, callbacks) {
|
||||||
depthFirstSearchRecursive(graph, startVertex, initCallbacks(callbacks));
|
const previousVertex = null;
|
||||||
|
depthFirstSearchRecursive(graph, startVertex, previousVertex, initCallbacks(callbacks));
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user