diff options
-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)), []) |