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

generate algebraic expression from cyclic graph #736

Merged
merged 3 commits into from Nov 13, 2019
Merged

Conversation

swilly22
Copy link
Collaborator

@swilly22 swilly22 added the bug label Nov 12, 2019
@swilly22 swilly22 self-assigned this Nov 12, 2019
@swilly22 swilly22 added this to In progress in RedisGraph 2.0 via automation Nov 12, 2019

// Scans the graph in a DFS fashion, keeps track after the longest path length.
static void __DFSMaxDepth(QGNode *n, int level, int *max_depth, rax *visited) {
if(level > *max_depth) *max_depth = level;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

else level=*max_depth +1
if we are going to allow cycles, consider the following query:
MATCH (a)-[]->(b)-[]->(c)-[]->(b)-[]->(d)
<level, max_len> values in this variation of dfs traversal:
a-0,0
b-1,1
c-2,2
b-3,3
returning to c
c-2, 3
returning to b
b-1,3
going to d
d-3,3
the path will end at b as start and end of the loop and will have wrong increments.
should be
a-0,0
b-1,1
c-2,2
b-3,3
d-4,4

@@ -13,7 +13,7 @@
bool IsAcyclicGraph(const QueryGraph *qg) {
assert(qg);

bool cycle = false; // Return value.
bool acycle = true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think acyclic is a more obvious variable name.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@@ -524,20 +509,30 @@ AlgebraicExpression **AlgebraicExpression_FromQueryGraph(const QueryGraph *qg, u
return NULL;
}

bool acyclic = IsAcyclicGraph(qg);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hadn't thought of this before, but both our BFS and DFS routines are direction-agnostic, traversing incoming edges as well as outgoing. I would expect that IsAcyclicGraph needs to act similarly, no?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No IsAcyclicGraph should conclude on the original user pattern.
(a)-[]->(b) doesn't contains a cycle, but if you like you can traverse from b to a on a reversed edge.

uint node_count = QueryGraph_NodeCount(g);

// Run DFS from each node.
for(uint i = 0; i < node_count; i++) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we're not respecting edge direction, I think that checking every node is excessive - all nodes are reachable from all other nodes. Can't we simply check all leaf nodes (either no outgoing edges or no incoming), and if no nodes match that criteria, the starting point makes no difference?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if for every node n in degree > 0 and out degree > 0, we're dealing with a cycle and any node can be a candidate for detecting the longest path.
Otherwise there should be a node with in degree = 0 or out degree = 0, in which case this node is not part of a cycle, but leads into one.

I believe you are correct and we can reduce the number of candidates we inspect.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 good call!

Copy link
Contributor

@jeffreylovitz jeffreylovitz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This all looks good to me, approving! Dvir had a question at #736 (comment) , I'm not sure if you have a reached a consensus on that?

@swilly22 swilly22 merged commit efa45de into master Nov 13, 2019
RedisGraph 2.0 automation moved this from In progress to Done Nov 13, 2019
@swilly22 swilly22 deleted the longest_path_length branch April 5, 2020 12:10
pnxguide pushed a commit to CMU-SPEED/RedisGraph that referenced this pull request Mar 22, 2023
* generate algebraic expression from cyclic graph

* improved longest path starting point candidate nomination
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
No open projects
Development

Successfully merging this pull request may close these issues.

None yet

3 participants