Пример #1
0
    def test_topological_figure(self):
        DG = nx.DiGraph()
        DG.add_edges_from([('a', 'b'), ('a', 'c'), ('b', 'c'), ('b', 'd')])
        print(list(nx.topological_sort(DG)))
        from ch07.digraph_search import topological_sort
        print(list(topological_sort(DG)))

        DG = nx.DiGraph()
        from ch07.book import make_sample_directed_graph
        DG = make_sample_directed_graph()
        print(list(topological_sort(DG)))
Пример #2
0
 def test_topological_example(self):
     from ch07.book import topological_example
     DG = nx.DiGraph()
     topological_example(DG, 5)
     print(list(nx.topological_sort(DG)))
     from ch07.digraph_search import topological_sort
     print(list(topological_sort(DG)))
Пример #3
0
def topological_sp(DAG, src):
    """Given a DAG, compute shortest path from src."""
    from ch07.digraph_search import topological_sort
    from ch07.single_source_sp import WEIGHT

    inf = float('inf')
    dist_to = {v:inf for v in DAG.nodes()}
    dist_to[src] = 0
    edge_to = {}

    def relax(e):
        n, v, weight = e[0], e[1], e[2][WEIGHT]
        if dist_to[n] + weight < dist_to[v]:
            dist_to[v] = dist_to[n] + weight
            edge_to[v] = e

    for n in topological_sort(DAG):
        for e in DAG.edges(n, data=True):
            relax(e)

    return (dist_to, edge_to)
Пример #4
0
        #         'B7': '=(B5+B6)',
        #         'B8': '=(B6+B7)',
        'C2': '=B2',
        'C3': '=(B3+C2)',
        'C4': '=(B4+C3)',
        'C5': '=(B5+C4)',
        #         'C6': '=(B6+C5)',
        #         'C7': '=(B7+C6)',
        #         'C8': '=(B8+C7)'
    }
    for k in entries:
        spreadsheet.set(k, entries[k])


#######################################################################
if __name__ == '__main__':
    if tkinter_error:
        print(
            'tkinter is not installed so unable to launch spreadsheet application'
        )
    else:
        import tkinter
        from ch07.spreadsheet import Spreadsheet

        root = tkinter.Tk()
        ss = Spreadsheet(root, nx.DiGraph())
        fibonacci_example(ss)
        from ch07.digraph_search import topological_sort
        print(list(topological_sort(ss.digraph)))
        root.mainloop()
Пример #5
0
def print_sample_linear_ordering():
    """Produce sample linear ordering for spreadsheet example."""
    from ch07.digraph_search import topological_sort
    DG = make_sample_directed_graph(output=False)
    linear = topological_sort(DG)
    print(list(linear))