Skip to content

Sarthak product subgraph#18

Open
sg60692 wants to merge 6 commits intomainfrom
sarthak-productSubgraph
Open

Sarthak product subgraph#18
sg60692 wants to merge 6 commits intomainfrom
sarthak-productSubgraph

Conversation

@sg60692
Copy link
Contributor

@sg60692 sg60692 commented Sep 1, 2023

The idea is to first create a $NPln(P)$ solution and then optimise it

Root the tree arbitrarily
For each connected subgraph we calculate it using the highest node in the subgraph, (must be unique)
For each node $u$ and each value $x$ we calculate the number of connect subgraphs such that u is the highest node of the subgraph and the product is equal to $x$

let $f_{u}(x)$ denote the above quantity
Clearly
$$ans = \sum_{u} {\sum_{x=1}^{P} f_{u}(x)}$$

let
$$g_{u}(x) = \sum_{i=1}^{x}f_{u}(x)$$
Therefore,
$$ans = \sum_{u} g_{u}(P)$$

lets look at following case,
We have a tree rooted as $u$ and another tree rooted at $v$
We want to combine these tree such that $u$ is parent of $v$,
We want to calculate the new $f_u$
Lets first find the new subgraphs formed by highest node $u$ that also contain $v$, denoted by$h$
$$h(x) = \sum_{i | x}f_{u}(i)*f_{v}(x/i)$$

The above mention is a Dirichlet Convolution
Clearly the updated $f_u$ be denoted by ${f'}$

$$f'{(x)} = f_{u}(x)+h(x)$$

Using this we can find and update the f values in dfs fashion

Instead of performing the above Drichlet Convolution naively we use the trick - Drichlet Convolution with prefix sums

Time Complexity:
$$O(N*P^{\frac{2}{3}})$$
We perform combine $N-1$ times, each time for one edge, each combine take $O(P^{\frac{2}{3}})$ operations

Space Complexity:
$$O(N*P^{\frac{2}{3}})$$
We have to save $O(P^{2,3})$ and the recursion depth can go upto $N$

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant