I am getting desprite, because I can't solve this problem. I hope this is the right place to ask for help in this context. The Problem is, that I have a graph that is the union of disjoint directed cycles. Additionally, there are 𝑘 nodes 𝑋(1), ..., 𝑋(𝑘) whose indices are stored in an array 𝑋 of length 𝑘 ≤ 𝑛. The graph is represented by an array 𝑆 of length 𝑛, where 𝑆(𝑖) is the successor node of 𝑖.
I need to calculate the length of the cycle containing each node in 𝑋. The result should be an array 𝑌 of length 𝑘, such that 𝑌(1), ..., 𝑌(𝑘) contains the length of the respective cycle.
Can anyone suggest an algorithm/pseudocode (in C) with a runtime of 𝑂(𝑘 + log 𝑛) and work 𝑂(𝑛) to compute 𝑌?
Any help or pointers would be greatly appreciated!
I could only create this Pseudocode:
for i in {1,...,k} par do
while S[i] != i
S[i] = S[ S[i] ]
Y[i]++
with a runtime of 𝑂(𝑘*log 𝑛) and work 𝑂(𝑘𝑛)
In order to meet your time constraint, you need to start a process for each node, and those processes must be able to collectively process each node's cycle in O(log n) time.
By "processing a cycle", I specifically mean collecting the minimum or maximum of some function over all nodes in the cycle, and broadcasting it to all nodes in the cycle. If you can do that, then your problem can be solved with this constant-length sequence of steps:
Then you can just look up the cycle length for each of your k target nodes.
To process all the cycles in O(log n) time:
Note that each iteration of the loop in (2) needs to be done effectively synchronously for all nodes. There are local O(1) ways to accomplish this. Basically step d=2n cannot be processed for any node until its predecessors and successors in T are finished reading the results for d=n.
Once d>=n for all nodes, V[i] holds the same min or max value for all nodes in each cycle.
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