Package: kpkc
PyKPartiteKCliqueA python wrapper ofhttps://github.com/kliem/KPartiteKClique.Iterate over all k-cliques of a k-partite graph.RequirementssetuptoolsCythonQuick startA trivial example:>>> from kpkc import KCliqueIterator
>>> edges = [[1, 2]]
>>> parts = [[1], [2]]
>>> it = KCliqueIterator(edges, parts)
>>> list(it)
[[1, 2]]The default algorithm iskpkc, which first selects nodes with few
edges:>>> parts = [[1, 2, 3, 4], [5, 6, 7, 8, 9]]
>>> edges = [[1, 6], [5, 2], [5, 3]]
>>> edges += [[i, j] for i in range(2, 5) for j in range(6, 10)]
>>> it = KCliqueIterator(edges, parts)
>>> list(it)[:3]
[[1, 6], [3, 5], [2, 5]]The algorithmFindCliquefirst selects parts with few nodes:>>> parts = [[1, 2, 3, 4], [5, 6, 7, 8, 9]]
>>> edges = [[1, 6], [5, 2], [5, 3]]
>>> edges += [[i, j] for i in range(2, 5) for j in range(6, 10)]
>>> it = KCliqueIterator(edges, parts, algorithm='FindClique')
>>> list(it)
[[1, 6], [2, 5], [2, 6], [2, 7], [2, 8], [2, 9], [3, 5], [3, 6], [3, 7], [3, 8], [3, 9], [4, 6], [4, 7], [4, 8], [4, 9]]BenchmarksWe benchmark the following algorithms/implementations:kpkc(our implementation)FindClique(our implementation)Cliquer(exposed viaSageMath)networkxmcqd(exposed viaSageMath)For this we use three types of graphs:Graphs insample_graphs/that can be tested withkpkc.test.load_tester.Also we benchmark random graphs with parameters(k, min_s, max_s, a_1, a_2),
wherekis the number of parts,
each part has size in[min_s, max_s]chosen with uniform distribution.
Each vertexvis assigned a random floatp(v)chosen with uniform
distribution from[a_1, a_2].
For all pairsv,wfrom different parts the edge is generated with
probability(p(v) + p(w))/2.This approach is described in:Grünert, Tore & Irnich, Stefan & Zimmermann, Hans-Jürgen & Schneider, Markus & Wulfhorst, Burkhard. (2001). Cliques in k-partite Graphs and their Application in Textile EngineeringSuch a random graph can be obtain withkpkc.test.get_random_k_partite_graph(k, min_s, max_s, a_1, a_2).In addition we benchmark examples with parameters(k, max_s, a),
wherekis the number of parts.
Parts have sizes1 + ((max_s -1) * i) // kforiin 1, ..., k.Letfbe the affine function determined byf(1) = 1andf(max_s) = a.
For all pairsv,wfrom different parts with sizess,t,
the edge is generated with probabilityf(min(s, t)).This means parts of size 1 will have all neighbors and the more
vertices a part has, the lower will be the density of its edges.Such a random graph can be obtain withkpkc.test.get_random_k_partite_graph_2(k, max_s, a).In many contexts this might be a more natural choice than the above
random graph. If the k-clique corresponds to some matching, than
this corresponds to the fact that fewer choices means that people will
be less picky. Example:Suppose there is only one cement mill in the area, two concrete
pumps, twenty conrete mixer trucks, and twenty concrete crews.
Nobody can question the quality of the cement mill, because there is
no alternative.
As there is only two concrete pumps, the truck drivers will usually
be willing to work with both of them.
Likewise the concrete crews will usually put up with both pump
operators.
However, it is very much possible that the conrete crews might refuse
to work with some truck drivers (always late) or the truck drivers
might refuse to work with some crews (always order more trucks than
they need).In particular, the graphs insample_graphs/behave somewhat like
this:
Vertices in smaller parts have more neighbors than vertices in larger
parts.
There are more than 14 million of those that we would like to
check. This is feasible withkpkcand appears infeasible with the
other implementations.The results have been obtained with an Intel i7-7700 CPU @3.60GHz.Checking for a k-cliqueWe time how long it takes to either determine the clique number or to
find the first k-clique, if any.Note that the graphs insample_graphsdo not have k-cliques.nanindicates that the computation was interrupted after 1000s (without
determination).GraphkpkcFindCliquenetworkxCliquermcqd01.73e+01nannannannan11.73e+01nannannannan21.72e+01nannannannan205.07e+00nannannannan1001.74e+01nannannannan10004.89e-012.15e+01nannannan100001.78e-013.62e+00nan6.26e+02nan10000001.32e+00nannannannan20000001.58e-017.63e-01nan9.44e+00nan50000001.87e-017.95e+00nannannan100000002.25e-026.11e-03nan1.24e+00nan(5, 50, 50, 0.14, 0.14)5.61e-042.31e-051.08e-021.28e-031.20e-03(5, 50, 50, 0.15, 0.15)2.78e-042.00e-053.57e-031.39e-031.27e-03(5, 50, 50, 0.2, 0.2)4.17e-055.01e-061.77e-031.67e-031.60e-03(5, 50, 50, 0.25, 0.25)5.44e-053.34e-061.36e-032.21e-032.06e-03(5, 50, 50, 0.0, 0.3)1.93e-044.77e-061.22e-031.27e-031.26e-03(5, 50, 50, 0.0, 0.4)3.12e-053.34e-061.15e-031.57e-031.58e-03(5, 50, 50, 0.0, 0.45)1.00e-053.34e-061.34e-032.13e-031.97e-03(5, 50, 50, 0.0, 0.5)1.22e-053.34e-061.42e-032.12e-032.00e-03(10, 26, 37, 0.49, 0.49)3.48e-035.84e-052.59e-014.55e-024.47e-02(10, 26, 37, 0.5, 0.5)9.58e-055.25e-063.51e-025.70e-023.83e-02(10, 26, 37, 0.51, 0.51)1.39e-031.14e-057.50e-011.10e-016.55e-02(10, 26, 37, 0.4, 0.6)1.88e-035.96e-062.17e-017.19e-024.76e-02(10, 26, 37, 0.3, 0.7)6.03e-041.03e-051.28e-021.52e-015.06e-02(10, 50, 50, 0.42, 0.42)1.33e-022.52e-041.09e+001.07e-011.13e-01(10, 50, 50, 0.43, 0.43)4.42e-031.56e-041.70e+001.80e-011.31e-01(10, 50, 50, 0.44, 0.44)2.25e-028.18e-053.16e-012.14e-011.70e-01(10, 50, 50, 0.46, 0.46)5.23e-032.17e-052.67e-023.24e-012.75e-01(10, 50, 50, 0.48, 0.48)9.79e-043.22e-054.92e-024.04e-013.95e-01(10, 50, 50, 0.5, 0.5)7.41e-049.78e-061.58e-021.16e+006.94e-01(50, 5, 15, 0.91, 0.91)nan6.35e-02nannannan(50, 5, 15, 0.918, 0.918)nan2.81e-02nannannan(50, 5, 15, 0.92, 0.92)nan7.89e-03nannannan(20, 23, 39, 0.7, 0.7)1.87e+021.06e-01nannannan(20, 23, 39, 0.71, 0.71)4.07e+015.18e-02nannannan(20, 23, 39, 0.72, 0.72)5.12e+001.81e-03nannannan(20, 23, 39, 0.7, 0.73)2.57e+003.55e-03nannannan(20, 23, 39, 0.65, 0.78)1.04e+007.39e-05nannannan(30, 11, 30, 0.6, 0.6)4.29e-011.29e-04nan7.96e+021.21e+02(30, 11, 30, 0.7, 0.7)1.63e+011.40e-03nannannan(30, 11, 30, 0.8, 0.8)nan1.16e+00nannannan(30, 11, 30, 0.81, 0.81)nan1.33e+00nannannan(30, 11, 30, 0.82, 0.82)nan4.36e-01nannannan(30, 11, 30, 0.84, 0.84)nan5.73e-03nannannan(30, 11, 30, 0.88, 0.88)1.44e+001.22e-05nannannan(100, 10, 10, 0.7, 0.7)5.20e-024.67e-05nannannan(100, 10, 10, 0.8, 0.8)4.76e+003.47e-04nannannan(100, 10, 10, 0.85, 0.85)2.21e+021.94e-03nannannan(100, 10, 10, 0.9, 0.9)nan1.49e-01nannannan(100, 10, 10, 0.92, 0.92)nan4.19e+00nannannan(100, 10, 10, 0.94, 0.94)nannannannannan(100, 10, 10, 0.95, 0.95)nannannannannan(100, 10, 10, 0.97, 0.97)nan2.61e-03nannannan(3, 100, 100, 0.1, 0.1)1.00e-052.62e-069.16e-041.29e-031.20e-03(4, 100, 100, 0.15, 0.15)1.45e-053.81e-062.01e-033.61e-033.47e-03(5, 100, 100, 0.2, 0.2)3.41e-053.34e-064.94e-038.59e-038.80e-03(6, 100, 100, 0.25, 0.25)1.02e-046.44e-068.21e-032.33e-022.31e-02(7, 50, 50, 0.35, 0.35)3.46e-056.91e-067.86e-031.26e-021.15e-02(8, 50, 50, 0.4, 0.4)1.40e-046.68e-061.17e-023.85e-023.41e-02(9, 50, 50, 0.45, 0.45)1.50e-045.98e-058.81e-031.71e-011.25e-01(10, 50, 50, 0.5, 0.5)8.56e-041.24e-051.43e-011.48e+006.43e-01(5, 10, 0.1)6.91e-062.86e-061.08e-048.94e-051.07e-04(5, 10, 0.2)5.48e-062.62e-069.66e-057.39e-059.49e-05(5, 20, 0.05)6.91e-062.86e-062.21e-042.05e-042.45e-04(5, 20, 0.1)6.44e-062.86e-062.56e-042.08e-042.54e-04(5, 50, 0.01)1.22e-053.10e-069.59e-041.09e-031.17e-03(5, 50, 0.02)1.10e-053.10e-069.73e-041.06e-031.20e-03(10, 10, 0.4)1.19e-053.34e-064.68e-042.12e-042.93e-04(10, 10, 0.6)1.53e-053.58e-063.96e-043.01e-043.04e-04(10, 20, 0.3)2.00e-054.29e-062.03e-038.79e-041.06e-03(10, 20, 0.5)2.31e-053.81e-061.33e-038.62e-043.01e-03(10, 50, 0.05)4.84e-053.81e-061.91e-023.87e-034.93e-03(10, 50, 0.1)6.82e-057.15e-064.94e-034.09e-035.39e-03(10, 100, 0.01)6.79e-051.12e-051.90e-021.74e-027.28e-02(10, 100, 0.02)7.37e-055.25e-061.35e-021.78e-027.60e-02(20, 10, 0.6)2.16e-048.54e-051.62e+003.42e-032.46e-03(20, 10, 0.7)6.68e-055.48e-064.99e-011.90e-028.59e-03(20, 20, 0.5)6.92e-045.39e-056.10e+003.48e-036.79e-02(20, 20, 0.6)1.01e-047.63e-061.37e-023.11e-039.25e-01(20, 50, 0.3)8.63e-011.55e+01nan4.66e-028.06e+00(20, 50, 0.35)2.53e-023.24e-02nan2.27e-029.18e+00(20, 100, 0.2)1.40e-011.40e+02nan1.21e-016.80e+01(20, 100, 0.25)1.03e-018.80e-01nan8.89e-025.48e+02(50, 10, 0.83)1.04e+002.67e-03nan1.36e+022.13e+02(50, 10, 0.85)2.09e-012.95e-03nannan9.45e+02(50, 20, 0.5)2.08e-012.00e-02nannannan(50, 20, 0.6)2.52e+004.22e-01nannannan(50, 20, 0.7)1.58e+022.38e+01nannannan(50, 20, 0.71)3.04e+022.48e+02nannannan(50, 20, 0.72)nan7.12e+02nannannan(50, 20, 0.73)nan7.61e+02nannannan(50, 20, 0.75)nannannannannan(50, 20, 0.76)nannannannannan(50, 20, 0.77)nan1.56e+01nannannan(50, 20, 0.78)nan1.50e+00nannannan(50, 20, 0.79)nan1.89e-01nannannan(50, 20, 0.8)nan6.43e-03nannannan(50, 50, 0.1)3.63e-024.46e+00nannannan(50, 50, 0.2)1.28e-013.33e+01nannannan(50, 50, 0.3)2.09e+001.74e+02nannannan(50, 50, 0.4)2.46e+01nannannannan(50, 50, 0.5)8.03e+02nannannannan(50, 50, 0.6)nannannannannan(50, 50, 0.71)nannannannannan(50, 50, 0.72)nan2.99e+01nan1.83e+02nan(50, 50, 0.73)nan1.17e+01nannannan(50, 50, 0.74)nan1.55e+00nannannan(50, 100, 0.1)2.43e-01nannannannan(50, 100, 0.2)1.11e+01nannannannan(50, 100, 0.3)2.35e+02nannannannan(50, 100, 0.4)nannannannannan(50, 100, 0.64)nannannannannan(50, 100, 0.65)nannannan1.19e+02nan(50, 100, 0.66)nannannannannan(50, 100, 0.67)nannannannannan(50, 100, 0.68)nannannannannan(50, 100, 0.69)nan2.28e+01nannannan(50, 100, 0.7)nan2.27e+00nan4.29e+01nan(100, 10, 0.6)6.79e-039.54e-06nannannan(100, 10, 0.7)2.35e-024.43e-05nannannan(100, 10, 0.8)6.69e-011.71e-03nannannan(100, 20, 0.4)4.81e-021.65e-03nannannan(100, 20, 0.5)4.35e-011.33e-02nannannan(100, 20, 0.6)4.65e+009.83e-02nannannan(100, 20, 0.7)4.75e+029.96e+00nannannan(100, 20, 0.8)nannannannannan(100, 20, 0.89)nannannannannan(100, 20, 0.9)nan2.43e+00nannannan(100, 50, 0.1)1.78e-017.15e+00nannannan(100, 50, 0.2)2.92e-018.19e+01nannannan(100, 50, 0.3)8.12e+004.96e+02nannannan(100, 50, 0.4)4.61e+01nannannannan(100, 50, 0.5)nannannannannan(100, 50, 0.87)nannannannannan(100, 50, 0.88)nan4.24e+00nannannan(100, 50, 0.89)nan1.85e-03nannannan(100, 50, 0.9)nan2.31e-04nannannan(100, 100, 0.1)8.92e-01nannannannan(100, 100, 0.2)4.16e+01nannannannan(100, 100, 0.3)2.60e+02nannannannan(100, 100, 0.4)nannannannannan(100, 100, 0.85)nannannannannan(100, 100, 0.86)nan7.75e+00nannannan(100, 100, 0.87)nan3.72e-03nannannan(100, 100, 0.88)nan9.73e-05nannannan(100, 100, 0.89)nan9.06e-05nannannan(100, 100, 0.9)nan7.77e-05nannannanFinding all k-cliquesWe time how long it takes to find all k-clique,
if this time differs from above.GraphkpkcFindCliquenetworkx(5, 50, 50, 0.15, 0.15)5.86e-043.34e-051.27e-02(5, 50, 50, 0.2, 0.2)8.09e-041.06e-042.39e-02(5, 50, 50, 0.25, 0.25)2.29e-036.21e-044.34e-02(5, 50, 50, 0.0, 0.3)6.28e-045.96e-051.33e-02(5, 50, 50, 0.0, 0.4)1.09e-032.58e-042.48e-02(5, 50, 50, 0.0, 0.45)3.20e-031.32e-034.34e-02(5, 50, 50, 0.0, 0.5)4.15e-032.01e-034.48e-02(10, 26, 37, 0.49, 0.49)4.04e-028.25e-049.43e+00(10, 26, 37, 0.5, 0.5)3.48e-027.26e-048.26e+00(10, 26, 37, 0.51, 0.51)6.90e-021.17e-031.42e+01(10, 26, 37, 0.4, 0.6)4.68e-029.55e-041.39e+01(10, 26, 37, 0.3, 0.7)6.58e-024.12e-031.27e+01(10, 50, 50, 0.42, 0.42)9.31e-021.38e-032.14e+01(10, 50, 50, 0.43, 0.43)1.21e-011.94e-032.78e+01(10, 50, 50, 0.44, 0.44)1.51e-012.48e-033.46e+01(10, 50, 50, 0.46, 0.46)3.04e-014.25e-035.52e+01(10, 50, 50, 0.48, 0.48)5.73e-017.96e-038.39e+01(10, 50, 50, 0.5, 0.5)9.97e-011.97e-021.72e+02(50, 5, 15, 0.918, 0.918)nan1.06e+01nan(50, 5, 15, 0.92, 0.92)nan2.91e+00nan(20, 23, 39, 0.7, 0.7)3.20e+021.93e-01nan(20, 23, 39, 0.71, 0.71)5.35e+023.45e-01nan(20, 23, 39, 0.72, 0.72)9.55e+026.42e-01nan(20, 23, 39, 0.7, 0.73)nan8.67e-01nan(20, 23, 39, 0.65, 0.78)4.54e+024.36e-01nan(30, 11, 30, 0.82, 0.82)nan3.45e+00nan(30, 11, 30, 0.84, 0.84)nan5.13e+01nan(3, 100, 100, 0.1, 0.1)2.57e-031.64e-036.51e-03(4, 100, 100, 0.15, 0.15)4.71e-032.08e-034.01e-02(5, 100, 100, 0.2, 0.2)1.17e-022.23e-031.90e-01(6, 100, 100, 0.25, 0.25)3.56e-022.80e-039.96e-01(7, 50, 50, 0.35, 0.35)1.56e-028.15e-047.38e-01(8, 50, 50, 0.4, 0.4)4.42e-021.91e-033.89e+00(9, 50, 50, 0.45, 0.45)1.74e-014.50e-032.05e+01(10, 50, 50, 0.5, 0.5)9.25e-011.67e-021.31e+02(5, 10, 0.1)8.42e-055.44e-054.51e-04(5, 10, 0.2)1.61e-041.08e-045.16e-04(5, 20, 0.05)1.26e-039.48e-043.76e-03(5, 20, 0.1)3.38e-032.70e-036.13e-03(5, 50, 0.01)9.16e-027.71e-021.55e-01(5, 50, 0.02)1.11e-019.42e-021.66e-01(10, 10, 0.4)4.48e-031.99e-032.19e-02(10, 10, 0.6)2.22e-021.15e-025.11e-02(10, 20, 0.3)4.96e-022.72e-027.90e-01(10, 20, 0.5)1.16e+007.80e-013.28e+00(10, 50, 0.05)4.92e-026.55e-023.68e+01(10, 50, 0.1)2.37e-011.89e-014.76e+01(10, 100, 0.01)5.87e+007.79e+00nan(10, 100, 0.02)8.49e+009.93e+00nan(20, 10, 0.6)3.58e-038.23e-042.36e+01(20, 10, 0.7)1.23e+004.68e-011.11e+02(20, 20, 0.5)6.57e-024.46e-02nan(20, 20, 0.6)2.82e+011.61e+01nan(20, 50, 0.35)4.09e+006.97e+01nan(20, 100, 0.2)9.58e+00nannan(20, 100, 0.25)4.70e+01nannan(50, 10, 0.83)1.30e+014.04e-01nan(50, 10, 0.85)nan3.50e+01nanConclusionAccording to the above timings,kpkcandFindCliqueappear to be best choices for finding k-cliques in k-partite graphs.If all vertices are expected to have somewhat the same number of neighbors,
thenFindCliqueis the best choice.If there are many edges and the expected number of k-cliques is large,
thenFindCliqueis the best choice to obtain
some k-cliques.If only few k-cliques (if any) are exepcted and
vertices in larger parts have fewer neighbors
than vertices in smaller parts, thenkpkcis
the best choice to obtain all k-cliques.Note that our implementation ofFindCliqueappears to be faster
in finding all k-cliques than the original implementations (which are
not published) inGrünert, Tore & Irnich, Stefan & Zimmermann, Hans-Jürgen & Schneider, Markus & Wulfhorst, Burkhard. (2001). Cliques in k-partite Graphs and their Application in Textile EngineeringandMirghorbani, M. & Krokhmal, P.. (2013). On finding k-cliques in k-partite graphs. Optimization Letters. 7. 10.1007/s11590-012-0536-yFor random graphs with parameters(k, min_s, max_s, a_1, a_2)andk = 100we improve the benchmarks of Grünert et. all by a factor
of 100. Note that we also use 3.6 GHz instead of 100 MHz leaving an
improvement factor of about 28.Mirghorbani et. al. improved the implementation ofFindCliqueby a
factor of up to9in comparison to Grünert et. al.