Example #1
0
def test_clique_removal():
    graph = nx.complete_graph(10)
    i, cs = apxa.clique_removal(graph)
    idens = nx.density(graph.subgraph(i))
    eq_(idens, 0.0, "i-set not found by clique_removal!")
    for clique in cs:
        cdens = nx.density(graph.subgraph(clique))
        eq_(cdens, 1.0, "clique not found by clique_removal!")

    graph = nx.trivial_graph(nx.Graph())
    i, cs = apxa.clique_removal(graph)
    idens = nx.density(graph.subgraph(i))
    eq_(idens, 0.0, "i-set not found by ramsey!")
    # we should only have 1-cliques. Just singleton nodes.
    for clique in cs:
        cdens = nx.density(graph.subgraph(clique))
        eq_(cdens, 0.0, "clique not found by clique_removal!")

    graph = nx.barbell_graph(10, 5, nx.Graph())
    i, cs = apxa.clique_removal(graph)
    idens = nx.density(graph.subgraph(i))
    eq_(idens, 0.0, "i-set not found by ramsey!")
    for clique in cs:
        cdens = nx.density(graph.subgraph(clique))
        eq_(cdens, 1.0, "clique not found by clique_removal!")
Example #2
0
def test_clique_removal():
    graph = nx.complete_graph(10)
    i, cs = apxa.clique_removal(graph)
    idens = nx.density(graph.subgraph(i))
    eq_(idens, 0.0, "i-set not found by clique_removal!")
    for clique in cs:
        cdens = nx.density(graph.subgraph(clique))
        eq_(cdens, 1.0, "clique not found by clique_removal!")

    graph = nx.trivial_graph(nx.Graph())
    i, cs = apxa.clique_removal(graph)
    idens = nx.density(graph.subgraph(i))
    eq_(idens, 0.0, "i-set not found by ramsey!")
    # we should only have 1-cliques. Just singleton nodes.
    for clique in cs:
        cdens = nx.density(graph.subgraph(clique))
        eq_(cdens, 0.0, "clique not found by clique_removal!")

    graph = nx.barbell_graph(10, 5, nx.Graph())
    i, cs = apxa.clique_removal(graph)
    idens = nx.density(graph.subgraph(i))
    eq_(idens, 0.0, "i-set not found by ramsey!")
    for clique in cs:
        cdens = nx.density(graph.subgraph(clique))
        eq_(cdens, 1.0, "clique not found by clique_removal!")
Example #3
0
 def test_trivial_graph(self):
     G = nx.trivial_graph()
     independent_set, cliques = clique_removal(G)
     assert is_independent_set(G, independent_set)
     assert all(is_clique(G, clique) for clique in cliques)
     # In fact, we should only have 1-cliques, that is, singleton nodes.
     assert all(len(clique) == 1 for clique in cliques)
Example #4
0
 def test_trivial_graph(self):
     G = nx.trivial_graph()
     independent_set, cliques = clique_removal(G)
     assert_true(is_independent_set(G, independent_set))
     assert_true(all(is_clique(G, clique) for clique in cliques))
     # In fact, we should only have 1-cliques, that is, singleton nodes.
     assert_true(all(len(clique) == 1 for clique in cliques))
Example #5
0
def maximum_independent_set(G):
    """Returns an approximate maximum independent set.

    Parameters
    ----------
    G : NetworkX graph
        Undirected graph

    Returns
    -------
    iset : Set
        The apx-maximum independent set

    Notes
    -----
    Finds the $O(|V|/(log|V|)^2)$ apx of independent set in the worst case.


    References
    ----------
    .. [1] Boppana, R., & Halldórsson, M. M. (1992).
       Approximating maximum independent sets by excluding subgraphs.
       BIT Numerical Mathematics, 32(2), 180–196. Springer.
    """
    iset, _ = clique_removal(G)
    return iset
Example #6
0
def maximum_independent_set(G):
    """Returns an approximate maximum independent set.

    Parameters
    ----------
    G : NetworkX graph
        Undirected graph

    Returns
    -------
    iset : Set
        The apx-maximum independent set

    Notes
    -----
    Finds the $O(|V|/(log|V|)^2)$ apx of independent set in the worst case.


    References
    ----------
    .. [1] Boppana, R., & Halldórsson, M. M. (1992).
       Approximating maximum independent sets by excluding subgraphs.
       BIT Numerical Mathematics, 32(2), 180–196. Springer.
    """
    iset, _ = clique_removal(G)
    return iset
Example #7
0
def solve_mcq_approximation(G):
    """
    最大クリーク問題を既存の近似解法で解く
    (Reference: Approximating Maximum Independent Sets by Excluding Subgraphs)
    入力
        G: 無向グラフ
    出力
        なし
        

    # ><><><><><><><><><><><><><><><><><><><><><
    # ><><><><><><><><><><><><><><><><><><><><><
    # ><><><><><><><><><><><><><><><><><><><><><

    """
    

    if G is None:
        raise ValueError("Expected NetworkX graph!")

    st1 = time.time()
    cgraph = nx.complement(G)
    et1 = time.time()

    print('elapsed_time (complement) :', et1-st1)

    st2 = time.time()
    iset, _ = clique_removal(cgraph)
    et2 = time.time()

    print('elapsed_time (clique) :', et2-st2)
    print(f'maximum clique number: {len(iset)}')
    print(f'maximum clique: {iset}')
Example #8
0
 def nx_sim_score(self):
     g = nx.Graph()
     print "Vertices: %d" % len(self.v)
     for node in self.v:
         n = node[V_LABEL]
         g.add_node(n)
     print "Edges: %d" % len(self.e)
     for edge in self.e:
         g.add_edge(*edge)
     maxw = 0
     maxc = []
     cnt = 0
     sys.stdout.write("%d\r" % cnt)
     '''
     for clique in nx.find_cliques(g):
         sys.stdout.write("%d\r" % cnt)
         w = self.clique_weight([self.v_by_label(c) for c in clique])
         if w > maxw:
             maxw = w
             maxc = clique
     '''
     from networkx.algorithms.approximation import max_clique, clique_removal
     for c in clique_removal(g)[1]:
         if len(c) > len(maxc):
             maxc = c
     maxw = self.clique_weight([self.v_by_label(c) for c in maxc])
     jscore = self.jaccard_coef(len(maxc), self.g1order + self.g2order)
     wscore = maxw / self.g1order
     print "MaxW", maxw
     print "JScore %d, %d", len(maxc), self.g1order + self.g2order, jscore
     print "Wscore", wscore
     return jscore, wscore
Example #9
0
 def nx_sim_score(self):
     g = nx.Graph()
     print "Vertices: %d" % len(self.v)
     for node in self.v:
         n = node[V_LABEL]
         g.add_node(n)
     print "Edges: %d" % len(self.e)
     for edge in self.e:
         g.add_edge(*edge)
     maxw = 0
     maxc = []
     cnt = 0
     sys.stdout.write("%d\r" % cnt)
     '''
     for clique in nx.find_cliques(g):
         sys.stdout.write("%d\r" % cnt)
         w = self.clique_weight([self.v_by_label(c) for c in clique])
         if w > maxw:
             maxw = w
             maxc = clique
     '''
     from networkx.algorithms.approximation import max_clique, clique_removal
     for c in clique_removal(g)[1]:
         if len(c) > len(maxc):
             maxc = c
     maxw = self.clique_weight([self.v_by_label(c) for c in maxc])
     jscore = self.jaccard_coef(len(maxc), self.g1order + self.g2order)
     wscore = maxw / self.g1order
     print "MaxW", maxw
     print "JScore %d, %d", len(maxc), self.g1order+self.g2order, jscore
     print "Wscore", wscore
     return jscore, wscore
def NetworkX_approximate_clique_cover(graph_dict):
    """
    NetworkX poly-time heuristic is based on
    Boppana, R., & Halldórsson, M. M. (1992).
    Approximating maximum independent sets by excluding subgraphs.
    BIT Numerical Mathematics, 32(2), 180–196. Springer.
    """
    G = nx.Graph()
    for src in graph_dict:
        for dst in graph_dict[src]:
            G.add_edge(src, dst)
    return approximation.clique_removal(G)[1]
Example #11
0
def clique_approx_find_greedy_eliminate(graph: nx.Graph) -> list:
    """Perform minimum clique cover by approximatly find maximum clique and iteratively eliminate it.

    Find the maximum clique with approximatino methods and iteratively eliminate it.

    Args:
        graph (nx.Graph): graph to solve
    Returns:
        list: list of node names for each clique
    """
    _, clique_list = approx.clique_removal(graph)
    clique_list = [list(item) for item in clique_list]
    return clique_list
def max_set_ec2_deploy(graph_data): 
'''
This code should be run on an Amazon EC2 instance or other cloud
computing platform. It actually runs max. ind. sets on the graph 
file created above.'''

	# Storing cardinality of largest set
	cliques = approximation.clique_removal(graph)[1]
	# Storing biggest cliques
	biggest = max([len(cliques[x]) for x in range(len(cliques))])
	# Adding 1 to get clinic number		 
	cliques = np.array([list(x) for x in cliques if len(x) == biggest])	
	# Adding location identifier 'K' 
	cliques = cliques[:][:]+ 1
	# Storing filepath											
	cliques = DataFrame(np.char.mod('K'+ '%04d', cliques))
	# Writing to Excel file				 
	filepath = wd + 'solutions.xlsx'
	# Exporting								 	 
	cliques.T.to_excel(filepath, index=False)							
Example #13
0
 def test_barbell_graph(self):
     G = nx.barbell_graph(10, 5)
     independent_set, cliques = clique_removal(G)
     assert is_independent_set(G, independent_set)
     assert all(is_clique(G, clique) for clique in cliques)
Example #14
0
 def test_complete_graph(self):
     G = nx.complete_graph(10)
     independent_set, cliques = clique_removal(G)
     assert is_independent_set(G, independent_set)
     assert all(is_clique(G, clique) for clique in cliques)
Example #15
0
 def test_barbell_graph(self):
     G = nx.barbell_graph(10, 5)
     independent_set, cliques = clique_removal(G)
     assert_true(is_independent_set(G, independent_set))
     assert_true(all(is_clique(G, clique) for clique in cliques))
Example #16
0
 def test_complete_graph(self):
     G = nx.complete_graph(10)
     independent_set, cliques = clique_removal(G)
     assert_true(is_independent_set(G, independent_set))
     assert_true(all(is_clique(G, clique) for clique in cliques))