3

I have a dictionary in the following form

{   0: [1, 2, 3, 4, 5]
    1: [6, 2, 3, 4, 5]
    2: [3, 4, 5, 6, 7]
    3: [8, 9]
    ...
}

Each of the values (unsorted lists) correspond to a clique that I want to induce in my graph. Unfortunately, as you can see, many of the cliques share vertices that are also part of other cliques. Right now I do a straight-forward inducing of these cliques:

for clique in clique_dict.itervalues(): 
    graph.add_edges_from(combinations(clique, 2))

But this is time-consuming in the sense that many pairs of edges are already induced earlier as part of other clique induction. Is there a more efficient way to go about inducing these cliques? Maybe some post processing on these cliques themselves?

neo4k
  • 159
  • 1
  • 12
  • I don't think it is that much the case of adding duplicated edges: this will scale at most with the number of keys. But cliques are simpy computationally expensive. – Willem Van Onsem Jan 20 '18 at 20:51
  • 1
    To avoid adding them you'd have to keep a set of all the ones you already added. But then you'd just be duplicated work already done within the graph (presumably edge existence is checked before insertion). You'd be better off benchmarking your code first before you assume this is a bottleneck. Often we make incorrect assumptions about what will be the bottleneck in our code. `combinations` is written in C, so it's about as fast as you're going to get. – Bailey Parker Jan 20 '18 at 20:52

1 Answers1

1

You may get some slight improvement if you prepare a list of all unique edges and then add them all at once:

edges = set(chain.from_iterable([tuple(sorted(pair)) for pair 
            in combinations(clique, 2)] for clique in clique_dict.values()))
graph.add_edges_from(edges)

Sorting is added to avoid having anti-parallel edges like (2,3) and (3,2).

DYZ
  • 55,249
  • 10
  • 64
  • 93
  • I can make sure that the list will always be in sorted order, so that is fine. As you said, this may benefit slightly, but maybe there is a better approach? Some post processing on the cliques lists themselves? – neo4k Jan 20 '18 at 21:15