数据结构之实现图的深度优先搜索算法

#include <iostream>


using namespace std;


int G[10][10]={ {0,1,0,1,0},
       {1,0,1,0,1},
             {0,1,0,1,1},
       {1,0,1,0,0},
       {0,1,1,0,0}, };

int vexnum=5;


int visited[5];


int init()
{
for(int v=0;v<vexnum;v++)
visited[v]=0;
}


void DFS(int v)
{
visited[v]=1;
cout<<"v="<<v+1<<endl<<endl;
for(int w=0;w<vexnum;w++)
if(G[v][w] && (!visited[w]))
DFS(w); 
}


void show()
{
for(int i=0;i<vexnum;i++)
{
for(int j=0;j<vexnum;j++)
cout<<G[i][j]<<" ";
cout<<endl;
}

cout<<endl;
}


int main()
{
  show();
  init();
  DFS(0);

return 0;
}

猜你喜欢

转载自blog.csdn.net/wrc_nb/article/details/80556728