Home > OS >  Algorithm for Colored Clique of Size K
Algorithm for Colored Clique of Size K

Time:04-25

I abstracted a real-world problem to a graph-theory-problem. Currently, I try to solve this problem by making use of a clique-algorithm from networkx library. I need ideas how my algorithm can be adapted to improve run-time performance.

Problem:

Let G be a undirected, colored graph and let R be a mapping, where each color is mapped to a given quantity, e.g.

R = {"red": 3, "blue": 1, "green": 1, "magenta": 1, "orange": 1}

I need to find a set of nodes V, with following properties (or determine that no such set exists):

  1. For each (color,amount) in R, we need at least the amount of colored nodes in V.

    (with above R for example, we need at least three red nodes, one blue node, etc. in V)

  2. The nodes in V are not allowed to be neighbors in G.

Simple Example:

With this colored graph and the above given requirement R, one possible solution is: V = {0, 1, 2, 8, 13, 15, 16}, because all the color-amounts required by R are contained in V and no node is adjacent to the other.

enter image description here

Current Approach:

  • Create complement graph CG from G
  • Iteratively find cliques and check, wether the amount of colored nodes in clique is as required by R.
import networkx as nx

def find_result(G, R):
    CG = nx.complement(G)
    for V in nx.find_cliques(CG):
        color_count = get_color_count(G, V)
        if all([(color_count.get(color, 0) >= R[color]) for color in R.keys()]):
            return V
        
# Helper method to count colors
def get_color_count(G, V):
    colors = [G.nodes[v]["color"] for v in V]
    count = dict()
    for color in colors:
        count[color] = count.get(color, 0)   1
    return count

Performance Issues:

With this trivial approach, I run into performance problems (i.e. the algorithm takes way too long for particular configurations of R). My graphs can get as worse as seen below, with 282 nodes, 4640 edges and 7 different colors.

So here is my question: how can the enter image description here

EDIT 1

Here is the graph-content (sparse6-format) from the big graph above. The content can be pasted into a file and then read with networkx.

>>sparse6<<:~?CY_A??@_??OA_??OA?M??@?G?oC_??OA?K@?D_??OA?K@?D?Y??@?G?oC?S@_F_??OA?K@?D?W@oG_??OA?K@?D?W@oG?e??@?G?oC?S@_F?_AOI`CCGO@ED?R`WBOT`_B_V`gEW[@mEo[`qF?]`}DWV`eGwYAKHG[@wFw\AYI_??C?_B?O@OE?[A?H?gAwk???OA?K@?D?W@oG?cA_J_??OA?K@?D?W@o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OpREK[qBHKgzRmM{{BpNG{rsN|?C@OH?sCOT@cFO`ASIOlBCLOxBsOPDCcRPPDSUP]aqKGsdQXHsfa^I?gQmI{katJWlqwJkna~K?oRAKKpBDKWprGKcqbiMozRmM{{BpNG{rsN|?C@OH?sCOT@cFO`ASIOlBCLOxBsOPDCcRPPDSUP\EN

To colorize the graph, use following implementation:

    G = nx.read_sparse6("graph_file")
    
    for i in range(0,12):
        G.nodes[i]["color"] = "yellow"
    for i in range(12,40):
        G.nodes[i]["color"] = "red"
    for i in range(40,63):
        G.nodes[i]["color"] = "turquoise"
    for i in range(63,85):
        G.nodes[i]["color"] = "green"
    for i in range(85,163):
        G.nodes[i]["color"] = "blue"
    for i in range(163, 176):
        G.nodes[i]["color"] = "magenta"
    for i in range(176, 282):
        G.nodes[i]["color"] = "orange"  

CodePudding user response:

Formally, we have an independent set problem with some covering constraints on the side. Integer programming seems well suited, though I don’t know which R specifically were giving you trouble.

The straightforward integer programming formulation of independent set has a 0–1 variable for each node and a constraint for each edge. The linear relaxation is poor, however, since (for example) a triangle graph has a fractional solution where each node is 0.5, for a “1.5-node” fractional independent set. Since G has only a few hundred maximal cliques, however, we can use a better formulation with a constraint for each maximal clique (for each clique, the independent set contains at most one clique node).

Implementation using NetworkX and OR-Tools below:

import networkx as nx

G = nx.read_sparse6("graph_file")

for i in range(0, 12):
    G.nodes[i]["color"] = "yellow"
for i in range(12, 40):
    G.nodes[i]["color"] = "red"
for i in range(40, 63):
    G.nodes[i]["color"] = "turquoise"
for i in range(63, 85):
    G.nodes[i]["color"] = "green"
for i in range(85, 163):
    G.nodes[i]["color"] = "blue"
for i in range(163, 176):
    G.nodes[i]["color"] = "magenta"
for i in range(176, 282):
    G.nodes[i]["color"] = "orange"


R = {
    "red": 11,
    "turquoise": 11,
    "blue": 8,
    "orange": 6,
    "green": 4,
    "magenta": 2,
    "yellow": 1,
}


import collections
from ortools.linear_solver import pywraplp

solver = pywraplp.Solver.CreateSolver("SCIP")
x = {v: solver.BoolVar(str(v)) for v in G.nodes}
for K in nx.find_cliques(G):
    solver.Add(sum(x[v] for v in K) <= 1)
nodes_with_color = collections.defaultdict(list)
for v in G.nodes:
    nodes_with_color[G.nodes[v]["color"]].append(v)
for color, bound in R.items():
    solver.Add(sum(x[v] for v in nodes_with_color[color]) >= bound)
solver.Maximize(sum(x_v for x_v in x.values()))
status = solver.Solve()
if status == pywraplp.Solver.OPTIMAL:
    solution = [v for (v, x_v) in x.items() if x_v.SolutionValue()]
    print(*solution)
    print(collections.Counter(G.nodes[v]["color"] for v in solution))
elif status == pywraplp.Solver.INFEASIBLE:
    print("no solution")
else:
    print("unknown status", status)
  • Related