#include<iostream>
#include<vector>
#include<list>
#include<queue>
#include<string>
using namespace std;
void CreateGraph(vector <list<string>> &Graph)
{
list <string> tmp;
string Content;
Content = "V1";
tmp.push_back(Content);
Content = "V2";
tmp.push_back(Content);
Content = "V3";
tmp.push_back(Content);
Graph.push_back(tmp);
tmp.clear();
Content = "V2";
tmp.push_back(Content);
Content = "V4";
tmp.push_back(Content);
Graph.push_back(tmp);
tmp.clear();
Content = "V3";
tmp.push_back(Content);
Content = "V6";
tmp.push_back(Content);
Content = "V7";
tmp.push_back(Content);
Graph.push_back(tmp);
tmp.clear();
Content = "V4";
tmp.push_back(Content);
Content = "V8";
tmp.push_back(Content);
Graph.push_back(tmp);
tmp.clear();
Content = "V5";
tmp.push_back(Content);
Content = "V2";
tmp.push_back(Content);
Content = "V8";
tmp.push_back(Content);
Graph.push_back(tmp);
tmp.clear();
Content = "V6";
tmp.push_back(Content);
Content = "V7";
tmp.push_back(Content);
Graph.push_back(tmp);
tmp.clear();
Content = "V7";
tmp.push_back(Content);
Graph.push_back(tmp);
tmp.clear();
Content = "V8";
tmp.push_back(Content);
Graph.push_back(tmp);
tmp.clear();
}
inline int FindNode(vector <list<string>> &Graph,string str)
{
int result;
for(result=0;result<Graph.size();result++)
{
if(*(Graph[result].begin())==str)
break;
}
return result;
}
void InnerBFS(vector <list<string>> &Graph,bool flag[],int index)
{
list<string>::iterator cur = Graph[index].begin();
queue<string> myQueue;
string temp;
int num;
cout<<*cur<<' ';
flag[index] = 1;
myQueue.push(*cur);
while(!myQueue.empty())
{
temp = myQueue.front();
myQueue.pop();
index = FindNode(Graph,temp);
if(index>=Graph.size())
return;
cur = Graph[index].begin();
cur++;
while(cur!=Graph[index].end())
{
num = FindNode(Graph,*cur);
if(num<Graph.size()&&flag[num]);
else
{
cout<<*(Graph[num].begin())<<' ';
myQueue.push(*(Graph[num].begin()));
flag[num] = 1;
}
cur++;
}
}
}
void BFStraver(vector <list<string>> &Graph)
{
bool flag[8] = {0};
int i = 0;
while(i<8)
{
if(!flag[i])
InnerBFS(Graph,flag,i);
i++;
}
cout<<endl;
}
void InnerDFS(vector <list<string>> &Graph,bool flag[],int index)
{
list<string>::iterator cur = Graph[index].begin();
int num;
if(!flag[index])
{
cout<<*cur<<' ';
flag[index] = 1;
}
cur++;
while(cur!=Graph[index].end())
{
num = FindNode(Graph,*cur);
if(index>=Graph.size())
return;
InnerDFS(Graph,flag,num); //递归寻找后继结点直到尽头
cur++; //再从当前结点下一个未被访问的邻接点尝试递归
}
}
void DFStraver(vector <list<string>> &Graph)
{
bool flag[8] = {0};
int i = 0;
while(i<8)
{
if(!flag[i])
InnerDFS(Graph,flag,i);
i++;
}
cout<<endl;
}
void main()
{
vector <list<string>> Graph;
CreateGraph(Graph);
cout<<"BFS: ";
BFStraver(Graph);
cout<<"DFS: ";
DFStraver(Graph);
}
#include<vector>
#include<list>
#include<queue>
#include<string>
using namespace std;
void CreateGraph(vector <list<string>> &Graph)
{
list <string> tmp;
string Content;
Content = "V1";
tmp.push_back(Content);
Content = "V2";
tmp.push_back(Content);
Content = "V3";
tmp.push_back(Content);
Graph.push_back(tmp);
tmp.clear();
Content = "V2";
tmp.push_back(Content);
Content = "V4";
tmp.push_back(Content);
Graph.push_back(tmp);
tmp.clear();
Content = "V3";
tmp.push_back(Content);
Content = "V6";
tmp.push_back(Content);
Content = "V7";
tmp.push_back(Content);
Graph.push_back(tmp);
tmp.clear();
Content = "V4";
tmp.push_back(Content);
Content = "V8";
tmp.push_back(Content);
Graph.push_back(tmp);
tmp.clear();
Content = "V5";
tmp.push_back(Content);
Content = "V2";
tmp.push_back(Content);
Content = "V8";
tmp.push_back(Content);
Graph.push_back(tmp);
tmp.clear();
Content = "V6";
tmp.push_back(Content);
Content = "V7";
tmp.push_back(Content);
Graph.push_back(tmp);
tmp.clear();
Content = "V7";
tmp.push_back(Content);
Graph.push_back(tmp);
tmp.clear();
Content = "V8";
tmp.push_back(Content);
Graph.push_back(tmp);
tmp.clear();
}
inline int FindNode(vector <list<string>> &Graph,string str)
{
int result;
for(result=0;result<Graph.size();result++)
{
if(*(Graph[result].begin())==str)
break;
}
return result;
}
void InnerBFS(vector <list<string>> &Graph,bool flag[],int index)
{
list<string>::iterator cur = Graph[index].begin();
queue<string> myQueue;
string temp;
int num;
cout<<*cur<<' ';
flag[index] = 1;
myQueue.push(*cur);
while(!myQueue.empty())
{
temp = myQueue.front();
myQueue.pop();
index = FindNode(Graph,temp);
if(index>=Graph.size())
return;
cur = Graph[index].begin();
cur++;
while(cur!=Graph[index].end())
{
num = FindNode(Graph,*cur);
if(num<Graph.size()&&flag[num]);
else
{
cout<<*(Graph[num].begin())<<' ';
myQueue.push(*(Graph[num].begin()));
flag[num] = 1;
}
cur++;
}
}
}
void BFStraver(vector <list<string>> &Graph)
{
bool flag[8] = {0};
int i = 0;
while(i<8)
{
if(!flag[i])
InnerBFS(Graph,flag,i);
i++;
}
cout<<endl;
}
void InnerDFS(vector <list<string>> &Graph,bool flag[],int index)
{
list<string>::iterator cur = Graph[index].begin();
int num;
if(!flag[index])
{
cout<<*cur<<' ';
flag[index] = 1;
}
cur++;
while(cur!=Graph[index].end())
{
num = FindNode(Graph,*cur);
if(index>=Graph.size())
return;
InnerDFS(Graph,flag,num); //递归寻找后继结点直到尽头
cur++; //再从当前结点下一个未被访问的邻接点尝试递归
}
}
void DFStraver(vector <list<string>> &Graph)
{
bool flag[8] = {0};
int i = 0;
while(i<8)
{
if(!flag[i])
InnerDFS(Graph,flag,i);
i++;
}
cout<<endl;
}
void main()
{
vector <list<string>> Graph;
CreateGraph(Graph);
cout<<"BFS: ";
BFStraver(Graph);
cout<<"DFS: ";
DFStraver(Graph);
}