halozatrombolas/analyse_p3.py

38 lines
1.2 KiB
Python
Raw Permalink Normal View History

2020-09-24 12:49:49 +02:00
import numpy as np
import networkx as nx
import argparse
import sys
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)
parser.add_argument('-d', '--destroy', dest='destroy', action='store', default=None)
2020-09-24 13:01:30 +02:00
parser.add_argument('-n', '--node', dest='isnode', action='store_const', const=False, default=True)
args = parser.parse_args()
2020-09-24 12:49:49 +02:00
if not args.graph or not args.destroy:
2020-09-24 13:01:30 +02:00
sys.stderr.write('need graph and destroylist as argument -g and/or -m')
exit(1)
2020-09-24 12:49:49 +02:00
2020-09-24 13:01:30 +02:00
D = []
2020-09-24 12:49:49 +02:00
with open(args.destroy) as inf:
for line in inf:
D.append(int(line))
with open(args.graph) as ing:
2020-09-24 13:01:30 +02:00
n = int(ing.readline())
m = int(ing.readline())
A = [[0] * n for _ in range(n)]
2020-09-24 12:49:49 +02:00
for _ in range(m):
if not args.isnode:
if _ in D:
continue
2020-09-24 13:01:30 +02:00
[u, v] = map(int, ing.readline().split())
2020-09-24 12:49:49 +02:00
if args.isnode:
if u in D or v in D:
2020-09-24 13:01:30 +02:00
continue
A[u][v] = 1
A[v][u] = 1
G = nx.Graph(np.array(A))
2020-09-24 12:49:49 +02:00
2020-09-24 13:01:30 +02:00
print(len(D), [len(c) for c in sorted(nx.connected_components(G), key=len, reverse=True)])