十字链表是有向图的一种链式存储结构;
弧结点:对应于有向图中的弧,结点结构如下:
tailvex | headvex | hlink | tlink | info |
---|
其中,尾域(tailvex)和头域(headvex)分别指示弧尾和弧头这两个顶点在图中的位置;链域hlink指向弧头相同的下一条弧,链域tlink指向弧尾相同的下一条弧;info指示与该弧相关的信息,如权重;
顶点结点:对应于有向图中的顶点,结点结构如下:
data | firstin | firstout |
---|
其中,数据域(data)存储和顶点相关的信息,如顶点名称等;firstin和firstout两链域分别指示以该顶点为弧头或弧尾的第一个弧结点;
优点:便于找到入度和出度,且建表的时间复杂度较低;
下面给出图的十字链表的Java实现:
package org.sky.graph;
import java.util.Scanner;
/**
* @Description 利用十字链表表示有向图
* @author sky
* @date 2016/12/29
*/
public class OrthogonalListGragh {
//弧结点,包含弧相关的信息
@SuppressWarnings("unused")
private static class ArcBox{
int tailVex; //尾域,指示弧尾顶点在图中的位置
int headVex; //头域,指示弧头顶点在图中的位置
ArcBox headLink; //弧头链域,指示弧头相同的下一条弧
ArcBox tailLink; //弧尾链域,指示弧尾相同的下一条弧
int value; //与当前弧相关的信息,如权重
public ArcBox(int tailVex, int headVex, ArcBox headLink,
ArcBox tailLink, int value) {
super();
this.tailVex = tailVex;
this.headVex = headVex;
this.headLink = headLink;
this.tailLink = tailLink;
this.value = value;
}
public int getTailVex() {
return tailVex;
}
public void setTailVex(int tailVex) {
this.tailVex = tailVex;
}
public int getHeadVex() {
return headVex;
}
public void setHeadVex(int headVex) {
this.headVex = headVex;
}
public ArcBox getHeadLink() {
return headLink;
}
public void setHeadLink(ArcBox headLink) {
this.headLink = headLink;
}
public ArcBox getTailLink() {
return tailLink;
}
public void setTailLink(ArcBox tailLink) {
this.tailLink = tailLink;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
}
@SuppressWarnings("unused")
private static class VexNode{
Object data; //顶点信息
ArcBox firstIn; //指向该顶点的第一条入弧
ArcBox firstOut; //指向该顶点的第一条出弧
public VexNode(Object data) {
super();
this.data = data;
}
public VexNode(Object data, ArcBox firstIn, ArcBox firstOut) {
super();
this.data = data;
this.firstIn = firstIn;
this.firstOut = firstOut;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public ArcBox getFirstIn() {
return firstIn;
}
public void setFirstIn(ArcBox firstIn) {
this.firstIn = firstIn;
}
public ArcBox getFirstOut() {
return firstOut;
}
public void setFirstOut(ArcBox firstOut) {
this.firstOut = firstOut;
}
}
private int numVex; //图的当前顶点数
private int numArc; //图的当前弧数
private VexNode[] vexs; //存储表头结点,通常采用顺序存储结构
public OrthogonalListGragh(int numVex, int numArc, VexNode[] vexs) {
super();
this.numVex = numVex;
this.numArc = numArc;
this.vexs = vexs;
}
/**
* @Desciption 采用十字链表存储表示,构造有向图
*/
public void createDG(){
@SuppressWarnings("resource")
Scanner sc = new Scanner(System.in);
System.out.println("请分别输入图的顶点数及边数");
numVex = sc.nextInt();
numArc = sc.nextInt();
vexs = new VexNode[numVex];
System.out.println("请分别输入图的各个顶点:");
for(int v = 0; v < numVex; v++){ //构造表头向量
vexs[v] = new VexNode(sc.next());
vexs[v].firstIn = null;
vexs[v].firstOut = null;
}
System.out.println("请输入各个边的起始顶点u、v及其权值value");
for(int k = 0; k < numArc; k++){ //输入各弧,并构造十字链表
int v = locateVex(sc.next()); //输入一条弧的始点,即弧尾
int u = locateVex(sc.next()); //输入一条弧的终点,即弧头
int value = sc.nextInt(); //输入弧的权重
ArcBox p = new ArcBox(v, u, vexs[u].firstIn, vexs[v].firstOut, value); //构造弧结点
vexs[u].firstIn = vexs[v].firstOut = p; //在顶点结点完成入弧和出弧连头的插入
}
}
/**
* @Desciption 返回与传入值相对应的定点位置
*/
public int locateVex(Object vex){
for(int v = 0; v < numVex; v++){
if(vexs[v].getData().equals(vex)){
return v;
}
}
return -1;
}
}