图的深度优先遍历算法java实现

深度优先遍历,也称为深度优先搜索,简称为DFS。它的实现原理为,它从图中某个顶点出发,访问此顶点,然后从v的未被访问的邻接点出发深度优先遍历图,直至图中所有和V有路径相通的顶点都被访问到。代码如下:
利用到的图

import java.util.Scanner;
class Point{   //这个类用于邻接表,因为每一个顶点在邻接表中都存在一个指向其它顶点的指针域所以要将指针域和数据域封装成一个具体的类
 int data;
 EdegeNode nextPoint;
 public Point() {}
 public Point(int data)
 {
  this.data=data;
  this.nextPoint=null;
 }
 
}
class EdegeNode{   //边表结点类
  int adjvex;
  EdegeNode nextEdge;
  public EdegeNode() {}
  public EdegeNode(int adjvex)   //初始化边结点
  {
   this.adjvex=adjvex;
   this.nextEdge=null;
  }
}
//图邻接表的表示法
class Graph{
 Point[] point; 
 int numPoint;
 int numEdeges;
 public Graph() {}
 public Graph(int numPoint,int numEdeges)
 {
  this.numPoint=numPoint;
  this.numEdeges=numEdeges;
  point=new Point[numPoint];  //初始化点集数组
 }
 public static void createGraph(Graph graph,int j,int k)  //创建图
 {
  Scanner in=new Scanner(System.in);
  for(int i=0;i<j;i++)
  {
   System.out.println("请输入第"+(i+1)+"顶点的数据:");
   graph.point[i]=new Point(in.nextInt());   //录入顶点的数据域
  }
  for(int i=0;i<k;i++)   //初始化边表,这里使用到了链表中间的头插法
  {
   System.out.println("请输入第"+(i+1)+"条边的两端顶点坐标:");
   int p1=in.nextInt();
   int p2=in.nextInt();
   EdegeNode a=new EdegeNode(p2);         //记录出度
   a.nextEdge=graph.point[p1].nextPoint;  //头插法
   graph.point[p1].nextPoint=a;
//   EdegeNode b=new EdegeNode(p2);         //记录出度
//   b.adjvex=i;                            //记录入度
//   b.nextEdge=graph.point[p2].nextPoint;
//   graph.point[p2].nextPoint=b;  
  }
 }
}
//图得邻接矩阵的表示法
class Graph1{                        //图类
 int[] point;                     //顶点矩阵
    int[][] edge;                    //边集矩阵
 int numPoint;                    //顶点数量
 int numEdeges;                   //边的数量
 public Graph1() {}
 public Graph1(int numPoint,int numEdeges)
 {
  this.numEdeges=numEdeges;
  this.numPoint=numPoint;
  point=new int[numPoint];
  edge=new int[numPoint][numPoint];
 }
 //邻接矩阵的表示法
 public static void createGraph1(Graph1 graph,int j,int k){
  Scanner in=new Scanner(System.in);
  for(int i=0;i<j;i++) {
     System.out.println("请输入第"+(i+1)+"顶点的数据:");
     graph.point[i]=in.nextInt();         //录入顶点数据
  }
  for(int i=0;i<j;i++) {
   for(int h=0;h<j;h++)
   {
    graph.edge[i][h]=Integer.MAX_VALUE;      //将邻接矩阵所有的边初始化为最大整数值
   }
  }
  for(int i=0;i<k;i++)                    //录入所有的边值
  {
   System.out.println("请输入第"+(i+1)+"条边的两端顶点坐标:");
   int p1=in.nextInt();
   int p2=in.nextInt();
   graph.edge[p1][p2]=1;
   graph.edge[p2][p1]=1;
  }
 }   
}
public class dsd {
 //邻接矩阵的深度优先算法
 public static boolean[] b;                 //用来记录每个顶点是否已经访问过
 public static void DFS(Graph1 a,int i) {   //传入的参数为一个图和一个整数
  int j;
  b[i]=true;                             //表示该节点已经被访问过了
  System.out.println("第"+i+"个顶点的信息:"+a.point[i]);
  for(j=0;j<a.numPoint;j++)              //表示循环遍历所有的顶点
  {
   if(a.edge[i][j]==1 && b[j]!=true)
    DFS(a,j);               
  }
 }
 public static void DFSTraverse(Graph1 a)
 {
  int i;
  for(i=0;i<a.numPoint;i++)
  {
   b[i]=false;                   //初始化所有的顶点都是未访问过的
  }
  for( i=0;i<a.numPoint;i++)
  {
   if(!b[i])                    //该顶点未访问若
   DFS(a,i);
  }
 }
 //邻接表的深度优先遍历图
 public static void DFS2(Graph graph,int m)
 {
  EdegeNode a = null;                                                 //创建一个边表结点的引用
  dsd.b[m]=true;                                                      //访问该顶点,将该顶点的标志设置未true
  System.out.println("第"+m+"个顶点的信息:"+graph.point[m].data);     //打印访问的顶点的数据域
  a=graph.point[m].nextPoint;
  while(a!=null)
  {
   if(!dsd.b[a.adjvex])
    DFS2(graph,a.adjvex);
   a=a.nextEdge;                                                    //实际上像是一种遍历链表的行为
  }
 }
 public static void DFSTraverse(Graph graph)
 {
  int i;
  for(i=0;i<graph.numPoint;i++)
   dsd.b[i]=false;                                                 //初始化所有的顶点对应的标记来表示该顶点不曾访问过
  for(i=0;i<graph.numPoint;i++)
   if(!dsd.b[i])
         DFS2(graph,i);
 }
    public static void main(String[] args)
    {
//     Graph1 graph=new Graph1(9,14);
//     b=new boolean[graph.numPoint];
//     Graph1.createGraph1(graph, graph.numPoint,graph.numEdeges);
//     DFSTraverse(graph);
     Graph graph2=new Graph(9,15);
     b=new boolean[graph2.numPoint];
     graph2.createGraph(graph2, graph2.numPoint,graph2.numEdeges);
     DFSTraverse(graph2);
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值