-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution_greedy.py
47 lines (38 loc) · 1.43 KB
/
solution_greedy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# -*- coding: utf-8 -*-
# Created by José Miguel García Benayas
import logging
import time
from abc import ABCMeta, abstractmethod
from graph_utils import GraphUtils
class SolutionGreedy(metaclass=ABCMeta):
LOGGER = logging.getLogger(__name__)
def __init__(self, graph, name):
self.name = name
self.graph = graph
self.density = self.graph.calculate_density()
self.clique = []
self.sol_value = 0.0
self.cardinality = 0.0
self.compute_time = 0.0
def find_clique(self, vertex):
clique = [vertex]
start_time = time.time()
self.find_clique_aux(vertex, clique)
finish_time = time.time()
self.clique = clique
self.cardinality = len(self.clique)
self.sol_value = GraphUtils.calculate_clique_ratio(self.graph, self.clique)
self.compute_time = finish_time - start_time
return clique
def find_clique_aux(self, father, clique):
adjacent = self.graph.get_node(father).neighbors_indices.copy()
while len(adjacent) != 0:
candidate = self.find_better(adjacent)
if GraphUtils.become_clique(self.graph, clique, candidate):
adjacent = GraphUtils.discard_adjacent(self.graph, adjacent, candidate)
clique.append(candidate)
else:
adjacent.remove(candidate)
@abstractmethod
def find_better(self, adjacent):
pass