使用DFS进行海报打印
Using DFS to do Postorder printing
我有一个从邻接表创建的图,我试图以某种方式使DFS也打印出图的海报。有谁对我如何在DFS函数中做到这一点有建议吗?非常感谢
样本输入:create 6
insert 0 3 0
insert 0 1 0
insert 1 3 0
insert 1 2 0
insert 2 1 0
insert 3 5 0
insert 3 4 0
insert 3 2 0
insert 4 2 0
insert 5 2 0
DFS 0
代码:
main.cpp
using namespace std;
#include "graph.h"
//Main Function
int main(){
string command,command1;
int src,dest,wght,start,size;
cout << "graph>";
cin>>command;
if(command=="create")
cin>>size;
graph A(size); //initial class instance
//handles all I/O
do{
cout << "graph>";
cin >> command;
//Creates an array of size "size" for nodes to be added too
//if(command == "create"){
//cin>>size;
//resize(size);
//}
//Tests for user insertion of node
if (command == "insert"){
cin >> src >> dest >> wght;
if(src <= size && dest <= size){
A.insert(src, dest, wght);
}
else{
cout<<"Error! Node does not exist!"<<endl;
}
}
//Tests for user removal of node
else if(command == "remove"){
cin >> src >> dest;
if(src <= size && dest <= size){
A.remove(src, dest);
}
else{
cout<<"Error! Node does not exist!"<<endl;
}
}
//Tests for user printing of graph
else if(command == "DFS"){
cin >> start;
A.DFS(start);
}
//Tests for user printing of graph
else if(command == "print"){
A.print();
}
else{
if(command != "quit")
cout<<"Error! Command not supported."<<endl;
}
}while(command != "quit" );
return 0;
}
graph.h
//graph class file
using namespace std;
#include "node.h"
class graph{
private:
int h,f;
int state[30];
int counter[30];
class alist* array;
public:
//Initializes the Graph and fills it with NULLs
graph(int h){
this->h = h;
array = new alist [h];
for (int i = 0; i < h; ++i)
array[i].head = NULL;
}
//Creates a new node by calling instance newlist of listnode
listnode* newlist(int dest){
listnode* newnode = new listnode;
newnode->dest = dest;
newnode->next = NULL;
return newnode;
}
//Connects the node of the Graph
void insert(int src, int dest, int weight){
listnode* newnode = newlist(dest);
newnode->next = array[src].head;
newnode->weight = weight;
array[src].head = newnode;
newnode = newlist(src);
newnode->next = array[dest].head;
}
//Removes a node from the graph
void remove(int srcr, int destr){
for (int i = 0; i < h; ++i){
listnode* move = array[i].head;
while (move){
if(destr == move->dest){
if(srcr==i)
array[i].head = NULL;
}
move = move->next;
}
}
}
//Prints the Graph
void print(){
for (int i = 0; i < h; ++i){
listnode* move = array[i].head;
while (move){
cout<<"("<<i <<","<< move->dest<< ","<< move->weight <<")";
move = move->next;
}
}
cout<<endl;
}
//Traverses the graph using DFS
int DFS(int start){
state[start]=1; //marks the node as visited
if(start==(h)){ //exits once all nodes have been visited
return 0;}
listnode* move = array[start].head;
cout<<"Visited "<<start<<endl;
if(state[start]!=1){
DFS(start);}
start++;
DFS(start);
}
};
list.h
//list class file
using namespace std;
#include <iostream>
#include <cstdlib>
//Adjacency List
class alist
{
public:
class listnode *head;
};
node.h
//node class file
using namespace std;
#include "list.h"
//Nodes
class listnode
{
public:
int dest;
int weight;
class listnode* next;
};
不要用c++重写你自己的图形类,使用boost::graph可能更快。
你的DFS实现似乎是错误的,你没有迭代节点的所有兄弟节点。
最初,您应该将所有状态初始化为WHITE。当您第一次发现一个节点时,您将其标记为GRAY。然后为所有对象递归DFS节点的兄弟节点(当节点未标记为WHITE时不执行任何操作)。一旦访问了所有兄弟节点,将节点标记为BLACK。此时,您可以输出节点的索引,这将为您提供后序遍历
相关文章:
- 如何循环打印顶点结构
- 为什么在popback()操作之后,它仍然打印完整的矢量
- 如何在c++中打印目录
- 有一个打印语句的函数是一种糟糕的编程实践吗
- 在线编译器中的分段C++没有打印消息
- 在C++中打印指向不同基元数据类型的指针的内存地址
- 这个指针和内存代码打印是什么?我不知道是打印垃圾还是如何打印我需要的值
- 如何将结构插入到集合中并打印集合的成员
- 在循环C++中指定字符串之后,不会打印该字符串
- 以螺旋方式打印矩阵的程序.(工作不好)
- 从控制台中删除最后打印的元素
- 如何使用Crypto++并为RSA返回可打印的字节/字符数组
- 如何仅使用对象名称打印特定于对象的成员
- 回溯C++不打印函数,因此文件
- 在一定长度后从数组中打印时缺少整数
- 为什么这个 c++ 代码打印出长度 5,当我打印出字符串时,程序会自动终止?
- 在gem5中打印文件中的所有cache_blocks
- 打印数字图案
- Log4cpp:以UTC/GMT时区打印日期
- 使用DFS进行海报打印