欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

图 邻接矩阵 深度优先遍历 广度优先遍历

程序员文章站 2022-06-13 11:42:33
...

Eclipse java工程: 图的深度优先遍历、广度优先遍历
demo:http://download.csdn.net/detail/keen_zuxwang/9875848

import java.util.ArrayList;
import java.util.LinkedList;
/**
* @description 邻接矩阵模型类
*/
public class Graph {
private ArrayList vertexList;//存储点的链表
private int[][] edges;//邻接矩阵,用来存储边
private int numOfEdges;//边的数目
private int n;

public Graph(int n) {
    //初始化矩阵,一维数组,和边的数目
    this.n = n;
    edges=new int[n][n];
    vertexList=new ArrayList(n);
    numOfEdges=0;
}

//得到结点的个数
public int getNumOfVertex() {
    return vertexList.size();
}

//得到边的数目
public int getNumOfEdges() {
    return numOfEdges;
}

//返回结点i的数据
public Object getValueByIndex(int i) {
    return vertexList.get(i);
}

//返回v1,v2的权值
public int getWeight(int v1,int v2) {
    if((v1>=0 && v1<n) && (v2>=0 && v2<n)){
        return edges[v1][v2];
    }else{
        return -1;
    }
}

//插入结点
public void insertVertex(Object vertex) {
    vertexList.add(vertexList.size(),vertex);
}

//插入边
public void insertEdge(int v1,int v2,int weight) {
    edges[v1][v2]=weight;
    numOfEdges++;
}

//删除边
public void deleteEdge(int v1,int v2) {
    edges[v1][v2]=0;
    numOfEdges--;
}

//得到第一个邻接结点的下标
public int getFirstNeighbor(int index) {
    for(int j=0;j<vertexList.size();j++) {
        if (edges[index][j]>0) {
            return j;
        }
    }
    return -1;
}

//根据前一个邻接结点的下标来取得下一个邻接结点
public int getNextNeighbor(int v1,int v2) {
    for (int j=v2+1;j<vertexList.size();j++) {
        if (edges[v1][j]>0) {
            return j;
        }
    }
    return -1;
}

public static void main(String args[]) {
    int n=4,e=4;//分别代表结点个数和边的数目
    int i = 0, j;
    int idx;
    String labels[]={"V0","V1","V2","V3"};//结点的标识
    Graph graph=new Graph(n);

    for(String label:labels) {
        graph.insertVertex(label);//插入结点
        System.out.print("结点"+i+", 标识"+label+"\n");
        i++;
    }
    System.out.print("\n");

    //插入四条边
    graph.insertEdge(0, 1, 2); //v0 v1 边
    graph.insertEdge(0, 2, 5); //v0 v2 边
    graph.insertEdge(0, 3, 1); //v0 v3 边
    graph.insertEdge(2, 3, 8); //v2 v3 边
    graph.insertEdge(3, 0, 7); //v3 v0 边

    System.out.println("结点个数是:"+graph.getNumOfVertex());
    System.out.println("边的个数是:"+graph.getNumOfEdges());

    System.out.print("\n");
    System.out.print("邻接矩阵 \n");
    for(i=0; i<n; i++){
        for(j=0; j<n; j++){
            System.out.print(graph.edges[i][j]+"  ");
        }
        System.out.print("\n");
    }
    System.out.print("\n");

    idx = graph.getFirstNeighbor(0);    
    System.out.print("结点0: 第一个邻接点"+idx+", 权重"+graph.getWeight(0,idx));
    System.out.print("\n");
    if(idx>0){
        idx = graph.getNextNeighbor(0, idx);
        System.out.print("结点0: 第二个邻接点"+idx+", 权重"+graph.getWeight(0,idx));
        System.out.print("\n");
        if(idx>0){
            idx = graph.getNextNeighbor(0, idx);
            System.out.print("结点0: 第三个邻接点"+idx+", 权重"+graph.getWeight(0,idx));
            System.out.print("\n");

            if(idx>0){
                graph.deleteEdge(0, idx);//删除<V0,V3>边
                System.out.print("删除<V0,V3>边后, ");
                idx = graph.getNextNeighbor(0, idx);
                System.out.print("结点0: 第三个邻接点"+idx+", 权重"+graph.getWeight(0,idx)); // -1 表示没有
                System.out.print("\n");

                System.out.print("\n邻接矩阵\n");
                for(i=0; i<n; i++){
                    for(j=0; j<n; j++){
                        System.out.print(graph.edges[i][j]+"  ");
                    }
                    System.out.print("\n");
                }
                System.out.print("\n");
            }
        }
    }

    idx = graph.getFirstNeighbor(1);
    System.out.print("结点1的邻接点"+idx);
    System.out.print("\n");

    idx = graph.getFirstNeighbor(2);
    System.out.print("结点2的邻接点"+idx);
    System.out.print("\n");

    idx = graph.getFirstNeighbor(3);
    System.out.print("结点3的邻接点"+idx);
    System.out.print("\n");
}

}

图 邻接矩阵 深度优先遍历 广度优先遍历

图 邻接矩阵 深度优先遍历 广度优先遍历