namespace DSList
{
//有向图邻接矩阵类(Directed Graph Adjacency Matrix Class)
public class DireGraAdjMatrix<T> : IDireGraph<T>
{
//Fields
private GvNode<T>[] nodes;
private int numArcs;
private int[,] matrix;
//Constructor
public DireGraAdjMatrix(int n)
{
nodes = new GvNode<T>[n];
numArcs = 0;
matrix = new int[n, n];
}
//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 == 1)
{
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)] == 1)
{
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)] == 1)
{
return true;
}
else
{
return false;
}
}
}
}