Last active
May 18, 2020 17:50
-
-
Save killreal17/9fb3b949426b624237cc55652525456c to your computer and use it in GitHub Desktop.
Поиск путей в направленном нецикличном графе
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
example of graph in input.json | |
{ | |
"start": "1", | |
"end": "12", | |
"nodes": { | |
"1": ["2", "3"], | |
"2": ["4", "7"], | |
"3": ["5", "11"], | |
"4": ["6"], | |
"5": ["9"], | |
"6": ["10"], | |
"7": ["10", "3"], | |
"9": ["11"], | |
"10": ["12"], | |
"11": ["12"] | |
} | |
} | |
''' | |
import json | |
def findPath(start, end, graph, path, nodes): | |
if start == end: | |
print(path) | |
else: | |
for node in graph[start]: | |
findPath(node, end, graph, path+'-'+node, nodes) | |
with open('input.json', 'r') as myfile: | |
data=myfile.read() | |
obj = json.loads(data) | |
findPath(obj["start"], obj["end"], obj["nodes"], obj["start"], {}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment