图的基本概念
程序员文章站
2023-12-23 17:11:04
...
package d18图的基本概念;
//顶点类
public class Vertex {
private char label;
public Vertex (char label){
this.label = label;
}
}
package d18图的基本概念;
//图
public class Graph {
//顶点数组
private Vertex[] vertexList;
//临界矩阵
private int [] [] adjMat;
//顶点的最大数目
private int maxSize;
//当前节点
private int nVertex;
public Graph(){
vertexList = new Vertex[maxSize];
adjMat = new int [maxSize][maxSize];
for(int i = 0;i<maxSize;i++){
for(int j = 0;j<maxSize;j++){
adjMat[i][j]=0;
}
}
nVertex=0;
}
//添加顶点
public void addVertex (char label){
vertexList[nVertex++]=new Vertex(label);
}
//添加边
public void addEdge(int start,int end){
adjMat[start][end]=1;
adjMat[end][start]=1;
}
}
package d18图的基本概念;
public class TestGraph {
public static void main(String[] args) {
Graph g = new Graph();
g.addVertex('A');
g.addVertex('B');
g.addVertex('C');
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 2);
}
}