BFS&DFS

BFS:http://www.jb51.net/article/53730.htm

自己的代码:

#include<iostream>
#include<algorithm>
#include<queue>
#include<list>
#include<stack>
using namespace std;


const unsigned char white = 0;
const unsigned char grey  = 1;
const unsigned char black = 2;


class Graph
{
public:
Graph(int V);//构造函数
void addEdge(int v,int w);//向图中添 加顶点为v,权值为w的边
void BFS(int v);


private:
int V;//顶点的数量
list<int> *Adj;//邻接表表头指针
void BFSNodes(int v,bool visited[],int *d);
};


Graph::Graph(int V)
{
this->V = V;
Adj = new list<int>[V];
}


void Graph::addEdge(int v, int w)
{
Adj[v].push_back(w);
Adj[w].push_back(v);
}


void Graph::BFSNodes(int v,bool visited[],int d[])
{
queue<int> Q;
visited[v] = true;
Q.push(v);


while(!Q.empty())
{
v = Q.front();
Q.pop();
cout<<v<<" "<<d[v]<<endl;


for(list<int>::iterator iter = Adj[v].begin(); iter != Adj[v].end(); ++iter)
{
if(!visited[*iter])
{
visited[*iter] = true;
Q.push(*iter);
d[*iter] = d[v]+1;
}
}
}


}


void Graph::BFS(int v)
{
bool *visited = new bool[V];
int  *d = new int[V];


for(int i = 0; i < V; ++i)
visited[i] = false;


for(int i = 0; i < V; ++i)
d[i] = 0;



BFSNodes(v,visited,d);


int max = 0,index = 0;
for(int i = 0; i < V; ++i)
{
if(max < d[i])
{
max = d[i];
index = i;
}
}
cout<<endl;
cout<<max<<" "<<index<<endl;
}


int main()
{
Graph g(8); 
g.addEdge(0, 1);
g.addEdge(1, 2);
g.addEdge(2, 3); 
g.addEdge(3, 4); 
g.addEdge(3, 5); 
g.addEdge(4, 5); 
g.addEdge(4, 7);
g.addEdge(5, 7);
g.addEdge(5, 6);
g.addEdge(6, 7);
  
cout << "Following is BFS Traversal (starting from vertex 2) \n"; 
g.BFS(2); 


return 0;
}

DFS的非递归版本参考:http://blog.chinaunix.net/uid-26456800-id-3494630.html


DFS代码:

#include<iostream>
#include<algorithm>
#include<queue>
#include<list>
#include<stack>
using namespace std;


#define uint unsigned int
#define uchar unsigned char


const unsigned char white = 0;
const unsigned char grey  = 1;
const unsigned char black = 2;


class Graph
{
public:
Graph(int V);//构造函数
    ~Graph();
void addEdge(int v,int w);//向图中添 加顶点为v,权值为w的边
void DFS();


private:
int V;//顶点的数量
uint time;
list<int> *Adj;//邻接表表头指针
int *p;//父亲节点
uint *start;//各节点的开始时间
uint *end;//各节点的结束时间
uchar *color;//各节点的着色
void DFSNodes(int v);
};


Graph::Graph(int V)
{
this->V = V;
Adj = new list<int>[V];
p = new int[V];
start = new uint[V];
end = new uint[V];
color = new uchar[V];
time = 0;
}


Graph::~Graph()
{
delete [] Adj;
delete [] p;
delete [] start;
delete [] end;
delete [] color;
}


void Graph::addEdge(int v, int w)
{
Adj[v].push_back(w);
//Adj[w].push_back(v);
}








/*
DFS的递归形式
void Graph::DFSNodes(int v)
{
color[v] = grey;
cout<<v<<" ";


time = time+1;
start[v] = time;


for(list<int>::iterator iter = Adj[v].begin(); iter != Adj[v].end(); ++iter)
{
if(color[*iter] == white)
{
p[*iter] = v;
DFSNodes(*iter);
}
}


color[v] == black;
++time;
end[v] = time;
}*/




/*****************DFS的非递归形式****************************/
void Graph::DFSNodes(int v)
{
int topV = 0;
stack<int> S;
S.push(v);
color[v] = grey;
cout<<v<<" ";
start[v] = time;


while(!S.empty())
{
topV = S.top();

bool finished = true;


for(list<int>::iterator iter = Adj[topV].begin(); iter != Adj[topV].end(); ++iter)
{
if(color[*iter]==white)
{
color[*iter] = grey;
p[*iter] = topV;
cout<<*iter<<" ";
S.push(*iter);
time++;
start[*iter] = time;
finished = false;
break;
}
}
if(finished == true)
{
color[topV] = black;
time++;
end[topV] = time;
S.pop();
}
}
}


void  Graph::DFS()
{
for(int i=0; i<V; ++i) 
{
color[i] = white;
p[i] = NULL;
}


for(int i = 0; i < V; ++i)
{
if(color[i] == white)
DFSNodes(i);
}
//DFSNodes(0);


cout<<endl;
for(int i = 0; i < V; ++i)
{
cout<<"Node "<<i<<" : ";
cout<<start[i]<<"->"<<end[i]<<endl;
}
}




int main()
{
Graph g(6); 
g.addEdge(0, 1);
g.addEdge(0, 3);
g.addEdge(3, 1);
g.addEdge(1, 4);
g.addEdge(4, 3);
g.addEdge(2, 4);
g.addEdge(2, 5);
g.addEdge(5, 5);


cout << "Following is DFS Traversal (starting from vertex 0) \n"; 

g.DFS();



return 0;
}

算导22.4-3判断无向图是否有回路

http://wenku.baidu.com/link?url=riiiqgFwjsW07gXZz4yLF0YBI-qsMuC8FzVDzssY0-OoJfxrr6dXJ9mjNQCoE_OBUeQgG3q3VlXrrfrrbv7I9c_fWgrUGok3pbgRyPe6qxe

算导22.4-5判断有向图是否存在回路

http://blog.csdn.net/lyflower/article/details/2137709

上述两题都提到可以使用DFS,DFS的算法复杂度为Theta(V+E),题目要求O(V+E)。这并不影响,因为渐进符号中定义Theta表示渐进上界与下界,O表示渐进上界,只能说题目的要求更为宽松。


猜你喜欢

转载自blog.csdn.net/gin077/article/details/42527119