Skip to content

Instantly share code, notes, and snippets.

@davebshow
Last active November 6, 2015 17:15
Show Gist options
  • Save davebshow/28f0a129992f7dd8039d to your computer and use it in GitHub Desktop.
Save davebshow/28f0a129992f7dd8039d to your computer and use it in GitHub Desktop.
My basic idea for creating a schema dependency graph based on the traversal fragments generated by SylvaDB query builder.
def df_edge_traversal(g):
"""
Based on networkx dfs_edges
"""
visited = set()
start = g.nodes()[0]
stack = [(start, iter(g[start]))]
while stack:
parent, children = stack[-1]
try:
child = next(children)
edge = (parent, child)
if edge not in visited and (child, parent) not in visited:
yield edge
visited.add(edge)
stack.append((child, iter(g[child])))
except StopIteration:
stack.pop()
if __name__ == "__main__":
import networkx as nx
# Demo graphs. Edges represent source and target from traversal
# fragments as delivered by the query builder. In the case of multiple
# components in the graphs, components would be iterated over.
graph1 = nx.Graph([(1, 2), (2, 3), (3, 4), (4, 1)])
graph2 = nx.Graph([(13, 8), (3, 50), (50, 10), (50, 90), (13, 10), (8, 2)])
print("Graph 1: {0}".format(list(df_edge_traversal(graph1))))
print("Graph 2: {0}".format(list(df_edge_traversal(graph2))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment