旅行售貨員問題(回溯法搜尋排列樹)
阿新 • • 發佈:2019-01-22
{
if (node.firstchild ==null)//到了葉結點
{
return (int)((ArrayList)g.AdjacentMatrix[node.data])[0];//葉結點與根結點之間的路徑,因為需要回路
}
int iTemp;
TreeNode lastnode;
int minPath;
Stack emptyStack =new Stack();//!!!!!!!!!!!!!!!!!!!!!!!!!!!!
lastnode = node.firstchild;
minPath = lastnode.upedgeweight + Traverse(lastnode,g,ref emptyStack);
emptyStack.Push(lastnode.data);//!!!!!!!!!!!!!!!!!
priPathStack =new Stack(emptyStack);//得到第一棵子樹內的最優路徑,並記下!!!!!!!!!!!
lastnode = lastnode.nextsib;
while (lastnode !=null)
{
emptyStack.Clear();//開始查詢下一棵子樹,emptyStack準備存放下一棵子樹的最優路徑!!!!!
iTemp = lastnode.upedgeweight + Traverse(lastnode,g,ref emptyStack);
if (iTemp < minPath)
{
minPath = iTemp;
emptyStack.Push(lastnode.data);//!!!!!!!!!!!!!!!!
priPathStack =new Stack(emptyStack);//!!!!!!!!!!!!!!!!!
}
lastnode = lastnode.nextsib;
}
priPathStack =new Stack(priPathStack);//因為堆疊的複製結果會顛倒順序,所以需要這樣倒一下!!!!!!
return minPath;
}
if (node.firstchild ==null)//到了葉結點
{
return (int)((ArrayList)g.AdjacentMatrix[node.data])[0];//葉結點與根結點之間的路徑,因為需要回路
}
int iTemp;
TreeNode lastnode;
int minPath;
Stack emptyStack =new Stack();//!!!!!!!!!!!!!!!!!!!!!!!!!!!!
lastnode =
minPath = lastnode.upedgeweight + Traverse(lastnode,g,ref emptyStack);
emptyStack.Push(lastnode.data);//!!!!!!!!!!!!!!!!!
priPathStack =new Stack(emptyStack);//得到第一棵子樹內的最優路徑,並記下!!!!!!!!!!!
lastnode = lastnode.nextsib;
while (lastnode !=null)
emptyStack.Clear();//開始查詢下一棵子樹,emptyStack準備存放下一棵子樹的最優路徑!!!!!
iTemp = lastnode.upedgeweight + Traverse(lastnode,g,ref emptyStack);
if (iTemp < minPath)
{
minPath = iTemp;
emptyStack.Push(lastnode.data);//!!!!!!!!!!!!!!!!
priPathStack =new Stack(emptyStack);//!!!!!!!!!!!!!!!!!
}
lastnode = lastnode.nextsib;
}
priPathStack =new Stack(priPathStack);//因為堆疊的複製結果會顛倒順序,所以需要這樣倒一下!!!!!!
return minPath;
}