邻接矩阵的概念:
所谓邻接矩阵,就是用两个数组来表示图的相关信息,其中用一个一维的顶点数组来表示图的顶点信息,用一个二维的边数组来表示图的边或者弧信息。 如下图是一个无向图的邻接矩阵表示,两个顶点之间若联通则二维数组对应位置为1,否则为0。
下图是一个有向图的邻接矩阵表示。
下图是一个带权值的有向图(又称为有向网)的邻接矩阵表示,两个顶点之间若连通则二维数组 对应位置为边上的权值,否则为无穷大,并且二维矩阵的对角线为0(当然也可以设置为无穷大)
代码部分:
该代码用一个类去实现邻接矩阵,其中类中含有一个一维顶点数组和一个二维边数组字段,并且 包含无向图、有向图、带权值的无向图(无向网)、带权值的有向图(有向网)的创建方法
public class MGraph01 {
public int numNodes; //图的顶点数目
public int numEdges; //图的边数
public Object[] vexs; //一维顶点数组
public int[][] arcs; //二维边数组
public static final int INF = Integer.MAX_VALUE; //无穷大
/**
* 创建无向图的邻接矩阵
*/
public void createUDG() {
Scanner sc = null;
try {
sc = new Scanner(System.in);
System.out.println("请输入图的顶点数、图的边数:");
numNodes = Integer.parseInt(sc.nextLine());
numEdges = Integer.parseInt(sc.nextLine());
vexs = new Object[numNodes];
//录入顶点信息
System.out.println("请输入图中的顶点:");
vexs = sc.nextLine().split(" ");
//录入边的信息
arcs = new int[numNodes][numNodes];
for (int i = 0; i < numEdges; i++) {
System.out.println("请输入第" + (i + 1) + "条边上的一个顶点:");
//locate方法用来定位某个顶点在数组中的索引
int index1 = locate(sc.nextLine());
System.out.println("请输入第" + (i + 1) + "条边上的另一个顶点:");
int index2 = locate(sc.nextLine());
//无向图是个对称矩阵
arcs[index1][index2] = arcs[index2][index1] = 1;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
sc.close();
}
}
/**
* 创建有向图的邻接矩阵
*/
public void createDG() {
Scanner sc = null;
try {
sc = new Scanner(System.in);
System.out.println("请输入图的顶点数、图的边数:");
numNodes = Integer.parseInt(sc.nextLine());
numEdges = Integer.parseInt(sc.nextLine());
vexs = new Object[numNodes];
//录入顶点信息
System.out.println("请输入图中的顶点:");
String str = sc.nextLine();
vexs = str.split(" ");
//录入边的信息
arcs = new int[numNodes][numNodes];
for (int i = 0; i < numEdges; i++) {
System.out.println("请输入第" + (i + 1) + "条弧上的弧尾:");
int index1 = locate(sc.nextLine());
System.out.println("请输入第" + (i + 1) + "条弧上的弧头:");
int index2 = locate(sc.nextLine());
arcs[index1][index2] = 1;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
sc.close();
}
}
/**
* 创建无向网的邻接矩阵
*/
public void createUDN() {
Scanner sc = null;
try {
sc = new Scanner(System.in);
System.out.println("请输入图的顶点数、图的边数:");
numNodes = Integer.parseInt(sc.nextLine());
numEdges = Integer.parseInt(sc.nextLine());
vexs = new Object[numNodes];
//录入顶点信息
System.out.println("请输入图中的顶点:");
String str = sc.nextLine();
vexs = str.split(" ");
//矩阵初始化,有向网中
arcs = new int[numNodes][numNodes];
for (int i = 0; i < numNodes; i++) {
for (int j = 0; j < numNodes; j++) {
arcs[i][j] = INF;
}
}
//录入边的信息
for (int i = 0; i < numEdges; i++) {
System.out.println("请输入第" + (i + 1) + "条边上的一个顶点:");
int index1 = locate(sc.nextLine());
System.out.println("请输入第" + (i + 1) + "条边上的另一个顶点:");
int index2 = locate(sc.nextLine());
System.out.println("请输入第" + (i + 1) + "条边上的权值:");
arcs[index1][index2] = arcs[index2][index1] = Integer.parseInt(sc.nextLine());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
sc.close();
}
}
/**
* 创建有向网的邻接矩阵
*/
public void createDN() {
Scanner sc = null;
try {
sc = new Scanner(System.in);
System.out.println("请输入图的顶点数、图的边数:");
numNodes = Integer.parseInt(sc.nextLine());
numEdges = Integer.parseInt(sc.nextLine());
vexs = new Object[numNodes];
//录入顶点信息
System.out.println("请输入图中的顶点:");
String str = sc.nextLine();
vexs = str.split(" ");
//录入边的信息
arcs = new int[numNodes][numNodes];
for (int i = 0; i < numEdges; i++) {
System.out.println("请输入第" + (i + 1) + "条弧上的弧尾:");
int index1 = locate(sc.nextLine());
System.out.println("请输入第" + (i + 1) + "条弧上的弧头:");
int index2 = locate(sc.nextLine());
System.out.println("请输入第" + (i + 1) + "条弧上的权值:");
arcs[index1][index2] = Integer.parseInt(sc.nextLine());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
sc.close();
}
}
/**
* 通过顶点信息来定位其在顶点数组中的索引
*
* @param s
* @return
*/
public int locate(Object s) {
for (int i = 0; i < vexs.length; i++) {
if (s.equals(vexs[i])) {
return i;
}
}
return -1;
}
}