namespace DSList
{
//有向网邻接矩阵类(Directed Net Adjacency Matrix Class)
public class DireNetAdjMatrix<T> : IDireGraph<T>
{
//Fields
private GvNode<T>[] nodes; //有向网的顶点数组
private int numArcs; //弧的数目
private int[,] matrix; //邻接矩阵数组
//Constructor
public DireNetAdjMatrix(int n)
{
nodes = new GvNode<T>[n];
matrix = new int[n, n];
numArcs = 0;
}
//Properties
public GvNode<T> GetGvNode(int index)
{
return nodes[index];
}
public void SetGvNode(int index, GvNode<T> v)
{
nodes[index] = v;
}
public int NumArcs
{
get
{
return numArcs;
}
set
{
numArcs = value;
}
}
public int GetMatrix(int index1, int index2)
{
return matrix[index1, index2];
}
public void SetMatrix(int index1, int index2, int v)
{
matrix[index1, index2] = v;
}
//Base methods
public int GetNumOfVertex()
{
return nodes.Length;
}
public int GetNumOfArc()
{
return numArcs;
}
public bool IsGvNode(GvNode<T> v)
{
foreach (GvNode<T> nd in nodes)
{
if (nd.Equals(v))
{
return true;
}
}
return false;
}
public int GetIndex(GvNode<T> v)
{
int i = -1;
for (i = 0; i < nodes.Length; ++i)
{
if (nodes[i].Equals(v))
{
return i;
}
}
return i;
}
public void SetArc(GvNode<T> v1, GvNode<T> v2, int v)
{
if (!IsGvNode(v1) || !IsGvNode(v2))
{
Console.WriteLine("GvNode is not belong to Graph!");
return;
}
if (v != 0)
{
matrix[GetIndex(v1), GetIndex(v2)] = v;
++numArcs;
}
else
{
Console.WriteLine("Weight is not right!");
return;
}
}
public void DelArc(GvNode<T> v1, GvNode<T> v2)
{
if (!IsGvNode(v1) || !IsGvNode(v2))
{
Console.WriteLine("GvNode is not belong to Graph!");
return;
}
if (matrix[GetIndex(v1), GetIndex(v2)] != 0)
{
matrix[GetIndex(v1), GetIndex(v2)] = 0;
--numArcs;
}
else
{
Console.WriteLine("Arc is not existent!");
return;
}
}
public bool IsArc(GvNode<T> v1, GvNode<T> v2)
{
if (!IsGvNode(v1) || !IsGvNode(v2))
{
Console.WriteLine("GvNode is not belong to Graph!");
return false;
}
if (matrix[GetIndex(v1), GetIndex(v2)] != 0)
{
return true;
}
else
{
return false;
}
}
//狄克斯特拉算法(最短路径问题(Shortest Path))
public void Dijkstra(ref bool[,] pathMatrixArr, ref int[] shortPathArr, GvNode<T> n)
{
int k = 0;
bool[] final = new bool[nodes.Length];
//初始化
for (int i = 0; i < nodes.Length; ++i)
{
final[i] = false;
shortPathArr[i] = matrix[GetIndex(n), i];
for (int j = 0; j < nodes.Length; ++j)
{
pathMatrixArr[i, j] = false;
}
if (shortPathArr[i] != 0 && shortPathArr[i] < int.MaxValue)
{
pathMatrixArr[i, GetIndex(n)] = true;
pathMatrixArr[i, i] = true;
}
}
//n为源点
shortPathArr[GetIndex(n)] = 0;
final[GetIndex(n)] = true;
//处理从源点到其余顶点的最短路径
for (int i = 0; i < nodes.Length; ++i)
{
int min = int.MaxValue;
//比较从源点到其余顶点的路径长度
for (int j = 0; j < nodes.Length; ++j)
{
//从源点到j顶点的最短路径还没有找到
if (!final[j])
{
//从源点到j顶点的路径长度最小
if (shortPathArr[j] < min)
{
k = j;
min = shortPathArr[j];
}
}
}
//从源点到顶点k的路径长度最小
final[k] = true;
//更新当前最短路径及距离
for (int j = 0; j < nodes.Length; ++j)
{
if (!final[j] && (min + matrix[k, j] < shortPathArr[j]))
{
shortPathArr[j] = min + matrix[k, j];
for (int w = 0; w < nodes.Length; ++w)
{
pathMatrixArr[j, w] = pathMatrixArr[k, w];
}
pathMatrixArr[j, j] = true;
}
}
}
}
}
}