Skip to content

Commit

Permalink
Create GenerateMatrix.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
SergeyKuz1001 authored Nov 1, 2019
1 parent 8e90ff5 commit defeff1
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions Task02/GenerateMatrix.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using System;
using System.IO;

namespace Task02
{
public class GenerateMatrix
{
static int Whole = 1000000;

public static void Execute(int n, int m, int minWeight, int maxWeight)
{
GeneralResources.n = n;
GeneralResources.graphMatrix = new int[n, n];
Random random = new Random();
int partAmountEdgesFromMax = (int)((long)m * Whole / (n * (n - 1) / 2));
m = 0;
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if (random.Next(Whole) < partAmountEdgesFromMax)
{
int weight = random.Next(minWeight, maxWeight);
GeneralResources.graphMatrix[i, j] = GeneralResources.graphMatrix[j, i] = weight;
m++;
}
else
{
GeneralResources.graphMatrix[i, j] = GeneralResources.graphMatrix[j, i] = GeneralResources.INF;
}
}
}

GeneralResources.graphListEdges = new (int, int, int)[m];
int k = 0;
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if (GeneralResources.graphMatrix[i, j] != GeneralResources.INF)
{
GeneralResources.graphListEdges[k] = (GeneralResources.graphMatrix[i, j], i, j);
k++;
}
}
}

StreamWriter foutMatrix = new StreamWriter("matrix.txt");
foutMatrix.Write($"{n} {m}\n");
for (k = 0; k < m; k++)
{
foutMatrix.Write($"{GeneralResources.graphListEdges[k].Item2 + 1} " +
$"{GeneralResources.graphListEdges[k].Item3 + 1} " +
$"{GeneralResources.graphListEdges[k].Item1}\n");
}
foutMatrix.Close();
}
}
}

0 comments on commit defeff1

Please sign in to comment.