0

A graph consisting of n nodes, where there is an edge from 1 to 2, 2 to 3, 3 to 4, ........, n-1 to n.

Now, there is an array consisting of permutation of 1 to n and there are number of queries given based on array segments. Determine number of connected components formed by the nodes(indicated by the array elements) for the given segment. For e.g.,

Array : 4 5 3 2 1 Queries are : [1, 5], [1, 4], [2, 4]

For [1, 5], array elements are 1 2 3 4 5 and all are connected and forms a single connected component.

For [1, 4], array elements are 2 3 4 5, they also forms a single connected component.

For [2, 4], array elements are 2 3 5, so 2 and 3 forms a single component and 5 forms a single component so total of 2 connected components are there in [2, 4].

Sukesh
  • 1
  • 3
  • Can you say what time complexity you have achieved so far, and what you would like to achieve? – Peter de Rivaz Oct 23 '16 at 13:52
  • I have done with processing every queries in O(n) but I think there might be some offline processing or pre-processing which will effectively provides answer on queries. – Sukesh Oct 23 '16 at 13:58

1 Answers1

1

Since the graph has no cycles, the number of components in the subgraph is equal to the number of vertices minus the number of edges. The number of vertices is the length of the query interval. The number of edges can be found quickly by constructing an oracle for 2D orthogonal range counting queries on points that are (position of k in the permutation, position of k+1 in the permutation) for all k.

David Eisenstat
  • 64,237
  • 7
  • 60
  • 120
  • Problem part is with edges how do we know number of edges for an interval apart from processing throughout the interval – Sukesh Oct 23 '16 at 13:59
  • I don't know about constructing an oracle for 2D orthogonal range, if you can just clarify it with an example. – Sukesh Oct 23 '16 at 14:07