Guest User

Untitled

a guest
Dec 11th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1. nodes = ('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H')
  2. distances = {
  3. 'A': {'B': 2, 'C': 1, 'D': 3, 'E': 9, 'F': 4},
  4. 'B': {'C': 4, 'E': 3},
  5. 'C': {'D': 8},
  6. 'D': {'E': 7},
  7. 'E': {'F': 5},
  8. 'F': {'C': 2, 'G': 2, 'H': 2},
  9. 'G': {'F': 1, 'H': 6},
  10. 'H': {'F': 9, 'G': 8}}
  11. unvisited = {node: None for node in nodes}
  12. visited = {}
  13. print(" ")
  14. print("Введите вершину:")
  15. current = input()
  16. currentDistance = 0
  17. unvisited[current] = currentDistance
  18.  
  19. while True:
  20. for neighbour, distance in distances[current].items():
  21. if neighbour not in unvisited: continue
  22. newDistance = currentDistance + distance
  23. if unvisited[neighbour] is None or unvisited[neighbour] > newDistance:
  24. unvisited[neighbour] = newDistance
  25. visited[current] = currentDistance
  26. del unvisited[current]
  27. if not unvisited: break
  28. candidates = [node for node in unvisited.items() if node[1]]
  29. if not candidates: break
  30. current, currentDistance = sorted(candidates, key = lambda x: x[1])[0]
  31.  
  32.  
  33. print(visited)
  34.  
  35. matrix = [[0,1,1,1,1,1,0,0], # a
  36. [0,0,1,0,1,0,0,0], # b
  37. [0,0,0,1,0,0,0,0], # c
  38. [0,0,0,0,1,0,0,0], # d
  39. [0,0,0,0,0,1,0,0], # e
  40. [0,0,1,0,0,0,1,1], # f
  41. [0,0,0,0,0,1,0,1], # g
  42. [0,0,0,0,0,1,1,0]] # h
  43.  
  44. import networkx as nx # pip install networkx
  45. import matplotlib.pyplot as plt
  46.  
  47. distances = {
  48. 'A': {'B': 2, 'C': 1, 'D': 3, 'E': 9, 'F': 4},
  49. 'B': {'C': 4, 'E': 3},
  50. 'C': {'D': 8},
  51. 'D': {'E': 7},
  52. 'E': {'F': 5},
  53. 'F': {'C': 2, 'G': 2, 'H': 2},
  54. 'G': {'F': 1, 'H': 6},
  55. 'H': {'F': 9, 'G': 8}}
  56.  
  57. G = nx.DiGraph()
  58. for k,v in distances.items():
  59. for k2,v2 in v.items():
  60. G.add_edge(k, k2, dist=v2)
  61.  
  62. In [24]: nx.draw(G, with_labels=True)
  63.  
  64. In [25]: nx.is_eulerian(G)
  65. Out[25]: False
  66.  
  67. In [26]: nx.eulerian_circuit(G)
  68. Out[26]: <generator object eulerian_circuit at 0x000000000AD631A8>
  69.  
  70. In [27]: list(nx.eulerian_circuit(G))
  71. ---------------------------------------------------------------------------
  72. NetworkXError Traceback (most recent call last)
  73. <ipython-input-27-d4bc2ec5ddbd> in <module>
  74. ----> 1 list(nx.eulerian_circuit(G))
  75.  
  76. ~Anaconda3envsmllibsite-packagesnetworkxalgorithmseuler.py in eulerian_circuit(G, source, keys)
  77. 168 """
  78. 169 if not is_eulerian(G):
  79. --> 170 raise nx.NetworkXError("G is not Eulerian.")
  80. 171 if G.is_directed():
  81. 172 G = G.reverse()
  82.  
  83. NetworkXError: G is not Eulerian.
  84.  
  85. In [30]: G2 = nx.from_edgelist([('A','B'),('B','C'),('C','D'),('D','A')], create_using=nx.DiGraph)
  86.  
  87. In [31]: list(nx.eulerian_circuit(G2))
  88. Out[31]: [('A', 'B'), ('B', 'C'), ('C', 'D'), ('D', 'A')]
  89.  
  90. In [32]: nx.draw(G2, with_labels=True)
Add Comment
Please, Sign In to add comment