halozatrombolas/destroy_p3.py

45 lines
1013 B
Python
Raw Normal View History

2020-09-24 12:49:49 +02:00
import numpy as np
import networkx as nx
import argparse
2020-09-24 13:01:30 +02:00
parser = argparse.ArgumentParser(description='')
2020-09-24 12:49:49 +02:00
parser.add_argument('-g', '--graph', dest='graph', action='store', default=None)
2020-09-24 13:01:30 +02:00
args = parser.parse_args()
2020-09-24 12:49:49 +02:00
if not args.graph:
2020-09-24 13:01:30 +02:00
print("need a graph as input")
2020-09-24 12:49:49 +02:00
exit()
2020-09-24 13:01:30 +02:00
2020-09-24 12:49:49 +02:00
def read_graph(graph_file):
with open(graph_file) as f:
2020-09-24 13:01:30 +02:00
n = int(f.readline())
m = int(f.readline())
A = [[0] * n for _ in range(n)]
2020-09-24 12:49:49 +02:00
for _ in range(m):
2020-09-24 13:01:30 +02:00
[u, v] = map(int, f.readline().split())
A[u][v] = 1
A[v][u] = 1
G = nx.Graph(np.array(A))
2020-09-24 12:49:49 +02:00
return G
2020-09-24 13:01:30 +02:00
2020-09-24 12:49:49 +02:00
def max_comp_size(G):
return max([len(c) for c in nx.connected_components(G)])
2020-09-24 13:01:30 +02:00
del_list = []
G = read_graph(args.graph)
n = len(G.nodes)
while max_comp_size(G) > n / 2:
u = np.random.choice(G.nodes)
2020-09-24 12:49:49 +02:00
if u not in del_list:
del_list.append(u)
G.remove_node(u)
2020-09-24 13:01:30 +02:00
# print len(del_list), del_list
2020-09-24 12:49:49 +02:00
for u in del_list:
2020-09-24 13:01:30 +02:00
print(u)