Python networkx.transitivity() Examples

The following are 16 code examples of networkx.transitivity(). 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_cluster.py    From Carnets with BSD 3-Clause "New" or "Revised" License 7 votes vote down vote up
def test_k5(self):
        G = nx.complete_graph(5)
        assert_equal(nx.transitivity(G), 1.0)
        G.remove_edge(1, 2)
        assert_equal(nx.transitivity(G), 0.875)

    # def test_clustering_transitivity(self):
    #     # check that weighted average of clustering is transitivity
    #     G = nx.complete_graph(5)
    #     G.remove_edge(1,2)
    #     t1=nx.transitivity(G)
    #     (cluster_d2,weights)=nx.clustering(G,weights=True)
    #     trans=[]
    #     for v in G.nodes():
    #         trans.append(cluster_d2[v]*weights[v])
    #     t2=sum(trans)
    #     assert_almost_equal(abs(t1-t2),0) 
Example #2
Source File: test_cluster.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 6 votes vote down vote up
def test_k5(self):
        G = nx.complete_graph(5)
        assert_equal(nx.transitivity(G),1.0)
        G.remove_edge(1,2)
        assert_equal(nx.transitivity(G),0.875)

    # def test_clustering_transitivity(self):
    #     # check that weighted average of clustering is transitivity
    #     G = nx.complete_graph(5)
    #     G.remove_edge(1,2)
    #     t1=nx.transitivity(G)
    #     (cluster_d2,weights)=nx.clustering(G,weights=True)
    #     trans=[]
    #     for v in G.nodes():
    #         trans.append(cluster_d2[v]*weights[v])
    #     t2=sum(trans)
    #     assert_almost_equal(abs(t1-t2),0) 
Example #3
Source File: test_cluster.py    From aws-kube-codesuite with Apache License 2.0 6 votes vote down vote up
def test_k5(self):
        G = nx.complete_graph(5)
        assert_equal(nx.transitivity(G), 1.0)
        G.remove_edge(1, 2)
        assert_equal(nx.transitivity(G), 0.875)

    # def test_clustering_transitivity(self):
    #     # check that weighted average of clustering is transitivity
    #     G = nx.complete_graph(5)
    #     G.remove_edge(1,2)
    #     t1=nx.transitivity(G)
    #     (cluster_d2,weights)=nx.clustering(G,weights=True)
    #     trans=[]
    #     for v in G.nodes():
    #         trans.append(cluster_d2[v]*weights[v])
    #     t2=sum(trans)
    #     assert_almost_equal(abs(t1-t2),0) 
Example #4
Source File: scd.py    From karateclub with GNU General Public License v3.0 5 votes vote down vote up
def _set_omega(self):
        """
        Calculating the graph level clustering coefficient.
        """
        self._omega = nx.transitivity(self._graph) 
Example #5
Source File: cluster.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 5 votes vote down vote up
def transitivity(G):
    r"""Compute graph transitivity, the fraction of all possible triangles 
    present in G.

    Possible triangles are identified by the number of "triads" 
    (two edges with a shared vertex).

    The transitivity is

    .. math::

        T = 3\frac{\#triangles}{\#triads}.

    Parameters
    ----------
    G : graph

    Returns
    -------
    out : float
       Transitivity

    Examples
    --------
    >>> G = nx.complete_graph(5)
    >>> print(nx.transitivity(G))
    1.0
    """
    triangles=0 # 6 times number of triangles
    contri=0  # 2 times number of connected triples
    for v,d,t in _triangles_and_degree_iter(G):
        contri += d*(d-1)
        triangles += t
    if triangles==0: # we had no triangles or possible triangles
        return 0.0
    else:
        return triangles/float(contri) 
Example #6
Source File: test_cluster.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 5 votes vote down vote up
def test_transitivity(self):
        G = nx.Graph()
        assert_equal(nx.transitivity(G),0.0) 
Example #7
Source File: test_cluster.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 5 votes vote down vote up
def test_path(self):
        G = nx.path_graph(10)
        assert_equal(nx.transitivity(G),0.0) 
Example #8
Source File: test_cluster.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 5 votes vote down vote up
def test_cubical(self):
        G = nx.cubical_graph()
        assert_equal(nx.transitivity(G),0.0) 
Example #9
Source File: cluster.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def transitivity(G):
    r"""Compute graph transitivity, the fraction of all possible triangles
    present in G.

    Possible triangles are identified by the number of "triads"
    (two edges with a shared vertex).

    The transitivity is

    .. math::

        T = 3\frac{\#triangles}{\#triads}.

    Parameters
    ----------
    G : graph

    Returns
    -------
    out : float
       Transitivity

    Examples
    --------
    >>> G = nx.complete_graph(5)
    >>> print(nx.transitivity(G))
    1.0
    """
    triangles = sum(t for v, d, t, _ in _triangles_and_degree_iter(G))
    contri = sum(d * (d - 1) for v, d, t, _ in _triangles_and_degree_iter(G))
    return 0 if triangles == 0 else triangles / contri 
Example #10
Source File: test_cluster.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_transitivity(self):
        G = nx.Graph()
        assert_equal(nx.transitivity(G), 0.0) 
Example #11
Source File: test_cluster.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(10)
        assert_equal(nx.transitivity(G), 0.0) 
Example #12
Source File: test_cluster.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_cubical(self):
        G = nx.cubical_graph()
        assert_equal(nx.transitivity(G), 0.0) 
Example #13
Source File: cluster.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def transitivity(G):
    r"""Compute graph transitivity, the fraction of all possible triangles
    present in G.

    Possible triangles are identified by the number of "triads"
    (two edges with a shared vertex).

    The transitivity is

    .. math::

        T = 3\frac{\#triangles}{\#triads}.

    Parameters
    ----------
    G : graph

    Returns
    -------
    out : float
       Transitivity

    Examples
    --------
    >>> G = nx.complete_graph(5)
    >>> print(nx.transitivity(G))
    1.0
    """
    triangles = sum(t for v, d, t, _ in _triangles_and_degree_iter(G))
    contri = sum(d * (d - 1) for v, d, t, _ in _triangles_and_degree_iter(G))
    return 0 if triangles == 0 else triangles / contri 
Example #14
Source File: test_cluster.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_transitivity(self):
        G = nx.Graph()
        assert_equal(nx.transitivity(G), 0.0) 
Example #15
Source File: test_cluster.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_path(self):
        G = nx.path_graph(10)
        assert_equal(nx.transitivity(G), 0.0) 
Example #16
Source File: test_cluster.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_cubical(self):
        G = nx.cubical_graph()
        assert_equal(nx.transitivity(G), 0.0)