I want to create a hexagonal lattice using the NetworkX python library. But it should be centered basically the whole lattice is a central hexagon and then layers of hexagon around
In python with NetworkX you would want to use the hexagonal_lattice_graph function which is part of it's generators class.
Something like this:
import networkx as nx
G = nx.generators.lattice.hexagonal_lattice_graph(2,2)
nx.draw(G)
Will give a graph which you'd want:
2x2 lattice output
Related
I need to draw fractal graph or self-similar graph (for example like in picture below)
Does the networkx library have a special function for constructing this graph?
I'm trying to draw a directed graph in python with networkx
It uses Fruchterman-Reingold force-directed algorithm to position nodes. However, I couldn't get a result of graph with minimum overlapping edges. I want something like this in NetworkX or some other python API.
Can Fruchterman-Reingold algorithm produce a graph with minimum overlapping edges? If it can, which parameter should I adjust? If it can not, is there any API or alogrithm to use in python?
My code to visualize
pos = nx.spring_layout(G, k=100, iterations=500, seed=1)
Let me know if you need more info.!
Thanks!
I want to generate a random graph using the edge distribution from the original graph.
Is there a way in networkx module to do that?
I'm assuming you mean same degree distribution.
The command nx.configuration_model(degree_list) will do it.
So in your case, given an existing graph G:
H = nx.configuration_model([G.degree(node) for node in G])
I have built a NetworkX Graph containing 50000 Nodes and about 100 Million edges. I have a list of all connected components of this group using nx.connected_components(G) method. This method results in me having clusters of nodes such that each node has a path to reach every other node in that cluster. Now what I want is, in each of these connected components, I want to find subgraphs/sub-clusters such that each of these subgraphs are connected to each other by exactly one edge. Is there a method in NetworkX that I can use directly or any other way in which I can get this done? Sorry I am very new to graph theory so need a little direction.
If I understand you correctly, then for each subgraph, you want to find all graph cuts of size 1, i.e. you want to find all edges, that if taken away partition the graph into two subgraphs. These edges are called bridges and there are efficient algorithms to find them. The implementation in networkx is accessible via networkx.algorithms.bridges.bridges.
What you want is called minimum spanning three. Using networkx you can do it like this:
import networkx as nx
import matplotlib.pyplot as plt
G = nx.Graph()
G.add_edges_from([(1,2), (1,3), (2,3), (4,5), (4,6), (5,6)])
nx.draw(nx.minimum_spanning_tree(G), with_labels=True)
plt.show()
However, I'm a little bit in doubt if networkx is able to perform on so many edges according to this benchmark. I have tested connected components algorithm on igraph, it worked for me as well (and, of course, much faster), so you might also like to look for igraph based solutions.
Result
I want to plot a network in Python using a co-occurence matrix as an input, such that nodes that have a non-zero co-occurence count are connected, and the weight of the edges is proportional to the number of co-occurrences between each node.
Is there a python library in existence that will facilitate this task using a co-occurence matrix as an input?
You might find NetworkX to be a useful tool for that. You can easily feed it the input nodes and edges in several ways.
In the case that you want to generate your network using a co-occurrence matrix, you can use NetworkX's method from_numpy_matrix, which allows you to create a graph from a numpy matrix matrix which will be interpreted as an adjacency matrix.
Here's a simply toy example from the documentation:
import numpy as np
import networkx as nx
A=np.matrix([[1,1],[2,1]])
G=nx.from_numpy_matrix(A)
It is indeed possible to do something like that with networkx
Check this: https://stackoverflow.com/a/25651827/4288795
With it you can generate graphs like this:
You can export the information to a graphml file format and use yEd Graph Editor to navigate and format the contents of your networkx graph.