upload exercise files
This commit is contained in:
41
destroy_p3.py
Normal file
41
destroy_p3.py
Normal file
@@ -0,0 +1,41 @@
|
||||
import numpy as np
|
||||
import networkx as nx
|
||||
import argparse
|
||||
|
||||
parser=argparse.ArgumentParser(description='')
|
||||
parser.add_argument('-g', '--graph', dest='graph', action='store', default=None)
|
||||
args=parser.parse_args()
|
||||
|
||||
if not args.graph:
|
||||
print ("need a graph as input")
|
||||
exit()
|
||||
|
||||
def read_graph(graph_file):
|
||||
with open(graph_file) as f:
|
||||
n=int(f.readline())
|
||||
m=int(f.readline())
|
||||
A=[[0]*n for _ in range(n)]
|
||||
for _ in range(m):
|
||||
[u,v]=map(int, f.readline().split())
|
||||
A[u][v]=1
|
||||
A[v][u]=1
|
||||
G=nx.Graph(np.array(A))
|
||||
return G
|
||||
|
||||
def max_comp_size(G):
|
||||
return max([len(c) for c in nx.connected_components(G)])
|
||||
|
||||
del_list=[]
|
||||
G=read_graph(args.graph)
|
||||
n=len(G.nodes)
|
||||
while max_comp_size(G) > n/2:
|
||||
u=np.random.choice(G.nodes)
|
||||
if u not in del_list:
|
||||
del_list.append(u)
|
||||
G.remove_node(u)
|
||||
|
||||
#print len(del_list), del_list
|
||||
|
||||
|
||||
for u in del_list:
|
||||
print (u)
|
||||
Reference in New Issue
Block a user