当前位置: 首页>>代码示例>>Python>>正文


Python Graph.add_edge方法代码示例

本文整理汇总了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.')
开发者ID:informaticienzero,项目名称:Graphs,代码行数:9,代码来源:test_graphs.py

示例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
开发者ID:wrongu,项目名称:sampling-dynamics,代码行数:12,代码来源:models.py

示例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.')
开发者ID:informaticienzero,项目名称:Graphs,代码行数:12,代码来源:test_graphs.py

示例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.')
开发者ID:informaticienzero,项目名称:Graphs,代码行数:14,代码来源:test_graphs.py

示例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)
开发者ID:nezaj,项目名称:Reference,代码行数:21,代码来源:test_graph.py

示例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)
开发者ID:YuJames,项目名称:Python,代码行数:33,代码来源:searching.py


注:本文中的graphs.Graph.add_edge方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。