<s> from nose . tools import * <EOL> import networkx as nx <EOL> class TestDFS : <EOL> def setUp ( self ) : <EOL> G = nx . Graph ( ) <EOL> G . add_edges_from ( [ ( <NUM_LIT:0> , <NUM_LIT:1> ) , ( <NUM_LIT:1> , <NUM_LIT:2> ) , ( <NUM_LIT:1> , <NUM_LIT:3> ) , ( <NUM_LIT:2> , <NUM_LIT:4> ) , ( <NUM_LIT:3> , <NUM_LIT:4> ) ] ) <EOL> self . G = G <EOL> D = nx . Graph ( ) <EOL> D . add_edges_from ( [ ( <NUM_LIT:0> , <NUM_LIT:1> ) , ( <NUM_LIT:2> , <NUM_LIT:3> ) ] ) <EOL> self . D = D <EOL> def test_preorder_nodes ( self ) : <EOL> assert_equal ( list ( nx . dfs_preorder_nodes ( self . G , source = <NUM_LIT:0> ) ) , <EOL> [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:4> , <NUM_LIT:3> ] ) <EOL> assert_equal ( list ( nx . dfs_preorder_nodes ( self . D ) ) , [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> def test_postorder_nodes ( self ) : <EOL> assert_equal ( list ( nx . dfs_postorder_nodes ( self . G , source = <NUM_LIT:0> ) ) , <EOL> [ <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:2> , <NUM_LIT:1> , <NUM_LIT:0> ] ) <EOL> assert_equal ( list ( nx . dfs_postorder_nodes ( self . D ) ) , [ <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:3> , <NUM_LIT:2> ] ) <EOL> def test_successor ( self ) : <EOL> assert_equal ( nx . dfs_successors ( self . G , source = <NUM_LIT:0> ) , <EOL> { <NUM_LIT:0> : [ <NUM_LIT:1> ] , <NUM_LIT:1> : [ <NUM_LIT:2> ] , <NUM_LIT:2> : [ <NUM_LIT:4> ] , <NUM_LIT:4> : [ <NUM_LIT:3> ] } ) <EOL> assert_equal ( nx . dfs_successors ( self . D ) , { <NUM_LIT:0> : [ <NUM_LIT:1> ] , <NUM_LIT:2> : [ <NUM_LIT:3> ] } ) <EOL> def test_predecessor ( self ) : <EOL> assert_equal ( nx . dfs_predecessors ( self . G , source = <NUM_LIT:0> ) , <EOL> { <NUM_LIT:1> : <NUM_LIT:0> , <NUM_LIT:2> : <NUM_LIT:1> , <NUM_LIT:3> : <NUM_LIT:4> , <NUM_LIT:4> : <NUM_LIT:2> } ) <EOL> assert_equal ( nx . dfs_predecessors ( self . D ) , { <NUM_LIT:1> : <NUM_LIT:0> , <NUM_LIT:3> : <NUM_LIT:2> } ) <EOL> def test_dfs_tree ( self ) : <EOL> T = nx . dfs_tree ( self . G , source = <NUM_LIT:0> ) <EOL> assert_equal ( sorted ( T . nodes ( ) ) , sorted ( self . G . nodes ( ) ) ) <EOL> assert_equal ( sorted ( T . edges ( ) ) , [ ( <NUM_LIT:0> , <NUM_LIT:1> ) , ( <NUM_LIT:1> , <NUM_LIT:2> ) , ( <NUM_LIT:2> , <NUM_LIT:4> ) , ( <NUM_LIT:4> , <NUM_LIT:3> ) ] ) <EOL> def test_dfs_edges ( self ) : <EOL> edges = nx . dfs_edges ( self . G , source = <NUM_LIT:0> ) <EOL> assert_equal ( list ( edges ) , [ ( <NUM_LIT:0> , <NUM_LIT:1> ) , ( <NUM_LIT:1> , <NUM_LIT:2> ) , ( <NUM_LIT:2> , <NUM_LIT:4> ) , ( <NUM_LIT:4> , <NUM_LIT:3> ) ] ) <EOL> edges = nx . dfs_edges ( self . D ) <EOL> assert_equal ( list ( edges ) , [ ( <NUM_LIT:0> , <NUM_LIT:1> ) , ( <NUM_LIT:2> , <NUM_LIT:3> ) ] ) <EOL> def test_dfs_labeled_edges ( self ) : <EOL> edges = list ( nx . dfs_labeled_edges ( self . G , source = <NUM_LIT:0> ) ) <EOL> forward = [ ( u , v ) for ( u , v , d ) in edges if d [ '<STR_LIT>' ] == '<STR_LIT>' ] <EOL> assert_equal ( forward , [ ( <NUM_LIT:0> , <NUM_LIT:0> ) , ( <NUM_LIT:0> , <NUM_LIT:1> ) , ( <NUM_LIT:1> , <NUM_LIT:2> ) , ( <NUM_LIT:2> , <NUM_LIT:4> ) , ( <NUM_LIT:4> , <NUM_LIT:3> ) ] ) <EOL> def test_dfs_labeled_disconnected_edges ( self ) : <EOL> edges = list ( nx . dfs_labeled_edges ( self . D ) ) <EOL> forward = [ ( u , v ) for ( u , v , d ) in edges if d [ '<STR_LIT>' ] == '<STR_LIT>' ] <EOL> assert_equal ( forward , [ ( <NUM_LIT:0> , <NUM_LIT:0> ) , ( <NUM_LIT:0> , <NUM_LIT:1> ) , ( <NUM_LIT:2> , <NUM_LIT:2> ) , ( <NUM_LIT:2> , <NUM_LIT:3> ) ] ) <EOL> def test_dfs_tree_isolates ( self ) : <EOL> G = nx . Graph ( ) <EOL> G . add_node ( <NUM_LIT:1> ) <EOL> G . add_node ( <NUM_LIT:2> ) <EOL> T = nx . dfs_tree ( G , source = <NUM_LIT:1> ) <EOL> assert_equal ( sorted ( T . nodes ( ) ) , [ <NUM_LIT:1> ] ) <EOL> assert_equal ( sorted ( T . edges ( ) ) , [ ] ) <EOL> T = nx . dfs_tree ( G , source = None ) <EOL> assert_equal ( sorted ( T . nodes ( ) ) , [ <NUM_LIT:1> , <NUM_LIT:2> ] ) <EOL> assert_equal ( sorted ( T . edges ( ) ) , [ ] ) </s>
