邻接表相关介绍
邻接表(Adjacency List)是图的一种链式存储方式。在邻接表中,对每个顶点(即表头节点)建立一个单链表,第i个单链表中节点表示依附于顶点vi 的边(对有向图而言,是以顶点vi为尾的弧)。所以在邻接表中,除了节点外,还有表头节点。
数据结构定义如下
节点和表头节点的结构定义如下
具体数据结构定义如下:
ArcNode表示表节点,VNode表示头节点,ALGraph表示图
class
ArcNode
{
int vex; //弧指向的顶点的位置
ArcNode next; //指向下一条弧的指针
String info; //该弧相关信息
double weight; // 弧的权重
}
class
VNode
{
int vex; // 顶点节点信息
ArcNode firstNode; // 指向第一条依附该顶点的弧的指针
}
class
ALGraph
{
List<VNode> vertices;
int vexNum; //顶点数量
int arcNum; //弧数量
int type; //类别:0无向图,1有向图
public ALGraph() {
vertices = new ArrayList<VNode>();
vexNum = 0; arcNum = 0;
type = 0;//无向图
}
}
拟建的图为:
最后拟生成的邻接表为:
头结点为V1,对应的邻接表为: 2--->4--->null
头结点为V2,对应的邻接表为: 1--->3--->5--->null
头结点为V3,对应的邻接表为: 2--->4--->5--->null
头结点为V4,对应的邻接表为: 1--->3--->null
头结点为V5,对应的邻接表为: 2--->3--->null
代码如下:
package Graph;
2:
import java.util.ArrayList;
import java.util.List;
5:
// http://algs4.cs.princeton.edu/41undirected
// jGraphT
class ArcNode {
//弧指向的顶点的位置
//指向下一条弧的指针
//该弧相关信息
// 弧的权重
13:
double weight) {
, weight);
16: }
17:
double weight) {
this.vex = vex;
this.next = next;
this.info = info;
this.weight = weight;
23: }
24: }
25:
class VNode {
// 顶点节点信息
// 指向第一条依附该顶点的弧的指针
29:
int vex) {
this(vex, null);
32: }
33:
int vex, ArcNode firstNode) {
this.vex = vex;
this.firstNode = firstNode;
37: }
38:
boolean equals(VNode node) {
if (vex == node.vex) {
return true;
42: }
return false;
44: }
45: }
46:
class ALGraph {
48: List<VNode> vertices;
//顶点数量
//弧数量
//类别:0无向图,1有向图
52:
public ALGraph() {
new ArrayList<VNode>();
55: vexNum = 0;
56: arcNum = 0;
//无向图
58: }
59:
int[][] arcs) {
//其实应该传进来参数是数组类型,这里为了操作简便,就简单的直接赋值
62: addVertex(1);
63: addVertex(2);
64: addVertex(3);
65: addVertex(4);
66: addVertex(5);
67:
68: addArc(1, 2);
69: addArc(1, 4);
70: addArc(2, 3);
71: addArc(2, 5);
72: addArc(3, 4);
73: addArc(3, 5);
74: }
75:
int vex) {
new VNode(vex);
if (!vertices.contains(node)) {
79: vertices.add(node);
80: vexNum++;
81: }
82: }
83:
int end) {
85: addArc(start, end, 0);
86: }
87:
// 直接建立
double weight) {
90: VNode sNode = vertices.get(start - 1);
91: VNode eNode = vertices.get(end - 1);
92:
if(type==0)
94: addArcNext(eNode,sNode,weight);
95: addArcNext(sNode,eNode,weight);
96: }
97:
double weight)
99: {
100: ArcNode p = sNode.firstNode;
new ArcNode(eNode.vex, weight);
if (p == null) {
103: sNode.firstNode = node;
else {
105: ArcNode temp = sNode.firstNode;
while(true)
107: {
if (temp.next == null&&temp.vex!=node.vex)
110: {
111: temp.next = node;
break;
114: }
if (temp.next!= null){
116: temp = temp.next;
117: }
118: }
119: }
120: arcNum++;
121: }
122:
void getAdjList(List<VNode> list)
124: {
125: VNode v;
int i=0;i<list.size();i++)
127: {
128: v = list.get(i);
129: getAdjNode(v);
130: System.out.println();
131: }
132: }
133:
void getAdjNode(VNode v)
135: {
136: ArcNode p = v.firstNode;
);
while (p != null) {
);
140: p = p.next;
141: }
142: System.out.print(p);
143: }
144: }
145:
class GraphTest {
void main(String[] args) {
new ALGraph();
int[5];
int[2][];
// 初始化图
152: graph.createGraph(vexs, arcs);
// 获取顶点的数目和边的数目
int arcNum = graph.arcNum;
if(graph.type == 0)
156: arcNum = graph.arcNum/2;
+ arcNum);
// 获取邻接表
159: graph.getAdjList(graph.vertices);
160: }
161: }