Generating a random planar graph in python - python

I am looking to generate a random planar graph in python with around 20 vertices. I checked out this planar graph generator but two problems emerged:
The algorithm on the aforementioned GitHub project seems a bit too overkill to generate a random planar graph that doesn’t have those many edges
Because it’s meant to generate massive graph, that algorithm is very complex, and therefore also a bit clunky and difficult to use
With that said, is there a simpler way to randomly generate a relatively small planar graph in python?

Create required number of nodes
Assign random x,y locations to the nodes.
WHILE nodes with no connected edges
Select N a random node with no edge
LOOP select M a different node at random
IF edge N-M does NOT intersect previous edges
Add N-M edge to graph
BREAK out of LOOP

Related

Looking for python packages that can calculate nodes/region/edges while respecting X,Y coordinates of nodes

I have a list of cities (nodes) plotted in a 2D plane each given by an X,Y coordinate.
I now want to add roads (edges) to it, but the roads cannot intersect. I want to create the most number of roads possible. By count, not by total length.
In more general graph theory parlance, I think I want the maximum number of edges (or regions?? maybe it's the same thing), where edges do not intersect in 2-dimensions, for a given set of Nodes at X,Y points.
In a brief view of NetworkX, it seems that they generate Graphs by making "nodes" but nodes can be "anywhere" and cannot force nodes to be at a certain location with respect to each other (they have abstracted too far!).
Edit: networkx add_node with specific position
suggests that you can plot them in a given location. #Stef thanks!!
Am i thinking about the problem correctly?
Can I visualize using some python package my Nodes/edges, where this package can automatically calculate the proper edges given a set of nodes?
Is automatically finding the maximum number of non-intersecting edges a thing (and what is this called so I can find out more about it?)
Very possibly similar to this question, but this question wasn't really answered and from 8 years ago (Algorithm for finding minimal cycle basis of planar graph)

How to find nearest neighbours of geometric subgraphs in networkx?

When I generate random geometric graphs using NetworkX in python the resulting graphs are not always connected. To change this property I would like to determine the separate subgraphs and find the two nodes in each respective subgraph that is closest to a node in the largest subgraph to connect them.
Preferably I would like to be able to also determine the second closest and the third and so on.
Is there a utility in networkx that does it. If not do you know the mathematical most efficient way to solve this? (Maybe randomly select two points in each graph and then execute a k-d tree algorithm - still problem is that at least for the smaller subgraph I would need to execute the algorithm for all nodes?!?!)
Would be great if you could advise me whether there is something existing in networkx that gets the job done or tell me the most efficient way to implement such a routine.
I would like to determine the separate subgraphs
This is the problem of finding the maximal cliques in a graph. That is the sets of vertices that are all reachable from each other, but not reachable from any vertex outside the set.
Here is the pseudo code for the algorithm
LOOP
CONSTRUCT empty current set
SELECT V arbitrary vertex
add V to current set
remove V from graph
LOOP // while set is growing
added_to_set = false
LOOP V over vertices in graph
LOOP Vset over current set
IF Vset connected to V
add V to current set
remove V from graph
added_to_set = true
break;
IF added_to_set == false
break; // the set is maximal
ADD current set to list of sets
IF graph has no remaining vertices
OUTPUT sets found
STOP
For a C++ implementation of this see code at https://github.com/JamesBremner/PathFinder2/blob/dbd6ff06edabd6a6d35d5eb10ed7972dc2d779a6/src/cPathFinder.cpp#L483
find the node in each respective subgraph that is closest to a node in
the largest subgraph
Probably the best and certainly simplest is to calculate the distance between every pair of nodes in the subgraphs, keeping the closest pair.
If you were satisfied with an approximate answer and if the subgraphs do not overlap then you could
calculate center of gravity of largest subgraph
compare distances of subgraph nodes to center of gravity ( instead of with every node in largest subgraph )

Vertices are double of edges

I am trying to detect the communities in a temporal network, that is, i want to detect communities at different time stamp and for that I am using Dataframe for filtering the data according to time.
For time, t=0, i want to plot the graph, I am using matplotlib for this purpose and also I am using igraph for the algorithms.
However, if I have n edges it is using 2n nodes when nodes are few. When I try to take only the unique vertices, it is showing the error vertex id invalid and if I let the code take 2n nodes then it is taking the vertices not even there in the time stamp. The graph formed is not meaningful as well

Tracking cycles while adding random edges to a sparse graph

Scenario: I have a graph, represented as a collection of nodes (0...n). There are no edges in this graph.
To this graph, I connect nodes at random, one at a time. An alternative way of saying this would be that I add random edges to the graph, one at a time.
I do not want to create simple cycles in this graph.
Is there a simple and/or very efficient way to track the creation of cycles as I add random edges? With a graph traversal, it is easy, since we only need to track the two end nodes of a single path. But, with this situation, we have any number of paths that we need to track - and sometimes these paths combine into a larger path, and we need to track that too.
I have tried several approaches, which mostly come down to maintaining a list of "outer nodes" and a set of nodes internal to them, and then when I add an edge going through it and updating it. But, it becomes extremely convoluted, especially if I remove an edge in the graph.
I have attempted to search out algorithms or discussions on this, and I can't really find anything. I know I can do a BFS to check for cycles, but it's so so so horribly inefficient to BFS after every single edge addition.
Possible solution I came up with while in the shower.
What I will do is maintain a list of size n, representing how many times that node has been on an edge.
When I add an edge (i,j), I will increment list[i] and list[j].
If after an edge addition, list[i] > 1, and list[j] > 1, I will do a DFS starting from that edge.
I realized I don't need to BFS, I only need to DFS from the last added edge, and I only need to do it if it at least has potential to be in a cycle (it's nodes show up twice).
I doubt it is optimal.. maybe some kind of list of disjoint sets would be better. But this is way better than anything I was thinking of before.
If you keep track of the connected components of your graph, you can test for every edge you insert whether the involved nodes are already in the same component. If they are, then the edge you are inserting will introduce a cycle to your graph.
Have a look at this post that seems to give some good references on how to do this: https://cstheory.stackexchange.com/questions/2548/is-there-an-online-algorithm-to-keep-track-of-components-in-a-changing-undirecte

Drawing massive networkx graph: Array too big

I'm trying to draw a networkx graph with weighted edges, but right now I'm having some difficulty.
As the title suggests, this graph is really huge:
Number of Nodes: 103362
Number of Edges: 1419671
And when I try to draw this graph with the following code:
pos = nx.spring_layout(G)
nx.draw(G, node_color='#A0CBE2',edge_color='#BB0000',width=2,edge_cmap=plt.cm.Blues,with_labels=False)
plt.savefig("edge_colormap.png") # save as png
plt.show() # display
(This is just me testing functionality, not my desired end result). I get the error:
ValueError: array is too big.
It's triggered from the spring_layout algorithm. Any idea what's causing this? Even when I use a different pos algorithm I get the same error, how can I avoid it (if I can)?
On another note, I want to colour the edges based on their weight. As you can see there are a lot of edges and probably a wide range of weights, what is the best way to do this?
Thanks for your patience.
EDIT FROM MY COMMENT:
I'm trying to investigate the density of the data I have. Basically I am looking at 50,000 matches each containing 10 players, and whenever two players meet in a game I +1 to the weight of the edge between them. The idea is that my end result will show me the strength of my data set. In my mind I want the heaviest edges at the centre and as we move out from the centre the data is less densely connected.
The problem lies in the spring_layout approach. With this many nodes it will take a while to calculate where all of them should go with respect to one another. With this many nodes I would suggest either figuring out the x,y positions yourself or plot much smaller subgraphs. (<5000 nodes or your computer might be a bit sluggish for a while.
Here is what 1000 nodes from an erdos_renyi_graph (randomly chosen edges) looks like.
It pulled off 2 nodes to highlight.
Next is what 1500 looks like
It got a little more detail. Now with 7-8 interesting nodes.
There isn't much to be gained by so many edges and so many nodes on a graph. And what happens if you don't like the output, you would need to re-run it again.
To get x,y positions of each node take a look at this. in NetworkX show graph with nodes at exact (x,y) position. Result is rotated

Categories