带权无向图
存储结构
存储结构选用邻接表。
当一个图为稀疏图时,使用邻接矩阵法显然要浪费大量的存储空间,而图的邻接表法结合了顺序存储和链式存储方法,大大减少了这种不必要的浪费。
当然,即使我们所处理的图不是稀疏图,邻接表法也是能够胜任的。
图、顶点以及边的代码定义
public class graph_t {
//邻接表实现的带权无向图
private ArrayList<VNode> vertices; //邻接表
private int vexNum; //顶点数目
private int arcNum = 0; //边数目
private static boolean[] visited;
public graph_t() {
this.setVertices(new ArrayList<VNode>());
}
public graph_t(int vexNum) {
this.vexNum = vexNum;
this.setVertices(new ArrayList<VNode>());
}
//还有一些getter和setter就不赘述了
}
class ArcNode{
//边表节点
VNode node1; //node1,node2为无向边连接的两个顶点
VNode node2;
int weight; //权值
ArcNode next; //指向下一条边的指针
public ArcNode() {}
public ArcNode(VNode node1, VNode node2, int weight) {
this.node1 = node1;
this.node2 = node2;
this.weight = weight;
this.next = null;
}
public ArcNode(ArcNode node){
this.node1 = node.node1;
this.node2 = node.node2;
this.weight = node.weight;
this.next = node.next;
}
@Override
public String toString() {
return "("+this.getNode1().getName()+","+this.getWeight()+","+this.getNode2().getName()+")";
}
}
class VNode{
//顶点表节点
String name; //顶点名称
ArcNode first; //指向与该顶点连接的第一条边
public VNode() {}
public VNode(String name) {
this.name = name;
this.first = null;
}
}
怎样创建图?
由于是控制台应用,所以我选择创建图的方法自然是通过用户从键盘输入。具体为以下步骤:
- 输入图的顶点个数;
- 输入图的边个数;
- 输入各个顶点的名称,在一行内输入,以空格分隔。例如:有四个顶点,做如下输入:
A B C D
; - 依次输入各条边的信息,具体做法是:有几条边就输入几行信息,每行表示一条边,以这种形式
"A 5 B"
来表示一条边,其表示这条边连接A,B两个顶点,权值为5.
实现代码
public void create(){
//以用户输入方式构造图
Scanner scanner = new Scanner(System.in);
int vexNumIn = this.vexNum;
int arcNumIn = this.arcNum;
if(this.vexNum == 0){
System.out.print("请输入顶点个数:");
vexNumIn = scanner.nextInt();
System.out.print("请输入边的个数:");
arcNumIn = scanner.nextInt();
scanner.nextLine();
}
System.out.println("请输入" + vexNumIn + "个顶点的名称,以空格分隔:");
String tmpStr = scanner.nextLine(); //临时字符串
String[] tmpDatas = tmpStr.split(" "); //临时数据列表
for (int i = 0; i < vexNumIn; i++) {
this.addNode(new VNode(tmpDatas[i]));
}
System.out.println("请输入" + arcNumIn + "条边的数据,每条占一行,形如: a b 5, 其中a为顶点1,b为顶点2,5为权值:");
int tmpCount = 0; //保存一个临时数据,表示当前已输入了几条边
while (tmpCount < arcNumIn){
tmpStr = scanner.nextLine();
tmpDatas = tmpStr.split(" ");
if(this.containVnode(tmpDatas[0]) >= 0&&this.containVnode(tmpDatas[1]) >= 0){
//当该边连接的两个顶点都存在时才能添加边
this.addArc(new ArcNode(this.getVNodeByName(tmpDatas[0]),this.getVNodeByName(tmpDatas[1]),
Integer.parseInt(tmpDatas[2])));
tmpCount ++; //成功添加一条边,标志位加一
}else{
for (int k = 0; k < 2; k++) {
if(!(this.containVnode(tmpDatas[0]) >= 0)){
//为不存在的顶点给出提示信息并重新输入
System.out.println("不存在顶点" + tmpDatas[k] + ",请重新输入该边");
break;
}
}
}
}
}
两个辅助创建图的方法
添加顶点
向图中添加顶点非常简单,只需要将邻接表增加一项并把顶点数加一即可。
public void addNode(VNode vnode){
//添加顶点
this.vertices.add(vnode);
this.vexNum ++;
}
添加边
向图中添加边稍难一些,因为要添加边首先要在邻接表中找到待添加边所连接的两个顶点,另外由于是无向图,所以每条边将会被添加两次,也就是在它所连接的两个顶点的边表中各出现一次。实现代码如下
public void addArc(ArcNode arcNode){
//添加边
boolean isAdded = false; //标记是否添加成功
for (VNode node:this.vertices) {
if(node.getName().equals(arcNode.getNode1().getName()) || node.getName().equals(arcNode.getNode2().getName())){
//找到对应顶点
/*这里必须复制一个arcNode的副本,不能只是简单地引用它否则有如下问题
添加arcNode=(A,1,B)时
匹配到A时,会改变arcNode.next的值,当在匹配到B时又会改变一次导致A的边表发生变化
*/
ArcNode tmpArcNode = new ArcNode(arcNode);
//头插法插入新边
tmpArcNode.next = node.first;
node.first = tmpArcNode;
isAdded = true;
}
}
//如果添加成功就将边数目加1
this.arcNum = isAdded == true?this.arcNum + 1 : this.arcNum;
}
在控制台展示图
由于直接在控制台展示图的原始形态,也就是把图画出来太过困难,于是我选择直接将图的邻接表打印在控制台上。具体的实现效果将在最后展示,实现代码如下:
public void showGraph(){
//展示图
for (VNode node: this.vertices) {
System.out.print(node.getName());
ArcNode tmpArcNode = node.first;
while (tmpArcNode!=null){
System.out.print("->" + tmpArcNode.toString());
tmpArcNode = tmpArcNode.next;
}
System.out.println();
}
}
运行效果
所执行的代码很简单:
public static void main(String[] args) {
graph_t g = new graph_t();
g.create();
g.showGraph();
}
所输入的是这样一张图:
输入输出结果如下:
作者只是学生,如有错误恳请指出