Beispiel #1
0
 def update_cliques( self ):
     result = set()
     for c in find_cliques( self.graph ):
         if len(c) > 1:
             result.add( Clique(c) )
     self.cliques = result
     return result
Beispiel #2
0
def mutual_coverage_subset(wordlist, threshold, concepts='concept'):
    """Compute maximal mutual coverage for all language in a wordlist.
    
    Parameters
    ----------
    wordlist : ~lingpy.basic.wordlist.Wordlist
        Your Wordlist object (or a descendant class).
    concepts : str (default="concept")
        The column which stores your concepts.
    threshold : int
        The threshold which should be checked.  

    Returns
    -------
    coverage : tuple
        A tuple consisting of the number of languages for which the coverage
        could be found as well as a list of all pairings in which this coverage
        is possible. The list itself contains the mutual coverage inside each
        pair and the list of languages.

    Examples
    --------
    Compute all sets of languages with coverage at 200 for the KSL dataset::

      >>> from lingpy.compare.sanity import mutual_coverage_subset
      >>> from lingpy import *
      >>> from lingpy.tests.util import test_data
      >>> wl = Wordlist(test_data('KSL.qlc'))
      >>> number_of_languages, pairs = mutual_coverage_subset(wl, 200)
      >>> for number_of_items, languages in pairs:
              print(number_of_items, ','.join(languages))
          200 Albanian,English,French,German,Hawaiian,Navajo,Turkish

    See also
    --------
    mutual_coverage
    mutual_coverage_check
    average_coverage
    """
    coverage = mutual_coverage(wordlist, concepts)

    G = nx.Graph()
    for tax in wordlist.cols:
        G.add_node(tax)
    for taxA, taxB in combinations(wordlist.cols, r=2):
        if len(coverage[taxA][taxB]) >= threshold:
            G.add_edge(taxA, taxB, coverage=coverage[taxA][taxB])
    
    best_cliques = defaultdict(list)
    best_clique = 0
    for clique in find_cliques(G):
        sums = []
        for taxA, taxB in combinations(clique, r=2):
            sums += [len(G[taxA][taxB]['coverage'])]
        if sums:
            val = int(sum(sums) / len(sums) + 0.5)
            best_cliques[len(clique)] += [(val, sorted(clique))]
            if len(clique) > best_clique:
                best_clique = len(clique)
    return best_clique, best_cliques[best_clique]
def FindLargestCliques(G):
    
    """
    -------------------------------------------------------
    This function finds the largest clique in a NetworkX graph.
    -------------------------------------------------------
    Preconditions:
        G - a NetworkX graph.
    
    Postconditions: 
        returns: result - a list of lists, where each list entry
        contains a list of vertices which comprise the largest clique(s)
        in G.
    -------------------------------------------------------
    """
    
    maximalCliques = list(find_cliques(G))
    largestSoFar = len(maximalCliques[0])
    
    for thisClique in maximalCliques:
        if len(thisClique) > largestSoFar:
            largestSoFar = len(thisClique)
            
    result = list()
    
    for thisClique in maximalCliques:
        if len(thisClique) == largestSoFar:
            result.append(thisClique)
            
    return result
def find_biggest_cluster(radius, points, order=None):
    graph = nx.Graph()
    for point in points:
            if order is 'lure_info':
                f = point['latitude'], point['longitude'], point['lure_info']['lure_expires_timestamp_ms']
            else:
                f = point['latitude'], point['longitude'], 0
            graph.add_node(f)
            for node in graph.nodes():
                if node != f and distance(f[0], f[1], node[0], node[1]) <= radius*2:
                    graph.add_edge(f, node)
    cliques = list(find_cliques(graph))
    if len(cliques) > 0:
        max_clique = max(list(find_cliques(graph)), key=lambda l: (len(l), sum(x[2] for x in l)))
        merc_clique = [coord2merc(x[0], x[1]) for x in max_clique]
        clique_x, clique_y = zip(*merc_clique)
        best_point = np.mean(clique_x), np.mean(clique_y)
        best_coord = merc2coord(best_point)
        return {'latitude': best_coord[0], 'longitude': best_coord[1], 'num_points': len(max_clique)}
    else:
        return None
def findLargestCliques(G):
    maximalCliques = list(find_cliques(G))
    largestSoFar = len(maximalCliques[0])
    
    for thisClique in maximalCliques:
        if len(thisClique) > largestSoFar:
            largestSoFar = len(thisClique)
            
    result = list()
    
    for thisClique in maximalCliques:
        if len(thisClique) == largestSoFar:
            result.append(thisClique)
            
    return result
Beispiel #6
0
def eval_cliques(graph):
    """this evaluates the main function and cach it for speed up."""
    cliques = [len(u) for u in list(clique.find_cliques(graph)) if len(u) > 1]
    return np.bincount(cliques)[np.nonzero(np.bincount(cliques))]
Beispiel #7
0
def cliques(a):
    from networkx.algorithms import approximation, clique
    cliques = clique.find_cliques(a)
    for i in cliques:
        print(i)
Beispiel #8
0
"""### Cliques

A clique is a sub-set of a network in which the nodes are more closely and intensely tied to one another than they are to other nodes of the network.The clique number of a graph is the size of the largest clique in the graph.

While we have imported approximation,clique from networkx.algorithms and then find the number of maximal cliques in the graph.Next,Print All the cliques in the network based on find of Undirected Bitcoin.
"""

from networkx.algorithms import clique

# Find the number of maximal cliques in the graph
n_of_cliques = clique.graph_number_of_cliques(bitcoin_undirected)
print("Number of cliques in the network:", n_of_cliques, end='\n\n')

# Print all the cliques
all_cliques = clique.find_cliques(bitcoin_undirected)
print("All the cliques in the network:")
for c in all_cliques:
    print(c)

"""### Ego Graph

Ego networks are the network is based off the ego and all other nodes directly connected to the ego are called alters.And is used to analyze the ties among those alters.

Here, ego and gk_ego were defining the cores and nx.ego_graph for bitcoin network.
"""

ego = cores[0][0]
gk_ego = nx.ego_graph(bitcoin, ego)

"""As we are drawing the final network graph using the previous section ego and gk_ego variable. Declaring Pos(Position), to  layout for gk_ego. And plt.title as Title for the graph. And using nx.draw for gk_ego which have seen in pervious code and adding the color,size for node. Finally we can see Ego Network by using plt.show()."""
Beispiel #9
0
def comparison_array(molecule, diameter=10, size=30, ignore_hydrogen=True):
    """ Generate comparison array
    Comparison array consists of node pairs in the graph and a collater.
    42 bit collater
        6 bit of distance (0-31)
        18 bit of bond attribute x2
            9 bit of atom attribute x 2
                7 bit of atom number (0-127)
                2 bit of atom pi(0-3)


    [Sheridan, R.P. and Miller, M.D., J. Chem. Inf. Comput. Sci. 38 (1998) 915]

    Using possible_path_length instead of shortest_path_length
    and find intersection of the distance set each other,
    it is possible to detect roundabout matching path.
    Therefore, exact graph isomorphism can be determined.
    However, set intersection is too costful to do
    in spite of a trivial improvement.

    Args:
        mol: Compound
        cutoff: more distant connection than the value is no longer used
                for comparison matrix graph due to performance reason.

    Returns:
        arr(list): comparison array. list of tuple (node1, node2, collater)
        max_mcs(int): maximum size of possible mcs
        int_to_node(dict): int -> node index pair reconverter dict
    Throws:
        ValueError: if len(mol) < 3
    """
    molecule.require("Valence")
    mol = molutil.clone(molecule)
    if ignore_hydrogen:
        mol = molutil.make_Hs_implicit(mol)
    # Ignore salt, water
    remover.remove_salt(mol)
    remover.remove_water(mol)
    # multivalent coordinated metals notably affect the performance
    remover.remove_coordinated_metal(mol)
    g = nx.line_graph(mol.graph)
    node_to_int = {}
    for i, e in enumerate(g.nodes()):
        node_to_int[e] = i
        a1 = mol.atom(e[0])
        a2 = mol.atom(e[1])
        a1t = a1.number << 2 | a1.pi
        a2t = a2.number << 2 | a2.pi
        pair = sorted((a1t, a2t))
        g.node[e]["type"] = pair[0] << 9 | pair[1]
    # convert node index pair to integer expression
    g = nx.relabel_nodes(g, node_to_int)
    # interger -> index pair reconverter
    int_to_node = {v: k for k, v in node_to_int.items()}
    arr = []
    matrix = nx.Graph()
    for ui, ua in g.nodes(data=True):
        r = _reachables(g, ui, diameter, size)
        for vi, d in r.items():
            if not d:
                continue
            matrix.add_edge(ui, vi)
            code = (d << 18 | ua["type"]) << 18 | g.node[vi]["type"]
            arr.append((ui, vi, code))
    max_size = len(max(find_cliques(matrix), key=len, default=[]))
    return arr, max_size, int_to_node
Beispiel #10
0
#!/usr/bin/env python

import networkx as nx
from networkx.algorithms import clique

g = nx.Graph()
with open("./7b24b79f-d898-4480-bc1b-e09742f704f7_mod.col", "r") as f:
    for l in f:
        if l[0] == 'e':
            _, a, b = l.split(' ', 2)
            g.add_edge(int(a), int(b))

cliques = clique.find_cliques(g)
# print(clique.graph_number_of_cliques(g))
kids=[104,118,55,51,123,110,111,116,95,84,72,69,126,70,76,65,71,33,61,40,124,115,48,60,62,83,79,42,82,121,125,45,98,114,101,97,100]
# print(len(set(kids)))
matching_cliques=0


# for kid in kids: 
#     print("#### KID: %s" % kid)
#     cliques = clique.cliques_containing_node(g , kid)
#     friends = 0
#     largest_clique = 0
#     for clicke in cliques:
#         clique_size = len(clicke)
#         largest_clique = clique_size if clique_size > largest_clique else largest_clique
#         friends += clique_size
#         # for c in clicke:
#         #     print(chr(c), end='')
#         # print()
Beispiel #11
0
 def get_max_cliques(self):
     # retourne un iterateur sur les cliques maximales du graphe primal
     return find_cliques(self.pg)