java 圖的鄰接表存儲,廣度優先遍歷

1.定義邊結點數據結構

public class ArcNode {
    /*邊表結點*/
    int adjvex;//頂點序號
    ArcNode next;//指向下一個鄰接點
    public ArcNode(){
        adjvex=-1;
        next=null;
    }
}

2.定義頂點數據結構

public class VexNode {
    /*頂點結點*/
    char data;//頂點信息
    ArcNode head;//邊表頭指針,指向鄰接邊
    public VexNode(char da){
        data=da;
        head=null;
    }
}

3.定義鄰接表的數據結構

public class VGraph {
    /*鄰接表數據結構*/
    VexNode list[];//表
    int edges,vexs;//邊數,頂點數
    public VGraph(int ve,int ed,VexNode [] li){
        vexs=ve;
        edges=ed;
        list=li;
    }
}

4.創建圖的鄰接表存儲,顯示鄰接表,訪問鄰接表元素,廣度優先遍歷鄰接表

public class GraphList {

    /*創建圖的鄰接表存儲*/
    public void CreateGraph(VGraph graph,int weight[][],int n){
        /*n爲頂點總數*/
        int i,j;
        for(i=0;i<n;i++){
            for(j=n-1;j>=0;j--){
                if(weight[i][j]!=0){
                    /*鄰接矩陣元素不爲0,則生成一個邊表結點*/
                    ArcNode edge=new ArcNode();
                    edge.adjvex=j;
                    edge.next=graph.list[i].head;//從鏈表頭部插入新的結點
                    graph.list[i].head=edge;
                    graph.edges++;
                }
            }
        }
    }

    /*顯示圖的鄰接表存儲*/
    public void ShowGraph(VGraph graph){
        int i;
        for(i=0;i<graph.vexs;i++){
            System.out.print(graph.list[i].data+": ");
            ArcNode edge=new ArcNode();
            edge=graph.list[i].head;
            while(edge!=null){
                System.out.print(edge.adjvex+" ");
                edge=edge.next;
            }   
            System.out.println();
        }
    }

    /*獲取第一個鄰接點*/
    public int GetFirst(VGraph graph,int k){
        if(k<0||k>graph.vexs-1){
            System.out.println("參數k超出範圍");
            return -1;
        }
        if(graph.list[k].head==null){
            return -1;
        }
        else {
            //System.out.println(graph.list[k].head.adjvex);
            return graph.list[k].head.adjvex;
        }
    }

    /*獲取下一個鄰接點*/
    public int GetNext(VGraph graph,int k,int t){
        if(k<0||k>graph.vexs-1||t<0||t>graph.vexs-1){
            System.out.println("參數 k or t 超出範圍");
            return -1;
        }
        if(graph.list[k].head==null){
            return -1;
        }
        else {
            ArcNode edge=new ArcNode();
            edge=graph.list[k].head;            
            while(edge!=null && edge.adjvex!=t){
                edge=edge.next;
            }
        //  System.out.println(edge.next.adjvex);   
            return edge.next.adjvex;
        }
    }


    /*廣度優先遍歷鄰接表*/
    public void BFSGraph(VGraph graph,int k,int n){
        //k爲第一個要訪問的頂點,n爲圖中頂點數
        if(k<0||k>graph.vexs-1){
            System.out.println("參數 k 超出範圍");
            return ;
        }

        int visited[]=new int[n];//設置結點被訪問標記
        for(int i=0;i<n;i++){//初始化,設置沒有被訪問過
            visited[i]=0;
        }

        //用隊列存儲圖中的頂點,先進先出
        Queue <Integer>queue=new LinkedList<Integer>();
        queue.add(k);
        visited[k]=1;
        int u;     //u用於存儲隊頂元素
        ArcNode v=new ArcNode();   //v用於存儲頂點u的鄰接頂點
        while(!queue.isEmpty()){
            u=queue.remove();
            System.out.print(graph.list[u].data);
            v=graph.list[u].head;
            while(v!=null){
                if(visited[v.adjvex]!=1){
                    queue.add(v.adjvex);
                    visited[v.adjvex]=1;
                }
                v=v.next;
            }           
        }               
    }


    public static void main(String args[]){
        char []data=new char[]{'A','B','C','D','E'};
        int n=data.length;
        int [][]weight=new int[][]{
                {0,1,0,0,1},
                {1,0,1,1,0},
                {0,1,0,0,0},
                {0,1,0,0,1},
                {1,0,0,1,0}
                };
        VexNode []list=new VexNode[n];
        for(int i=0;i<n;i++){
            /*初始化結點鏈表*/
            list[i]=new VexNode(data[i]);
        }

        VGraph graph=new VGraph(n,0,list); //初始化鄰接表
        GraphList gp=new GraphList();
        gp.CreateGraph(graph,weight, n);  //創建鄰接表
        gp.ShowGraph(graph);  //顯示鄰接表
        gp.GetNext(graph, 0,1); 
        gp.BFSGraph(graph, 0, n);  //廣度優先遍歷鄰接表
    }

}
發佈了34 篇原創文章 · 獲贊 6 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章