def hits(graph, min_diff=0.01): nodes = graph.nodes() auth = dict.fromkeys(nodes, 1) hub = dict.fromkeys(nodes, 1) iteration = 0 while True: iteration += 1 prev_auth = auth.copy() prev_hub = hub.copy() for node in nodes: auth[node] = sum( prev_hub.get(parent, 0) for parent in graph.parents(node)) hub[node] = sum( prev_auth.get(child, 0) for child in graph.childrens(node)) norm_auth = sum(auth.values()) norm_hub = sum(hub.values()) auth = {key: val / norm_auth for key, val in auth.items()} hub = {key: val / norm_hub for key, val in hub.items()} diff = sum((abs(prev_hub[k] - hub[k]) for k in hub)) + sum( (abs(prev_auth[k] - auth[k]) for k in auth)) if diff <= min_diff: break return auth, hub, iteration
def simrank(graph, min_diff=0.01, decay_factor=0.8): nodes = graph.nodes() sim = np.identity(len(nodes)) iteration = 0 while True: iteration += 1 prev_sim = np.copy(sim) for idx_u, u in enumerate(nodes): for idx_v, v in enumerate(nodes): if u is v: continue len_up = len(graph.parents(u)) len_vp = len(graph.parents(v)) if len_up == 0 or len_vp == 0: sim[idx_u][idx_v] = 0 else: sum = 0 for u_p in graph.parents(u): for v_p in graph.parents(v): sum += prev_sim[nodes.index(u_p)][nodes.index(v_p)] sim[idx_u][idx_v] = (decay_factor / (len_up * len_vp)) * sum if np.allclose(sim, prev_sim, atol=min_diff): break return sim, iteration
def shortest_path(graph, sourceNode, dist = {}, previous = {}): """ Return the shortest path distance between sourceNode and all other nodes using Dijkstra's algorithm. See http://en.wikipedia.org/wiki/Dijkstra%27s_algorithm. @attention All weights must be nonnegative. @type graph: graph @param graph: Graph. @type sourceNode: node @param sourceNode: Node from which to start the search. @rtype tuple @return A tuple containing two dictionaries, each keyed by targetNodes. The first dictionary provides the shortest distance from the sourceNode to the targetNode. The second dictionary provides the previous node in the shortest path traversal. Inaccessible targetNodes do not appear in either dictionary. """ vertices = graph.nodes() # dist and previous are dictionnaries where the key is the node and # the value is respectively the distance and the previous node. # dist = {} # previous = {} for v in vertices: dist[v] = float('inf') previous[v] = None ; dist[sourceNode] = 0 while vertices: temp_dist = float('inf') u = None for node in vertices: if dist[node] <= temp_dist: temp_dist = dist[node] u = node vertices.remove(u) if dist[u] == float('inf'): break neighbours = graph.neighbors(u) for v in neighbours: alt = dist[u] + graph.edge_weight((u, v)) if alt < dist[v]: dist[v] = alt ; previous[v] = u ; return dist;
def shortest_path(graph, sourceNode, dist={}, previous={}): """ Return the shortest path distance between sourceNode and all other nodes using Dijkstra's algorithm. See http://en.wikipedia.org/wiki/Dijkstra%27s_algorithm. @attention All weights must be nonnegative. @type graph: graph @param graph: Graph. @type sourceNode: node @param sourceNode: Node from which to start the search. @rtype tuple @return A tuple containing two dictionaries, each keyed by targetNodes. The first dictionary provides the shortest distance from the sourceNode to the targetNode. The second dictionary provides the previous node in the shortest path traversal. Inaccessible targetNodes do not appear in either dictionary. """ vertices = graph.nodes() # dist and previous are dictionnaries where the key is the node and # the value is respectively the distance and the previous node. # dist = {} # previous = {} for v in vertices: dist[v] = float('inf') previous[v] = None dist[sourceNode] = 0 while vertices: temp_dist = float('inf') u = None for node in vertices: if dist[node] <= temp_dist: temp_dist = dist[node] u = node vertices.remove(u) if dist[u] == float('inf'): break neighbours = graph.neighbors(u) for v in neighbours: alt = dist[u] + graph.edge_weight((u, v)) if alt < dist[v]: dist[v] = alt previous[v] = u return dist
def get_next_step(graph, sourceNode): ''' Returns a dictionnary with a key for each vertex in the graph (except sourceNode) and as value the next step form sourceNode in order to reach the key by the smallest path. ''' dist = {} previous = {} shortest_path(graph, sourceNode, dist, previous) #print('dist', dist) #print('prev', previous) result = {} vertices = graph.nodes() vertices.remove(sourceNode) to_pop = [] for key in dist: if not previous[key]: to_pop += [key] for key in to_pop: dist.pop(key) #print('dist2', dist) #dist.pop(sourceNode) while dist: value = 0 nextkey = None for key in dist: if dist[key] > value: value = dist[key] nextkey = key treated = [] while previous[nextkey] != sourceNode: treated += [nextkey] nextkey = previous[nextkey] for node in treated: result[node] = nextkey if node in dist: dist.pop(node) result[nextkey] = nextkey #print(dist) #print(nextkey) if nextkey in dist: dist.pop(nextkey) return result
def pagerank(graph, min_diff=0.0001, damping_factor=0.15): nodes = graph.nodes() pagerank = dict.fromkeys(nodes, 1.0 / len(nodes)) iteration = 0 while True: iteration += 1 diff = 0 for node in nodes: rank = (1.0 - damping_factor) / len(nodes) for parent in graph.parents(node): rank += damping_factor * pagerank[parent] / len( graph.childrens(parent)) diff += abs(pagerank[node] - rank) pagerank[node] = rank if diff <= min_diff: break return pagerank, iteration