For each edge in a tree, counting paths passing through this edge

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












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.







share|cite|improve this question

















  • 1




    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).
    – Henning Makholm
    yesterday











  • @HenningMakholm you are correct, I updated the example. Actually 5 is not connected to 6, instead 4 is connected to 6.
    – Sushil Verma
    yesterday














up vote
1
down vote

favorite












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.







share|cite|improve this question

















  • 1




    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).
    – Henning Makholm
    yesterday











  • @HenningMakholm you are correct, I updated the example. Actually 5 is not connected to 6, instead 4 is connected to 6.
    – Sushil Verma
    yesterday












up vote
1
down vote

favorite









up vote
1
down vote

favorite











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.







share|cite|improve this question













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.









share|cite|improve this question












share|cite|improve this question




share|cite|improve this question








edited yesterday
























asked yesterday









Sushil Verma

406




406







  • 1




    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).
    – Henning Makholm
    yesterday











  • @HenningMakholm you are correct, I updated the example. Actually 5 is not connected to 6, instead 4 is connected to 6.
    – Sushil Verma
    yesterday












  • 1




    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).
    – Henning Makholm
    yesterday











  • @HenningMakholm you are correct, I updated the example. Actually 5 is not connected to 6, instead 4 is connected to 6.
    – Sushil Verma
    yesterday







1




1




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).
– Henning Makholm
yesterday





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).
– Henning Makholm
yesterday













@HenningMakholm you are correct, I updated the example. Actually 5 is not connected to 6, instead 4 is connected to 6.
– Sushil Verma
yesterday




@HenningMakholm you are correct, I updated the example. Actually 5 is not connected to 6, instead 4 is connected to 6.
– Sushil Verma
yesterday










1 Answer
1






active

oldest

votes

















up vote
0
down vote













If the edge is, say, u,v, it seems to me that you should be able to do a BFS from u (ignoring v and its neighbours) and a BFS from v (ignoring u and its neighbours).



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.






share|cite|improve this answer























    Your Answer




    StackExchange.ifUsing("editor", function ()
    return StackExchange.using("mathjaxEditing", function ()
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
    );
    );
    , "mathjax-editing");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "69"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    noCode: true, onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );








     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f2872647%2ffor-each-edge-in-a-tree-counting-paths-passing-through-this-edge%23new-answer', 'question_page');

    );

    Post as a guest






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    If the edge is, say, u,v, it seems to me that you should be able to do a BFS from u (ignoring v and its neighbours) and a BFS from v (ignoring u and its neighbours).



    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.






    share|cite|improve this answer



























      up vote
      0
      down vote













      If the edge is, say, u,v, it seems to me that you should be able to do a BFS from u (ignoring v and its neighbours) and a BFS from v (ignoring u and its neighbours).



      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.






      share|cite|improve this answer

























        up vote
        0
        down vote










        up vote
        0
        down vote









        If the edge is, say, u,v, it seems to me that you should be able to do a BFS from u (ignoring v and its neighbours) and a BFS from v (ignoring u and its neighbours).



        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.






        share|cite|improve this answer















        If the edge is, say, u,v, it seems to me that you should be able to do a BFS from u (ignoring v and its neighbours) and a BFS from v (ignoring u and its neighbours).



        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.







        share|cite|improve this answer















        share|cite|improve this answer



        share|cite|improve this answer








        edited yesterday


























        answered yesterday









        vorpal

        12




        12






















             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f2872647%2ffor-each-edge-in-a-tree-counting-paths-passing-through-this-edge%23new-answer', 'question_page');

            );

            Post as a guest













































































            Comments

            Popular posts from this blog

            What is the equation of a 3D cone with generalised tilt?

            Color the edges and diagonals of a regular polygon

            Relationship between determinant of matrix and determinant of adjoint?