def dijkstra(self, source, target): distance = {} previous = {} pque = pqdict.PQDict() for node in self: if node == source: pque[node] = 0 else: pque[node] = float("inf") for (node, node_distance) in pque.iteritems(): distance[node] = node_distance if node == target: break # XXX: hack if node != source and not node.circulatable: continue for neighbor_node in self[node]: if neighbor_node not in pque: continue try: alt_dist = distance[node] + self[node][neighbor_node] except TypeError: alt_dist = distance[node] + 1 if alt_dist < pque[neighbor_node]: pque[neighbor_node] = alt_dist previous[neighbor_node] = node return (distance, previous)
def label_by_revoting(u, ep, test_labeled): ################# # # Initialize priorities # tolabel = pqdict.PQDict() for ego in u: if "total_weight" in u.node[ego]: if u.node[ego]["orientation"] == 0: if u.node[ego]["gay_weight"] > u.node[ego]["straight_weight"]: u.node[ego]["orientation"] = -1 elif u.node[ego]["gay_weight"] < u.node[ego]["straight_weight"]: u.node[ego]["orientation"] = 1 tolabel[ego] = u.node[ego]["orientation"] * ( u.node[ego]["gay_weight"] - u.node[ego]["straight_weight"]) / u.node[ego]["total_weight"] #print ego sys.stdout.write( "%d %d %f %f %f\n" % (ego, u.node[ego]["orientation"], u.node[ego]["gay_weight"], u.node[ego]["straight_weight"], u.node[ego]["total_weight"])) ################# # # Create labels # #logfile = file("labeling_log.txt", "w") while True: (ego, score) = tolabel.popitem() if score >= 0: break for alter in u.neighbors(ego): if "total_weight" in u.node[alter]: u.node[alter]["gay_weight"] -= u.node[ego]["orientation"] * u[ ego][alter]["embeddedness"]**ep u.node[alter]["straight_weight"] += u.node[ego][ "orientation"] * u[ego][alter]["embeddedness"]**ep if u.node[alter]["orientation"] == 0: if u.node[alter]["gay_weight"] > u.node[alter][ "straight_weight"]: u.node[alter]["orientation"] = -1 elif u.node[alter]["gay_weight"] < u.node[alter][ "straight_weight"]: u.node[alter]["orientation"] = 1 tolabel[alter] = u.node[alter]["orientation"] * ( u.node[alter]["gay_weight"] - u.node[alter]["straight_weight"] ) / u.node[alter]["total_weight"] u.node[ego]["orientation"] *= -1 tolabel[ego] = u.node[ego]["orientation"] * ( u.node[ego]["gay_weight"] - u.node[ego]["straight_weight"]) / u.node[ego]["total_weight"] print "%d %f" % (ego, score) return u
def label_by_weighted_voting (u, ep): ################# # # Initialize priorities # unlabeled = pqdict.PQDict() for ego in u: if not "orientation" in u.node[ego]: count = 0 total_weight = 0 for alter in u.neighbors(ego): if "orientation" in u.node[alter]: count += u[ego][alter]["embeddedness"]**ep total_weight += u[ego][alter]["embeddedness"]**ep priority = -float(count)/float(total_weight) unlabeled[ego] = priority ################# # # Create labels # logfile = file("labeling_log.txt", "w") while len(unlabeled) > 0: (ego, score) = unlabeled.popitem() if score == 0: break gay_alters = 0.0 straight_alters = 0.0 gay_list = list() straight_list = list() for alter in u.neighbors(ego): if "orientation" in u.node[alter]: if u.node[alter]["orientation"] == 1: gay_alters += u[ego][alter]["embeddedness"]**ep gay_list.append(alter) else: straight_alters += u[ego][alter]["embeddedness"] **ep straight_list.append(alter) else: priority = -unlabeled[alter] total_weight_alters = 0.0 for alteralter in u.neighbors(alter): total_weight_alters += u[alter][alteralter]["embeddedness"]**ep priority = -(priority * float(total_weight_alters) + float((u[ego][alter]["embeddedness"])**ep))/float(total_weight_alters) unlabeled[alter] = priority if gay_alters > straight_alters: u.node[ego]["orientation"] = 1 logfile.write ("GAY\t\t"); else: u.node[ego]["orientation"] = -1 logfile.write ("STRAIGHT\t"); logfile.write ("%d: degree: %d, priority: %f, gay: %s; straight %s\n" % (ego, u.degree(ego), score, str(gay_list), str(straight_list))) #priority = -float(count)/float(len(u.neighbors(ego))) logfile.close() return u
def label_by_weighted_voting3 (u, ep, test_labeled): gay_labeled_gay = 0 gay_labeled_straight = 0 gay_unlabeled = 0 straight_labeled_straight = 0 straight_labeled_gay = 0 straight_unlabeled = 0 straight_count = 0 gay_count = 0 ################# # # Initialize priorities # unlabeled = pqdict.PQDict() for ego in u: if not "orientation" in u.node[ego]: u.node[ego]["total_weight"] = 0 count = 0 total_weight = 0 for alter in u.neighbors(ego): if "orientation" in u.node[alter]: count += u[ego][alter]["embeddedness"]**ep #total_weight += u[ego][alter]["embeddedness"]**ep u.node[ego]["total_weight"] += u[ego][alter]["embeddedness"]**ep priority = -float(count)/float(u.node[ego]["total_weight"]) unlabeled[ego] = priority ################# # # Create labels # logfile = file("labeling_log.txt", "w") while len(unlabeled) > 0: (ego, score) = unlabeled.popitem() gay_alters = 0.0 straight_alters = 0.0 gay_list = list() straight_list = list() u.node[ego]["gay_weight"] = 0 u.node[ego]["straight_weight"] = 0 for alter in u.neighbors(ego): if "orientation" in u.node[alter]: if u.node[alter]["orientation"] == 1: u.node[ego]["gay_weight"] += u[ego][alter]["embeddedness"]**ep gay_list.append(alter) else: u.node[ego]["straight_weight"] += u[ego][alter]["embeddedness"] **ep straight_list.append(alter) u.node[ego]["orientation"] = 0 return u
def label_by_voting (u): ################# # # Initialize priorities # unlabeled = pqdict.PQDict() for ego in u: if not "orientation" in u.node[ego]: count = 0 for alter in u.neighbors(ego): if "orientation" in u.node[alter]: count += 1 priority = -float(count)/float(len(u.neighbors(ego))) unlabeled[ego] = priority ################# # # Create labels # logfile = file("labeling_log.txt", "w") while len(unlabeled) > 0: (ego, score) = unlabeled.popitem() if score == 0: break gay_alters = 0 straight_alters = 0 gay_list = list() straight_list = list() for alter in u.neighbors(ego): if "orientation" in u.node[alter]: if u.node[alter]["orientation"] == 1: gay_alters += 1 gay_list.append(alter) else: straight_alters += 1 straight_list.append(alter) else: priority = -unlabeled[alter] fneighbs = float(len(u.neighbors(alter))) priority = -(priority * fneighbs + 1)/fneighbs unlabeled[alter] = priority if gay_alters > straight_alters: u.node[ego]["orientation"] = 1 else: u.node[ego]["orientation"] = -1 #priority = -float(count)/float(len(u.neighbors(ego))) return u
def dijkstra(graph, source, target=None): """ Computes the shortests paths from a source vertex to every other vertex in a graph """ # The entire main loop is O( (m+n) log n ), where n is the number of # vertices and m is the number of edges. If the graph is connected # (i.e. the graph is in one piece), m normally dominates over n, making the # algorithm O(m log n) overall. dist = {} pred = {} # Store distance scores in a priority queue dictionary pq = pqdict.PQDict() for node in graph: if node == source: pq[node] = 0 else: pq[node] = float('inf') # Remove the head node of the "frontier" edge from pqdict: O(log n). for node, min_dist in pq.iteritems(): # Each node in the graph gets processed just once. # Overall this is O(n log n). dist[node] = min_dist if node == target: break # Updating the score of any edge's node is O(log n) using pqdict. # There is _at most_ one score update for each _edge_ in the graph. # Overall this is O(m log n). for neighbor in graph[node]: if neighbor in pq: new_score = dist[node] + graph[node][neighbor] if new_score < pq[neighbor]: pq[neighbor] = new_score pred[neighbor] = node return dist, pred
def dijkstra(graph, source, target=None): ''' SP from one source to all other vertices, from: https://github.com/nvictus/priority-queue-dictionary/ blob/master/examples/dijkstra.py Uses priority queues, so is O( (m+n) log n ), where n is the number of vertices and m is the number of edges. If the graph is connected (i.e. the graph is in one piece), m normally dominates over n, making the algorithm O(m log n) overall. ''' dist = {} pred = {} # Store distance scores in a priority queue dictionary pq = pqdict.PQDict() for node in graph: if node == source: pq[node] = 0 else: pq[node] = float('inf') # Remove the head node of the "frontier" edge from pqdict: O(log n). for node, min_dist in pq.iteritems(): # Each node in the graph gets processed just once. # Overall this is O(n log n). dist[node] = min_dist if node == target: break # Updating the score of any edge's node is O(log n) using pqdict. # There is _at most_ one score update for each _edge_ in the graph. # Overall this is O(m log n). for neighbor in graph[node]: if neighbor in pq: new_score = dist[node] + graph[node][neighbor] if new_score < pq[neighbor]: pq[neighbor] = new_score pred[neighbor] = node return dist, pred
def label_by_weighted_voting(u, ep, test_labeled, threshold): gay_labeled_gay = 0 gay_labeled_straight = 0 gay_unlabeled = 0 straight_labeled_straight = 0 straight_labeled_gay = 0 straight_unlabeled = 0 straight_count = 0 gay_count = 0 ################# # # Initialize priorities # unlabeled = pqdict.PQDict() for ego in u: if not "orientation" in u.node[ego]: u.node[ego]["total_weight"] = 0 u.node[ego]["gay_weight"] = 0 u.node[ego]["straight_weight"] = 0 count = 0 total_weight = 0 for alter in u.neighbors(ego): if "orientation" in u.node[alter]: if u.node[alter]["orientation"] == 1: u.node[ego]["gay_weight"] += u[ego][alter][ "embeddedness"]**ep if u.node[alter]["orientation"] == -1: u.node[ego]["straight_weight"] += u[ego][alter][ "embeddedness"]**ep #total_weight += u[ego][alter]["embeddedness"]**ep u.node[ego]["total_weight"] += u[ego][alter][ "embeddedness"]**ep if u.node[ego]["gay_weight"] > u.node[ego]["straight_weight"]: priority = -float(u.node[ego]["gay_weight"]) / float( u.node[ego]["total_weight"]) else: priority = -float(u.node[ego]["straight_weight"]) / float( u.node[ego]["total_weight"]) unlabeled[ego] = priority ################# # # Create labels # logfile = file("labeling_log.txt", "w") while len(unlabeled) > 0: (ego, score) = unlabeled.popitem() if score == 0: break gay_alters = 0.0 straight_alters = 0.0 gay_list = list() straight_list = list() if float(u.node[ego]["gay_weight"] - u.node[ego]["straight_weight"] ) / float(u.node[ego]["total_weight"]) > threshold: u.node[ego]["orientation"] = 1 logfile.write("GAY\t\t") gay_count += 1 else: u.node[ego]["orientation"] = -1 logfile.write("STRAIGHT\t") straight_count += 1 if ego in test_labeled: if u.node[ego]["orientation"] == 1: if u.node[ego]["test_orientation"] == 1: gay_labeled_gay += 1 else: straight_labeled_gay += 1 else: if u.node[ego]["test_orientation"] == 1: gay_labeled_straight += 1 else: straight_labeled_straight += 1 logfile.write( "%d: degree: %d, priority: %f, gay: %s; straight %s\n" % (ego, u.degree(ego), score, str(gay_list), str(straight_list))) #print "%d %d %f %d %d %d %d %d %d" % (ego, u.degree(ego) , u.node[ego]["total_weight"], gay_count, straight_count, gay_labeled_gay, gay_labeled_straight, straight_labeled_gay, straight_labeled_straight) #priority = -float(count)/float(len(u.neighbors(ego))) for alter in u.neighbors(ego): if not "orientation" in u.node[alter]: if u.node[ego]["orientation"] == 1: u.node[alter]["gay_weight"] += u[ego][alter][ "embeddedness"]**ep elif u.node[ego]["orientation"] == -1: u.node[alter]["straight_weight"] += u[ego][alter][ "embeddedness"]**ep if u.node[alter]["gay_weight"] > u.node[alter][ "straight_weight"]: priority = -float(u.node[alter]["gay_weight"]) / float( u.node[alter]["total_weight"]) else: priority = -float( u.node[alter]["straight_weight"]) / float( u.node[alter]["total_weight"]) unlabeled[alter] = priority logfile.close() return u
def label_by_weighted_voting4 (u, ep, test_labeled): gay_labeled_gay = 0 gay_labeled_straight = 0 gay_unlabeled = 0 straight_labeled_straight = 0 straight_labeled_gay = 0 straight_unlabeled = 0 straight_count = 0 gay_count = 0 ################# # # Initialize priorities # unlabeled = pqdict.PQDict() for ego in u: if not "orientation" in u.node[ego]: u.node[ego]["total_weight"] = 0 count = 0 total_weight = 0 for alter in u.neighbors(ego): if "orientation" in u.node[alter]: count += u[ego][alter]["embeddedness"]**ep #total_weight += u[ego][alter]["embeddedness"]**ep u.node[ego]["total_weight"] += u[ego][alter]["embeddedness"]**ep priority = -float(count)/float(u.node[ego]["total_weight"]) unlabeled[ego] = priority ################# # # Create labels # logfile = file("labeling_log.txt", "w") while len(unlabeled) > 0: (ego, score) = unlabeled.popitem() if score == 0: break gay_alters = 0.0 straight_alters = 0.0 gay_list = list() straight_list = list() u.node[ego]["gay_weight"] = 0 u.node[ego]["straight_weight"] = 0 for alter in u.neighbors(ego): if "orientation" in u.node[alter]: if u.node[alter]["orientation"] == 1: u.node[ego]["gay_weight"] += u[ego][alter]["embeddedness"]**ep gay_list.append(alter) else: u.node[ego]["straight_weight"] += u[ego][alter]["embeddedness"] **ep straight_list.append(alter) else: priority = -unlabeled[alter] total_weight_alters = 0.0 #for alteralter in u.neighbors(alter): # total_weight_alters += u[alter][alteralter]["embeddedness"]**ep priority = -(priority * float(u.node[alter]["total_weight"]) + float((u[ego][alter]["embeddedness"])**ep))/float(u.node[alter]["total_weight"]) unlabeled[alter] = priority u.node[ego]["orientation"] = (u.node[ego]["gay_weight"] - u.node[ego]["straight_weight"])/float(u.node[ego]["total_weight"]) logfile.write ("%d: degree: %d, priority: %f, gay: %s; straight %s\n" % (ego, u.degree(ego), score, str(gay_list), str(straight_list))) print "%d %d %f %f %f %f" % (ego, u.degree(ego), u.node[ego]["gay_weight"], u.node[ego]["straight_weight"] , u.node[ego]["total_weight"], u.node[ego]["orientation"] ) #priority = -float(count)/float(len(u.neighbors(ego))) logfile.close() for ego in u: if u.node[ego]["orientation"] > 0: u.node[ego]["orientation"] = 1; else: u.node[ego]["orientation"] = -1; return u
def __init__(self, max_capacity=sys.maxint): self.max_capacity = max_capacity self.pq = pqdict.PQDict() self.lock = threading.Lock()