图一般分为点、边、权重(如果有),在定义存储一个图的数据结构时,需要对点集、边集进行存储。图的结构表示可以分为邻接矩阵和邻接表,也就是数组和链表的形式,本文主要针对邻接矩阵表示定义。
public class AdjMatrixGraph {
//节点类
class Vertex{
char label;
boolean isVisted;
public Vertex(char label) {
// TODO Auto-generated constructor stub
this.label=label;
isVisted=false;
}
}
private final int MAX_VERTEXS=20;
private Vertex vertexList[];
private int adjMat[][];
private int nVertex;
private Stack theStack;
public AdjMatrixGraph() {
// TODO Auto-generated constructor stub
vertexList=new Vertex[MAX_VERTEXS];
adjMat=new int[MAX_VERTEXS][MAX_VERTEXS];
nVertex=0;
for(int i=0;i<MAX_VERTEXS;i++){
for(int j=0;j<MAX_VERTEXS;j++){
adjMat[i][j]=0;
}
}
theStack=new Stack();
}
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;
}
public void display(int v){
System.out.println(vertexList[v].label);
}
}