-
Notifications
You must be signed in to change notification settings - Fork 167
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add to_undirected method for PyDiGraph (#161)
This commit adds a new method to the PyDiGraph class, to_undirected(), which will generate an undirected PyGraph object from the PyDiGraph object. Fixes #153
- Loading branch information
Showing
2 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# Licensed under the Apache License, Version 2.0 (the "License"); you may | ||
# not use this file except in compliance with the License. You may obtain | ||
# a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
# License for the specific language governing permissions and limitations | ||
# under the License. | ||
|
||
import unittest | ||
|
||
import retworkx | ||
|
||
|
||
class TestToUndirected(unittest.TestCase): | ||
|
||
def test_to_undirected_empty_graph(self): | ||
digraph = retworkx.PyDiGraph() | ||
graph = digraph.to_undirected() | ||
self.assertEqual(0, len(graph)) | ||
|
||
def test_single_direction_graph(self): | ||
digraph = retworkx.generators.directed_path_graph(5) | ||
graph = digraph.to_undirected() | ||
self.assertEqual(digraph.weighted_edge_list(), | ||
graph.weighted_edge_list()) | ||
|
||
def test_bidirectional_graph(self): | ||
digraph = retworkx.generators.directed_path_graph(5) | ||
for i in range(0, 4): | ||
digraph.add_edge(i + 1, i, None) | ||
graph = digraph.to_undirected() | ||
self.assertEqual(digraph.weighted_edge_list(), | ||
graph.weighted_edge_list()) | ||
|
||
def test_shared_ref(self): | ||
digraph = retworkx.PyDiGraph() | ||
node_weight = {'a': 1} | ||
node_a = digraph.add_node(node_weight) | ||
edge_weight = {'a': 1} | ||
digraph.add_child(node_a, 'b', edge_weight) | ||
graph = digraph.to_undirected() | ||
self.assertEqual(digraph[node_a], {'a': 1}) | ||
self.assertEqual(graph[node_a], {'a': 1}) | ||
node_weight['b'] = 2 | ||
self.assertEqual(digraph[node_a], {'a': 1, 'b': 2}) | ||
self.assertEqual(graph[node_a], {'a': 1, 'b': 2}) | ||
self.assertEqual(digraph.get_edge_data(0, 1), {'a': 1}) | ||
self.assertEqual(graph.get_edge_data(0, 1), {'a': 1}) | ||
edge_weight['b'] = 2 | ||
self.assertEqual(digraph.get_edge_data(0, 1), {'a': 1, 'b': 2}) | ||
self.assertEqual(graph.get_edge_data(0, 1), {'a': 1, 'b': 2}) |