-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added function and tests for cyclic dependency check #25
Conversation
dependency-graph.js
Outdated
const VISITED = 2; | ||
const statusMap = new Map(Object.keys(graph).map(key => [key, UNVISITED])); | ||
|
||
// Utility function for depth first search |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
convert these to jsdocs?
/**
* Utility function for depth first search. Updates `cycle` to keep track of potential circular dependency.
* @param {unknown} node The node to visit
* @param {Array<unknown>} neighbours The neighbours of the node being visited.
* @returns {undefined} Nothing is returned
* @throws {Error} If a cycle is encountered while doing DFS
*/
function dfs(node, neighbours){
test/dependency-graph.js
Outdated
const graph = depGraph.createDependencyTree(expression); | ||
const cycle = depGraph.checkForCyclicDependency(graph); | ||
expect(cycle).to.deep.equal([ | ||
'a', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is a bit weird...
it seems that a
has a circular dependency but its actually x
that has it?
shouldn't the execution just stop when the first x
is encountered?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe clarify this a bit in the comment describing the checkForCyclicDependency
function?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm returning the entire dfs path now. I think it'll make more sense to return the cyclic part of it only.
Also, dependency graph is of the format baseVar -> [ vars affected by baseVar ]
, thats why the cycle is sort of reverse of what you'd expect.
Reversing cycle array just before returning should do the trick.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just to confirm, here the expected cycle is [ x, z, p, q, x ]
right ? @codebreach
Add function
checkForCyclicDependency
which checks if the given graph has cycle in it using Depth First Search. If cycle is found, it return the cyclic path, otherwise returns[]
.