Skip to content

SubGraph filter on nodes/edges - #2523

Merged
dschult merged 3 commits into
networkx:masterfrom
dschult:subgraph_filter
Jul 28, 2017
Merged

dschult merged 3 commits into
networkx:masterfrom
dschult:subgraph_filter

Conversation

@dschult

@dschult dschult commented Jul 18, 2017

Copy link
Copy Markdown
Member

Here SubGraph means a view restricting (or enhancing) a graph.
Two filters are possible: node_filter and edge_filter
node_filter takes a node argument and returns True to keep the node.
edge_filter takes two nodes as arguments (or for multigraph,
an additional key argument) and returns True to keep the edge.

This seems cleaner than the GraphView of #2512 and can hide nodes easily by building a filter function: lambda n: n not in hidden_nodes, but can be much more involved.

def fish_nodes(n):
    return 'fish' in G.nodes[n]
def red_edges(u,v):
    return 'red' == G.edges[u, v]['color']
SG = nx.SubGraph(G, node_filter=fish_nodes, edge_filter=red_edges)

Related tickets include:
#2512
#762
#1011
#793
#335
and there are probably many other places where this feature has been discussed.

@dschult
dschult force-pushed the subgraph_filter branch 2 times, most recently from 40d21b5 to 8d66edf Compare July 25, 2017 21:50
This change also enforces read-only subgraph views
@dschult dschult added this to the networkx-2.0 milestone Jul 27, 2017
@dschult

dschult commented Jul 28, 2017

Copy link
Copy Markdown
Member Author

I'm going to merge this. I think we should consider replacing the G.subgraph method but that should be a different pull request. Initial tests suggest it doesn't save much time. It should save memory but I haven't looked at it yet.

@dschult
dschult merged commit e5f70a4 into networkx:master Jul 28, 2017
@dschult
dschult deleted the subgraph_filter branch July 28, 2017 21:56
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.

2 participants