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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
namespace ConsoleApplication10
{
class graph
{
private List<int> heads;
private List<int> leafs = new List<int>(0);
private const int empty = -1;
private List<int> starts = new List<int>(0);
private List<int> ends = new List<int>(0);
private int _arcNumber = 0;
private int getNewArcNumber()
{
int number = this._arcNumber;
this._arcNumber++;
leafs.Insert(number, -1);
return number;
}
public graph(int nodeCount)
{
heads = new List<int>(nodeCount);
for (int i = 0; i < nodeCount; i++)
{
heads.Add(empty);
}
}
public void addArc(int start, int end)
{
Debug.Assert(start < heads.Count);
Debug.Assert(end < heads.Count);
int number = getNewArcNumber();
starts.Insert(number, start);
ends.Insert(number, end);
leafs[number] = heads[start];
heads[start] = number;
}
private int getArcNumber(int start, int end)
{
Debug.Assert(start < heads.Count);
Debug.Assert(end < heads.Count);
var index = starts.IndexOf(start);
checkIndex(index);
while (ends[index] != end)
{
index = starts.IndexOf(start, index + 1);
checkIndex(index);
}
return index;
}
private void checkIndex(int index)
{
if (index < 0)
{
throw new Exception("Arc not found");
}
}
public void printData()
{
printList(starts);
printList(ends);
printList(heads);
printList(leafs);
}
private void printList(List<int> list)
{
foreach (var item in list)
{
Console.Write(item);
Console.Write(" ");
}
Console.WriteLine();
}
}
class Program
{
static void Main(string[] args)
{
var graph = new graph(4);
graph.addArc(0, 1);
graph.addArc(1, 2);
graph.addArc(2, 3);
graph.addArc(1, 3);
graph.addArc(0, 2);
graph.printData();
Console.ReadKey();
}
}
}