Advertisement
Guest User

Untitled

a guest
Jan 16th, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.50 KB | None | 0 0
  1. import io
  2.  
  3. import networkx as nx
  4. from networkx.drawing.nx_pydot import read_dot
  5. from networkx.drawing.nx_pydot import from_pydot
  6. from IPython.display import SVG
  7. import pydot
  8.  
  9. '''
  10. Utility Functions
  11. '''
  12.  
  13.  
  14. def toGraph(g):
  15. G = nx.Graph()
  16. for n in g:
  17. for n2 in g[n]:
  18. G.add_edge(n, n2)
  19. return G
  20.  
  21.  
  22. def nM(d):
  23. '''Generate node labeler from dictionary'''
  24.  
  25. def m(n, N):
  26. if n in d:
  27. N.attr['label'] = n + ' ' + str(d[n])
  28.  
  29. return m
  30.  
  31.  
  32. def eM(d):
  33. '''Generate edge labeler from dictionary'''
  34.  
  35. def m(e, E):
  36. if e in d:
  37. E.attr['label'] = str(d[e])
  38.  
  39. return m
  40.  
  41.  
  42. def pathColorer(path, color='red'):
  43. '''Generate edge colorer from path'''
  44. pairs = set()
  45. for i in range(len(path) - 1):
  46. l = sorted([path[i], path[i + 1]])
  47. pairs.add((l[0], l[1]))
  48.  
  49. def colorer(e, E):
  50. e = tuple(sorted(e))
  51. if e in pairs:
  52. E.attr['color'] = color
  53.  
  54. return colorer
  55.  
  56.  
  57. def pathNumberer(path):
  58. '''Generate edge numberer from path'''
  59. pairs = dict()
  60. for i in range(len(path) - 1):
  61. l = tuple(sorted([path[i], path[i + 1]]))
  62. pairs[l] = i + 1
  63.  
  64. def colorer(e, E):
  65. e = tuple(sorted(e))
  66. if e in pairs:
  67. E.attr['label'] = str(pairs[e])
  68.  
  69. return colorer
  70.  
  71.  
  72. def strongWeakEdges():
  73. '''Label edges with strength'''
  74.  
  75. def labeler(e, E):
  76. if float(E.attr['weight']) <= 0.6:
  77. E.attr['label'] = "w"
  78. else:
  79. E.attr['label'] = "s"
  80.  
  81. return labeler
  82.  
  83.  
  84. def draw(G, mapping=None, emapping=None):
  85. '''draw graph with node mapping and emapping'''
  86. A = nx.drawing.nx_agraph.to_agraph(G)
  87. A.graph_attr['overlap'] = 'False'
  88. if mapping:
  89. if isinstance(mapping, dict):
  90. mapping = nM(mapping)
  91. for n in A.nodes():
  92. mapping(n, A.get_node(n))
  93. if emapping:
  94. if isinstance(emapping, dict):
  95. emapping = eM(emapping)
  96. for e in A.edges():
  97. emapping(e, A.get_edge(e[0], e[1]))
  98. A.layout()
  99. output = io.StringIO()
  100. A.draw(output, format='svg')
  101. return SVG(data=output.getvalue())
  102.  
  103.  
  104. def fromDot(s):
  105. P_list = pydot.graph_from_dot_data(s)
  106. return from_pydot(P_list[0])
  107.  
  108.  
  109. def bfs(graph, start):
  110. '''
  111. In breadth first search discovered nodes are attached to the end of the queue. Thus,
  112. the algorithm first searches through all nodes in the same distance from the start node.
  113. The dictionary visited maps each node to a visiting time and thus keeps track of
  114. which node was already visited.
  115. '''
  116. visited, queue = dict(), [start]
  117. while queue:
  118. vertex = queue.pop(0)
  119. if vertex not in visited:
  120. visited[vertex] = len(visited)
  121. queue.extend(set(graph[vertex]) - set(visited))
  122. return visited
  123.  
  124.  
  125. def dfs(graph, start):
  126. '''
  127. Depth-first-search in graph starting from the node 'start'.
  128. The algorithm starts at start and explores as far as possible along each branch before backtracking.
  129. '''
  130. visited, stack = dict(), [start]
  131. while stack:
  132. vertex = stack.pop()
  133. if vertex not in visited:
  134. visited[vertex] = len(visited)
  135. stack.extend(set(graph[vertex]) - set(visited.keys()))
  136. return visited
  137.  
  138.  
  139. def isStrong(e):
  140. return float(e['weight']) > 0.5
  141.  
  142.  
  143. def neighborPairs(G, a):
  144. return [(n1, n2) for n1 in G['A'] for n2 in G['A'] if n1 < n2]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement