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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.IO;
namespace ShortestWaySearch
{
class Program
{
static void Main(string[] args)
{
BundleRelationGraph graph = new BundleRelationGraph();
graph.AddRelation(0, 1);
graph.AddRelation(1, 2);
graph.AddRelation(2, 0);
graph.AddRelation(1, 0);
graph.AddRelation(2, 3);
graph.AddRelation(0, 3);
graph.AddRelation(0, 4);
graph.AddRelation(4, 0);
graph.AddRelation(4, 3);
printList(graph.W);
printList(graph.L);
printList(graph.J);
printList(graph.getShortestWayTreeFor(0));
File.WriteAllText("C:\\Users\\11\\Links\\output.txt", "Graph:" + graph.getIJFormatedText() + "Tree:" + graph.getIJFormatedTreeTextFor(0));
Console.WriteLine(graph.getIJFormatedText());
Console.WriteLine();
Console.WriteLine(graph.getIJFormatedTreeTextFor(0));
Console.ReadKey();
}
static void printList(List<int> lst)
{
for (int i = 0; i < lst.Count; i++)
{
Console.Write(" " + lst[i]);
}
Console.WriteLine();
}
}
class BundleRelationGraph
{
public List<int> W;
public List<int> L;
public List<int> J;
public BundleRelationGraph()
{
W = new List<int>();
L = new List<int>();
J = new List<int>();
}
private void RestructBundlesFor(int vertNum)
{
Debug.Assert(vertNum < W.Count);
if (W[vertNum] < 0)
{
W[vertNum] = L.Count - 1;
}
else
{
int j = W[vertNum];
while (L[j] >= 0)
{
j = L[j];
}
L[j] = L.Count - 1;
}
}
public string getIJFormatedText()
{
string text = "";
for (int i = 0; i < W.Count; i++)
{
if (W[i] >= 0)
{
int j = W[i];
while (j >= 0)
{
text += i + " -> " + J[j] + " ";
j = L[j];
}
}
}
return text;
}
public string getIJFormatedTreeTextFor(int vertNum)
{
Debug.Assert(vertNum < W.Count);
string text = "";
List<int> parentTree = this.getShortestWayTreeFor(vertNum);
for (int i = 1; i < parentTree.Count; i++)
{
text += i + " -> " + parentTree[i] + " ";
}
return text;
}
/// <summary>
///
/// </summary>
/// <param name="i">from vertex</param>
/// <param name="j">to vertex</param>
public void AddRelation(int i, int j)
{
L.Add(-1);
if (i < W.Count)
{
RestructBundlesFor(i);
}
else
{
W.Add(L.Count - 1);
}
if (j >= W.Count)
{
W.Add(-1);
}
J.Add(j);
}
public List<int> getShortestWayTreeFor(int relNum)
{
Debug.Assert(W.Count > relNum);
List<int> parentList = new List<int>(W.Count);
Queue<int> Q = new Queue<int>(W.Count);
Queue<int> S = new Queue<int>(W.Count);
List<int> Ri = new List<int>(W.Count);
Q.Enqueue(relNum);
for (int i = 0; i < W.Count; i++)
{
Ri.Add((relNum == i) ? 0 : int.MaxValue);
parentList.Add(-1);
}
while (Q.Count > 0)
{
int rel = Q.Dequeue();
int j = W[rel];
while (j >= 0 && !S.Contains(rel))
{
int destRel = J[j];
if (!Q.Contains(destRel) && !S.Contains(destRel))
{
Q.Enqueue(destRel);
parentList[destRel] = rel;
Ri[destRel] = Ri[rel] + 1;
}
j = L[j];
}
S.Enqueue(rel);
}
return parentList;
}
}
}