|
| 1 | +/** |
| 2 | + * Copyright (C) 2014 Tayllan Búrigo |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | + * of this software and associated documentation files (the "Software"], to |
| 6 | + * deal in the Software without restriction, including without limitation the |
| 7 | + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 8 | + * sell copies of the Software, and to permit persons to whom the Software is |
| 9 | + * furnished to do so, subject to the following conditions: |
| 10 | + * |
| 11 | + * The above copyright notice and this permission notice shall be included in |
| 12 | + * all copies or substantial portions of the Software. |
| 13 | + * |
| 14 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 19 | + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 20 | + * IN THE SOFTWARE. |
| 21 | + */ |
| 22 | +'use strict'; |
| 23 | + |
| 24 | +var depthFirstSearch = require('../../../algorithms/graph/depth_first_search'), |
| 25 | + Graph = require('../../../data_structures/graph'), |
| 26 | + assert = require('assert'), |
| 27 | + graph = new Graph(true); |
| 28 | + |
| 29 | +graph.addEdge('one', 'three'); |
| 30 | +graph.addEdge('one', 'four'); |
| 31 | +graph.addEdge('four', 'two'); |
| 32 | +graph.addEdge('two', 'one'); |
| 33 | +graph.addEdge('three', 'one'); |
| 34 | +graph.addEdge('five', 'six'); |
| 35 | + |
| 36 | +describe('Depth First Search Algorithm', function () { |
| 37 | + it('should visit only the nodes reachable from the node {one} (inclusive)', |
| 38 | + function () { |
| 39 | + var finishingTimes = depthFirstSearch.dfsAdjacencyList(graph, 'one'); |
| 40 | + |
| 41 | + assert.equal(finishingTimes.five, undefined); |
| 42 | + assert.equal(finishingTimes.six, undefined); |
| 43 | + |
| 44 | + assert.equal(finishingTimes.one, 3); |
| 45 | + assert.equal(finishingTimes.two, 1); |
| 46 | + assert.equal(finishingTimes.three, 0); |
| 47 | + assert.equal(finishingTimes.four, 2); |
| 48 | + } |
| 49 | + ); |
| 50 | + |
| 51 | + it('should visit all the nodes in the graph', |
| 52 | + function () { |
| 53 | + var finishingTimes = depthFirstSearch.dfsAdjacencyListStart(graph); |
| 54 | + |
| 55 | + assert.equal(finishingTimes.one, 3); |
| 56 | + assert.equal(finishingTimes.two, 1); |
| 57 | + assert.equal(finishingTimes.three, 0); |
| 58 | + assert.equal(finishingTimes.four, 2); |
| 59 | + assert.equal(finishingTimes.five, 5); |
| 60 | + assert.equal(finishingTimes.six, 4); |
| 61 | + } |
| 62 | + ); |
| 63 | +}); |
| 64 | + |
0 commit comments