本文整理汇总了Python中graphs.Graph.add_edge方法的典型用法代码示例。如果您正苦于以下问题:Python Graph.add_edge方法的具体用法?Python Graph.add_edge怎么用?Python Graph.add_edge使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类graphs.Graph
的用法示例。
在下文中一共展示了Graph.add_edge方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_add_edge_not_valid_vertex
# 需要导入模块: from graphs import Graph [as 别名]
# 或者: from graphs.Graph import add_edge [as 别名]
def test_add_edge_not_valid_vertex(self):
"""Tests the add_edge method in a graph with a non-existing vertex.
"""
graph = Graph(graph = { 'A' : [] })
graph.add_edge('A', 'B')
self.assertFalse(graph.has_edge('A', 'B'), 'No edge between A and non-existing B.')
示例2: erdos_renyi
# 需要导入模块: from graphs import Graph [as 别名]
# 或者: from graphs.Graph import add_edge [as 别名]
def erdos_renyi(n, p):
g = Graph()
for i in xrange(n):
g.add_node(DiscreteVariable([0,1], name=str(i)))
for i,j in itertools.product(xrange(n), xrange(n)):
if random.random() < p:
g.add_edge(g.get_node_by_name(str(i)),g.get_node_by_name(str(j)))
return g
示例3: test_add_edge_undirected
# 需要导入模块: from graphs import Graph [as 别名]
# 或者: from graphs.Graph import add_edge [as 别名]
def test_add_edge_undirected(self):
"""Tests the add_edge method for an undirected graph.
"""
graph = Graph(graph = { 'A' : [], 'B' : [] })
self.assertFalse(graph.has_edge('A', 'B'), 'There is no edge between A and B.')
graph.add_edge('A', 'B')
self.assertTrue(graph.has_edge('A', 'B'), 'There is now an edge between A and B.')
示例4: test_add_edge_directed
# 需要导入模块: from graphs import Graph [as 别名]
# 或者: from graphs.Graph import add_edge [as 别名]
def test_add_edge_directed(self):
"""Tests the add_edge method for a directed graph.
"""
graph = Graph(graph = { 'A' : [], 'B' : [] }, is_directed = True)
self.assertFalse(graph.has_edge('A', 'B'), 'There is no edge from A to B.')
self.assertFalse(graph.has_edge('B', 'A'), 'There is no edge from B to A.')
graph.add_edge('A', 'B')
self.assertTrue(graph.has_edge('A', 'B'), 'There is now an edge from A to B.')
self.assertFalse(graph.has_edge('B', 'A'), 'There is still no edge from B to A.')
示例5: test_is_connected
# 需要导入模块: from graphs import Graph [as 别名]
# 或者: from graphs.Graph import add_edge [as 别名]
def test_is_connected(self):
# Returns False for an empty graph
v, w = self.v, self.w
g = Graph([v, w], [])
eq_(g.is_connected(), False)
# Returns True for a complete graph
g.add_all_edges()
eq_(g.is_connected(), True)
# Returns False when a new vertex is added
x = Vertex('x')
g.add_vertex(x)
eq_(g.is_connected(), False)
# Returns True when edge is added to connect it
e2 = Edge(w, x)
g.add_edge(e2)
eq_(g.is_connected(), True)
示例6: while
# 需要导入模块: from graphs import Graph [as 别名]
# 或者: from graphs.Graph import add_edge [as 别名]
queue.append(start)
# do BFS
while(len(queue) > 0):
next_node = queue.pop(0)
for neighbor in graph.get_edges(next_node):
if neighbor not in visited:
queue.append(neighbor)
visited.append(next_node)
# print results
return {"reachable_nodes": visited}
# test
if __name__ == "__main__":
my_graph = Graph()
node1 = Node(10, 1)
node2 = Node(13, 2)
node3 = Node(20, 3)
my_graph.add_node(node1)
my_graph.add_edge((node2, node3))
my_graph.add_edge((node3, node1))
my_graph.print_nodes()
my_graph.print_edges()
print breadth_first_search(my_graph, node2)