图类代码:
package
{public class AdjMatrixGraph
{
private var vertexlist:SeqList; // 顺序表存储图的顶点集合
private var adjmatrix:Array; // 图的邻接矩阵 二维图 存储的是每个顶点的名称(A,B,C,D....)
private var MAX_WEIGHT:int = int.MAX_VALUE/2;
public function AdjMatrixGraph(n:int)
{
initialGraph(n);
}
public function initialGraph(n:int):void
{
// n为顶点的数目
vertexlist = new SeqList(n);
adjmatrix = new Array(n);
for (var i:int = 0; i < n; i++)
{
var arrTemp:Array = new Array();
for (var j:int = 0; j < n; j++)
{
arrTemp[j] = (i == j) ? 0 : MAX_WEIGHT;
}
adjmatrix[i] = arrTemp;
}
// 对角线上为0,其他的都为无穷大。
}
// 构造函数内一个是字符串数组,一个是edge的set集合
public function RestructByVE(vertices:Vector.<Vertex>,edges:Vector.<Edge>):void
{
initialGraph(vertices.length);
for (var i:int = 0; i < vertices.length; i++)
insertVertex(vertices[i]);// 添加顶点
for (var j:int = 0; j < edges.length; j++)
insertEdge(edges[j]);// 添加边
}
// 构造函数内一个是数组集合,一个是edge的set集合
public function RestructByE(list:SeqList, edges:Vector.<Edge>):void
{
initialGraph(list.GetLength());
this.vertexlist = list;
for (var j:int = 0; j < edges.length; j++)
insertEdge(edges[j]);
}
public function GetMaxsize():int
{
return vertexlist.GetMaxsize();
}
public function vertexCount():int
{
return vertexlist.GetLength();
}
// 根据编号得到该顶点
/**
*
* @param i 要查找项目的索引,从1开始
* @return 索引指定的项目
*
*/
public function get(i:int):Vertex
{
return vertexlist.GetElem(i);
}
/**
*
* @param v 要查找的项目
* @return 指定结点在顺序表中的位置
*
*/
public function getItemIndex(v:Vertex):int
{
return vertexlist.Locate(v);
}
/**
*
* @param i 边在矩阵中的行索引
* @param j 边在矩阵中的列索引
* @return 返回指定索引位置的边
*
*/
public function getEdge(i:int , j:int):Edge
{
var E:Edge = new Edge();
return E.structEdge(i,j,adjmatrix[i][j]);
}
/**
*
* @return 得到所有边的集合
*
*/
public function getEdgeSet():Vector.<Edge>
{
var n:int = vertexCount();
if (n<=0)
{
trace("不存在边");
return null;
}
var vectorEdge:Vector.<Edge> = new Vector.<Edge>;
var E:Edge = null;
for (var i:int = 0; i < n; i++)
for (var j:int = 0; j < n; j++)
{
E = new Edge();
E = E.structEdge(i,j,adjmatrix[i][j]);
vectorEdge.push(E);
}
return vectorEdge;
}
public function insertVertex(vertex:Vertex):Boolean
{ // 插入一个顶点,若插入成功,返回true
return vertexlist.Append(vertex);
}
public function insertEdgeByIndex(i:int,j:int,weight:int = 1):Boolean
// 插入一条权值为weight的边<vi,vj>,若该边已有,则不插入
{
if (i >= 0 && i < vertexCount() && j >= 0 && j < vertexCount()&& i != j && adjmatrix[i][j] == MAX_WEIGHT)
{
// 先判断该边两个顶点的编号是否在范围,该边的值是否为最大值,来确定所添加边的值是否存在;
adjmatrix[i][j] = weight;// 添加权值
return true;
}
return false;
}
public function insertEdge(edge:Edge):Boolean
{
if (edge != null)return false ;
return insertEdgeByIndex(edge.start, edge.dest, edge.weight);
}
public function toString():String
{
var str:String = "顶点集合: " + vertexlist.toString() + "\n";
str += "邻近矩阵: \n";
var n:int = vertexCount();
for (var i:int = 0; i < n; i++) {
for (var j:int = 0; j < n; j++) {
if (adjmatrix[i][j] == MAX_WEIGHT)
str += " ∞";// 最大值(不存在)的时候的显示方式;
else
str += " " + adjmatrix[i][j];// 每一个顶点到其他顶点的权值
}
str += "\n";
}
return str;
}
public function removeEdge(i:int, j:int):Boolean // 删除边〈vi,vj〉,若成功,返回T
{
if (i >= 0 && i < vertexCount() && j >= 0 && j < vertexCount()
&& i != j && adjmatrix[i][j] != MAX_WEIGHT) {
// 判断该边的两个顶点是否存在,以及改边的值是否为最大值来判断改边是否存在;
adjmatrix[i][j] = MAX_WEIGHT; // 设置该边的权值为无穷大,说明已不存在;
return true;
}
return false;
}
/**
*
* @param v 要删除项目在顺序表中的索引,逻辑上从1开始
* @return 删除是否成功
*
*/
public function removeVertex(v:int):Boolean // 删除序号为v的顶点及其关联的边
{
var n:int = vertexCount(); // 删除之前的顶点数
if (v >= 0 && v < n) {// V的要求范围
vertexlist.Remove(v); // 删除顺序表的第i个元素,顶点数已减一
for (var i:int = v; i < n - 1; i++)
for (var j:int = 0; j < n; j++)
adjmatrix[i][j] = adjmatrix[i + 1][j]; // 邻接矩阵:删除点以下往上移动一位
for (var j:int = v; j < n - 1; j++)
for (var i:int = 0; i < n - 1; i++)
adjmatrix[i][j] = adjmatrix[i][j + 1]; // 邻接矩阵:删除点以右往左移动一位
return true;
}
return false;
}
/**
*
* @param v 要查找项目的索引,从0开始
* @return 返回顶点v的第一个邻接顶点的序号 ,若不存在第一个邻接顶点,则返回-1
*
*/
public function getFirstNeighbor(v:int):int
{
return getNextNeighbor(v, -1);
}
/**
*
* @param v 项目在矩阵中的行索引
* @param w 在矩阵中判断w列以后的项目是否与有与v相邻接的
* @return 找到v在w后的邻接顶点的索引,这里的v,w是指项目在seqlist中的索引,主要是在邻接矩阵中查找,所以要有顺序,如果不存则返回-1
*
*/
public function getNextNeighbor(v:int, w:int):int
{ // 返回v在w后的下一个邻接顶点
if (v >= 0 && v < vertexCount() && w >= -1 && w < vertexCount()&& v != w) // 对v w的范围限定
for (var j:int = w + 1; j < vertexCount(); j++)
// w=-1时,j从0开始寻找下一个邻接顶点
if (adjmatrix[v][j] > 0 && adjmatrix[v][j] < MAX_WEIGHT)
// 遍历和v相关的点,得到下一个点
return j;
return -1;
}
// -------图的遍历(广度遍历,深度遍历)-------------------------//
public function DFStraverse():void
{
var n:int = this.vertexCount();
var visited:Vector.<Boolean> = new Vector.<Boolean>(n);
for (var i:int = 1; i < n; i++)
{
visited[i] = false;
}
// 编号0为起始点,进行一次深度优先遍历(一次得到一个连通分量)
for (var j:int = 0; j < n; j++)
{
if (!visited[j])
{
trace("以该顶点为" + j + "起始点的遍历:");
DFS(j, visited);
}
}
}
// 参数1:遍历起始点的编号,参数2:记录各个顶点是否被访问过
public function DFS(v:int, visited2:Vector.<Boolean>):void
{
var visited:Vector.<Boolean> = visited2;
visited[v] = true;
trace("遍历顶点" + v);
for (var w:int = this.getFirstNeighbor(v); w >= 0; w = this.getNextNeighbor(v, w))
{
if (!visited[w])
{
visited[w] = true;
DFS(w, visited);
}
}
}
public function BFStraverse():void
{
/*var n:int = this.vertexCount();
var visited:Vector.<Boolean> = new Vector.<Boolean>(n);
// MyQueue myqueue = new MyQueue();
for (var i:int = 1; i < n; i++)
{
visited[i] = false;
}
for (var j:int = 0; j < n; j++)
{
if (!visited[j])
{
visited[j] = true;
trace("遍历起点:" + j);
// myqueue.EnQueue(j);
while (!myqueue.empty())
{
var v:int = int( myqueue.DeQueue());
trace("遍历点:" + v);
for (var w:int = this.getFirstNeighbor(v); w >= 0; w = this.getNextNeighbor(v, w))
{
if (!visited[w])
{
visited[w] = true;
myqueue.EnQueue(w);
}
}
}
}
} */
}
}
}
SeqList代码:
package
{
public class SeqList extends Object
{
//fields
private var data:Vector.<Vertex>;
private var last:int;
private var maxsize:int;
//properties
public function GetDataByIndex(index:int):Vertex
{
return data[index];
}
public function SetData(index:int,value:Vertex):void
{
data[index] = value
}
public function GetLast():int
{
return last;
}
public function GetMaxsize():int
{
return maxsize;
}
public function SetMaxsize(value:int):void
{
maxsize = value;
}
//Constructor
// public SeqList(var n:int = 100)
// {
// SeqList(n);
// }
public function SeqList(size:int)
{
data = new Vector.<Vertex>(size);
last = -1;
maxsize = size;
}
// base Methods
public function GetLength():int
{
return last + 1;
}
public function IsEmpty():Boolean
{
if (last == -1)
{
return true;
}
else
{
return false;
}
}
public function IsFull():Boolean
{
if (maxsize == last + 1)
{
return true;
}
else
{
return false;
}
}
public function Clear():void
{
last = -1;
}
public function Append(item:Vertex):Boolean
{
if (IsFull())
{
trace("List is full!");
return false;
}
data[++last] = item;
return true;
}
/**
*
* @param pos 插入对象的位置,从1开始
* @param item 要插入的对象
*
*/
public function Insert(pos:int, item:Vertex):void
{
if (IsFull())
{
trace("List is full!");
return;
}
if ( pos < 1 || pos > last + 2)
{
trace("Position is error!");
return;
}
if (pos == last + 2)
{
data[last+1] = item;
return;
}
else
{
for (var index:int = last; index >= pos-1; --index)
{
data[index + 1] = data[index];
}
data[pos-1] = item;
}
++last;
return;
}
public function Remove(pos:int):Vertex
{
var temp:Vertex = null;
if (IsEmpty() || pos < 1 || pos > last + 1)
{
trace("List is empty or Position is error!");
return temp;
}
if (pos == last + 1)
{
temp = data[last];
// return temp;
}
else
{
temp = data[pos - 1];
for (var index:int = pos - 1; index < last; ++index)
{
data[index] = data[index + 1];
}
}
--last;
return temp;
}
public function Delete(item:Vertex):void
{
var pos:int = Locate(item);
Remove(pos);
}
/**
*
* @param pos 索引从1开始
* @return 根据索引返回对象
*
*/
public function GetElem(pos:int):Vertex
{
if (IsEmpty() || pos < 1 || pos > last + 1)
{
trace("List is empty or Position is error!");
return null;
}
return data[pos - 1];
}
public function Locate(item:Vertex):int
{
/*if (IsEmpty())
{
trace("List is empty!");
return 0;
}
var index:int = 0;
for (index = 0; index <= last; ++index)
{
if (data[index].Equals(item))
{
break;
}
}
if (index > last)
{
return -1;
}
return ++index;*/
return 0;
}
//Other methods
//反转顺序表
public function Reverse():void
{
var temp:Vertex = null;
var len:int = this.GetLength() - 1;
for (var index:int = 0; index <= len / 2; ++index)
{
temp = data[len - index];
data[len - index] = data[index];
data[index] = temp;
}
}
//合并两个已升序排列的顺序表,合并后也升序排列
/*public function Merge(SeqList<T> la, SeqList<T> lb):void
{
this.maxsize = la.maxsize + lb.maxsize;
var i:int = 0;
var j:int = 0;
while (i < la.GetLength() && j<lb.GetLength())
{
if (la[i].CompareTo(lb[j]) < 0)
{
this.Append(la[i++]);
}
else
{
this.Append(lb[j++]);
}
}
while (i < la.GetLength())
{
this.Append(la[i++]);
}
while (j < lb.GetLength())
{
this.Append(lb[j++]);
}
}*/
//删除顺序表中相同的元素
public function Purge(la:Vector/*SeqList<T>*/):void
{
/*this.Append(la[0]);
for (var i:int = 1; i <la.GetLength(); ++i)
{
var j:int = 0;
for (j = 0; j < this.GetLength(); ++j)
{
if (la[i].Equals(data[j]))
{
break;
}
}
if (j> this.GetLength()-1)
{
this.Append(la[i]);
}
} */
}
//直接插入排序
public function InsertSort():void
{
/*for (var i:int = 1; i <= last; ++i)
{
if (data[i].CompareTo( data[i - 1])<0)
{
T tmp = data[i];
var j:int = 0;
for (j = i - 1; j >= 0 && tmp.CompareTo(data[j])<0; --j)
{
data[j + 1] = data[j];
}
data[j + 1] = tmp;
}
}*/
}
//冒泡排序
public function BubbleSort():void
{
// T tmp = default(T);
// for (int i = 0; i <= Last; ++i)
// {
// for (int j = Last - 1; j >= i; --j)
// {
// if (data[j + 1].CompareTo(data[j])<0)
// {
// tmp = data[j + 1];
// data[j + 1] = data[j];
// data[j] = tmp;
// }
// }
// }
}
public function toString():void
{
trace("");
}
}
}
顶点Vertex代码:
package
{
public class Vertex
{
public var name:String = null;
public function Vertex(n:String)
{
name = n
}
}
}