How to prevent edge crossing in the implementation of the Fruchterman and Reingold force-directed layout...












0















I have implemented the force-directed algorithm made by Fruchterman and Reingold in C++, but I still get edge crossings. enter image description here



I used the implementation that i found on this GitHub page.



This is the function that computes everything:



void Graph2::forceDirectedLayout2(double width, double height)
{
m_t = 10 * sqrt(m_nodes.size());
m_width = width;
m_height = height;
m_k = sqrt(m_width*m_height / m_nodes.size());
m_iterations=700;

for (size_t it = 0; it < m_iterations; it++) {

for (int v = 0; v < m_nodes.size(); v++) {
auto nodeV = m_nodes[v];
nodeV->disp = Coord();
for (int u = v+1; u < m_nodes.size(); u++) {

auto nodeU = m_nodes[u];

Coord delta = nodeV->pos - nodeU->pos;

double distance = delta.length()+0.0001;
if (distance > 1000) {
continue;
}

double repulsion = m_k*m_k / distance;
nodeV->disp += delta / distance*repulsion;
nodeU->disp -= delta / distance*repulsion;
}

for (int u = 0; u < m_nodes.size(); u++) {

if (v > u) {
continue;
}
auto nodeU = m_nodes[u];
Coord delta = nodeV->pos - nodeU->pos;
double distance = delta.length()+0.00001;
double attraction = distance*distance / m_k;
nodeV->disp -= delta / distance*attraction;
nodeU->disp += delta / distance*attraction;
}
}
for (int i = 0; i < m_nodes.size(); i++) {
double dispNorm = m_nodes[i]->disp.length();

if (dispNorm < 1.0) {
continue;
}

double capDisp = std::min(dispNorm, m_t);

auto disp = m_nodes[i]->disp / dispNorm*capDisp;
m_nodes[i]->pos += disp;
}

if (m_t > 1.5) {
m_t *= 0.85;
} else {
m_t = 1.5;
}
}
}


From other examples I have seen, there aren't supposed to be any edge crossings.
Does anyone know how to fix this? Thanks










share|improve this question




















  • 1





    It is hard to tell from the picture, but your graph may not be planar. A non-planar graph cannot be drawn without edge-crossing. Algorithms like the one you are using only try to minimize them to a certain degree. I suggest you compare this output with another implementation of the algorithm using the same graph or you could modify the parameters, like m_iterations and the rate of change of m_t to try to get a better result. I did not verify the implementation.

    – user10605163
    Dec 31 '18 at 15:17


















0















I have implemented the force-directed algorithm made by Fruchterman and Reingold in C++, but I still get edge crossings. enter image description here



I used the implementation that i found on this GitHub page.



This is the function that computes everything:



void Graph2::forceDirectedLayout2(double width, double height)
{
m_t = 10 * sqrt(m_nodes.size());
m_width = width;
m_height = height;
m_k = sqrt(m_width*m_height / m_nodes.size());
m_iterations=700;

for (size_t it = 0; it < m_iterations; it++) {

for (int v = 0; v < m_nodes.size(); v++) {
auto nodeV = m_nodes[v];
nodeV->disp = Coord();
for (int u = v+1; u < m_nodes.size(); u++) {

auto nodeU = m_nodes[u];

Coord delta = nodeV->pos - nodeU->pos;

double distance = delta.length()+0.0001;
if (distance > 1000) {
continue;
}

double repulsion = m_k*m_k / distance;
nodeV->disp += delta / distance*repulsion;
nodeU->disp -= delta / distance*repulsion;
}

for (int u = 0; u < m_nodes.size(); u++) {

if (v > u) {
continue;
}
auto nodeU = m_nodes[u];
Coord delta = nodeV->pos - nodeU->pos;
double distance = delta.length()+0.00001;
double attraction = distance*distance / m_k;
nodeV->disp -= delta / distance*attraction;
nodeU->disp += delta / distance*attraction;
}
}
for (int i = 0; i < m_nodes.size(); i++) {
double dispNorm = m_nodes[i]->disp.length();

if (dispNorm < 1.0) {
continue;
}

double capDisp = std::min(dispNorm, m_t);

auto disp = m_nodes[i]->disp / dispNorm*capDisp;
m_nodes[i]->pos += disp;
}

if (m_t > 1.5) {
m_t *= 0.85;
} else {
m_t = 1.5;
}
}
}


From other examples I have seen, there aren't supposed to be any edge crossings.
Does anyone know how to fix this? Thanks










share|improve this question




















  • 1





    It is hard to tell from the picture, but your graph may not be planar. A non-planar graph cannot be drawn without edge-crossing. Algorithms like the one you are using only try to minimize them to a certain degree. I suggest you compare this output with another implementation of the algorithm using the same graph or you could modify the parameters, like m_iterations and the rate of change of m_t to try to get a better result. I did not verify the implementation.

    – user10605163
    Dec 31 '18 at 15:17
















0












0








0








I have implemented the force-directed algorithm made by Fruchterman and Reingold in C++, but I still get edge crossings. enter image description here



I used the implementation that i found on this GitHub page.



This is the function that computes everything:



void Graph2::forceDirectedLayout2(double width, double height)
{
m_t = 10 * sqrt(m_nodes.size());
m_width = width;
m_height = height;
m_k = sqrt(m_width*m_height / m_nodes.size());
m_iterations=700;

for (size_t it = 0; it < m_iterations; it++) {

for (int v = 0; v < m_nodes.size(); v++) {
auto nodeV = m_nodes[v];
nodeV->disp = Coord();
for (int u = v+1; u < m_nodes.size(); u++) {

auto nodeU = m_nodes[u];

Coord delta = nodeV->pos - nodeU->pos;

double distance = delta.length()+0.0001;
if (distance > 1000) {
continue;
}

double repulsion = m_k*m_k / distance;
nodeV->disp += delta / distance*repulsion;
nodeU->disp -= delta / distance*repulsion;
}

for (int u = 0; u < m_nodes.size(); u++) {

if (v > u) {
continue;
}
auto nodeU = m_nodes[u];
Coord delta = nodeV->pos - nodeU->pos;
double distance = delta.length()+0.00001;
double attraction = distance*distance / m_k;
nodeV->disp -= delta / distance*attraction;
nodeU->disp += delta / distance*attraction;
}
}
for (int i = 0; i < m_nodes.size(); i++) {
double dispNorm = m_nodes[i]->disp.length();

if (dispNorm < 1.0) {
continue;
}

double capDisp = std::min(dispNorm, m_t);

auto disp = m_nodes[i]->disp / dispNorm*capDisp;
m_nodes[i]->pos += disp;
}

if (m_t > 1.5) {
m_t *= 0.85;
} else {
m_t = 1.5;
}
}
}


From other examples I have seen, there aren't supposed to be any edge crossings.
Does anyone know how to fix this? Thanks










share|improve this question
















I have implemented the force-directed algorithm made by Fruchterman and Reingold in C++, but I still get edge crossings. enter image description here



I used the implementation that i found on this GitHub page.



This is the function that computes everything:



void Graph2::forceDirectedLayout2(double width, double height)
{
m_t = 10 * sqrt(m_nodes.size());
m_width = width;
m_height = height;
m_k = sqrt(m_width*m_height / m_nodes.size());
m_iterations=700;

for (size_t it = 0; it < m_iterations; it++) {

for (int v = 0; v < m_nodes.size(); v++) {
auto nodeV = m_nodes[v];
nodeV->disp = Coord();
for (int u = v+1; u < m_nodes.size(); u++) {

auto nodeU = m_nodes[u];

Coord delta = nodeV->pos - nodeU->pos;

double distance = delta.length()+0.0001;
if (distance > 1000) {
continue;
}

double repulsion = m_k*m_k / distance;
nodeV->disp += delta / distance*repulsion;
nodeU->disp -= delta / distance*repulsion;
}

for (int u = 0; u < m_nodes.size(); u++) {

if (v > u) {
continue;
}
auto nodeU = m_nodes[u];
Coord delta = nodeV->pos - nodeU->pos;
double distance = delta.length()+0.00001;
double attraction = distance*distance / m_k;
nodeV->disp -= delta / distance*attraction;
nodeU->disp += delta / distance*attraction;
}
}
for (int i = 0; i < m_nodes.size(); i++) {
double dispNorm = m_nodes[i]->disp.length();

if (dispNorm < 1.0) {
continue;
}

double capDisp = std::min(dispNorm, m_t);

auto disp = m_nodes[i]->disp / dispNorm*capDisp;
m_nodes[i]->pos += disp;
}

if (m_t > 1.5) {
m_t *= 0.85;
} else {
m_t = 1.5;
}
}
}


From other examples I have seen, there aren't supposed to be any edge crossings.
Does anyone know how to fix this? Thanks







c++ algorithm layout graph






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 31 '18 at 17:37









TriskalJM

1,72311217




1,72311217










asked Dec 31 '18 at 15:01









user10468090user10468090

133




133








  • 1





    It is hard to tell from the picture, but your graph may not be planar. A non-planar graph cannot be drawn without edge-crossing. Algorithms like the one you are using only try to minimize them to a certain degree. I suggest you compare this output with another implementation of the algorithm using the same graph or you could modify the parameters, like m_iterations and the rate of change of m_t to try to get a better result. I did not verify the implementation.

    – user10605163
    Dec 31 '18 at 15:17
















  • 1





    It is hard to tell from the picture, but your graph may not be planar. A non-planar graph cannot be drawn without edge-crossing. Algorithms like the one you are using only try to minimize them to a certain degree. I suggest you compare this output with another implementation of the algorithm using the same graph or you could modify the parameters, like m_iterations and the rate of change of m_t to try to get a better result. I did not verify the implementation.

    – user10605163
    Dec 31 '18 at 15:17










1




1





It is hard to tell from the picture, but your graph may not be planar. A non-planar graph cannot be drawn without edge-crossing. Algorithms like the one you are using only try to minimize them to a certain degree. I suggest you compare this output with another implementation of the algorithm using the same graph or you could modify the parameters, like m_iterations and the rate of change of m_t to try to get a better result. I did not verify the implementation.

– user10605163
Dec 31 '18 at 15:17







It is hard to tell from the picture, but your graph may not be planar. A non-planar graph cannot be drawn without edge-crossing. Algorithms like the one you are using only try to minimize them to a certain degree. I suggest you compare this output with another implementation of the algorithm using the same graph or you could modify the parameters, like m_iterations and the rate of change of m_t to try to get a better result. I did not verify the implementation.

– user10605163
Dec 31 '18 at 15:17














1 Answer
1






active

oldest

votes


















0














The algorithm you use is not guaranteed to eliminate edge crossings, even if the input is planar. E.g. this is an equilibrium state (for some parameters of algorithm):



enter image description here



If you have a guarantee that your graph is planar and you'd like to find its crossing-free drawing, then you may use C++ boost library and its implementation of straight line drawing of planar graphs:
https://www.boost.org/doc/libs/1_67_0/libs/graph/doc/straight_line_drawing.html



Perhaps you'll get a nice result if you combine your current approach with the straight line drawing algorithm as follows:





  1. Produce a straight line drawing of your graph:



    a. temporarily make it maximal planar by adding edges (make_maximal_planar)



    b. draw it without edge intersections using chrobak_payne_straight_line_drawing



    c. remove edges you added in 1.a.




  2. Use your implementation of Fruchterman and Reingold:



    a. initialise the positions of nodes using the result of step 1.



    b. run the force-directed layout algorithm.




My intuition is that it should produce nicer-looking results than just step 1., probably without introducing edge intersections. But that's only intuition, I don't know how to prove that formally.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    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',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53988812%2fhow-to-prevent-edge-crossing-in-the-implementation-of-the-fruchterman-and-reingo%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    The algorithm you use is not guaranteed to eliminate edge crossings, even if the input is planar. E.g. this is an equilibrium state (for some parameters of algorithm):



    enter image description here



    If you have a guarantee that your graph is planar and you'd like to find its crossing-free drawing, then you may use C++ boost library and its implementation of straight line drawing of planar graphs:
    https://www.boost.org/doc/libs/1_67_0/libs/graph/doc/straight_line_drawing.html



    Perhaps you'll get a nice result if you combine your current approach with the straight line drawing algorithm as follows:





    1. Produce a straight line drawing of your graph:



      a. temporarily make it maximal planar by adding edges (make_maximal_planar)



      b. draw it without edge intersections using chrobak_payne_straight_line_drawing



      c. remove edges you added in 1.a.




    2. Use your implementation of Fruchterman and Reingold:



      a. initialise the positions of nodes using the result of step 1.



      b. run the force-directed layout algorithm.




    My intuition is that it should produce nicer-looking results than just step 1., probably without introducing edge intersections. But that's only intuition, I don't know how to prove that formally.






    share|improve this answer




























      0














      The algorithm you use is not guaranteed to eliminate edge crossings, even if the input is planar. E.g. this is an equilibrium state (for some parameters of algorithm):



      enter image description here



      If you have a guarantee that your graph is planar and you'd like to find its crossing-free drawing, then you may use C++ boost library and its implementation of straight line drawing of planar graphs:
      https://www.boost.org/doc/libs/1_67_0/libs/graph/doc/straight_line_drawing.html



      Perhaps you'll get a nice result if you combine your current approach with the straight line drawing algorithm as follows:





      1. Produce a straight line drawing of your graph:



        a. temporarily make it maximal planar by adding edges (make_maximal_planar)



        b. draw it without edge intersections using chrobak_payne_straight_line_drawing



        c. remove edges you added in 1.a.




      2. Use your implementation of Fruchterman and Reingold:



        a. initialise the positions of nodes using the result of step 1.



        b. run the force-directed layout algorithm.




      My intuition is that it should produce nicer-looking results than just step 1., probably without introducing edge intersections. But that's only intuition, I don't know how to prove that formally.






      share|improve this answer


























        0












        0








        0







        The algorithm you use is not guaranteed to eliminate edge crossings, even if the input is planar. E.g. this is an equilibrium state (for some parameters of algorithm):



        enter image description here



        If you have a guarantee that your graph is planar and you'd like to find its crossing-free drawing, then you may use C++ boost library and its implementation of straight line drawing of planar graphs:
        https://www.boost.org/doc/libs/1_67_0/libs/graph/doc/straight_line_drawing.html



        Perhaps you'll get a nice result if you combine your current approach with the straight line drawing algorithm as follows:





        1. Produce a straight line drawing of your graph:



          a. temporarily make it maximal planar by adding edges (make_maximal_planar)



          b. draw it without edge intersections using chrobak_payne_straight_line_drawing



          c. remove edges you added in 1.a.




        2. Use your implementation of Fruchterman and Reingold:



          a. initialise the positions of nodes using the result of step 1.



          b. run the force-directed layout algorithm.




        My intuition is that it should produce nicer-looking results than just step 1., probably without introducing edge intersections. But that's only intuition, I don't know how to prove that formally.






        share|improve this answer













        The algorithm you use is not guaranteed to eliminate edge crossings, even if the input is planar. E.g. this is an equilibrium state (for some parameters of algorithm):



        enter image description here



        If you have a guarantee that your graph is planar and you'd like to find its crossing-free drawing, then you may use C++ boost library and its implementation of straight line drawing of planar graphs:
        https://www.boost.org/doc/libs/1_67_0/libs/graph/doc/straight_line_drawing.html



        Perhaps you'll get a nice result if you combine your current approach with the straight line drawing algorithm as follows:





        1. Produce a straight line drawing of your graph:



          a. temporarily make it maximal planar by adding edges (make_maximal_planar)



          b. draw it without edge intersections using chrobak_payne_straight_line_drawing



          c. remove edges you added in 1.a.




        2. Use your implementation of Fruchterman and Reingold:



          a. initialise the positions of nodes using the result of step 1.



          b. run the force-directed layout algorithm.




        My intuition is that it should produce nicer-looking results than just step 1., probably without introducing edge intersections. But that's only intuition, I don't know how to prove that formally.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 2 at 13:32









        RadekRadek

        736315




        736315
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53988812%2fhow-to-prevent-edge-crossing-in-the-implementation-of-the-fruchterman-and-reingo%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Monofisismo

            Angular Downloading a file using contenturl with Basic Authentication

            Olmecas