2017-07-14 184 views
-2

我正在爲一個項目編寫算法(我確定在我的代碼中有其他地方有錯誤,但不幫我,因爲我將在以後處理這些錯誤)。但我遇到了基本的Python組織錯誤。基本上我使用一個庫(networkX),並有一個功能完整的圖(我已經測試了它,節點和邊)。我遇到了如何設置我的參數,然後實際執行ham_main()函數的錯誤。在另一個函數(Python)中調用函數時沒有被定義

def ham_walk(self, graph, path): 
    if len(path) == len(graph.nodes()): 
     return True 
    for neighbor in graph.neighbors(path.pop()): 
     if neighbor not in path: 
      ham_walk(graph, path.append(neighbor)) 
    return False 

def ham_main(self): 
    graph = self.getGraph() 
    print(graph.nodes()) 
    print(graph.edges()) 
    path = [] 
    for node in graph.nodes(): 
     path = [node] 
     if ham_walk(self, graph, path): 
      return print("Hamiltonian Path: " + path) 
     else: 
      print("False") 
      return False 
    return print("Hamiltonian Path: " + path) 
    class Main: 
     execute = HamParser() 
     execute.ham_main() 

當我嘗試在我的主類中執行時,出現以下錯誤;

 File "C:/Users/Chris/Dropbox/HamProgram.py", line 33, in ham_main 
     if ham_walk(self, graph, path): 
     NameError: name 'ham_walk' is not defined 

看起來好像ham_walk沒有註冊。我錯過了一些至關重要的東西嗎

編輯:全碼

from sys import argv 
import networkx as nx 


class HamParser: 

    def getGraph(self): 
     adjLines = [] 
     test = "input001.txt" 
     with open(test, 'r') as adjFile: 
     #with open(sys.argv[1], 'r') as adjFile: 
      adjFile.readline() 
      for line in adjFile: 
       adjLines.append(line.strip()) 
     G = nx.parse_adjlist(adjLines, nodetype=int) 
     return G 

    def ham_walk(self, graph, path): 
     if len(path) == len(graph.nodes()): 
      return True 
     for neighbor in graph.neighbors(path.pop()): 
      if neighbor not in path: 
       ham_walk(graph, path.append(neighbor)) 
     return False 

    def ham_main(self): 
     graph = self.getGraph() 
     print(graph.nodes()) 
     print(graph.edges()) 
     path = [] 
     for node in graph.nodes(): 
      path = [node] 
      if ham_walk(self, graph, path): 
       return print("Hamiltonian Path: " + path) 
      else: 
       print("False") 
       return False 
     return print("Hamiltonian Path: " + path) 


class Main: 
    execute = HamParser() 
    execute.ham_main() 
+0

顯示完整的代碼?? – babygame0ver

+0

什麼是Main類? – MxyL

+1

將'ham_walk(self,graph,path)'改爲'self.ham_walk(圖形,路徑)' – eyllanesc

回答

1

你可以試試這個方法:

from sys import argv 
import networkx as nx 


class HamParser: 

    def getGraph(self): 
     adjLines = [] 
     test = "input001.txt" 
     with open(test, 'r') as adjFile: 
     #with open(sys.argv[1], 'r') as adjFile: 
      adjFile.readline() 
      for line in adjFile: 
       adjLines.append(line.strip()) 
     G = nx.parse_adjlist(adjLines, nodetype=int) 
     return G 

    def ham_walk(self, graph, path): 
     if len(path) == len(graph.nodes()): 
      return True 
     for neighbor in graph.neighbors(path.pop()): 
      if neighbor not in path: 
       self.ham_walk(graph, path.append(neighbor)) 
     return False 

    def ham_main(self): 
     graph = self.getGraph() 
     print(graph.nodes()) 
     print(graph.edges()) 
     path = [] 
     for node in graph.nodes(): 
      path = [node] 
      if self.ham_walk(self, graph, path): 
       return print("Hamiltonian Path: " + path) 
      else: 
       print("False") 
       return False 
     return print("Hamiltonian Path: " + path) 


class Main: 
    execute = HamParser() 
    execute.ham_main() 
+0

是的,這工作。謝謝,我知道這不是最有條理的問題。 – neuro9

1

你的問題就在這裏:

if ham_walk(self, graph, path): 

你不需要在通過自己作爲一個參數的ham_walk定義 知道哪個對象實例參考。只需將其更改爲:

if self.ham_walk(graph, path): 
+0

再次是這個修復它。我感謝幫助! – neuro9

1

從SYS進口的argv 進口networkx爲NX

類HamParser:

def getGraph(self): 
    adjLines = [] 
    test = "input001.txt" 
    with open(test, 'r') as adjFile: 
    #with open(sys.argv[1], 'r') as adjFile: 
     adjFile.readline() 
     for line in adjFile: 
      adjLines.append(line.strip()) 
    G = nx.parse_adjlist(adjLines, nodetype=int) 
    return G 

def ham_walk(self, graph, path): 
    if len(path) == len(graph.nodes()): 
     return True 
    for neighbor in graph.neighbors(path.pop()): 
     if neighbor not in path: 
      self.ham_walk(graph, path.append(neighbor)) 
    return False 

def ham_main(self): 
    graph = self.getGraph() 
    print(graph.nodes()) 
    print(graph.edges()) 
    path = [] 
    for node in graph.nodes(): 
     path = [node] 
     if self.ham_walk(graph, path): 
     # NOT if self.ham_walk(self, graph, path): 
     # self is bound as first arg when HamPath() is instantiated. 
     #  by putting self in the call again, you are basically passing self in a second time, and calling ham_walk with a second "self" where graph should be. 
     # in this case, it will throw error b/c number of args (3) does not match signature (2) 
      return print("Hamiltonian Path: " + path) 
     else: 
      print("False") 
      return False 
    return print("Hamiltonian Path: " + path) 

類主: execute = HamParser() exe cute.ham_main()

相關問題