Implemented kebab algo
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Pünkösd Marcell 2020-09-24 15:43:13 +02:00
parent aed7f80c42
commit 69594217cf

View File

@ -1,6 +1,7 @@
import numpy as np import numpy as np
import networkx as nx import networkx as nx
import argparse import argparse
import time
parser = argparse.ArgumentParser(description='') parser = argparse.ArgumentParser(description='')
parser.add_argument('-g', '--graph', dest='graph', action='store', default=None) parser.add_argument('-g', '--graph', dest='graph', action='store', default=None)
@ -13,9 +14,9 @@ if not args.graph:
def read_graph(graph_file): def read_graph(graph_file):
with open(graph_file) as f: with open(graph_file) as f:
n = int(f.readline()) n = int(f.readline()) # pontok száma
m = int(f.readline()) m = int(f.readline()) # élek száma
A = [[0] * n for _ in range(n)] A = [[0] * n for _ in range(n)] # táblázat, ahol az élek helyén 1 van, ammeg 0
for _ in range(m): for _ in range(m):
[u, v] = map(int, f.readline().split()) [u, v] = map(int, f.readline().split())
A[u][v] = 1 A[u][v] = 1
@ -24,21 +25,55 @@ def read_graph(graph_file):
return G return G
input_graph = read_graph(args.graph)
def max_comp_size(G): def max_comp_size(G):
return max([len(c) for c in nx.connected_components(G)]) return max([len(c) for c in nx.connected_components(G)])
def run_random_once() -> list:
del_list = [] del_list = []
G = read_graph(args.graph) G = input_graph.copy()
n = len(G.nodes) target = len(G.nodes) / 2
while max_comp_size(G) > n / 2: while max_comp_size(G) > target:
u = np.random.choice(G.nodes) u = np.random.choice(G.nodes)
if u not in del_list: if u not in del_list:
del_list.append(u) del_list.append(u)
G.remove_node(u) G.remove_node(u)
# print len(del_list), del_list return del_list
for u in del_list: def remove_highest_kebab() -> list:
print(u) del_list = []
G = input_graph.copy()
target = len(G.nodes) / 2
while max_comp_size(G) > target:
highest_node = None
highest_node_deg = 0
for node in G.nodes:
if G.degree[node] > highest_node_deg:
highest_node = node
highest_node_deg = G.degree[node]
del_list.append(highest_node)
G.remove_node(highest_node)
return del_list
print(len(remove_highest_kebab()))
# target_runtime = 600
# start_time = time.time()
# results = []
# while (time.time() - start_time) < target_runtime:
# for _ in range(50):
# results.append(run_random_once())
#
# print("Total runs: ", len(results))
# print("Min: ", min(results))
# print("Max: ", max(results))
# print("Runtime: ", time.time() - start_time)