C++中的迭代深化搜索
Iterative Deepening Search in C++
好吧,首先,我不知道迭代深化在做什么。我一直在努力让这段代码发挥作用,但我做不到。我在网上查了一下,在C++中找不到任何关于这次搜索的参考资料。
void Graph::IDS(int x, int required, int depth = 1)
{
if(x == required) return;
cout << "Iterated Deepening Search for " << required << ", starting from vertex " << x << " : " << endl;
IDS_util(x, required, depth);
cout << endl;
}
void Graph::IDS_util(int x, int required, int depth)
{
stack s;
bool *visited = new bool[n+1];
int i, j, k;
for(i = 0; i <= n; i++)
visited[i] = false;
cout << "Depth = " << depth << ": ";
visited[x] = true;
for (int c = 1; c <= n; c++){
s.push(x);
if(isConnected(x, c) && !visited[c])
{
for (j = 0; j < depth; j++){
k = s.pop();
if(k == required) return;
cout << "[" << k <<"] ";
for (i = n; i >= 0 ; --i)
if (isConnected(k, i) && !visited[i]) {
s.push(i);
visited[i] = true;
}
}
}
}
if(depth == n) return;
cout << endl;
IDS_util(x, required, depth+1);
}
邻接矩阵的输出:
0,1,1,1,0,0,0,0,0
0,0,0,0,1,0,0,0,0
0,0,0,0,0,1,1,0,0
0,0,0,0,0,0,0,1,0
0,0,0,0,0,0,0,0,1
0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0
这是该图的定向版本:
[1]
/ |
[2] [3] [4]
/ /
[5] [6] [7] [8]
/
[9]
是:
Iterated Deepening Search for 7, starting from vertex 1 :
Depth = 0:
Depth = 1: [1]
Depth = 2: [1] [2]
Depth = 3: [1] [2] [5]
Depth = 4: [1] [2] [5] [9]
Depth = 5: [1] [2] [5] [9] [3]
Depth = 6: [1] [2] [5] [9] [3] [6]
Depth = 7: [1] [2] [5] [9] [3] [6]
从理论上讲,我知道搜索应该做什么,我可以在一定程度上判断我的搜索在做什么,但我不知道如何解决它。任何人能提供的任何帮助都将不胜感激。
我几乎可以肯定这不是最有效的方法,但我找到了一种有效的方法。
void Graph::IDS(int x, int required)
{
if(x == required) return;
cout << "Iterated Deepening Search for " << required << ", starting from vertex " << x << " : " << endl;
for (int d = 0 ; d <= n ; d++)
if (IDS_util(x, required, d))
return;
cout << required << " was unable to be located via " << x << endl;
}
bool Graph::IDS_util(int x, int required, int depth){
if(x == required) return true;
stack s, x_child;
bool *visited = new bool[n+1];
int i,k, d, sub_k;
for(i = 0; i <= n; i++) visited[i] = false;
visited[x] = true;
for (i = n; i >= 0 ; --i)
if (isConnected(x, i))
x_child.push(i);
cout << '[' << x << "] ";
while(!x_child.isEmpty()){
k = x_child.pop();
s.push(k);
for(d = 0; d < depth; d++){
sub_k = s.pop();
if(sub_k == required) return true;
cout << '[' << sub_k << "] ";
for (i = 0; i <= n; i++){
if (isConnected(sub_k, i) && !visited[i]) {
if (i == required){
cout << "nn" << required << " is a child of " << sub_k << endl;
return true;
}
s.push(i);
visited[i] = true;
}
}
}
}
cout<<endl;
delete [] visited;
return false;
}
相关文章:
- 高级选择排序 - 在一次迭代中搜索两个元素
- 我可以在没有堆栈的情况下在二叉搜索树中实现迭代器吗?
- C++以迭代方式搜索混合类型地图
- 基于函数而不是集合的二分搜索或迭代器?
- 迭代向量以获得搜索功能
- 使用迭代深度优先搜索算法的未加权图的最短路径
- 无法打印完整的二叉搜索树,因为我从最低节点向后迭代的逻辑有缺陷
- 编译时二叉搜索树错误的反向迭代器表示"no matching function call for operator=()"
- 使用二进制搜索树中的迭代器对象访问左侧节点
- 使用迭代器的二叉搜索
- 使用迭代器搜索映射中的值
- 按属性搜索对象的向量并返回迭代器
- 矢量迭代器在尝试对图形进行深度优先搜索时导致分割错误 11
- 从预序遍历迭代(不是递归)构造二叉搜索树
- 二进制搜索树中按顺序遍历的复杂性(使用迭代器)
- 迭代深化搜索应该那么慢吗?
- 广度优先搜索中的 C++ 迭代器和指针用法
- 使用迭代器位置的线性和二叉搜索
- C++:二叉搜索树 end() 迭代器
- c++初级二进制搜索迭代器