diff options
author | Jeffrey Finkelstein <jeffrey.finkelstein@gamalon.com> | 2017-02-26 15:36:56 -0500 |
---|---|---|
committer | Jeffrey Finkelstein <jeffrey.finkelstein@gamalon.com> | 2017-02-26 15:36:56 -0500 |
commit | 36d6c45e7d0407ed5a8018ec3d1b2f949982fdea (patch) | |
tree | 236053934625a907a3fe81230733f27e66acfcbe | |
parent | ebed70ecf0798c21420dcbe1bd2bb231dd5a4635 (diff) | |
download | networkx-digraph-edge-subgraph.tar.gz |
Update predecessors/successors in edge subgraphdigraph-edge-subgraph
-rw-r--r-- | networkx/classes/digraph.py | 11 | ||||
-rw-r--r-- | networkx/classes/tests/test_digraph.py | 14 |
2 files changed, 20 insertions, 5 deletions
diff --git a/networkx/classes/digraph.py b/networkx/classes/digraph.py index 9e086536..df8f7260 100644 --- a/networkx/classes/digraph.py +++ b/networkx/classes/digraph.py @@ -1324,16 +1324,17 @@ class DiGraph(Graph): for u, v in edges: # Copy the node attributes if they haven't been copied # already. + # + # Also, create an entry in the successors and predecessors + # dictionary for the nodes u and v. if u not in H.node: H.node[u] = self.node[u] + H.pred[u] = H.adjlist_inner_dict_factory() + H.succ[u] = H.adjlist_inner_dict_factory() if v not in H.node: H.node[v] = self.node[v] - # Create an entry in the successors and predecessors - # dictionary for the nodes u and v if they don't exist yet. - if u not in H.succ: - H.succ[u] = H.adjlist_inner_dict_factory() - if v not in H.pred: H.pred[v] = H.adjlist_inner_dict_factory() + H.succ[v] = H.adjlist_inner_dict_factory() # Copy the edge attributes. H.edge[u][v] = self.edge[u][v] H.pred[v][u] = self.pred[v][u] diff --git a/networkx/classes/tests/test_digraph.py b/networkx/classes/tests/test_digraph.py index 64f8855a..eee86e37 100644 --- a/networkx/classes/tests/test_digraph.py +++ b/networkx/classes/tests/test_digraph.py @@ -260,3 +260,17 @@ class TestEdgeSubgraph(TestGraphEdgeSubgraph): # Get the subgraph induced by the first and last edges. self.G = G self.H = G.edge_subgraph([(0, 1), (3, 4)]) + + def test_pred_succ(self): + """Test that nodes are added to predecessors and successors. + + For more information, see GitHub issue #2370. + + """ + G = networkx.DiGraph() + G.add_edge(0, 1) + H = G.edge_subgraph([(0, 1)]) + assert_equal(list(H.predecessors(0)), []) + assert_equal(list(H.successors(0)), [1]) + assert_equal(list(H.predecessors(1)), [0]) + assert_equal(list(H.successors(1)), []) |