我正在使用维基百科页面中的以下伪代码来实现对图形的迭代加深深度优先搜索
function IDDFS(root)
for depth from 0 to ∞
found ← DLS(root, depth)
if found ≠ null
return found
function DLS(node, depth)
if depth = 0 and node is a goal
return node
if depth > 0
foreach child of node
found ← DLS(child, depth−1)
if found ≠ null
return found
return null
这是我的代码:
bool DLS(GrapheMat* graphe, Node* source, NomSom but, int limit) {
bool found = false;
printf("%s\n", (char*)source->etat);
if (strcmp((char*)source->etat, (char*)but) == 0) {
return true;
}
if (limit > 0) {
List* listSon = nodeSon(graphe, source);
while(!listEpmty(listSon)) {
Node* son = (Node*)popList(listSon);
if (DLS(graphe, son, but, limit-1)) {
return true;
}
}
}
return false;
}
bool IDDLS (GrapheMat* graphe, NomSom goal, int limit) {
bool found = false;
node* source = createNode(graphe, graphe->nomS[0]);
for (int i = 0; i <= limit; i++) {
printf("/nLimit : %d\n", i);
DLS(graphe, source, goal, i);
}
return false;
}
我正在使用下图进行测试:
它是从以下文件构建的:
A B C D E F G H I J ;
A : B (140) C (118) D (75) ;
B : A (140) E (99) F (151) G (80);
C : A (118) ;
D : A (75) F (71) ;
E : B (99) H (211) ;
F : D (71) B (151) ;
G : B (80) I (146) J (97) ;
H : E (211) J (101) ;
I : G (146) J (138) ;
J : G (97) H (101) I (138) ;
调用IDDLS(graphe, "J", 4)
输出如下:
/nLimit : 0
A
就这样。
调用DLS(graphe, "A", "J", 4)
输出以下内容(已删除换行符):
ABABAEFGCADAFEBAEFGHEJ
据我了解,DLS 函数实际上应该遵循以下路径:
ABEGHCDEFGHIJ