def test_add_edge(): test = Graph() test.add_node(5) test.add_node(42) test.add_edge(5, 42) assert 42 in test.graph_dict[5]
def test_multiple_edges(): from simple_graph import Graph new_graph = Graph() for pair in PAIR_LIST: new_graph.add_edge(pair[0], pair[1]) for pair in PAIR_LIST: assert {(pair[0], pair[1]): 1} in new_graph.edges()
def test_df_two_nodes(): from simple_graph import Graph graph = Graph() graph.add_node('a') graph.add_node('b') graph.add_edge('a', 'b') assert graph.depth_first('a') == ['a', 'b']
def test_adjacent(key, value): from simple_graph import Graph new_graph = Graph() node1 = new_graph.add_node(key) node2 = new_graph.add_node(value) new_graph.add_edge(node1, node2) assert new_graph.adjacent(node1, node2) is True
def test_neighbors(): output = ["person", 'are', 77, 6.66778] from simple_graph import Graph new_graph = Graph() for pair in MAP_LIST: new_graph.add_edge(pair[0], pair[1]) for item in output: assert item in new_graph.neighbors(10)
def test_adjacent_true(): """Test if node returns true.""" from simple_graph import Graph graph = Graph() graph.add_node('chicken') graph.add_node('egg') graph.add_edge('egg', 'chicken') assert graph.adjacent('egg', 'chicken') is True
def test_neighbors(): """Return the list of all nodes connect to node.""" from simple_graph import Graph graph = Graph() graph.add_node('chicken') graph.add_node('egg') graph.add_edge('egg', 'chicken') assert graph.get_neighbors('egg') == {'chicken': 0}
def test_add_edge_destination_node_not_exist(): """Test if a node is added if it is the destination and does not exist.""" from simple_graph import Graph graph = Graph() graph.add_node('chicken') graph.add_edge('chicken', 'egg') assert 'egg' in graph.container assert 'egg' in graph.container['chicken']
def test_add_edge(): """Test if there is an edge between two nodes.""" from simple_graph import Graph graph = Graph() graph.add_node('chicken') graph.add_node('egg') graph.add_edge('egg', 'chicken') assert 'chicken' in graph.container['egg']
def test_bf_two_nodes(): """Test breadth first with two nodes.""" from simple_graph import Graph graph = Graph() graph.add_node('a') graph.add_node('b') graph.add_edge('a', 'b') assert graph.breadth_first('a') == ['a', 'b']
def populated(): g = Graph() g.add_node(5) g.add_node(7) g.add_node("Hello") g.add_edge(5, 7, 30) g.add_edge(5, "Hello", 10) return g
def test_del_edge(node, result): """Test to check the deleted edges aren't there.""" from simple_graph import Graph g = Graph() for idx in node: g.add_node(idx) g.add_edge(2, 3) g.add_edge(1, 4) assert g.del_edge(1, 4) == result
def test_edge(node, result): """Test to check if all edges are there.""" from simple_graph import Graph g = Graph() for idx in node: g.add_node(idx) g.add_edge(2, 3) g.add_edge(1, 4) assert g.edges() == result
def test_delete_edge_in_graph(): """Test if an edge between two nodes is removed.""" from simple_graph import Graph graph = Graph() graph.add_node('chicken') graph.add_node('egg') graph.add_edge('egg', 'chicken') graph.delete_edge('egg', 'chicken') assert graph.container['egg'] == {}
def test_neighbor(node, n, result): """Test to check that the correct node have the right edge.""" from simple_graph import Graph g = Graph() for idx in node: g.add_node(idx) g.add_edge(1, 1) g.add_edge(1, 4) g.add_edge(4, 2) g.add_edge(3, 5) assert g.neighbors(n) == result
def test_bf_three_nodes(): """Test breadth first with three nodes.""" from simple_graph import Graph graph = Graph() graph.add_node('a') graph.add_node('b') graph.add_node('c') graph.add_edge('a', 'b') graph.add_edge('a', 'c') print(graph.container) assert graph.breadth_first('a') == ['a', 'b', 'c']
def test_df_three_nodes(): """Test if depth first with four nodes.""" from simple_graph import Graph graph = Graph() graph.add_node('a') graph.add_node('b') graph.add_node('d') graph.add_edge('a', 'b') graph.add_node('c') graph.add_edge('b', 'c') graph.add_edge('b', 'd') assert graph.depth_first('a') == ['a', 'b', 'c', 'd']
def test_loop_weighted_graph(): test_graph = Graph() test_graph.add_edge("A", "B", 1) test_graph.add_edge("B", "C", 2) test_graph.add_edge("B", "E", 7) test_graph.add_edge("C", "B", 4) test_graph.add_edge("C", "D", 6) test_graph.add_edge("D", "C", 1) test_graph.add_edge("D", "E", 7) test_graph.add_edge("E", "D", 3) test_graph.add_edge("E", "B", 10) return test_graph
def cyclic_graph(): g = Graph() g.add_node(3) g.add_node(10) g.add_node(5) g.add_node(2) g.add_node(7) g.add_edge(3, 10, 1) g.add_edge(3, 5, 2) g.add_edge(10, 5, 3) g.add_edge(5, 2, 4) g.add_edge(2, 7, 5) g.add_edge(5, 3, 5) return g
def test_bf_five_nodes(): """Test breadth first with 5 nodes.""" from simple_graph import Graph graph = Graph() graph.add_node('a') graph.add_node('b') graph.add_node('c') graph.add_node('d') graph.add_node('e') graph.add_edge('a', 'b') graph.add_edge('a', 'c') graph.add_edge('a', 'd') graph.add_edge('b', 'e') assert graph.breadth_first('a') == ['a', 'b', 'c', 'd', 'e']
def rebuild_graph(self): ''' create a new graph to merge communities to vertices ''' graph = Graph() for u, v in self.G.edges: u_c = self.nc_map[u] v_c = self.nc_map[v] weight = self.G.edge_weight(u, v) edge = graph.edge(u_c, v_c) if edge: edge.weight += weight else: graph.add_edge(u_c, v_c, weight=weight) self.G = graph
def test_depth_traversal_graph(): test_graph = Graph() for i in range(10): test_graph.add_node(i) test_graph.add_edge(0, 1) test_graph.add_edge(0, 2) test_graph.add_edge(0, 3) test_graph.add_edge(1, 4) test_graph.add_edge(1, 5) test_graph.add_edge(1, 8) test_graph.add_edge(5, 6) test_graph.add_edge(6, 7) test_graph.add_edge(2, 9) return test_graph
def test_edge_weight(self): G = Graph({0: [1, 2], 1: [2]}) self.assertEqual(G.total_edge_weight(1), 2) self.assertEqual(G.total_edge_weight(), 6) G = Graph({ 1: { 1: { 'weight': 6 }, 2: { 'weight': 2 }, 0: { 'weight': 2 } }, 2: { 1: { 'weight': 2 }, 2: { 'weight': 6 }, 0: { 'weight': 2 } }, 0: { 1: { 'weight': 2 }, 2: { 'weight': 2 }, 0: { 'weight': 6 } } }) self.assertEqual(G.total_edge_weight(), 30) self.assertEqual(G.total_edge_weight(1), 10) G = Graph(undirected=False) G.add_edge(1, 2) self.assertEqual(G.total_edge_weight(1), 0) self.assertEqual(G.total_edge_weight(), 1) self.assertEqual(G.total_edge_weight(2), 1) self.assertEqual(G.total_edge_weight(1, 'out'), 1) self.assertEqual(G.total_edge_weight(1, 'all'), 1)
def negative_graph(): g = Graph() g.add_edge(1, 2, 10) g.add_edge(1, 3, -4) g.add_edge(2, 4, 6) g.add_edge(3, 4, 9) g.add_edge(4, 5, 8) g.add_edge(2, 5, -6) return g
def full_graph(): g = Graph() g.add_node(3) g.add_node(10) g.add_node(15) g.add_node(7) g.add_node(14) g.add_edge(3, 7, 1) g.add_edge(15, 10, 2) g.add_edge(14, 3, 3) g.add_edge(3, 10, 4) g.add_edge(7, 15, 5) return g
def test_remove_vertex(self): G = Graph(undirected=False) G.add_edge(1, 2) G.remove_vertex(1) self.assertEqual(set(G.vertices), {2}) G.remove_edge(1, 2) G = Graph({ 'V': [ '1', '2', '0', '4', '3', '7', '6', '5', '11', '10', '8', '15', '14', '9', '12', '13' ], 'E': [('1', '2'), ('1', '4'), ('1', '7'), ('2', '0'), ('2', '4'), ('2', '6'), ('0', '3'), ('0', '5'), ('7', '5'), ('7', '6'), ('5', '11'), ('4', '10'), ('8', '15'), ('8', '14'), ('8', '9'), ('14', '9'), ('9', '12'), ('10', '14'), ('10', '13'), ('11', '10'), ('6', '11'), ('3', '7')] }) G.remove_vertex('1') self.assertNotIn('1', G.vertices) self.assertNotIn(('1', '2'), G.edges) self.assertNotIn(('1', '4'), G.edges) self.assertNotIn(('1', '7'), G.edges) G.remove_vertex('4') self.assertNotIn('4', G.vertices) self.assertNotIn(('2', '4'), G.edges) self.assertNotIn(('4', '10'), G.edges) G = Graph({ 'E': { "a": ["d"], "b": ["c"], "c": ["b", "c", "d", "e"], "d": ["a", "c"], "e": ["c"], "f": [] } }) G.remove_vertex('a') G.remove_vertex('c') self.assertEqual(set(G.vertices), {'d', 'b', 'e', 'f'}) self.assertEqual(G.edges, [])
def test_vertices(self): G = Graph({ 1: { 1: { 'weight': 6 }, 2: { 'weight': 2 }, 0: { 'weight': 2 } }, 2: { 1: { 'weight': 2 }, 2: { 'weight': 6 }, 0: { 'weight': 2 } }, 0: { 1: { 'weight': 2 }, 2: { 'weight': 2 }, 0: { 'weight': 6 } } }) self.assertEqual(set(G.vertices), {1, 2, 0}) G = Graph(undirected=False) G.add_edge(1, 2) self.assertEqual(set(G.vertices), {1, 2})
def test_breadth_traversal_graph(): test_graph = Graph() for i in range(1, 10): test_graph.add_node(i) test_graph.add_edge(1, 2) test_graph.add_edge(1, 3) test_graph.add_edge(1, 4) test_graph.add_edge(2, 5) test_graph.add_edge(2, 6) test_graph.add_edge(4, 7) test_graph.add_edge(4, 8) test_graph.add_edge(5, 9) return test_graph
def test_weighted_graph(): test_graph = Graph() test_graph.add_edge("A", "B", 2) test_graph.add_edge("A", "C", 5) test_graph.add_edge("B", "C", 2) test_graph.add_edge("B", "D", 6) test_graph.add_edge("C", "D", 2) return test_graph
def non_multi_connected_nodes(): ''' Define a graph with no multi-connected nodes. ''' g = Graph() g.add_edge("A", "B") g.add_edge("A", "C") g.add_edge("A", "E") g.add_edge("B", "D") g.add_edge("B", "F") g.add_edge("C", "G") return g
def test_graph(): test_graph = Graph() test_graph.add_node(5) test_graph.add_node(42) test_graph.add_node("test") test_graph.add_edge(5, 42) test_graph.add_edge(42, "test", 1000) test_graph.add_edge("test", 5) return test_graph
def test_depth_multiple_edges(): test = Graph() for i in range(5): test.add_node(i) test.add_edge(0, 1) test.add_edge(0, 2) test.add_edge(1, 2) test.add_edge(1, 3) test.add_edge(2, 3) test.add_edge(3, 4) assert test.depth_first_traversal(0) == [0, 1, 2, 3, 4]
def test_shortest_path_Bellman(): """Return the shortest path.""" from simple_graph import Graph graph = Graph() graph.add_node('a') graph.add_node('b') graph.add_node('c') graph.add_node('d') graph.add_edge('a', 'b', 5) graph.add_edge('a', 'c', 10) graph.add_edge('b', 'd', 8) graph.add_edge('c', 'd', 4) assert graph.shortest_path('a') == ['a', 'b', 'd']
def test_df_three_nodes_complex(): """Test depth first with even more nodes.""" from simple_graph import Graph graph = Graph() graph.add_node('a') graph.add_node('b') graph.add_node('c') graph.add_node('d') graph.add_edge('a', 'c') graph.add_edge('a', 'd') graph.add_edge('b', 'c') graph.add_edge('b', 'a') graph.add_edge('d', 'b') assert graph.depth_first('a') == ['a', 'c', 'd', 'b']
def test_add_edge(): g = Graph() g.add_node(5) g.add_node(10) g.add_edge(5, 10, 6) assert g.gdict[5] == {10: 6}
def test_edges(key, value): from simple_graph import Graph new_graph = Graph() new_graph.add_edge(key, value) assert new_graph.edges() == [{(key, value): 1}]
def test_del_edge(key, value): from simple_graph import Graph new_graph = Graph() new_graph.add_edge(key, value) new_graph.del_edge(key, value) assert new_graph.node_map[key][value] == 0
def cyclic(): g = Graph() g.add_node(1) g.add_node(2) g.add_node(3) g.add_node(4) g.add_node(5) g.add_node(6) g.add_node(7) g.add_node(8) g.add_node(9) g.add_edge(1, 2, 1) g.add_edge(1, 3, 3) g.add_edge(1, 6, 3) g.add_edge(5, 1, 10) g.add_edge(2, 8, 1000) g.add_edge(2, 9, 1) g.add_edge(2, 4, 4) g.add_edge(2, 3, 50) g.add_edge(3, 7, 4) g.add_edge(7, 1, 5) g.add_edge(7, 6, 10) return g