I'm trying to write a MySQL PROCEDURE
which takes an edge e
and an edge set eset
as inputs and outputs a boolean value iscyclic
to determine whether the additional edge results in a cyclic graph. Would there be any more straightforward way to do this other than creating a table of all the vertices with a column for something like "visit count
" and then checking if any vertex is visited more than once while running through the edge set?
As the comments of Billiska indicate, you need to keep track of the individual trees of your forest, i.e. the connected sets.
The easiest implementation of a disjoint set data structure would consist of a single temporary table, which maps the ID
of every vertex to the ID
of a parent. You can follow these parent links from one vertex to the next until you end up with the root of this tree, which is a vertex pointing at itself. This root serves as a unique representative that identifies the whole connected set.
So to check whether two sets are already connected, you compute their roots and simply compare them.
There are additional tools to keep the depth of the tree low:
All of this can be modeled in MySQL as well, but the performance behaviour might be different from an in-memory implementation.
So I'd suggest postponing that until you actually know that you need more performance, and have some data to test and compare different implementations.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With