Skip to content
Snippets Groups Projects
Commit 3c42e73d authored by Prof. Dr. Robert Jäschke's avatar Prof. Dr. Robert Jäschke
Browse files

Ein Beispiel

parent 6be5e9f3
No related branches found
No related tags found
No related merge requests found
%% Cell type:code id: tags:
```
s = """
5
/ \\
3---4
|\ /|
| X |
|/ \|
1---2
"""
nikolaus = {
1: [2, 3, 4],
2: [1, 3, 4],
3: [1, 2, 4, 5],
4: [1, 2, 3, 5],
5: [3, 4]
}
def get_neighbors(graph, path):
neighbors = []
for neighbor in graph[path[-1]]:
if not contains(path[-1], neighbor, path):
neighbors.append(neighbor)
return neighbors
def contains(a, b, path):
"""Returns True if path contains the sequence a, b or b, a."""
return any(path[pos:pos + 2] == [a, b] or path[pos:pos + 2] == [b, a] for pos in range(0, len(path) - 1))
def traverse(graph, path):
neighbors = get_neighbors(graph, path)
if len(neighbors) == 0:
print("→".join([str(n) for n in path]), end=' ')
if len(path) == 9: # FIXME: constant!
print(":-)")
else:
print(":-(")
else:
for neighbor in neighbors:
traverse(graph, path + [neighbor])
if __name__ == '__main__':
print(s)
traverse(nikolaus, [1])
```
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment