I'm using Networkx to compute some measures of a graph such as diameter, clustering coefficient, etc. It's straight forward how to do this for graph as a whole. What I'm interested in is finding these measures between nodes that have same attribute(say color). I'm thinking if I could partition the graph into different sub graphs, where nodes in each sub graph are of the same color, then I could accomplish go ahead and measure diameter in this sub graph. So my question is: Is there a way to partition a graph into sub graphs which contain nodes of same color?
I would really appreciate any insight.
Use
Graph.subgraph(nodes)
NetworkX 2.x+:
Demo
older NetworkX's
Iterate over (
Graph.iter_nodes()
) and filter the nodes based on your criteria. Pass that list toGraph.subgraph()
and it'll return a copy of those nodes and their internal edges.For example: