question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

findCycles only finds longest cycle when there is some overlap

See original GitHub issue

Consider this example:

g2 = new graphlib.Graph({directed: true});

g2.setNode("1");
g2.setNode("2");
g2.setNode("3");

g2.setEdge("1", "2");
g2.setEdge("2", "3");
g2.setEdge("3", "1");

graphlib.alg.findCycles(g2);
// [ [ '3', '2', '1' ] ]

g2.setNode("4");

g2.setEdge("3", "4");
g2.setEdge("4", "1");

graphlib.alg.findCycles(g2);
// [ [ '4', '3', '2', '1' ] ]

Surely the cycle [3, 2, 1] should also be found by the last statement?

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:7
  • Comments:7

github_iconTop GitHub Comments

6reactions
mstoucommented, Aug 27, 2018

Looking at the source code, the findCycles() implementation is the following:

function findCycles(g) {
  return _.filter(tarjan(g), function(cmpt) {
    return cmpt.length > 1 || (cmpt.length === 1 && g.hasEdge(cmpt[0], cmpt[0]));
  });
}

tarjan(g) returns an array with all the strongly connected components of the graph We filter that array ,removing singleton arrays whose nodes have no self edges, and return it. So, the returned array is just the strongly connected components filtered.

This falsely assumes that a strongly connected component contains only one cycle.

Finding all the simple cycles in a graph is a bit more complex, we need to run Johnson’s Algorithm I will try to implement it and open a pull request

3reactions
VotingToolcommented, Jun 9, 2020

This is a severe bug because “findCycles” simply does not return all cycles and users like me have a hard time to figure out why our projects break! Please fix this and until then, make clear in the wiki that it’s only “findSomeCycles” at the moment!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Finding all cycles in undirected graphs - Stack Overflow
For an undirected graph the standard approach is to look for a so called cycle base : a set of simple cycles from...
Read more >
Find cycles in an undirected graph - MATLAB Answers
It uses graph theoretic functions only. Here is its application to the data example that you provided. Each partitioned polygon is contained in...
Read more >
Approximating the Longest Cycle Problem in Sparse Graphs
We first consider the problem of finding long cycles in 3-connected cubic graphs whose edges have weights $w_i\geq 0$. We find cycles of ......
Read more >
FindCycle - Wolfram Language Documentation
FindCycle [g] finds a cycle in the graph g. FindCycle[g, k] finds a cycle of length at most k in the graph g....
Read more >
Finding cycles in a directed Graph - Codeforces
Lets say the graph had 2 OVERLAPPING cycles, so answer should be 3 along with their lengths. We must find smaller as well...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found