Skip to content

Instantly share code, notes, and snippets.

@killreal17
Last active May 18, 2020 17:50
Show Gist options
  • Save killreal17/9fb3b949426b624237cc55652525456c to your computer and use it in GitHub Desktop.
Save killreal17/9fb3b949426b624237cc55652525456c to your computer and use it in GitHub Desktop.
Поиск путей в направленном нецикличном графе
'''
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