def moletrust_tm(G, a, b, rh = False): #it is useful to mark every moletrust with his horizon #on the datasets folder if rh: return horizon debug = False if debug: print "predict trust from", a, "to", b # Do something with connected_components here # UG = G.to_undirected() # subgraphs = connected_component_subgraphs(UG) # find a # if not b in subgraph_with_a: # return 0.0 # path_length_dict and trust_map should be cached in a very smart way path_length_dict = path.single_source_shortest_path_length(G, a, horizon) if not b in path_length_dict or path_length_dict[b] > horizon: return 0.0 path_length_list = map(lambda (x,y): (y,x), path_length_dict.items()) path_length_list.sort() # order by distance # initialize trust map with node a and a bunch of empty dicts trust_map = [{a: 1.0}] + [{}] * horizon for (dist, node) in path_length_list[1:]: useful_in_edges = filter(lambda x: x[0] in (trust_map[dist-1]), G.in_edges(node)) # We have to benchmark this, it could be a lot faster? #if len(useful_in_edges) == 1: # pred_trust = G.trust_on_edge(useful_in_edges[0]) # not considering the negative trust (or e.g. <0.5) # statements, very good for our accuracy! yay! big hugs! useful_in_edges = filter(lambda x: (G.trust_on_edge(x) >= edge_trust_threshold), useful_in_edges) for edge in useful_in_edges: if debug: print ("useful edge:", edge, "predecessor tvalue", trust_map[dist-1][edge[0]]) pred_trust = weighted_average(map(lambda x: (G.trust_on_edge(x), trust_map[dist-1][x[0]]), useful_in_edges)) if node == b: return pred_trust # only keep edges over pred_node_trust_threshold if pred_trust >= pred_node_trust_threshold: trust_map[dist][node] = pred_trust return 0.0
def build_adv_flow_graph(G, seeds): """Build a flow graph from a graph, as described in the Advogato trust metric paper: * add supersink * for every node: add N_node and P_node * set initial flow to 0 """ # This also ensures that there are no nodes named "source" or "supersink" neg, pos = lambda n: "N" + str(n), lambda n: "P" + str(n) G_flow = networkx.XDiGraph() G_flow.add_node("source") G_flow.add_node("supersink") for src in seeds: if not src in G: seeds.remove(src) distance_map = {} for src in seeds: # build the distance map, we don't want to add a new source node to G, # so we have to get a temp_dist_map for every seed node temp_dist_map = path.single_source_shortest_path_length(G, src) for n, d in temp_dist_map.items(): if distance_map.has_key(n): distance_map[n] = min(distance_map[n], d) else: distance_map[n] = d for n in G.nodes_iter(): cap_value = 1 if distance_map.has_key(n): dist = distance_map[n] if dist <= 5: cap_value = cap_dict[dist] if n in seeds: # it's not clear yet if this is actually what is happening # in tmetric.c G_flow.add_edge("source", neg(n), {'flow': 0}) assert cap_value == 800 G_flow.add_edge(neg(n), pos(n), {'cap': cap_value - 1, 'flow': 0}) G_flow.add_edge(neg(n), "supersink", {'cap': 1, 'flow': 0}) for e in G.edges_iter(): G_flow.add_edge(pos(e[0]), neg(e[1]), {'flow': 0}) return G_flow
def build_adv_flow_graph(G, seeds): """Build a flow graph from a graph, as described in the Advogato trust metric paper: * add supersink * for every node: add N_node and P_node * set initial flow to 0 """ # This also ensures that there are no nodes named "source" or "supersink" neg, pos = lambda n: "N" + str(n), lambda n: "P" + str(n) G_flow = networkx.XDiGraph() G_flow.add_node("source") G_flow.add_node("supersink") for src in seeds: if not src in G: seeds.remove(src) distance_map = {} for src in seeds: # build the distance map, we don't want to add a new source node to G, # so we have to get a temp_dist_map for every seed node temp_dist_map = path.single_source_shortest_path_length(G, src) for n,d in temp_dist_map.items(): if distance_map.has_key(n): distance_map[n] = min(distance_map[n], d) else: distance_map[n] = d for n in G.nodes_iter(): cap_value = 1 if distance_map.has_key(n): dist = distance_map[n] if dist <= 5: cap_value = cap_dict[dist] if n in seeds: # it's not clear yet if this is actually what is happening # in tmetric.c G_flow.add_edge("source", neg(n), {'flow': 0}) assert cap_value == 800 G_flow.add_edge(neg(n), pos(n), {'cap': cap_value - 1, 'flow': 0}) G_flow.add_edge(neg(n), "supersink", {'cap': 1, 'flow': 0}) for e in G.edges_iter(): G_flow.add_edge(pos(e[0]), neg(e[1]), {'flow': 0}) return G_flow