JAVA实现图的基本操作——生成邻接表结构的图、输出邻接矩阵、深度优先遍历

1、定义的图的数据结构,对于有向图和无向图是通用的。

2、在数据结构中定义了遍历标志,方便深度优先遍历的实现。

3、遇到最大的bug就是:

    //weight=edgs[i].charAt(2);  //这里特别容易出处,每次获取int数值的时候都要特别注意。
    //weight=Character.getNumericValue(edgs[i].charAt(2));    //正确的处理应该要再转换一次,用Character包装类的方法
            
    String wei=edgs[i].substring(2);
    weight=Integer.valueOf(wei);
            

最开始是直接用String转char,然后获取的是ASCII码值,跟输入的权值不相等。

然后我就用了char的包装类Character中的getNumbericValue方法,确实就获取了int类型的数据,

但是这里出了另一个问题: 如果是10以内的数字,这个方法是OK的,但是如果输入为10,权值存的是edg.charAt(3)是1而不是10

所以最后我用的是获取子串的方法,将剩下的数据打包成子串再转成int,这样就OK了。

最终代码如下:


import java.util.Scanner;

/** 
*  @author   LilyLee
 * @date     2017年4月25日
 * @time     下午8:02:01
 * @Version  1.0
 * @email    lilylee_1213@foxmail.com
 *
 */

class GraphMatrix{
    static final int MaxNum=20;
    static final int MaxValue=65535;
    char[] Vertex=new char [MaxValue];//顶点信息
    int GType;//图的类型:无向图/有向图
    int VertexNum;//顶点数量
    int EdgeNum;//边的数量
    int [][] EdgeWeight=new int[MaxNum][MaxNum];//边的权值
    int [] isTrav=new int [MaxValue];//遍历标志
}


public class My_Graph {
    
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        GraphMatrix GM= new GraphMatrix();
        System.out.println("输入图的类型:0为无向图,1为有向图");
        GM.GType=sc.nextInt();
        System.out.println("输入顶点数量");
        GM.VertexNum=sc.nextInt();
        System.out.println("输入全部顶点,如 ABCDEF");
        String vd=sc.next();
        System.out.println("输入图的边数");
        GM.EdgeNum=sc.nextInt();
        
        String []edgs=new String[GM.EdgeNum];
        System.out.println("输入构成各边的顶点及权值,如 :AB3   AC1  BC6 ");
        for(int i=0;i<GM.EdgeNum;i++){
            edgs[i]=sc.next();
        }
        
        
        
        ClearGraph(GM);//清空图(也就是初始化,将所有边置为无穷大)
        CreateGraph(GM,vd,edgs);//创建邻接表结构的图
        System.out.println("该图的邻接矩阵如下");
        OutGraph(GM);//输出邻接矩阵
        DeepTraGraph(GM);//输出深度优先搜索遍历结果
    }
    
    static void CreateGraph(GraphMatrix GM,String vd,String [] edgs){
        int weight; //权重
        char startV,endV; //边的起始点
        int j=0,k=0;
        GM.Vertex=vd.toCharArray();
        
        for(int i=0;i<GM.EdgeNum;i++){
            startV=edgs[i].charAt(0);
            endV=edgs[i].charAt(1);
            //weight=edgs[i].charAt(2);  //这里特别容易出处,每次获取int数值的时候都要特别注意。
            //weight=Character.getNumericValue(edgs[i].charAt(2));    //正确的处理应该要再转换一次,用Character包装类的方法
            
            String wei=edgs[i].substring(2);
            weight=Integer.valueOf(wei);
            
            
            
            for( j=0;startV!=GM.Vertex[j];j++); //查找已有顶点中的开始点
            for( k=0;endV!=GM.Vertex[k];k++);//查找已有顶点中的结束点
            GM.EdgeWeight[j][k]=weight;
            if(GM.GType==0){GM.EdgeWeight[k][j]=weight;} //如果是无向图,则对角位置还要记录一次
        }
        
    }
    
    static void ClearGraph(GraphMatrix GM){
        for(int i=0;i<GM.VertexNum;i++){
            for(int j=0;j<GM.VertexNum;j++){
                GM.EdgeWeight[i][j]=GraphMatrix.MaxValue;
            }
        }
    }
    
    static void OutGraph(GraphMatrix GM){
        for(int i=0;i<GM.VertexNum;i++){
            System.out.printf("\t%c",GM.Vertex[i]);
        }
        System.out.println("\n");
        for(int j=0;j<GM.VertexNum;j++){
            System.out.printf("%c",GM.Vertex[j]);
            for(int k=0;k<GM.VertexNum;k++){
                if(GM.EdgeWeight[k][j]==GraphMatrix.MaxValue){System.out.printf("\t∞");}
                else{System.out.printf("\t%d",GM.EdgeWeight[k][j]);}
            }
            System.out.println("\n");
        }
    }
    
    static void DeepTraGraph(GraphMatrix GM){
        
        for(int i=0;i<GM.VertexNum;i++){GM.isTrav[i]=0;} //清理标志位
        for(int j=0;j<GM.VertexNum;j++){
            if(GM.isTrav[j]==0){
                DeepTraGraphOne(GM,j);
            }
        }
    }
    
    
    static void DeepTraGraphOne(GraphMatrix GM,int n){ //从某一个特定的节点开始深度遍历图
        GM.isTrav[n]=1; //处理遍历标志位
        System.out.printf("->"+GM.Vertex[n]);
        for(int i=0;i<GM.VertexNum;i++){
            if(GM.EdgeWeight[n][i]!=GraphMatrix.MaxValue && GM.isTrav[n]==0){
                DeepTraGraphOne(GM,i);
            }
        }
    }
    
    
    
    
    
    
    
}

    原文作者:数据结构之图
    原文地址: https://blog.csdn.net/u014282557/article/details/70767771
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞