Skip to content

Example fix for issue 7633 - #7634

Merged
dschult merged 3 commits into
networkx:mainfrom
WoojinJung-04:example-fix-for-issue-7633
Sep 14, 2024
Merged

dschult merged 3 commits into
networkx:mainfrom
WoojinJung-04:example-fix-for-issue-7633

Conversation

@WoojinJung-04

@WoojinJung-04 WoojinJung-04 commented Sep 13, 2024 •

Copy link
Copy Markdown
Contributor

Modified plot_maximum_independent_set.py (changed seed to 251134665)

So that it shows a better example of the graph.

Document needs to be modified

Closes #7633

@rossbar rossbar left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @WoojinJung-04 , +1 for fixing the visualization. I have a minor suggestion (see above) for how to more reliably improve the layout independent of the seed!

print(f"Maximum independent set of G: {I}")

pos = nx.spring_layout(G, seed=39299899)
pos = nx.spring_layout(G, seed=251134665)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In principle, we'd want a solution that doesn't depend on the seed. One thing to try that should robustly give better results is to increase the number of iterations to prevent premature termination of the layout method:

Suggested change
pos = nx.spring_layout(G, seed=251134665)
pos = nx.spring_layout(G, iterations=100, seed=251134665)

@WoojinJung-04 WoojinJung-04 Sep 13, 2024 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rossbar
Thank you for the comment! It's really exciting to me as I'm just starting on my journey to the world of open source projects! I'm planning to get a career heavily related to Data Analysis so I' m hunting for issues from libraries like networkx or mathplotlib I can work on to gradually increase my skill!😊

I really appreciate your feedback!

/

I've looked into the code of 'nx.spring_layout' and tried multiple times increasing the iterations variable in this case.

While the frequency of graphs such as this where vertices overlap
Untitled3

decreased,

the frequency of graphs such as this where vertices cross each other

Untitled
increased

I have confirmed by multiple different seeds that iterations > 400 consistently avoids the overlapping of vertices in our example from #7633

\

In the function '_fruchterman_reingold' inside nx.spring_layout, the equation to calculate the attracting forces between vertices is

A * distance / k

while 'A' is an adjacency matrix and k the optimal distance between vertices

This means that there aren't any forces simulated between vertices that aren't connected.

Perhaps modifying the function could also be an solution.

The simplest solution(to avoid any interesting phenomena) I could think of is to add a weak force between unconnected vertices. I haven't tried it yet but it could possibly solve the issue in a aesthetically pleasing way, but it might diverge from the original algorithm.

@rossbar

rossbar commented Sep 13, 2024

Copy link
Copy Markdown
Contributor

the frequency of graphs such as this where vertices cross each other

Do you mean edges crossing each other (instead of vertices)? Generally edge crossings are completely fine! The main issue here is when the nodes 1, 6, and 7 are colinear, which causes the edge masking. The success criterion is that all three edges (1, 6), (6, 7) and (1, 7) are visible, regardless whether they cross one another.

The simplest solution(to avoid any interesting phenomena) I could think of is to add a weak force between unconnected vertices. I haven't tried it yet but it could possibly solve the issue in a aesthetically pleasing way, but it might diverge from the original algorithm.

This could be interesting to consider but is certainly out-of-scope for this PR. Please feel free to investigate the FR algorithm (I'd recommend starting with the paper), but I'd advocate opening separate issues/PRs for that so we don't block this fix.

For this PR, IMO the best way forward is to pick a combination of seed and iterations that produces the desired visualization and go with that. For example, iterations=100, seed=42 seems to work!

@WoojinJung-04

WoojinJung-04 commented Sep 13, 2024 •

Copy link
Copy Markdown
Contributor Author

Oh, I understand. While the criterion is whether all of the edges are distinctly visible, we also want it to be a replicable solution!

I have made the necessary changes and commited them!

@rossbar rossbar left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @WoojinJung-04 , LGTM!

Here's the updated example for other reviewers - note that edge-masking in the node 1, 6, 7 cluster is fixed!

@dschult dschult left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me. Thanks very much @WoojinJung-04 .
And Thanks @rossbar for the nice review.

@dschult
dschult merged commit f0e6fe5 into networkx:main Sep 14, 2024
@jarrodmillman jarrodmillman added this to the 3.4 milestone Sep 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

Doc: visually hidden edge on gallery example Max Ind Set

4 participants