2
$\begingroup$

I have a tree and for its each edge, I want to know the numbers paths passing through this edge.

For example: a tree with vertex-set = [1, 2, 3, 4, 5, 6] and edge-set = [(1,2), (2,3), (2,4), (4,5), (4,6)] Number of paths for each edge:

(1,2)-> 5

(2,3)-> 5

(2,4)-> 9

(4,5)-> 5

(4,6)-> 5

I am not able to think of any algorithm other than brute force (enumerating all paths of tree).

Please provide any linear/quadratic method.

$\endgroup$
2
  • 1
    $\begingroup$ Shouldn't there be 8 paths including $\{4,5\}$? Namely one for each combination of "vertex on one side of the chosen edge" (1,2,3, or 4) and "vertex on the other side of the chosen edge" (5 or 6). $\endgroup$ Commented Aug 5, 2018 at 5:23
  • $\begingroup$ @HenningMakholm you are correct, I updated the example. Actually 5 is not connected to 6, instead 4 is connected to 6. $\endgroup$ Commented Aug 5, 2018 at 7:26

2 Answers 2

3
$\begingroup$

The answer mentioned above is $\mathcal{O}(N)$ for ONE edge but doing it for each one will make it $O(N^2)$.

Although we can compute the answer in O(N) total for all the edges.

I've added pseudocode for the algorithm at the end.
Steps:

1) As we have a tree, we can root it at some node, say node 1.

2) Run a dfs and compute an array subtreeSize[], where subtreeSize[i] gives the size of the subtree rooted at node i,(Subtree of a node x contains node x as well as all those nodes which passes through x to reach the root in shortest path). [I've added the pseudocode for computing subtreeSize array at the end]

3) Now that we have this array computed, for each edge {u,v} we know for sure either of these is a parent and other is the child. Lets say u is parent and v is its child. Now let A = subtreeSize[v], then answer for this edge will be (N - A) * (A). Why? Because we have these two sets of nodes, lets say we remove the {u,v} edge then the we'll get 2 trees, say all nodes in u side are in set X and all nodes in v side are in set Y. Then we can see that for each node in set X we can make a path to each node in set Y that crosses the {u,v} edge. So total number of paths will be = |X| * |Y|, (|setS| = size of setS). You can achieve this whole step in the same dfs if you notice closely whie computing subtreeSize

pseudocode for the complete O(N) algorithm:

dfs(node, parent):

subtreeSize[node] = 1
for each child c of node do:
    dfs(c, node)
    subtreeSize[node] = subtreeSize[node] + subtreeSize[c]
 answerForEdge[node and parent] = (N - subtreeSize[node])*(subtreeSize[node]) // N is the total number of nodes
 return
$\endgroup$
1
  • $\begingroup$ Welcome to Mathematics Stack Exchange community! The quick tour will help you get the most benefit from your time here. Also, please use MathJax for your equations. $\endgroup$
    – dantopa
    Commented Jan 12, 2019 at 5:32
1
$\begingroup$

If the edge is, say, {u,v}, it seems to me that you should be able to do a BFS from u (not including v) and a BFS from v (not including u).

From the BFS from u (omitting v), count the number of vertices you visit and add one to it (for u). This should be the number of paths leading to the u end of the edge (including the trivial path of just starting at u). Call this n(u).

Do the same thing from v (omitting u): count the number of vertices you visit and add one to it (for v). This will be the number of paths leading to the v end of the edge (including the trivial path of starting at v). Call this n(v).

Then you can simply multiply these together to get all the paths passing through edge {u,v}: the product is basically picking a path to u (of which there are n(u) such paths) and then picking a path to v (of which there are n(v) such paths).

BFS, on a tree, runs in O(V), where V is the number of nodes in the tree, so two BFS algorithms also run in time O(V) as well: thus, the algorithm is linear in the number of vertices. Indeed, since the double BFS visits every vertex exactly once, it can be done in V steps precisely.

You'll note that this works with your example: for instance, if we take {2,4}, starting a BFS at 2 gives us 2 vertices, namely 1 and 3, so:

n(2) = 2 + 1 = 3.

(Remember we add 1 for vertex 2 itself.)

Then, starting a BFS at 4 also gives us 2 vertices, namely 5 and 6, so:

n(4) = 2 + 1 = 3

(Remember that we add 1 for vertex 4 itself.)

Thus, the total number of paths through edge {2,4} is:

n(2) * n(4) = 3 * 3 = 9.

$\endgroup$
3
  • $\begingroup$ vorpal the solution mentioned by you will be O(n^2) not O(n) as you have to do it for each edge. $\endgroup$
    – manofp
    Commented Aug 8, 2018 at 4:20
  • $\begingroup$ @manofp It will be a linear time. Think. Hint: use post order traversal $\endgroup$ Commented Aug 10, 2018 at 17:39
  • $\begingroup$ A tree over n vertices has exactly n-1 edges, so it's not O(n^2) but O(n). For generic graphs, it is O(V + E), where V is the number of vertices and E is the number of edges. In this case, E = V-1, so O(V + (V-1)) = O(V) as desired. $\endgroup$
    – vorpal
    Commented Aug 28, 2018 at 22:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .