This commit is contained in:
Oleksii Trekhleb 2018-05-04 16:56:06 +03:00
parent 47ac5fcd70
commit 97f8bd3a9f
2 changed files with 87 additions and 24 deletions

View File

@ -48,11 +48,40 @@ describe('depthFirstSearch', () => {
leaveVertex: leaveVertexCallback,
});
expect(enterVertexCallback).toHaveBeenCalledTimes(7);
expect(leaveVertexCallback).toHaveBeenCalledTimes(7);
expect(enterVertexCallback).toHaveBeenCalledTimes(graph.getAllVertices().length);
expect(leaveVertexCallback).toHaveBeenCalledTimes(graph.getAllVertices().length);
expect(enterVertexCallback.mock.calls.toString()).toBe('A,B,C,G,D,E,F');
expect(leaveVertexCallback.mock.calls.toString()).toBe('G,C,B,D,F,E,A');
const enterVertexParamsMap = [
{ 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', () => {
@ -93,15 +122,44 @@ describe('depthFirstSearch', () => {
depthFirstSearch(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,G,E,F,D,G');
expect(leaveVertexCallback.mock.calls.toString()).toBe('G,D,G,D,F,E,A');
const enterVertexParamsMap = [
{ 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);
}
});
});

View File

@ -1,10 +1,13 @@
/**
* @typedef {Object} Callbacks
* @property {function(vertex: GraphVertex, neighbor: GraphVertex): 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): 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(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 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;
@ -38,19 +41,20 @@ function initCallbacks(callbacks = {}) {
/**
* @param {Graph} graph
* @param {GraphVertex} vertex
* @param {GraphVertex} currentVertex
* @param {GraphVertex} previousVertex
* @param {Callbacks} callbacks
*/
function depthFirstSearchRecursive(graph, vertex, callbacks) {
callbacks.enterVertex(vertex);
function depthFirstSearchRecursive(graph, currentVertex, previousVertex, callbacks) {
callbacks.enterVertex({ currentVertex, previousVertex });
graph.getNeighbors(vertex).forEach((neighbor) => {
if (callbacks.allowTraversal(vertex, neighbor)) {
depthFirstSearchRecursive(graph, neighbor, callbacks);
graph.getNeighbors(currentVertex).forEach((nextVertex) => {
if (callbacks.allowTraversal({ previousVertex, currentVertex, nextVertex })) {
depthFirstSearchRecursive(graph, nextVertex, currentVertex, callbacks);
}
});
callbacks.leaveVertex(vertex);
callbacks.leaveVertex({ currentVertex, previousVertex });
}
/**
@ -59,5 +63,6 @@ function depthFirstSearchRecursive(graph, vertex, callbacks) {
* @param {Callbacks} [callbacks]
*/
export default function depthFirstSearch(graph, startVertex, callbacks) {
depthFirstSearchRecursive(graph, startVertex, initCallbacks(callbacks));
const previousVertex = null;
depthFirstSearchRecursive(graph, startVertex, previousVertex, initCallbacks(callbacks));
}