24.无向网邻接矩阵类
namespace DSList
{
//无向网邻接矩阵类(Undirected Net Adjacency Matrix Class)
public class NetAdjMatrix<T> : IGraph<T>
{
//Fields
private GvNode<T>[] nodes;
private int numEdges;
private int[,] matrix;
//Constructor
public NetAdjMatrix(int n)
{
nodes = new GvNode<T>[n];
matrix = new int[n, n];
numEdges = 0;
}
//Properties
public GvNode<T> GetGvNode(int index)
{
return nodes[index];
}
public void SetGvNode(int index, GvNode<T> v)
{
nodes[index] = v;
}
public int NumEdges
{
get
{
return numEdges;
}
set
{
numEdges = 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 GetNumOfEdge()
{
return numEdges;
}
public bool IsGvNode(GvNode<T> v)
{
foreach (GvNode<T> nd in nodes)
{
if (v.Equals(nd))
{
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 SetEdge(GvNode<T> v1, GvNode<T> v2, int v)
{
if (!IsGvNode(v1) || !IsGvNode(v2))
{
Console.WriteLine("GvNode is not belong Graph!");
return;
}
if (v != 0)
{
matrix[GetIndex(v1), GetIndex(v2)] = v;
matrix[GetIndex(v2), GetIndex(v1)] = v;
++numEdges;
}
else
{
Console.WriteLine("Weight is not right!");
}
}
public void DelEdge(GvNode<T> v1, GvNode<T> v2)
{
if (!IsGvNode(v1) || !IsGvNode(v2))
{
Console.WriteLine("GvNode is not belong Graph!");
return;
}
if (matrix[GetIndex(v1), GetIndex(v2)] != 0)
{
matrix[GetIndex(v1), GetIndex(v2)] = 0;
matrix[GetIndex(v2), GetIndex(v1)] = 0;
--numEdges;
}
else
{
Console.WriteLine("Edge is not existent");
return;
}
}
public bool IsEdge(GvNode<T> v1, GvNode<T> v2)
{
if (!IsGvNode(v1) || !IsGvNode(v2))
{
Console.WriteLine("GvNode is not belong Graph!");
return false;
}
if (matrix[GetIndex(v1), GetIndex(v2)] != 0)
{
return true;
}
else
{
return false;
}
}
//普里姆算法(最小生成树问题(Minimum Cost Spanning Tree))
public void Prim()
{
int n = nodes.Length;
int Max = 2 * n;
int[] lowcost = new int[Max]; //权值数组
int[] closevex = new int[Max]; //顶点数组
int[,] M = new int[Max, Max]; //辅助矩阵数组
//矩阵数组 M[,] 初始化
for (int i = 0; i <= n; i++)
{
for (int j = 0; j <= n; j++)
{
M[i, j] = int.MaxValue;
}
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (matrix[i, j] != 0)
{
M[i + 1, j + 1] = matrix[i, j];
}
}
}
//辅助数组lowcost[]和closevex[]初始化
for (int i = 2; i <= n; i++)
{
lowcost[i] = M[1, i];
closevex[i] = 1;
}
//某个顶点加入集合U
closevex[1] = 0;
for (int i = 2; i <= n; i++)
{
int mincost = int.MaxValue; //最小权值
int k = i;
int j = 1;
//选取最小的边和相应的顶点
while (j <= n)
{
if (lowcost[j] < mincost && closevex[j] != 0)
{
mincost = lowcost[j];
k = j;
}
j++;
}
Console.WriteLine("({0} , {1}) = {2}", GetGvNode(closevex[k] - 1).Data, GetGvNode(k - 1).Data, M[closevex[k], k]);
//新顶点加入集合U
closevex[k] = 0;
//重新计算该顶点到其余顶点的边的权值
for (j = 2; j <= n; j++)
{
if (M[k, j] < lowcost[j] && closevex[j] != 0)
{
lowcost[j] = M[k, j];
closevex[j] = k;
}
}
}
}
}
}