def greedy_local_heuristic(sccs,degree_dict,queue): edges_to_be_removed = [] while True: graph = sccs.pop() temp_nodes_degree_dict = {} for node in graph.nodes(): temp_nodes_degree_dict[node] = degree_dict[node][0] from helper_funs import pick_from_dict max_node,_ = pick_from_dict(temp_nodes_degree_dict) max_value = degree_dict[max_node] #degrees = [(node,degree_dict[node]) for node in list(graph.nodes())] #max_node,max_value = max(degrees,key = lambda x: x[1][0]) if max_value[1] == "in": # indegree > outdegree, remove out-edges edges = [(max_node,o) for o in graph.neighbors(max_node)] else: # outdegree > indegree, remove in-edges edges = [(i,max_node) for i in graph.predecessors(max_node)] edges_to_be_removed += edges sub_graphs = filter_big_scc(graph,edges_to_be_removed) if sub_graphs: for index,sub in enumerate(sub_graphs): sccs.append(sub) if not sccs: break queue.put(edges_to_be_removed)
def remove_cycle_edges_by_agony(graph, players, edges_to_be_removed): pair_agony_dict = {} for pair in graph.edges(): u, v = pair agony = max(players[u] - players[v], 0) pair_agony_dict[pair] = agony from helper_funs import pick_from_dict pair_max_agony, agony = pick_from_dict(pair_agony_dict) edges_to_be_removed.append(pair_max_agony) # print("edge to be removed: %s, agony: %0.4f" % (pair_max_agony,max_agony)) sub_graphs = filter_big_scc(graph, [pair_max_agony]) if sub_graphs: num_subs = len(sub_graphs) for index, sub in enumerate(sub_graphs): # print("%d / %d scc: (%d,%d)" % (index+1,num_subs,sub.number_of_nodes(),sub.number_of_edges())) remove_cycle_edges_by_agony(sub, players, edges_to_be_removed) else: return None
def remove_cycle_edges_by_ranking_score_iterately(sccs, players, edges_to_be_removed, is_Forward): while True: graph = sccs.pop() node_scores_dict = {} for node in graph.nodes_iter(): node_scores_dict[node] = players[node] from helper_funs import pick_from_dict max_k, max_v, min_k, min_v = pick_from_dict(node_scores_dict, "both") #node_scores = [(node,players[node]) for node in graph.nodes_iter()] #sorted_node_scores = sorted(node_scores,key = lambda x: x[1]) if is_Forward: node, score = max_k, max_v target_edges = [(node, v) for v in graph.successors_iter(node)] #target_edges = [(v,node) for v in graph.predecessors_iter(node)] else: node, score = min_k, min_v #node,score = sorted_node_scores.pop() target_edges = [(v, node) for v in graph.predecessors_iter(node)] ''' from remove_cycle_edges_by_agony import get_agonies pair_agony_dict = get_agonies(target_edges,players) from helper_funs import pick_from_dict pair_max_agony,agony = pick_from_dict(pair_agony_dict) print("edge with max agony: %s, max agony: %0.4f" % (pair_max_agony,agony)) target_edges = [pair_max_agony] ''' edges_to_be_removed += target_edges graph.remove_edges_from(target_edges) sub_graphs = filter_big_scc(graph, target_edges) if sub_graphs: sccs += sub_graphs if not sccs: return