Python networkx.line_graph() Examples

The following are 26 code examples of networkx.line_graph(). 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: test_line.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_create2(self):
        G = nx.Graph()
        G.add_edges_from([(0, 1), (1, 2), (2, 3)])
        L = nx.line_graph(G, create_using=nx.DiGraph())
        assert_edges_equal(L.edges(), [((0, 1), (1, 2)), ((1, 2), (2, 3))]) 
Example #2
Source File: test_line.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_create2(self):
        G = nx.Graph()
        G.add_edges_from([(0, 1), (1, 2), (2, 3)])
        L = nx.line_graph(G, create_using=nx.DiGraph())
        assert_edges_equal(L.edges(), [((0, 1), (1, 2)), ((1, 2), (2, 3))]) 
Example #3
Source File: test_line.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_digraph2(self):
        G = nx.DiGraph()
        G.add_edges_from([(0, 1), (1, 2), (2, 3)])
        L = nx.line_graph(G)
        assert_edges_equal(L.edges(), [((0, 1), (1, 2)), ((1, 2), (2, 3))]) 
Example #4
Source File: test_line.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_digraph1(self):
        G = nx.DiGraph()
        G.add_edges_from([(0, 1), (0, 2), (0, 3)])
        L = nx.line_graph(G)
        # no edge graph, but with nodes
        assert_equal(L.adj, {(0, 1): {}, (0, 2): {}, (0, 3): {}}) 
Example #5
Source File: test_line.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_cycle(self):
        G = nx.cycle_graph(5)
        L = nx.line_graph(G)
        assert_true(nx.is_isomorphic(L, G)) 
Example #6
Source File: test_line.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_path(self):
        G = nx.path_graph(5)
        L = nx.line_graph(G)
        assert_true(nx.is_isomorphic(L, nx.path_graph(4))) 
Example #7
Source File: test_line.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_star(self):
        G = nx.star_graph(5)
        L = nx.line_graph(G)
        assert_true(nx.is_isomorphic(L, nx.complete_graph(5))) 
Example #8
Source File: test_line.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_line_inverse_line_dgm(self):
        G = nx.dorogovtsev_goltsev_mendes_graph(4)
        H = nx.line_graph(G)
        J = nx.inverse_line_graph(H)
        assert_true(nx.is_isomorphic(G, J)) 
Example #9
Source File: test_line.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_line_inverse_line_multipartite(self):
        G = nx.complete_multipartite_graph(3, 4, 5)
        H = nx.line_graph(G)
        J = nx.inverse_line_graph(H)
        assert_true(nx.is_isomorphic(G, J)) 
Example #10
Source File: test_line.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_line_inverse_line_cycle(self):
        G = nx.cycle_graph(10)
        H = nx.line_graph(G)
        J = nx.inverse_line_graph(H)
        assert_true(nx.is_isomorphic(G, J)) 
Example #11
Source File: test_line.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_line_inverse_line_hypercube(self):
        G = nx.hypercube_graph(5)
        H = nx.line_graph(G)
        J = nx.inverse_line_graph(H)
        assert_true(nx.is_isomorphic(G, J)) 
Example #12
Source File: test_line.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_line_inverse_line_path(self):
        G = nx.path_graph(10)
        H = nx.line_graph(G)
        J = nx.inverse_line_graph(H)
        assert_true(nx.is_isomorphic(G, J)) 
Example #13
Source File: test_line.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_line_inverse_line_complete(self):
        G = nx.complete_graph(10)
        H = nx.line_graph(G)
        J = nx.inverse_line_graph(H)
        assert_true(nx.is_isomorphic(G, J)) 
Example #14
Source File: gl2vec.py    From karateclub with GNU General Public License v3.0 5 votes vote down vote up
def _create_line_graph(self, graph):
        r"""Getting the embedding of graphs.

        Arg types:
            * **graph** *(NetworkX graph)* - The graph transformed to be a line graph.

        Return types:
            * **line_graph** *(NetworkX graph)* - The line graph of the source graph.
        """
        graph = nx.line_graph(graph)
        node_mapper = {node: i for i, node in enumerate(graph.nodes())}
        edges = [[node_mapper[edge[0]], node_mapper[edge[1]]] for edge in graph.edges()]
        line_graph = nx.from_edgelist(edges)
        return line_graph 
Example #15
Source File: test_line.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_digraph2(self):
        G = nx.DiGraph()
        G.add_edges_from([(0, 1), (1, 2), (2, 3)])
        L = nx.line_graph(G)
        assert_edges_equal(L.edges(), [((0, 1), (1, 2)), ((1, 2), (2, 3))]) 
Example #16
Source File: test_line.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_digraph1(self):
        G = nx.DiGraph()
        G.add_edges_from([(0, 1), (0, 2), (0, 3)])
        L = nx.line_graph(G)
        # no edge graph, but with nodes
        assert_equal(L.adj, {(0, 1): {}, (0, 2): {}, (0, 3): {}}) 
Example #17
Source File: test_line.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_cycle(self):
        G = nx.cycle_graph(5)
        L = nx.line_graph(G)
        assert_true(nx.is_isomorphic(L, G)) 
Example #18
Source File: test_line.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_path(self):
        G = nx.path_graph(5)
        L = nx.line_graph(G)
        assert_true(nx.is_isomorphic(L, nx.path_graph(4))) 
Example #19
Source File: test_line.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_star(self):
        G = nx.star_graph(5)
        L = nx.line_graph(G)
        assert_true(nx.is_isomorphic(L, nx.complete_graph(5))) 
Example #20
Source File: test_line.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 5 votes vote down vote up
def test_create2(self):
        G = nx.Graph()
        G.add_edges_from([(0,1),(1,2),(2,3)])
        L = nx.line_graph(G, create_using=nx.DiGraph())
        assert_equal(sorted(L.edges()), [((0, 1), (1, 2)), ((1, 2), (2, 3))]) 
Example #21
Source File: test_line.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 5 votes vote down vote up
def test_digraph2(self):
        G = nx.DiGraph()
        G.add_edges_from([(0,1),(1,2),(2,3)])
        L = nx.line_graph(G)
        assert_equal(sorted(L.edges()), [((0, 1), (1, 2)), ((1, 2), (2, 3))]) 
Example #22
Source File: test_line.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 5 votes vote down vote up
def test_digraph1(self):
        G = nx.DiGraph()
        G.add_edges_from([(0,1),(0,2),(0,3)])
        L = nx.line_graph(G)
        # no edge graph, but with nodes
        assert_equal(L.adj, {(0,1):{}, (0,2):{}, (0,3):{}}) 
Example #23
Source File: test_line.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 5 votes vote down vote up
def test_cycle(self):
        G = nx.cycle_graph(5)
        L = nx.line_graph(G)
        assert_true(nx.is_isomorphic(L, G)) 
Example #24
Source File: test_line.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 5 votes vote down vote up
def test_path(self):
        G = nx.path_graph(5)
        L = nx.line_graph(G)
        assert_true(nx.is_isomorphic(L, nx.path_graph(4))) 
Example #25
Source File: test_line.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 5 votes vote down vote up
def test_star(self):
        G = nx.star_graph(5)
        L = nx.line_graph(G)
        assert_true(nx.is_isomorphic(L, nx.complete_graph(5))) 
Example #26
Source File: ripple_carry_adder.py    From forest-benchmarking with Apache License 2.0 4 votes vote down vote up
def get_qubit_registers_for_adder(qc: QuantumComputer, num_length: int,
                                  qubits: Optional[Sequence[int]] = None)\
        -> Tuple[Sequence[int], Sequence[int], int, int]:
    """
    Searches for a layout among the given qubits for the two n-bit registers and two additional
    ancilla that matches the simple layout given in figure 4 of [CDKM96]_.

    This method ignores any considerations of physical characteristics of the qc aside from the
    qubit layout. An error is thrown if the appropriate layout is not found.

    :param qc: the quantum resource on which an adder program will be executed.
    :param num_length: the length of the bitstring representation of one summand
    :param qubits: the available qubits on which to run the adder program.
    :return: the necessary registers and ancilla labels for implementing an adder
        program to add the numbers a and b. The output can be passed directly to :func:`adder`
    """
    if qubits is None:
        unavailable = []  # assume this means all qubits in qc are available
    else:
        unavailable = [qubit for qubit in qc.qubits() if qubit not in qubits]

    graph = qc.qubit_topology().copy()
    for qubit in unavailable:
        graph.remove_node(qubit)

    # network x only provides subgraph isomorphism, but we want a subgraph monomorphism, i.e. we
    # specifically want to match the edges desired_layout with some subgraph of graph. To
    # accomplish this, we swap the nodes and edges of graph by making a line graph.
    line_graph = nx.line_graph(graph)

    # We want a path of n nodes, which has n-1 edges. Since we are matching edges of graph with
    # nodes of layout we make a layout of n-1 nodes.
    num_desired_nodes = 2 * num_length + 2
    desired_layout = nx.path_graph(num_desired_nodes - 1)

    g_matcher = nx.algorithms.isomorphism.GraphMatcher(line_graph, desired_layout)

    try:
        # pick out a subgraph isomorphic to the desired_layout if one exists
        # this is an isomorphic mapping from edges in graph (equivalently nodes of line_graph) to
        # nodes in desired_layout (equivalently edges of a path graph with one more node)
        edge_iso = next(g_matcher.subgraph_isomorphisms_iter())
    except IndexError:
        raise Exception("An appropriate layout for the qubits could not be found among the "
                        "provided qubits.")

    # pick out the edges of the isomorphism from the original graph
    subgraph = nx.Graph(graph.edge_subgraph(edge_iso.keys()))

    # pick out an endpoint of our path to start the assignment
    start_node = -1
    for node in subgraph.nodes:
        if subgraph.degree(node) == 1:  # found an endpoint
            start_node = node
            break

    return assign_registers_to_line_or_cycle(start_node, subgraph, num_length)