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


Python networkx.eulerian_circuit方法代码示例

本文整理汇总了Python中networkx.eulerian_circuit方法的典型用法代码示例。如果您正苦于以下问题:Python networkx.eulerian_circuit方法的具体用法?Python networkx.eulerian_circuit怎么用?Python networkx.eulerian_circuit使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在networkx的用法示例。


在下文中一共展示了networkx.eulerian_circuit方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: run_diffusion_process

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import eulerian_circuit [as 别名]
def run_diffusion_process(self, node):
        """
        Generating a diffusion tree from a given source node.
        Linearizing it with an Eulerian tour.
        :param node: Source of diffusion.
        :return euler: Eulerian linear node sequence.
        """
        infected = [node]
        sub_graph = nx.DiGraph()
        sub_graph.add_node(node)
        infected_counter = 1
        while infected_counter < self.number_of_nodes:
            end_point = random.sample(infected, 1)[0]
            nebs = [node for node in self.graph.neighbors(end_point)]
            sample = random.sample(nebs, 1)[0]
            if sample not in infected:
                infected_counter = infected_counter + 1
                infected = infected + [sample]
                sub_graph.add_edges_from([(end_point, sample), (sample, end_point)])
                if infected_counter == self.number_of_nodes:
                    break
        euler = [str(u) for u, v in nx.eulerian_circuit(sub_graph, infected[0])]
        if len(euler) == 0:
            euler = [str(u) for u, v in nx.eulerian_circuit(graph, infected[0])]
        return euler 
开发者ID:benedekrozemberczki,项目名称:diff2vec,代码行数:27,代码来源:diffusiontrees.py

示例2: test_eulerian_circuit_cycle

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import eulerian_circuit [as 别名]
def test_eulerian_circuit_cycle(self):
        G=nx.cycle_graph(4)

        edges=list(eulerian_circuit(G,source=0))
        nodes=[u for u,v in edges]
        assert_equal(nodes,[0,3,2,1])
        assert_equal(edges,[(0,3),(3,2),(2,1),(1,0)])

        edges=list(eulerian_circuit(G,source=1))
        nodes=[u for u,v in edges]
        assert_equal(nodes,[1,2,3,0])
        assert_equal(edges,[(1,2),(2,3),(3,0),(0,1)])

        G=nx.complete_graph(3)
        
        edges=list(eulerian_circuit(G,source=0))
        nodes=[u for u,v in edges]
        assert_equal(nodes,[0,2,1])
        assert_equal(edges,[(0,2),(2,1),(1,0)])
        
        edges=list(eulerian_circuit(G,source=1))
        nodes=[u for u,v in edges]
        assert_equal(nodes,[1,2,0])
        assert_equal(edges,[(1,2),(2,0),(0,1)]) 
开发者ID:SpaceGroupUCL,项目名称:qgisSpaceSyntaxToolkit,代码行数:26,代码来源:test_euler.py

示例3: test_eulerian_circuit_cycle

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import eulerian_circuit [as 别名]
def test_eulerian_circuit_cycle(self):
        G = nx.cycle_graph(4)

        edges = list(eulerian_circuit(G, source=0))
        nodes = [u for u, v in edges]
        assert_equal(nodes, [0, 3, 2, 1])
        assert_equal(edges, [(0, 3), (3, 2), (2, 1), (1, 0)])

        edges = list(eulerian_circuit(G, source=1))
        nodes = [u for u, v in edges]
        assert_equal(nodes, [1, 2, 3, 0])
        assert_equal(edges, [(1, 2), (2, 3), (3, 0), (0, 1)])

        G = nx.complete_graph(3)

        edges = list(eulerian_circuit(G, source=0))
        nodes = [u for u, v in edges]
        assert_equal(nodes, [0, 2, 1])
        assert_equal(edges, [(0, 2), (2, 1), (1, 0)])

        edges = list(eulerian_circuit(G, source=1))
        nodes = [u for u, v in edges]
        assert_equal(nodes, [1, 2, 0])
        assert_equal(edges, [(1, 2), (2, 0), (0, 1)]) 
开发者ID:holzschu,项目名称:Carnets,代码行数:26,代码来源:test_euler.py

示例4: _run_diffusion_process

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import eulerian_circuit [as 别名]
def _run_diffusion_process(self, node):
        """
        Generating a diffusion tree from a given source node and linearizing it
        with a directed Eulerian tour.

        Arg types:
            * **node** *(int)* - The source node of the diffusion.
        Return types:
            * **euler** *(list of strings)* - The list of nodes in the walk.
        """
        infected = [node]
        sub_graph = nx.DiGraph()
        sub_graph.add_node(node)
        infected_counter = 1
        while infected_counter < self.diffusion_cover:
            end_point = random.sample(infected, 1)[0]
            nebs = [node for node in self.graph.neighbors(end_point)]
            sample = random.choice(nebs)
            if sample not in infected:
                infected_counter = infected_counter + 1
                infected = infected + [sample]
                sub_graph.add_edges_from([(end_point, sample), (sample, end_point)])
                if infected_counter == self.diffusion_cover:
                    break
        euler = [str(u) for u, v in nx.eulerian_circuit(sub_graph, infected[0])]
        return euler 
开发者ID:benedekrozemberczki,项目名称:karateclub,代码行数:28,代码来源:diffuser.py

示例5: test_eulerian_circuit_digraph

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import eulerian_circuit [as 别名]
def test_eulerian_circuit_digraph(self):
        G=nx.DiGraph()
        G.add_cycle([0,1,2,3])

        edges=list(eulerian_circuit(G,source=0))
        nodes=[u for u,v in edges]
        assert_equal(nodes,[0,1,2,3])
        assert_equal(edges,[(0,1),(1,2),(2,3),(3,0)])

        edges=list(eulerian_circuit(G,source=1))
        nodes=[u for u,v in edges]
        assert_equal(nodes,[1,2,3,0])
        assert_equal(edges,[(1,2),(2,3),(3,0),(0,1)]) 
开发者ID:SpaceGroupUCL,项目名称:qgisSpaceSyntaxToolkit,代码行数:15,代码来源:test_euler.py

示例6: test_eulerian_circuit_multigraph

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import eulerian_circuit [as 别名]
def test_eulerian_circuit_multigraph(self):
        G=nx.MultiGraph()
        G.add_cycle([0,1,2,3])
        G.add_edge(1,2)
        G.add_edge(1,2)
        edges=list(eulerian_circuit(G,source=0))
        nodes=[u for u,v in edges]
        assert_equal(nodes,[0,3,2,1,2,1])
        assert_equal(edges,[(0,3),(3,2),(2,1),(1,2),(2,1),(1,0)]) 
开发者ID:SpaceGroupUCL,项目名称:qgisSpaceSyntaxToolkit,代码行数:11,代码来源:test_euler.py

示例7: test_not_eulerian

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import eulerian_circuit [as 别名]
def test_not_eulerian(self):    
        f=list(eulerian_circuit(nx.complete_graph(4))) 
开发者ID:SpaceGroupUCL,项目名称:qgisSpaceSyntaxToolkit,代码行数:4,代码来源:test_euler.py

示例8: test_eulerian_circuit_digraph

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import eulerian_circuit [as 别名]
def test_eulerian_circuit_digraph(self):
        G = nx.DiGraph()
        nx.add_cycle(G, [0, 1, 2, 3])

        edges = list(eulerian_circuit(G, source=0))
        nodes = [u for u, v in edges]
        assert_equal(nodes, [0, 1, 2, 3])
        assert_equal(edges, [(0, 1), (1, 2), (2, 3), (3, 0)])

        edges = list(eulerian_circuit(G, source=1))
        nodes = [u for u, v in edges]
        assert_equal(nodes, [1, 2, 3, 0])
        assert_equal(edges, [(1, 2), (2, 3), (3, 0), (0, 1)]) 
开发者ID:holzschu,项目名称:Carnets,代码行数:15,代码来源:test_euler.py

示例9: test_multigraph

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import eulerian_circuit [as 别名]
def test_multigraph(self):
        G = nx.MultiGraph()
        nx.add_cycle(G, [0, 1, 2, 3])
        G.add_edge(1, 2)
        G.add_edge(1, 2)
        edges = list(eulerian_circuit(G, source=0))
        nodes = [u for u, v in edges]
        assert_equal(nodes, [0, 3, 2, 1, 2, 1])
        assert_equal(edges, [(0, 3), (3, 2), (2, 1), (1, 2), (2, 1), (1, 0)]) 
开发者ID:holzschu,项目名称:Carnets,代码行数:11,代码来源:test_euler.py

示例10: test_multigraph_with_keys

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import eulerian_circuit [as 别名]
def test_multigraph_with_keys(self):
        G = nx.MultiGraph()
        nx.add_cycle(G, [0, 1, 2, 3])
        G.add_edge(1, 2)
        G.add_edge(1, 2)
        edges = list(eulerian_circuit(G, source=0, keys=True))
        nodes = [u for u, v, k in edges]
        assert_equal(nodes, [0, 3, 2, 1, 2, 1])
        assert_equal(edges[:2], [(0, 3, 0), (3, 2, 0)])
        assert_count_equal(edges[2:5], [(2, 1, 0), (1, 2, 1), (2, 1, 2)])
        assert_equal(edges[5:], [(1, 0, 0)]) 
开发者ID:holzschu,项目名称:Carnets,代码行数:13,代码来源:test_euler.py

示例11: test_not_eulerian

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import eulerian_circuit [as 别名]
def test_not_eulerian(self):
        f = list(eulerian_circuit(nx.complete_graph(4))) 
开发者ID:holzschu,项目名称:Carnets,代码行数:4,代码来源:test_euler.py

示例12: create_eulerian_circuit

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import eulerian_circuit [as 别名]
def create_eulerian_circuit(graph_augmented, graph_original, start_node=None):
    """
    networkx.eulerian_circuit only returns the order in which we hit each node.  It does not return the attributes of the
    edges needed to complete the circuit.  This is necessary for the postman problem where we need to keep track of which
    edges have been covered already when multiple edges exist between two nodes.
    We also need to annotate the edges added to make the eulerian to follow the actual shortest path trails (not
    the direct shortest path pairings between the odd nodes for which there might not be a direct trail)

    Args:
        graph_augmented (networkx graph): graph w links between odd degree nodes created from `add_augmenting_path_to_graph`.
        graph_original (networkx graph): orginal graph created from `create_networkx_graph_from_edgelist`
        start_node (str): name of starting (and ending) node for CPP solution.

    Returns:
        networkx graph (`graph_original`) augmented with edges directly between the odd nodes
    """

    euler_circuit = list(nx.eulerian_circuit(graph_augmented, source=start_node, keys=True))
    assert len(graph_augmented.edges()) == len(euler_circuit), 'graph and euler_circuit do not have equal number of edges.'

    for edge in euler_circuit:
        aug_path = nx.shortest_path(graph_original, edge[0], edge[1], weight='distance')
        edge_attr = graph_augmented[edge[0]][edge[1]][edge[2]]
        if not edge_attr.get('augmented'):
            yield edge + (edge_attr,)
        else:
            for edge_aug in list(zip(aug_path[:-1], aug_path[1:])):
                # find edge with shortest distance (if there are two parallel edges between the same nodes)
                edge_aug_dict = graph_original[edge_aug[0]][edge_aug[1]]
                edge_key = min(edge_aug_dict.keys(), key=(lambda k: edge_aug_dict[k]['distance']))  # index with min distance
                edge_aug_shortest = edge_aug_dict[edge_key]
                edge_aug_shortest['augmented'] = True
                edge_aug_shortest['id'] = edge_aug_dict[edge_key]['id']
                yield edge_aug + (edge_key, edge_aug_shortest, ) 
开发者ID:brooksandrew,项目名称:postman_problems,代码行数:36,代码来源:graph.py


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