I would like to use graph tool subpackage : minimize_blockmodel_dl
https://graph-tool.skewed.de/static/doc/inference.html#graph_tool.inference.minimize_blockmodel_dl to find communities in a network.
I write code like this:
state = gt.minimize_blockmodel_dl(G). G is the graph containing nodes and edges. I wonder how can I do next to get the specific communities?
Related
I'm not sure why my network graph doesn't include edges.
I'm creating a network from a pandas dataframe that looks like the following:
I created the network as follows:
G = nx.from_pandas_edgelist(network_df,
edge_attr='weight',
source='Source',
target='Target',
create_using=nx.Graph())
but nx.draw(G) produces a graph without edges.
I tried using nx.DigGraph() but the result is the same.
Any help is greatly appreciated.
That central "blob" in your plot is a lot of nodes connected together which probably do have edges, but they are obscured by the dense mass of nodes. On the periphery there are a few nodes joined together by edges, but due to the plotting algorithm they pairs (or somewhat larger cluster) are again so close together that the nodes are obscured. The isolated nodes are isolated.
It's probably best to try another layout. The default is spring_layout. Here's another that will probably show it better:
pos = nx.circular_layout(G)
nx.draw(G, pos)
As a general rule, networkx was not designed for the purpose of graph visualization. So you may need to look at other tools like graphviz.
I would like to visually group a set of nodes in networkx. Of course one could do so by changing node color, size etc. My preferred solution however should look something like the plot created with igraph below. Is this possible in networkx?
I'm trying to visualise a multi-edge directed graph, with hyper-edges. I've looked into python NetworkX, I'm starting with the directed graph, which can basically give me this:
But I would like to visualise my edges combined (since they are a kind of "bundle", with multiple source vertices, and one destination vertex).
Like this:
Any help would be appreciated.
I have a text document that is an edge list file. I know how to read the file (using Canopy Enthought), but I don't know how to get the information about the graph that I want.
Main question: Is there a way to detect whether this graph (created from the edge list file) is directed or undirected using networkx commands? Or just if it is weighted or unweighed?
I believe that you have to specify the type of the graph before using the edge list file. Because the edge list file is simply composed of tuples containing nodes to be connected without saying how they are connected. Thus, for instance if you create a graph G = nx.Graph(), then if the node pairs in the file are repeated, there will still be one edge between them and the order of the nodes does not matter; ((node1,node2) is equivalent to (node2,node1)). While if you created the graph as G = nx.DiGraph() the order of nodes makes a difference. Also, specifying G = nx.MultiGraph() more than one edge will exist in case of repetition. G = nx.MultiDiGraph() will have a different result when reading the edge list file. So, check the the graph types documentation to know which type you need to have.
To check if the graph is directed you can use
nx.is_directed(G), you can find the documentation here.
To check if the graph is weighted
There is no specific type to say if the graph has weighted edges or not. But a work around can be to check if edges contain an attribute called weight, as mentioned here. It can be done by
'weight' in G[1][2] # Returns true if an attribute called weight exists in the edge connecting nodes 1 and 2.
I'm using Python's NetworkX package to calculate a bunch of network statistics for networks of varying size. I'm sweeping an independent parameter that systematically prunes edges, so sometimes a small network will become disconnected from the main network. Is there an easy way to detect and remove those smaller disconnected networks in NetworkX?
Sorin is correct. The function is called connected_component_subgraphs in NetworkX.
Documentation: http://networkx.github.io/documentation/latest/reference/generated/networkx.algorithms.components.connected.connected_component_subgraphs.html#networkx.algorithms.components.connected.connected_component_subgraphs
Here's some code that finds the largest network in a NetworkX graph:
cur_graph = # whatever graph you're working with
if not nx.is_connected(cur_graph):
# get a list of unconnected networks
sub_graphs = nx.connected_component_subgraphs(cur_graph)
main_graph = sub_graphs[0]
# find the largest network in that list
for sg in sub_graphs:
if len(sg.nodes()) > len(main_graph.nodes()):
main_graph = sg
cur_graph = main_graph
As the accepted answer is now deprecated here is a better solution for an undirected graph = G:
# Generate connected components and select the largest:
largest_component = max(nx.connected_components(G), key=len)
# Create a subgraph of G consisting only of this component:
G2 = G.subgraph(largest_component)
For a directed graph, you will need either strongly_connected_components(G) or weakly_connected_components(G) in the place of connected_components(G).
https://networkx.github.io/documentation/stable/reference/algorithms/component.html
The generic algorithm is called connected components. You can find a description here: http://en.wikipedia.org/wiki/Connected_component_(graph_theory). It's fairly easy to implement and linear in the number of edges to run.
Not sure about NetworkX.