本文整理汇总了Python中src.graph.Graph类的典型用法代码示例。如果您正苦于以下问题:Python Graph类的具体用法?Python Graph怎么用?Python Graph使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Graph类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: test_dijkstra_simple
def test_dijkstra_simple(self):
graph = Graph('test/netfiles/test1.net.xml', False)
costs = {}
for edge in graph.edges:
costs[edge] = 1
graph.set_destination_and_costs(graph.edges[-1], costs)
self.assertEqual(graph.dijkstra(graph.edges[1], costs), ['-25','5'])
开发者ID:OR2013,项目名称:OnlinePathOptimalization,代码行数:7,代码来源:graph_test.py
示例2: test_dijkstra_complex
def test_dijkstra_complex(self):
graph = Graph('test/netfiles/test4.net.xml', False)
costs = {}
for edge in graph.edges:
costs[edge] = 1
graph.set_destination_and_costs('30668', costs)
self.assertEqual(graph.dijkstra('-31439', costs), ['-31439','-30057','-28887','28866','-23377','-22520','-21950','-17064','-16421','-15637','-3694','-3348','-158','144','-212','213','-370','420','-809','1807','-2481','2488','2556','-3837','3836','4158','4982','12338','-20947','-20171','20161','20824','-22781','-22135','22133','22780','27742','29467','30668'])
开发者ID:OR2013,项目名称:OnlinePathOptimalization,代码行数:7,代码来源:graph_test.py
示例3: test_get_vertices_for_directed_graph
def test_get_vertices_for_directed_graph(self):
g = Graph(True)
g.add_edge((1,2))
actual = g.get_vertices()
expected = [1,2]
self.assertEqual(actual, expected, 'should return all vertices')
开发者ID:mindis,项目名称:learn,代码行数:7,代码来源:test_graph.py
示例4: test_set_edge_value_if_edge_exists
def test_set_edge_value_if_edge_exists(self):
g = Graph(True)
g.add_edge((1,2,10))
g.set_edge_value((1,2), 20)
self.assertEqual(g.table[1][2], 20,
'should have updated the edge value')
开发者ID:mindis,项目名称:learn,代码行数:7,代码来源:test_graph.py
示例5: test_add_users_to_a_graph
def test_add_users_to_a_graph(self):
graph = Graph()
user = User()
user_id = user.get_id()
graph.add_user(user)
assert user_id in graph.users and graph.users[user_id] == user
开发者ID:ehyoon,项目名称:ka-interview,代码行数:7,代码来源:test_classes.py
示例6: test_set_edge_value_works_for_undirected_graphs
def test_set_edge_value_works_for_undirected_graphs(self):
g = Graph(False)
g.add_edge((1,2,10))
g.set_edge_value((1,2), 30)
self.assertEqual(g.table[1][2], 30, 'has updated the edge')
self.assertEqual(g.table[2][1], 30, 'reverse edge was updated too')
开发者ID:mindis,项目名称:learn,代码行数:7,代码来源:test_graph.py
示例7: test_split_edge_with_value
def test_split_edge_with_value(self):
edge = (1,2,3)
g = Graph()
(tail, head, value) = g.split_edge(edge)
self.assertEqual(tail, 1)
self.assertEqual(head, 2)
self.assertEqual(value, 3)
开发者ID:mindis,项目名称:learn,代码行数:8,代码来源:test_graph.py
示例8: test_incident_in_directed_graph
def test_incident_in_directed_graph(self):
g = Graph(directed = True)
g.add_edge((2,1))
g.add_edge((3,1))
actual = g.incident(1)
expected = [2, 3]
self.assertEqual(actual, expected, '1 has two incident vertexes')
开发者ID:mindis,项目名称:learn,代码行数:8,代码来源:test_graph.py
示例9: test_get_edge_value_in_directed_graph
def test_get_edge_value_in_directed_graph(self):
g = Graph(True)
g.add_edge((1,2,3))
self.assertEqual(g.get_edge_value((1,2)), 3,
'should have stored value')
self.assertIsNone(g.get_edge_value((2,1)),
'should have no value for reverse edge')
开发者ID:mindis,项目名称:learn,代码行数:8,代码来源:test_graph.py
示例10: test_set_edge_value_if_graph_is_directed_and_inverse_edge_exists
def test_set_edge_value_if_graph_is_directed_and_inverse_edge_exists(self):
g = Graph(True)
g.add_edge((1,2,10))
g.add_edge((2,1,20))
g.set_edge_value((1,2), 30)
self.assertEqual(g.table[1][2], 30, 'has updated the edge')
self.assertEqual(g.table[2][1], 20, 'reverse edge remained the same')
开发者ID:mindis,项目名称:learn,代码行数:8,代码来源:test_graph.py
示例11: test_add_edge_multiple_times_nothing_happens
def test_add_edge_multiple_times_nothing_happens(self):
g = Graph(True)
g.add_edge((1,2,3))
g.add_edge((1,2,3))
g.add_edge((1,2,3))
g.add_edge((1,2,3))
self.assertIn(1, g.table, 'should have stored the tail in table')
self.assertIn(2, g.table[1], 'should have stored the head in table')
开发者ID:mindis,项目名称:learn,代码行数:9,代码来源:test_graph.py
示例12: test_connect_users_in_a_graph
def test_connect_users_in_a_graph(self):
graph = Graph()
user1 = User()
user2 = User()
graph.add_user(user1)
graph.add_user(user2)
graph.connect_users(user1.get_id(), user2.get_id())
assert user2 in user1.get_connections() and user1 in user2.get_connections()
开发者ID:ehyoon,项目名称:ka-interview,代码行数:9,代码来源:test_classes.py
示例13: test_add_edge_for_directed_graph
def test_add_edge_for_directed_graph(self):
g = Graph(True)
g.add_edge((1,2,3))
self.assertIn(1, g.table, 'should have stored a key for tail vertex')
self.assertIn(2, g.table[1], 'should have stored the edge')
self.assertEqual(g.table[1][2], 3, 'should have stored the edge value')
self.assertIn(2, g.table, 'should not have stored the node')
self.assertNotIn(1, g.table[2], 'should not have stored the reverse edge')
开发者ID:mindis,项目名称:learn,代码行数:10,代码来源:test_graph.py
示例14: constructor
def constructor(M, sigma, kind='log'):
g = Graph()
for i in range(len(M)):
for j in range(len(M[i])):
for p in range(len(M)):
for q in range(len(M[i])):
if p != i or q != j:
g.add_edge(get_name(i, j), get_name(p, q), weight(M, [i, j], [p, q], kind, sigma))
g.normalize()
return g
开发者ID:162,项目名称:gbvs-justgecko,代码行数:10,代码来源:constructor.py
示例15: test_add_edge_for_undirected_graph
def test_add_edge_for_undirected_graph(self):
g = Graph(False)
g.add_edge((1,2,3))
self.assertIn(1, g.table, 'should have stored a key for tail vertex')
self.assertIn(2, g.table[1], 'should have stored the edge')
self.assertEqual(g.table[1][2], 3, 'should have stored the edge value')
self.assertIn(2, g.table, 'should have stored a key for head vertex')
self.assertIn(1, g.table[2], 'should have stored the reversed edge')
self.assertEqual(g.table[2][1], 3, 'should have stored the edge value')
开发者ID:mindis,项目名称:learn,代码行数:11,代码来源:test_graph.py
示例16: test_neighbours_in_directed_graph
def test_neighbours_in_directed_graph(self):
g = Graph(True)
g.add_edge((1,2))
g.add_edge((1,3))
expected = [2, 3]
actual = g.neighbours(1)
self.assertEqual(actual, expected, 'should find neighbours of 1')
expected = []
actual = g.neighbours(2)
self.assertEqual(actual, expected, '2 has no neighbours')
开发者ID:mindis,项目名称:learn,代码行数:12,代码来源:test_graph.py
示例17: test_is_optimalization_needed
def test_is_optimalization_needed(self):
graph = Graph('test/netfiles/test1.net.xml', False)
graph.edges = ['1', '2', '3', '4', '5']
graph.set_destination_and_costs('5', {'1':10, '2':10, '3':10, '4':10, '5':10})
self.assertTrue(graph.is_optimalization_needed(['1','3','5'], {'1':10, '2':10, '3':12, '4':10, '5':10}, 0.1))
self.assertTrue(graph.is_optimalization_needed(['1','3','5'], {'1':10, '2':8, '3':10, '4':10, '5':10}, 0.1))
self.assertFalse(graph.is_optimalization_needed(['1','3','5'], {'1':10, '2':10, '3':8, '4':10, '5':10}, 0.1))
self.assertFalse(graph.is_optimalization_needed(['1','3','5'], {'1':10, '2':12, '3':10, '4':10, '5':10}, 0.1))
开发者ID:OR2013,项目名称:OnlinePathOptimalization,代码行数:8,代码来源:graph_test.py
示例18: test_scc
def test_scc(self):
""" Given the following graph it computes SCCs in it.
/>(2)---------->(8)----------v
/ | \ /->(10)
(1) | v / |
^\ v /---->(9)<-\ v
\(3)----->(4) \-(11)
\ ^| \ /^
\ / | v /
>(5) | (7)----/
^\ | /
\v v
(6)
"""
g = Graph.build(edges=[(1,2), (3,2), (2,3), (3,4), (3,5), (5,4),
(4,6), (4,7), (7,6), (6,5), (4,9), (7, 11),
(11,9), (9,10), (10,11), (2,8), (8,9), (8,10)],
directed=True)
connected_components = scc(g)
expected = [[8], [1], [2, 3], [9, 10, 11], [4, 5, 6, 7]]
self.assertEqual(len(connected_components), len(expected),
'should return the same number of components')
for component in connected_components:
self.assertIn(component, expected,
'should detect strongly connected components')
开发者ID:mindis,项目名称:learn,代码行数:27,代码来源:test_strongly_connected_components.py
示例19: test_johnson
def test_johnson(self):
""" Given the following graph from the lecture:
-2
(a)-------->(b)
^| |
| |
4\ -1/
\ /
\(c)</
/ \
2/ \-3
v/ \v
(x) (y)
\^ /^
\ /
1\ /-4
\ /
(z)
"""
g = Graph.build(edges=[('a', 'b', -2), ('c', 'a', 4), ('b', 'c', -1),
('c', 'x', 2), ('c', 'y', -3), ('z', 'x', 1), ('z', 'y', -4)],
directed=True)
actual = johnson(g)
expected = {
'a': {'a': 0, 'c': -3, 'b': -2, 'y': -6, 'x': -1, 'z': INF},
'c': {'a': 4, 'c': 0, 'b': 2, 'y': -3, 'x': 2, 'z': INF},
'b': {'a': 3, 'c': -1, 'b': 0, 'y': -4, 'x': 1, 'z': INF},
'y': {'a': INF, 'c': INF, 'b': INF, 'y': 0, 'x': INF, 'z': INF},
'x': {'a': INF, 'c': INF, 'b': INF, 'y': INF, 'x': 0, 'z': INF},
'z': {'a': INF, 'c': INF, 'b': INF, 'y': -4, 'x': 1, 'z': 0}
}
self.assertEqual(actual, expected,
'should return correct all pairs shortest path distances')
开发者ID:mindis,项目名称:learn,代码行数:34,代码来源:test_all_pairs_shortest_paths.py
示例20: setUp
def setUp(self):
self.graph = Graph()
self.user1 = User()
self.user2 = User()
self.user3 = User()
self.user4 = User()
self.users = [self.user1, self.user2, self.user3, self.user4]
开发者ID:ehyoon,项目名称:ka-interview,代码行数:7,代码来源:test_infection.py
注:本文中的src.graph.Graph类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论