#include<iostream>
using namespace std;
typedef int **Graph;
int *visted;
void InitGraph(Graph &G,int n)
{
int i;
int j;
visted=new int [n];
G=new int *[n];
for(i=0;i<n;++i)
G[i]=new int [n];
for(i=0;i<n;++i)
for(j=0;j<n;++j)
G[i][j]=0;
for(i=0;i<n;++i)
visted[i]=0;
}
void CreateGraph(Graph &G,int n,int edge)//创建图
{
int i,j;
int k;
for(k=0;k<edge;++k)
{
cin>>i>>j;
G[i][j]=G[j][i]=1;
}
}
void DFS(Graph &G,int n,int i)//i为初始的遍历顶点
{
int k;
cout<<"==>>"<<i;
visted[i]=1;
for(k=0;k<n;++k)
{
if(!visted[k] &&G[i][k]!=0)
DFS(G,n,k);
}
}
int main()
{
Graph G;
InitGraph(G,6);
CreateGraph(G,6,8);
DFS(G,6,5);
}
图的深度遍历(DFS)
最新推荐文章于 2024-03-13 15:38:05 发布