Exemplo n.º 1
0
def draw_node(graph, source_node, graph_node):
    """
    graph_node is dispaly name in image.
    """
    g = graph
    out_format = ['svg']
    pLogger.debug("Begin to traversal {!r} deges use dfs".format(source_node))
    dfs_edges_result = nx_a_t.dfs_edges(g, source=source_node)

    pLogger.debug(
        "Begin to reverse traversal {!r} deges use dfs".format(source_node))
    gr = g.reverse(copy=True)
    gr_dfs_edges_result = nx_a_t.dfs_edges(gr, source=source_node)

    # gv_graph(gr, filename='gr')
    follows = list(dfs_edges_result)
    leaders = list(gr_dfs_edges_result)
    pLogger.debug("follows type {}: {} \n leaders type {} :{}".format(
        type(follows), follows, type(leaders), leaders))
    pLogger.info("create new graph with {!r}".format(source_node))
    gr_new = nx.DiGraph(name='Service Connections Evolution')
    gr_new.add_edges_from(leaders)
    pLogger.debug(gr_new.graph)
    g_new = gr_new.reverse(copy=True)
    g_new.add_edges_from(follows)
    pLogger.debug("g_new.graph is {!r}".format(g_new.graph))

    # draw a graph
    for fmt in out_format:
        gv_graph(g_new, filename=graph_node, node_name=source_node, fmt=fmt)
    pLogger.info("draw {!r} over.".format(source_node))
Exemplo n.º 2
0
 def graph(self):
     if self._subgraph is not None:
         return self._subgraph
     if self._target is None:
         return self._graph
     nodes = [self._target]
     nodes.extend(dst for _src, dst in
                  traversal.dfs_edges(self._graph.reverse(), self._target))
     self._subgraph = nx.freeze(self._graph.subgraph(nodes))
     return self._subgraph
Exemplo n.º 3
0
 def graph(self):
     if self._subgraph is not None:
         return self._subgraph
     if self._target is None:
         return self._graph
     nodes = [self._target]
     nodes.extend(dst for _src, dst in traversal.dfs_edges(
         self._graph.reverse(), self._target))
     self._subgraph = nx.freeze(self._graph.subgraph(nodes))
     return self._subgraph
Exemplo n.º 4
0
def draw_node(graph):
    g = graph
    for source_node in g.node:
        dfs_edges_result = nx_a_t.dfs_edges(g, source=source_node)
        gr = g.reverse(copy=True)
        gr_dfs_edges_result = nx_a_t.dfs_edges(gr, source=source_node)

        # gv_graph(gr, filename='gr')
        follows = list(dfs_edges_result)
        leaders = list(gr_dfs_edges_result)
        pLogger.debug("\nfollows type{}: {}\ntype {} leaders: {}".format(
            type(follows), follows, type(leaders), leaders))
        gr_new = nx.DiGraph(name='gr')
        gr_new.add_edges_from(leaders)
        pLogger.debug(gr_new.graph)
        g_new = gr_new.reverse(copy=True)
        g_new.add_edges_from(follows)
        pLogger.debug(g_new.graph)
        # draw a graph
        gv_graph(g_new,
                 filename=source_node.replace(
                     ' ', '-').replace('/', '_').replace('=', '').replace('.', '_'),
                 node_name=source_node)
Exemplo n.º 5
0
Programming The algorithm of Depth-First Traversal
'''
# def DFS_nodes(graph, node, visited=[]):
# visited.append(node)
# for neighbor in graph[node]:
# if not neighbor in visited:
# DFS_nodes(graph, neighbor, visited)
# return visited

# def DFS_edges(graph, node, visited=[], edges=[]):
# visited.append(node)
# for ni in graph[node]:
# if not ni in visited:
# edges.append((node, ni))
# DFS_edges(graph, ni, visited, edges)
# return edges

# print DFS_nodes(g, 2)    # choose node 0 as the starting point
# print DFS_edges(g, 2)
'''
DFS in NetworkX 
'''
print list(traversal.dfs_edges(g))
print traversal.dfs_successors(g)
print traversal.dfs_predecessors(g)
tree = traversal.dfs_tree(g)
tree.successors(0)
# tree.succ
net.draw(tree)
plt.show()
Exemplo n.º 6
0
 def iterate_subgraph(self, retry):
     """Iterates a subgraph connected to current retry controller, including
     nested retry controllers and its nodes.
     """
     for _src, dst in traversal.dfs_edges(self._graph, retry):
         yield dst
Exemplo n.º 7
0
 def iterate_subgraph(self, atom):
     """Iterates a subgraph connected to given atom."""
     for _src, dst in traversal.dfs_edges(self._execution_graph, atom):
         yield dst
Exemplo n.º 8
0
 def iterate_subgraph(self, atom):
     """Iterates a subgraph connected to given atom."""
     for _src, dst in traversal.dfs_edges(self._execution_graph, atom):
         yield dst
Exemplo n.º 9
0
 def iterate_subgraph(self, retry):
     """Iterates a subgraph connected to given retry controller."""
     for _src, dst in traversal.dfs_edges(self._graph, retry):
         yield dst
Exemplo n.º 10
0
    nx.write_pajek(g, path)
    
def sorted_map(map):
    ms = sorted(map.iteritems(), key=lambda (k,v): (-v,k))
    return ms

def trim_degrees(g,degree=1):
    d = nx.degree(g)
    for n in g.nodes():
        if d[n] <= degree:
            g.remove_node(n)
    return g

def podar_red_hasta(g, n_nodos):
    for i in range(1,10):
        g = trim_degrees(g, i)
        if len(g) <= n_nodos:
            break
    return g

if __name__ == '__main__':
    g = ns.krackhardt_kite_graph()
    print g.number_of_edges()
    print g.number_of_nodes()
    print g.adjacency_list()
    print g.edges()
    print dict((x, g.neighbors(x)) for x in g.nodes())
    print list(tr.bfs_edges(g,0))
    print list(tr.dfs_edges(g,0))
    nx.draw_circular(g)
    #nx.draw_graphviz(g)
Exemplo n.º 11
0
import networkx.generators.small
import networkx as net
import matplotlib.pyplot as plot
from networkx.algorithms import traversal
from networkx import algorithms


g = networkx.generators.small.krackhardt_kite_graph()
g.number_of_edges()
g.number_of_nodes()
g.adjacency_list()
dict((x, g.neighbors(x)) for x in g.nodes())
# networkx.generators.small.krackhardt_kite_graph?
net.draw(g)
plot.show()

edges=traversal.dfs_edges(g,0)
list(edges)

edges = traversal.bfs_edges(g, 0)
bfs = list(edges)

net.draw(traversal.bfs_tree(g,0))
plot.show()

algorithms.shortest_path(g,0,7)
shorts = algorithms.all_pairs_shortest_path(g)
print "all shortest lengths", shorts