2014-11-05 111 views
1

所以我想排序一系列數字,以便每個連續數字的總和爲平方數(4,9,16,25,...)我被告知應該使用深度首次搜索,但無法正確實現,特別是回溯部分。這是做到這一點的正確方法,我沒有看到什麼,做錯了什麼? DFS甚至是正確的方法嗎?何時使用深度優先搜索

class Node:       #Define node Object 
    def __init__(self, value): 
     self.value = value   #Node "Name" 
     self.adjacentNodes=list() #Node Neighbours 

#Set limit 
limit=16 
squares=list() #list for squared numbers in rang smaller than 2Xlimit 
tree=list() #Tree containing nodes 
path=list() #current path taken 

def calculate(limit): #Population control legal squares 
    for i in range(1,(limit+1)): 
     i=i*i 
     if i<(limit+limit): 
      squares.append(i) 

calculate(limit) #Populate squares list 

for a in range(1,(limit+1)): #creating the nodes 
    newnode=Node(a) 
    for s in squares: 
     legalsum=s-a 
     if legalsum>0 and legalsum<limit and legalsum!=a: #zero and lower can't play,keeping non-exiting nodes out of node.adjacentNodes, don't connect to selve. 
      newnode.adjacentNodes.append(legalsum) #Add adjacentnode 
    tree.append(newnode) #add newborn to tree 

for b in range(0,limit): 
    print tree[b].value,tree[b].adjacentNodes #Quick checkup 
#Now the tree is build and can be used to search for paths. 
''' 
Take a node and check adjnodes 
is it already visited? check path 
yes-> are all nodes visited (loop over tree(currentnode).adjnodes) Maybe use len() 
     yes->pop last node from path and take other node -> fear for infinte loops, do I need a current node discard pile? 
     no -> visit next available adje node next in line from loop got from len(tree.adjn) 
no-> take this as node and check adj nodes. 

''' 
def dfs (node): 
    path.append(node) 
    #load/check adjacent nodes 
    tree[node].adjacentNodes 
    for adjnode in tree[node-1].adjacentNodes: 
     if adjnode in path: 
      #skip to next adj node 
      continue 


     else: 
      print path #Quick checkup 
      dfs(adjnode) 
dfs(8) # start with 8 since 1-16 should be {8,1,15,10,6,3,13,12,4,5,11,14,2,7,9,16} 
+0

呵呵是沒有道理?你怎麼能在這些條件下對[1,2,3,4,5,6,7,8,9]進行排序呢? – 2014-11-05 23:55:58

+0

什麼是#加載/檢查相鄰節點應該在做什麼?你只是引用一個列表然後忽略它。如果這是某種緩存或VM預加載優化,即使它是有用的,當然它不屬於你仍然試圖理解和調試的例子... – abarnert 2014-11-06 00:00:54

+0

另外,你正在編寫一個遞歸函數,它不會任何東西都不會打印任何東西,除了一些臨時調試輸出外,並不會改變任何東西,除非保存它所走過的所有節點的列表(如果你的樹實際上是一棵樹,或者甚至是一個連接的非樹形圖,你做DFS的權利,保證是所有的節點),所以...那有什麼意義呢? – abarnert 2014-11-06 00:04:03

回答

0

我不知道你說的是帶有方塊什麼,但這裏有一些問題

def dfs (node): 
    #always assume the path starts with this node 
    path=[node] 
    if node == GoalState: return path #we win!! 
    #load/check adjacent nodes 
    #this line did nothing `tree[node].adjacentNodes` 
    for adjnode in tree[node-1].adjacentNodes: 
    # if adjnode in path: ****this will never be true, lets eliminate it 
    #   #skip to next adj node 
    #  continue 

    # else: 
      subpath = dfs(adjnode) 
      if subpath and subpath.endswith(GoalState): 
       return path + subpath #return solution 

    #if we get here there is no solution from this node to the goal state 
+0

我認爲樹實際上不是一棵樹,而是一個無向圖,因此需要防止循環。我建議儘可能通過路徑! – Blckknght 2014-11-06 00:18:42

+0

呃那麼你已經探索過並且有前沿來管理他們......但是他說樹並且在他的代碼中一直使用樹(我懷疑它也是一個無向圖),但是我想我會根據這個來解決他的問題。 ..)他顯然需要更好地掌握DFS,然後才能進入循環的可能性 – 2014-11-06 00:21:15