объясните немного.
как работает.
Код:
using System;
namespace GraphsMinCoverage.Models.GraphLab
{
public class RoundGraph
{
#region PublicMethods
public static GraphData Generate(int verticesCount, double edgesPercentage)
{
GraphData graphData = new GraphData();
for (int i = 0; i < verticesCount; i++)
{
var vertex = new VertexData("" + (i + 1)) { ID = i + 1 };
graphData.Vertices.Add(vertex);
}
var edgesCount = CalculateEdgesCount(verticesCount, edgesPercentage);
bool[,] adjacencyMatrix = GenerateAdjacencyMatrix(verticesCount, edgesCount);
for (int i = 0; i < verticesCount; i++)
{
for (int j = 0; j < verticesCount; j++)
{
if (adjacencyMatrix[i, j])
{
var edge = new EdgeData(graphData.Vertices[i], graphData.Vertices[j]);
graphData.Edges.Add(edge);
graphData.Vertices[i].IncidentEdges.Add(edge);
graphData.Vertices[j].IncidentEdges.Add(edge);
}
}
}
return graphData;
}
#endregion
#region PrivateMethods
private static bool[,] GenerateAdjacencyMatrix(int verticesCount, int edgesCount)
{
int vertex1, vertex2;
Random rand = new Random();
bool[,] adjacencyMatrix = new bool[verticesCount, verticesCount];
adjacencyMatrix[0, 1] = true;
for (int i = 2; i < verticesCount; i++)
{
vertex1 = i;
vertex2 = rand.Next(0, i);
adjacencyMatrix[vertex1, vertex2] = true;
}
int edgesToAdd = edgesCount - (verticesCount - 1);
for (int i = 0; i < edgesToAdd; i++)
{
do
{
vertex1 = rand.Next(0, verticesCount);
vertex2 = rand.Next(0, verticesCount);
} while (vertex1 == vertex2 || adjacencyMatrix[vertex1, vertex2] || adjacencyMatrix[vertex2, vertex1]);
adjacencyMatrix[vertex1, vertex2] = true;
}
for (int i = 0; i < verticesCount; i++)
{
for (int j = 0; j < verticesCount; j++)
{
if (i > j && adjacencyMatrix[i, j])
{
adjacencyMatrix[j, i] = adjacencyMatrix[i, j];
adjacencyMatrix[i, j] = false;
}
}
}
return adjacencyMatrix;
}
//private void GenerateWeightMatrix()
//{
// GraphData.WeightMatrix = new int[GraphData.VerticesCount, GraphData.VerticesCount];
// for (int i = 0; i < GraphData.VerticesCount; i++)
// {
// for (int j = 0; j < GraphData.VerticesCount; j++)
// {
// if (GraphData.AdjacencyMatrix[i, j])
// {
// GraphData.WeightMatrix[i, j] = _rand.Next(1, 10);
// }
// }
// }
//}
public static int CalculateEdgesCount(int verticesCount, double edgesPercentage)
{
int maxCount = verticesCount * (verticesCount - 1) / 2;
int edgesCount = Convert.ToInt32(maxCount * edgesPercentage);
if (edgesCount < verticesCount - 1)
{
edgesCount = verticesCount - 1;
}
return edgesCount;
}
#endregion
}
}