java图的邻接矩阵的表示和实现

本文介绍了一种使用邻接矩阵表示带权图的方法,详细解释了如何通过自定义边类和邻接矩阵类来创建和操作带权无向图。包括插入顶点、插入边、删除顶点和边等操作。

邻接矩阵表示的带权图。。。 
首先创建了一个带权值的边类,在图中插入图的权值,所谓权值就是边上的数字,可以表示两个顶点之间的边的含义(可以是距离,路费。。。)

public class Edge implements Comparable<Edge> {  
  
    public int start,dest,weight;  
      
    public Edge(int start,int dest,int weight){  
        this.start = start;  
        this.dest = dest;  
        this.weight = weight;  
    }  
      
    public String toString(){  
        return "("+start+","+dest+","+weight+")";  
    }  
    public int compareTo(Edge e) {  
        // TODO Auto-generated method stub  
        if(this.start!=e.start)  
            return this.start - e.start;  
        return this.dest - e.dest;  
    }  
  
}  

其次创建邻接矩阵带权图类,在此类中用到的SeqList<T>是我前几天所写的一个顺序表类,主要是用来存放顶点集合的,可以自己定义也可以用jdk中给的顺序表 

public class AdjMatrixGraph<T> {  
    protected SeqList<T> vertexlist;          //顺序表存储的顶底集合  
    protected int[][] adjmatrix;                //图的邻接矩阵,用二维数组表示  
    private final int MAX_WEIGHT = 99999;       //设置最大权值,设置成常量  
    public AdjMatrixGraph(int size){  
        size = size<10?10:size;  
        this.vertexlist = new SeqList<T>(size); //构造容量为size的空顺序表  
        this.adjmatrix = new int[size][size];     
          
        for(int i=0;i<size;i++){             //初始化邻接矩阵  
            for(int j=0;j<size;j++){  
                this.adjmatrix[i][j] = (i==j)?0:MAX_WEIGHT;  
            }  
        }  
    }  
          
        public AdjMatrixGraph(T[] vertices,Edge[] edges){  
            this(vertices.length);  
            if(vertices == null)  
                return;  
            for(int i=0;i<vertices.length;i++)  
                insertVertex(vertices[i]);  
            if(edges!=null)  
                    for(int j=0;j<edges.length;j++)  
                        insertEdge(edges[j]);  
        }  
      
        public int vertexCount(){return this.vertexlist.length();}      //返回定点顺序表的元素个数  
          
        public T get(int i){return this.vertexlist.get(i);}             //返回第i个定点的元素  
          
        public int getWeight(int i,int j){return this.adjmatrix[i][j];} //返<vi,vj>边的权值  
          
        public String toString(){  
            String str = "顶点集合:"+this.vertexlist.toString()+"\n邻接矩阵:\n";  
            int n = this.vertexCount();  
            for(int i=0;i<n;i++){  
                for(int j=0;j<n;j++)  
                    str += this.adjmatrix[i][j] == MAX_WEIGHT?" $":" "+this.adjmatrix[i][j];  
                str +="\n";  
            }  
            return str;  
        }  
          
        public int insertVertex(T x){  
            this.vertexlist.append(x);              //顺序表追加元素,自动扩充  
            if(this.vertexCount()>this.adjmatrix.length){        //若二维数组不足,则扩充  
                int temp[][] = adjmatrix,i,j;                   //定义了局部变量i,j;  
                this.adjmatrix = new int[temp.length*2][temp.length*2];     //二维数组扩充2倍  
                for(i=0;i<temp.length;i++){  
                    for(j=0;j<temp.length;j++)  
                        this.adjmatrix[i][j] = temp[i][j];  
                    for(j=temp.length;j<temp.length*2;j++)  
                        this.adjmatrix[i][j] = MAX_WEIGHT;  
                }  
                for(i=temp.length;i<temp.length*2;i++)  
                    for(j=0;j<temp.length*2;j++)  
                        this.adjmatrix[i][j] = (i == j)?0:MAX_WEIGHT;  
            }  
            return this.vertexlist.length()-1;                  //返回插入顶点的序号           
        }  
          
        public void  insertEdge(int i,int j,int weight){       //插入一条边  
            int n = this.vertexCount();  
            if(i>=0&&i<n&&j>=0&&j<n&&this.adjmatrix[i][j]==MAX_WEIGHT&&i!=j)  
                this.adjmatrix[i][j] = weight;  
        }  
          
        public void insertEdge(Edge edge){  
            this.insertEdge(edge.start, edge.dest, edge.weight);  
        }  
              
        public void removeEdge(int i,int j){                    //删除一条边  
            if(i>=0&&i<vertexCount()&&j>=0&&j<vertexCount()&&i!=j)  
                this.adjmatrix[i][j] = MAX_WEIGHT;  
        }  
          
        public void removeVertex(int i){                        //删除顶点以及和顶点有关系的边  
            int n = this.vertexCount();  
            if(i<0||i>n)  
                return;  
            this.vertexlist.remove(i);  
            for(int j=0;j<i;j++)  
                for(int k=i+1;k<n;k++)  
                    this.adjmatrix[j][k-1] = this.adjmatrix[j][k];      //元素向左移一行  
            for(int j=i+1;j<n;j++)  
                for(int k=0;k<i;k++)  
                    this.adjmatrix[j-1][k] = this.adjmatrix[j][k];      //元素向上移一行  
              
            for(int j=i+1;j<n;j++)  
                for(int k=i+1;k<n;k++)  
                    this.adjmatrix[j-1][k-1] = this.adjmatrix[j][k];  
        }  
          
        public static void main(String[] args){  
            String[] verices = {"A","B","C","D","E"};  
            Edge edges[] = {new Edge(0,1,5),new Edge(0,3,2),new Edge(1,0,5),  
                            new Edge(1,2,7),new Edge(1,3,6),new Edge(2,1,7),  
                            new Edge(2,3,8),new Edge(2,4,3),new Edge(3,0,2),  
                            new Edge(3,1,6),new Edge(3,2,8),new Edge(3,4,9),  
                            new Edge(4,2,3),new Edge(4,3,9)};  
            AdjMatrixGraph<String> graph = new AdjMatrixGraph<String>(verices,edges);  
            System.out.println("带权无向图"+graph.toString());  
            System.out.println("插入顶点F,插入边(A,F,9),删除顶点C,删除边(D,E)");  
            int i = graph.insertVertex("F");  
            graph.insertEdge(0,i,9);  
            graph.insertEdge(i,0,9);  
            graph.removeVertex(2);  
            graph.removeEdge(2, 3);  
            graph.removeEdge(3, 2);  
            System.out.println(graph.toString());  
        }  
}  

输出结果如下(其中"$"表示最大权值) 

带权无向图顶点集合:A B C D E   
邻接矩阵:  
 0 5 $ 2 $  
 5 0 7 6 $  
 $ 7 0 8 3  
 2 6 8 0 9  
 $ $ 3 9 0  
  
插入顶点F,插入边(A,F,9),删除顶点C,删除边(D,E)  
顶点集合:A B D E F   
邻接矩阵:  
 0 5 2 $ 9  
 5 0 6 $ $  
 2 6 0 $ $  
 $ $ $ 0 $  
 9 $ $ $ 0  

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值