我想知道什麼是錯在下面的C#代碼產生通過Hierholzer的算法頂點索引的歐拉,假設所有頂點甚至有度,使旅遊可以從任何地方開始:歐拉在C#
(何pt_id是表示由邊緣組成的起點和終點索引的整數列表)
ps。下列項目的GraphMatrix包括最初的情況下,它是任何幫助,由於對pt_id缺乏信息:
n y y n y y n n n n n
y n n n y n n n n n n
y n n y y y n n n n n
n n y n n y n y y n n
y y y n n y n n n n n
y n y y y n n n n n n
n n n n n n n y y y y
n n n y n n y n y n y
n n n y n n y y n n y
n n n n n n y n n n y
n n n n n n y y y y n
代碼:
Stack tempPath = new Stack();
ArrayList finalPath = new ArrayList();
int total = 11;//total number of nodes
string[] nodeList = new string [total];//to store the nodes
string[,] GraphMatrix = new string [total, total];//to store the edge representation of graph
//node/vertice to nodeList array
for(int i = 0; i < total; ++i) {
nodeList[i] = i.ToString();
}
//edge details in the graph(connectivity boolean matrix)
//matrix first filled with "n"
for(int i = 0; i < total; ++i) {
for(int j = 0; j < total; ++j) {
GraphMatrix[i, j] = "n";
}
}
//if connection exists, matrix item is changed to "y"
for(int i = 0; i < pt_id.Count; ++i) {
GraphMatrix[pt_id[i][0], pt_id[i][1]] = "y";
GraphMatrix[pt_id[i][1], pt_id[i][0]] = "y";
}
//start tour
int s = 0;
int ind = 0;
tempPath.Clear();
tempPath.Push(nodeList[0]);
while(tempPath.Count != 0 && s < 100) {
int index = 0;
while (tempPath.Peek().ToString() != nodeList[index] && s < 100) {
index++;
s++;
}
ind = index;
for(int i = 0; i < total; ++i) {
if(GraphMatrix[ind, i] != "y") {
finalPath.Add(tempPath.Pop());
}
else{
GraphMatrix[ind, i] = "n";
GraphMatrix[i, ind] = "n";
tempPath.Push(nodeList[i]);
}
}
s++;
}
我收到錯誤說堆棧是空的。
非常感謝你,
添
那麼,你有沒有使用調試器來跟蹤堆棧?或使用系統輸出做同樣的事情? – Aziuth
嗨@Aziuth感謝您的迴應 - 我目前正在嘗試在VisualStudio中,但仍然不太明白。 – tim
然後我建議你找一些關於調試的教程。也許一個視頻就是一個例子。系統輸出的方法是插入輸出以隔離發生錯誤的部分,跟蹤程序(如何處理多少次等等),並在上下文中打印出應該更改的堆棧大小。試圖找出錯誤的來源何時首次發生。你可以做的另一件事是讓所有東西都變小,然後添加東西。就像初始化堆棧,然後推入一個元素,然後......直到發生錯誤或程序運行。 – Aziuth