Untitled

 avatar
unknown
python
2 years ago
955 B
1
Indexable
class Djikstra:

    def __init__(self, graph=None):
        if graph is None:
            self.graph = {}
            return
        else:
            self.graph = graph

        self.array = []

    def find_all_paths(self, graph, start, end, path=None, pathlen=0):
        if path is None:
            path = []
        path = path + [start]
        if start == end:
            yield pathlen, path
        if start not in graph:
            yield [], 0
            return

        for node, val in graph[start].items():
            if node not in path:
                yield from self.find_all_paths(graph, node, end, path, pathlen + val)


graph_elements = {
    1: {2: 4},
    2: {4: 5},
    3: {1: 1, 4: 7, 5: 2},
    4: {2: 5, 3: 7},
    5: {3: 2, 4: 6}
}

DJ = Djikstra(graph_elements)

All = DJ.find_all_paths(DJ.graph, 1,5)

print("----------")

Print = sorted(All)

for i in Print:
    print(i)


Editor is loading...