-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest7.py
More file actions
166 lines (142 loc) · 4.07 KB
/
Copy pathtest7.py
File metadata and controls
166 lines (142 loc) · 4.07 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
from operator import itemgetter
import random
def graphConv(filename):
graph = {}
size = 0
with open(filename, 'r') as file:
for line in file:
contents = line.split()
if contents[0] == 'p':
size = int(contents[2])
break
for i in range(1, size + 1):
graph[i] = []
with open(filename, 'r') as file:
for line in file:
edge = line.split()
if edge[0] == 'e':
e1 = int(edge[1])
e2 = int(edge[2])
graph[e1].append(e2)
graph[e2].append(e1)
return graph
def MIS(root, neigh, graph):
for i in range(len(neigh)):
neighbors = []
for n in neigh:
neighbors.append(n)
allnodes = []
for key in graph.keys():
allnodes.append(key)
allnodes.remove(root)
miset = []
guy = neighbors.pop(i)
miset.append(guy)
adj = graph[guy]
allnodes.remove(guy) #remove v
#print(allnodes)
#i = 0
while len(neighbors) != 0:
#print(miset)
#print(i)
#print("hello2")
#print(len(neighbors))
for friend in adj:
if friend in neighbors:
neighbors.remove(friend)
if friend in allnodes: #remove all its neighbors
allnodes.remove(friend)
#print(allnodes)
if len(neighbors) != 0:
guy = neighbors.pop(0)
allnodes.remove(guy)
miset.append(guy)
adj = graph[guy]
#i += 1
while len(allnodes) != 0:
#print("hello3")
for friend in adj:
if friend in allnodes:
allnodes.remove(friend)
#print(allnodes)
if len(allnodes) != 0:
guy = allnodes.pop(0)
#print(guy)
miset.append(guy)
adj = graph[guy]
if len(miset) > 1:
return miset
else:
continue
return ["hello"]
def connected(root, graph):
adj = graph[root]
size = len(graph.keys()) - 1
result = True
for neighbor in adj:
if len(graph[neighbor]) != size:
result = False
break
return result
def reduction(root, graph):
G = graph.copy()
neighbors = G[root]
#print(neighbors)
misam = MIS(root, neighbors, G)
while not connected(root, G):
#print("hello1")
merged = misam[0]
for i in range(1, len(misam)):
node = misam[i]
for item in G[node]:
#if len(misam) == 5:
# print(G[node])
# print(item)
G[item].remove(node)
if item not in G[merged]:
G[merged].append(item)
G[item].append(merged)
G.pop(node, None)
neighbors = G[root]
misam = MIS(root, neighbors, G)
return G
def complete(graph):
result = True
size = len(graph.keys()) - 1
for node in graph.keys():
if len(graph[node]) != size:
result = False
break
return result
def iterate(filename, vertex):
graph = graphConv(filename)
graph = reduction(vertex, graph)
return complete(graph)
'''
def MCP(filename):
G = graphConv(filename)
for vertex in G.keys():
graph = {}
for item in G.keys():
graph[item] = G[item]
print(G[1])
graph = reduction(vertex, graph)
print(G[1])
if not complete(graph):
vadj = G[vertex]
for item in vadj:
G[item].remove(vertex)
G.pop(vertex, None)
print(G.keys())
print(len(G.keys()))
return G
'''
def MCP(filename):
G = graphConv(filename)
counter = 0
for vertex in G.keys():
if iterate(filename, vertex):
counter += 1
print(counter)
return counter
MCP("c125.txt")