Python networkx.eulerian_circuit() Examples

The following are 17 code examples of networkx.eulerian_circuit(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module networkx , or try the search function .
Example #1
Source File: diffusiontrees.py    From diff2vec with GNU General Public License v3.0 6 votes vote down vote up
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 
Example #2
Source File: test_euler.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 6 votes vote down vote up
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)]) 
Example #3
Source File: test_euler.py    From Carnets with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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)]) 
Example #4
Source File: test_euler.py    From aws-kube-codesuite with Apache License 2.0 6 votes vote down vote up
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)]) 
Example #5
Source File: graph.py    From postman_problems with MIT License 5 votes vote down vote up
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, ) 
Example #6
Source File: diffuser.py    From karateclub with GNU General Public License v3.0 5 votes vote down vote up
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 
Example #7
Source File: test_euler.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_not_eulerian(self):
        f = list(eulerian_circuit(nx.complete_graph(4))) 
Example #8
Source File: test_euler.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
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)]) 
Example #9
Source File: test_euler.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
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)]) 
Example #10
Source File: test_euler.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
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)]) 
Example #11
Source File: test_euler.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_not_eulerian(self):
        f = list(eulerian_circuit(nx.complete_graph(4))) 
Example #12
Source File: test_euler.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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)]) 
Example #13
Source File: test_euler.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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)]) 
Example #14
Source File: test_euler.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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)]) 
Example #15
Source File: test_euler.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 5 votes vote down vote up
def test_not_eulerian(self):    
        f=list(eulerian_circuit(nx.complete_graph(4))) 
Example #16
Source File: test_euler.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 5 votes vote down vote up
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)]) 
Example #17
Source File: test_euler.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 5 votes vote down vote up
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)])