Skip to content
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

Merged
merged 2 commits into from
Dec 30, 2020

Conversation

sanket0896
Copy link

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 [].

const VISITED = 2;
const statusMap = new Map(Object.keys(graph).map(key => [key, UNVISITED]));

// Utility function for depth first search

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){

const graph = depGraph.createDependencyTree(expression);
const cycle = depGraph.checkForCyclicDependency(graph);
expect(cycle).to.deep.equal([
'a',

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?

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?

Copy link
Author

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.

Copy link
Author

@sanket0896 sanket0896 Dec 30, 2020

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

@codebreach codebreach merged commit 669faa7 into master Dec 30, 2020
@sanket0896 sanket0896 deleted the sanket/dependency-graph-cyclic-check branch March 3, 2021 08:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants