Skip to content

Commit

Permalink
pull-req for 524 (#531)
Browse files Browse the repository at this point in the history
* fix for #524 hasGraphCycle

* add new task for someone else
  • Loading branch information
deB4SH authored and BlakeGuilloud committed Oct 28, 2017
1 parent 611f461 commit 4caecf3
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 6 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ dist
node_modules
npm-debug.log
.DS_Store
.idea
.idea
.vscode
7 changes: 7 additions & 0 deletions lib/getMapKey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Write a function that get the nth key in the map
// eg. Map([A,(object)][B,(object)][C,(object)][D,(object)]) -> get the third key in this map
function getMapKey(map, n) {

}

module.exports = getMapKey;
41 changes: 37 additions & 4 deletions lib/hasGraphCycle.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,44 @@
*/

// Given a directed graph, a character representing a start node,
// and a character representing a destination, determine whether
// and a character representing a destination, determine whether
// the graph contains a cycle

function hasGraphCycle(graph){

function hasGraphCycle(graph) {
//build an array of known nodes
var nodes = new Map();
//check all out-going nodes
graph.forEach(function (element) {
if (nodes.has(element.from)) {//node already known in map
nodes.get(element.from).links.push(element.to);
} else {//node not known
nodes.set(element.from, { visited: false, links: [] });
nodes.get(element.from).links.push(element.to);
}
});
//check nodes that are only in-going
graph.forEach(function (element) {
if (!nodes.has(element.to)) {//node already known in map
nodes.set(element.from, { visited: false, links: [] });
}
});
var first = nodes.keys().next().value;
//start with the first element of this list
var explorationList = [first];
var isCyclic = false;
nodes.get(first).visited = true;
while (explorationList.length > 0) {
var curr = explorationList.shift();
//parse through each nextNode of this current element
nodes.get(curr).links.forEach(function (child) {
if (!nodes.get(child).visited) {
nodes.get(child).visited = true;
explorationList.push(child);
} else {
isCyclic = true;
}
});
}
return isCyclic;
}

module.exports = hasGraphCycle;
13 changes: 13 additions & 0 deletions test/getMapKey.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const { getMapKey } = require("../lib");
describe("getMapKey", () => {
test("Get the third key of the given map.", () => {
var nodes = new Map();
nodes.set("A", { some: false, data: [] });
nodes.set("B", { some: false, data: [] });
nodes.set("C", { some: false, data: [] });
nodes.set("D", { some: false, data: [] });
nodes.set("E", { some: false, data: [] });
nodes.set("F", { some: false, data: [] });
expect(getMapKey(nodes,3)).toBe("C");
});
});
2 changes: 1 addition & 1 deletion test/hasGraphCycle.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ describe("hasGraphCycle", () => {
{from: "D", to: "A"},
{from: "E", to: "F"}
];
expect(hasGraphCycle(directedGraph)).toBe(true);
expect(hasGraphCycle(directedGraph)).toBe(false);
});
});

0 comments on commit 4caecf3

Please sign in to comment.