Monday, November 18, 2024
Google search engine

Successor Graph

A Successor Graph is a directed graph in which each vertex has outdegree one, i.e., exactly one edge starts at each node. A successor graph consists of one or more components, each of which contains one cycle and some paths that lead to it.

Successor graphs are sometimes called functional graphs. The reason for this is that any successor graph corresponds to a function that defines the edges of the graph. The parameter for the function is a node of the graph, and the function gives the successor of that node. For Example, the function

x 1 2 3 4 5 6 7 8 9
succ(x) 3 5 7 6 2 2 1 6 3

The above function defines the below graph:

Since each node of a successor graph has a unique successor, a function succ(x, k) can also be defined that gives the node when a traversal begin at node x and walk k steps forward. For example, in the above graph succ(4, 6) = 2, because node 2 can be reached by walking 6 steps from node 4:

A straightforward way to calculate a value of succ(x, k) is to start at node x and walk k steps forward, which takes O(k) time. However, using preprocessing, any value of succ(x, k) can be calculated in only O(logk) time.

The idea is to precalculate all values of succ(x, k) where k is a power of two and at most u, where u is the maximum number of steps we will ever walk. This can be efficiently done because we can use the following recursion:

Precalculating the values takes O(n*log u) time because O(log u) values are calculated for each node. In the above graph, the first values are as follows:

x 1 2 3 4 5 6 7 8 9
succ(x, 1) 3 5 7 6 2 2 1 6 3
succ(x, 2) 7 2 1 2 5 5 3 2 7
succ(x, 4) 3 2 7 2 5 5 1 2 3
succ(x, 8) 7 2 1 2 5 5 3 2 7
                 

After this, any value of succ(x, k) can be calculated by presenting the number of steps k as a sum of powers of two. 
For example, if we want to calculate the value of succ(x, 11), we first form the representation 11 = 8 + 2 + 1. Using that

succ(x, 11) = succ(succ(succ(x, 8), 2), 1)

For example, in the previous graph

succ(4, 11) = succ(succ(succ(4, 8), 2), 1) = 5

Such a representation always consists of O(log k) parts, so calculating a value of succ(x, k) takes O(log k) time. 

Brute force 

A straightforward way to calculate a value of succ(x, k) is to start at node x and walk k steps forward, which takes O(k) time.

/* Function to find the Kth successor of node x */ 

function succ(x, k, successor) 

   //   repeat k times, change x -> successor(x) 

   for i from 1 to k 

     x = successor(x) 

   return x

Time Complexity:

O(K) per query, where ‘K’ is the number of successors we are interested in. 

Space Complexity: 

O(1), since we are not using any additional space to find the kth successor of a node.

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments