Skip to content

Commit

Permalink
[MERGE] Multiprocessing max clique solver
Browse files Browse the repository at this point in the history
[MERGE] Multiprocessing max clique solver
  • Loading branch information
alexgiving authored Oct 1, 2023
1 parent ac0aa74 commit 5e68ace
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 17 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# HeuroGraph Library

![GitHub stars](https://img.shields.io/github/stars/alexgiving/HeuroGraph-Library?style=social)
<!-- ![GitHub stars](https://img.shields.io/github/stars/alexgiving/HeuroGraph-Library?style=social)
![GitHub forks](https://img.shields.io/github/forks/alexgiving/HeuroGraph-Library?style=social)
![GitHub issues](https://img.shields.io/github/issues/alexgiving/HeuroGraph-Library)
![GitHub contributors](https://img.shields.io/github/contributors/alexgiving/HeuroGraph-Library)
![GitHub contributors](https://img.shields.io/github/contributors/alexgiving/HeuroGraph-Library) -->

Welcome to the HeuroGraph-Library! This is a Python library designed for solving graph-related problems using heuristic algorithms. Whether you're working on graph coloring, maximum clique, or other graph optimization problems, this library provides a set of powerful heuristic algorithms to assist you in finding efficient solutions.

Expand Down
4 changes: 2 additions & 2 deletions benchmark_coloring.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

from heurograph import Graph
from heurograph.coloring import (color_graph_greedy_randomized_sorted,
color_graph_greedy_sorted,
color_graph_greedy_sorted_shuffled)
color_graph_greedy_sorted,
color_graph_greedy_sorted_shuffled)


class ResultColumns(Enum):
Expand Down
45 changes: 32 additions & 13 deletions heurograph/clique/greedy_randomized_clique.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import random
from functools import partial
from multiprocessing import Pool, cpu_count

import networkx as nx
import numpy as np
Expand All @@ -15,27 +17,44 @@ def nx_clique(graph: Graph) -> list[int]:
return best_clique


def _get_clique(start_vertex: int, graph: Graph) -> list[int]:
clique = [start_vertex]
vertices_candidates = graph.neighbors(start_vertex)
np.random.shuffle(vertices_candidates)

for vertex in vertices_candidates:
is_connected = True
for clique_vertex in clique:
if clique_vertex not in graph.neighbors(vertex):
is_connected = False
break
if is_connected:
clique.append(vertex)
return clique


def greedy_randomized_clique(graph: Graph, iterations: int = 10) -> list[int]:
best_clique = []
vertices = graph.vertexes

for _ in range(iterations):
clique = []
random_vertex = random.choice(vertices)
clique = _get_clique(random_vertex, graph)
if len(clique) > len(best_clique):
best_clique = clique
return best_clique

clique.append(random_vertex)

vertices_candidates = graph.neighbors(random_vertex)
np.random.shuffle(vertices_candidates)
def greedy_randomized_mp_clique(graph: Graph, iterations: int = 10, num_proc: int = -1) -> list[int]:
best_clique = []
vertices = graph.vertexes

for vertex in vertices_candidates:
is_connected = True
for clique_vertex in clique:
if clique_vertex not in graph.neighbors(vertex):
is_connected = False
break
if is_connected:
clique.append(vertex)
if len(clique) > len(best_clique):
best_clique = clique
random_vertices = np.random.choice(vertices, iterations)
num_process = num_proc if num_proc > 1 else cpu_count()

partial_get_clique = partial(_get_clique, graph=graph)
with Pool(num_process) as pool:
cliques = pool.map(partial_get_clique, random_vertices)
best_clique = max(cliques, key=len)
return best_clique

0 comments on commit 5e68ace

Please sign in to comment.