数据结构Java版之邻接矩阵实现图(十一)
邻接矩阵实现图,是用一个矩阵,把矩阵下标作为一个顶点,如果顶点与顶点之间有边。那么在矩阵对应的点上把值设为 1 。(默认是0)
package mygraph; import java.util.List; /** * 邻接矩阵表示图 * @author Ranter * */ public class Vertex { private char lable; //矩阵元素 private int[][] list = new int[20][20]; private char[] vertexList = new char[20]; private int nVerts; //当前顶点下标 Vertex() { this.nVerts = 0; } //增加一个顶点 public void addVertex(char lable) { vertexList[nVerts++] = lable; } //增加一条边 public void addEdge(int start, int end) { list[start][end] = 1; list[end][start] = 1; } //打印矩阵 public void printMatrix() { for(int i = 0; i < nVerts; i ++) { for(int j = 0; j < nVerts; j ++) { System.out.print(list[i][j]); } System.out.println(); } } }
测试程序:
public static void main(String[] args) { Vertex v = new Vertex(); v.addVertex('a'); //0 v.addVertex('b'); //1 v.addVertex('c'); //2 v.addVertex('d'); //3 v.addVertex('e'); //4 v.addEdge(0, 2); //a-c v.addEdge(1, 4); //b-e v.addEdge(2, 4); //b-e v.addEdge(0, 4); //a-e v.printMatrix(); }
测试结果:
a b c d e a 0 0 1 0 1 b 0 0 0 0 1 c 1 0 0 0 1 d 0 0 0 0 0 e 1 1 1 0 0