-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTest.py
65 lines (48 loc) · 1.84 KB
/
Test.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# from google.cloud import translate_v3beta1 as translate
# client = translate.TranslationServiceClient()
#
# project_id = "myprojectr"
# text = 'Text you wish to translate'
# location = 'global'
#
# parent = client.location_path(project_id, location)
#
# response = client.translate_text(
# parent=parent,
# contents=[text],
# mime_type='text/plain', # mime types: text/plain, text/html
# source_language_code='en-US',
# target_language_code='sr-Latn')
#
# for translation in response.translations:
# print('Translated Text: {}'.format(translation))
import networkx as nx
from networkx.algorithms.coloring import greedy_coloring
graph = nx.Graph()
def build_graph_from_file(file="graph.txt"):
file = open(file, "r")
lines = file.readlines()
num_vertices = int(lines[0])
for i in range(0, num_vertices):
graph.add_node(i)
for i in range(1, len(lines)):
v1, v2 = lines[i].split()
graph.add_edge(int(v1), int(v2))
print "Original Graph Edges count: ", str(len(graph.edges()))
#extending the original graph
augmented_graph = nx.Graph()
for edge in graph.edges():
augmented_graph.add_edge(edge[0], edge[1])
for node in graph.nodes():
neighbours = graph.neighbors(node)
for neighbour in neighbours:
n_neighbours = graph.neighbors(neighbour)
for n_neighbour in n_neighbours:
if not graph.has_edge(node, n_neighbour):
augmented_graph.add_edge(node, n_neighbour)
print "Augmented Graph Edges count: ", str(len(augmented_graph.edges()))
return augmented_graph
g = build_graph_from_file() #buiding an augmented graph
node_colors = greedy_coloring.greedy_color(g) #running first order coloring algorithm
print "Number of Colors required: ", str(len(set(node_colors.values())))
print node_colors