using System using System Collections Generic using System Linq using

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace test
{
class Graph
{
private int[] I;
private int[] J;
private int[] H;
private int[] L;
public Graph(int[] iI, int[] jJ)
{
I = iI;
J = jJ;
H = new int[1];
L = new int[1];
}
public void Add(int start, int end)
{
Array.Resize(ref I, I.Length + 1);
Array.Resize(ref J, J.Length + 1);
I[I.Length - 1] = start;
J[J.Length - 1] = end;
}
public void printListOfArcs()
{
for (int i = 0; i < I.Length; i++)
{
Console.WriteLine("дуга из-в {0}-{1}", I[i], J[i]);
}
}
public int CountNodes()
{
int nodesCount = 0;
var hashSet = new HashSet<int>();//Создаём объект типа множество
for (int i = 0; i < I.Length; i++)
{
if (!hashSet.Contains(I[i]))
{
hashSet.Add(I[i]);// Проходимся по массиву и добавляем только те элементы, которых во множестве ещё нет
nodesCount++;
}
if (!hashSet.Contains(J[i]))
{
hashSet.Add(J[i]);
nodesCount++;
}
}
return nodesCount;
}
public void BuildArcsBeamsList()
{
Array.Resize(ref H, CountNodes());
Array.Resize(ref L, I.Length);
for (int h = 0; h < CountNodes(); h++)
{
H[h] = -1;
}
for (int l = 0; l < CountNodes(); l++)
{
H[l] = -1;
}
int i;
for (int k = 0; k < I.Length; k++)
{
i = I[k];
L[k] = H[i];
H[i] = k;
}
}
public void PrintIJHL()
{
BuildArcsBeamsList();
Console.WriteLine("I: J: H: L:");
for (int i = 0; i < I.Length; i++)
{
if (i < H.Length)
Console.WriteLine("{0} {1} {2} {3}", I[i], J[i], H[i], L[i]);
else
Console.WriteLine("{0} {1} {2}", I[i], J[i], L[i]);
}
}
}
class Program
{
static void Main(string[] args)
{
var startArc = new int[] { 0, 1, 2 };
var endArc = new int[] { 1, 2, 3 };
var graph = new Graph(startArc, endArc);
graph.Add(1, 3);
graph.Add(0, 2 );
graph.printListOfArcs();
int a = graph.CountNodes();
Console.WriteLine(a);
graph.PrintIJHL();
Console.ReadKey();
}
}
}