-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathgreedy.py
45 lines (35 loc) · 1.11 KB
/
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
import math
import numpy as np
import random
import networkx as nx
class Greedy(object):
def __init__(self, Ma):
self.Ma = Ma
self.n = Ma.shape[0]
self.solution = np.zeros( self.n ) - 1 #no color assigned to any vertex
def execute(self):
self.solution[0] = 0 #first color to first vertex
all_colors = set( range(self.n) )
unavailable = set()
for i in range(1, self.n):
for j in range( 0, self.n):
if self.Ma[i,j] == 1:
if self.solution[j] != -1:
unavailable.add( self.solution[j] )
#end if
#end if
#end for
available = all_colors - unavailable
self.solution[i] = np.amin( np.array( list( available )))
unavailable.clear()
#end for
for i in range(self.n):
for j in range( i + 1, self.n):
if self.Ma[i,j] == 1 and self.solution[i] == self.solution[j]:
return -1
return np.amax( self.solution) + 1
if __name__ == "__main__":
g = nx.chvatal_graph()
g = nx.to_numpy_matrix( g )
greedy = Greedy( g )
print( greedy.execute() )