1
|
using Core.Entities;
|
2
|
|
3
|
namespace Core.GraphUtils
|
4
|
{
|
5
|
public class Intersections
|
6
|
{
|
7
|
/// <summary>
|
8
|
/// Finds intersections between 1D segments
|
9
|
/// </summary>
|
10
|
/// <param name="tags"></param>
|
11
|
/// <returns></returns>
|
12
|
public static Dictionary<AnnotationTagGeneric, List<AnnotationTagGeneric>> FindIntersections(List<AnnotationTagGeneric> tags)
|
13
|
{
|
14
|
var intersections = new Dictionary<AnnotationTagGeneric, List<AnnotationTagGeneric>>();
|
15
|
|
16
|
for (int i = 0; i < tags.Count; i++)
|
17
|
{
|
18
|
var tagWho = tags[i];
|
19
|
intersections[tagWho] = new();
|
20
|
for (int j = 0; j < tags.Count; j++)
|
21
|
{
|
22
|
if (i == j) { continue; }
|
23
|
var tagWith = tags[j];
|
24
|
|
25
|
if (!((tagWho.Position + tagWho.Length < tagWith.Position) || (tagWith.Position + tagWith.Length < tagWho.Position)))
|
26
|
{
|
27
|
intersections[tagWho].Add(tagWith);
|
28
|
}
|
29
|
}
|
30
|
}
|
31
|
|
32
|
return intersections;
|
33
|
}
|
34
|
|
35
|
/// <summary>
|
36
|
/// Graph coloring
|
37
|
/// </summary>
|
38
|
/// <param name="source"></param>
|
39
|
/// <returns></returns>
|
40
|
public static Dictionary<AnnotationTagGeneric, int> ColorGraph(Dictionary<AnnotationTagGeneric, List<AnnotationTagGeneric>> source)
|
41
|
{
|
42
|
var res = ConvertToMatrix(source);
|
43
|
var matrix = res.Item1;
|
44
|
var tagToIntDict = res.Item2;
|
45
|
var intToTagDict = res.Item3;
|
46
|
|
47
|
var colors = new int[matrix.GetLength(0)];
|
48
|
for (int i = 0; i < colors.Length; i++)
|
49
|
{
|
50
|
colors[i] = -1;
|
51
|
}
|
52
|
|
53
|
for (int coloredNode = 0; coloredNode < colors.Length; coloredNode++)
|
54
|
{
|
55
|
var neighbours = GetNeighbours(coloredNode, matrix);
|
56
|
var neighbourColors = GetNodeColors(neighbours, colors);
|
57
|
colors[coloredNode] = GetLowestUnusedColor(neighbourColors);
|
58
|
}
|
59
|
|
60
|
var coloring = new Dictionary<AnnotationTagGeneric, int>();
|
61
|
for (int i = 0; i < colors.Length; i++)
|
62
|
{
|
63
|
coloring[intToTagDict[i]] = colors[i];
|
64
|
}
|
65
|
|
66
|
return coloring;
|
67
|
}
|
68
|
|
69
|
private static int GetLowestUnusedColor(List<int> colors)
|
70
|
{
|
71
|
int lowestUnusedColor = 0;
|
72
|
colors.Sort();
|
73
|
for (int i = 0; i < colors.Count; i++)
|
74
|
{
|
75
|
if (colors[i] > i)
|
76
|
{
|
77
|
lowestUnusedColor = i;
|
78
|
break;
|
79
|
}
|
80
|
else
|
81
|
{
|
82
|
lowestUnusedColor++;
|
83
|
}
|
84
|
}
|
85
|
|
86
|
return lowestUnusedColor;
|
87
|
}
|
88
|
|
89
|
private static List<int> GetNodeColors(List<int> nodes, int[] colors)
|
90
|
{
|
91
|
var usedColors = new List<int>();
|
92
|
foreach (var node in nodes)
|
93
|
{
|
94
|
if (colors[node] != -1)
|
95
|
{
|
96
|
usedColors.Add(colors[node]);
|
97
|
}
|
98
|
}
|
99
|
|
100
|
return usedColors;
|
101
|
}
|
102
|
|
103
|
private static List<int> GetNeighbours(int node, int[,] matrix)
|
104
|
{
|
105
|
List<int> neighbours = new();
|
106
|
for (int i = 0; i < matrix.GetLength(0); i++)
|
107
|
{
|
108
|
if (matrix[node, i] == 1)
|
109
|
{
|
110
|
neighbours.Add(i);
|
111
|
}
|
112
|
}
|
113
|
|
114
|
return neighbours;
|
115
|
}
|
116
|
|
117
|
private static (int[,], Dictionary<AnnotationTagGeneric, int>, Dictionary<int, AnnotationTagGeneric>) ConvertToMatrix(Dictionary<AnnotationTagGeneric, List<AnnotationTagGeneric>> source)
|
118
|
{
|
119
|
int[,] matrix = new int[source.Count, source.Count];
|
120
|
for (int i = 0; i < source.Count; i++)
|
121
|
{
|
122
|
for (int j = 0; j < source.Count; j++)
|
123
|
{
|
124
|
matrix[i, j] = 0;
|
125
|
}
|
126
|
}
|
127
|
|
128
|
var tagToIntDict = new Dictionary<AnnotationTagGeneric, int>();
|
129
|
var intToTagDict = new Dictionary<int, AnnotationTagGeneric>();
|
130
|
|
131
|
int k = 0;
|
132
|
foreach (var key in source.Keys)
|
133
|
{
|
134
|
tagToIntDict[key] = k;
|
135
|
intToTagDict[k] = key;
|
136
|
k++;
|
137
|
}
|
138
|
|
139
|
foreach (var key in source.Keys)
|
140
|
{
|
141
|
int keyInt = tagToIntDict[key];
|
142
|
foreach (var neighbour in source[key])
|
143
|
{
|
144
|
int neighbourInt = tagToIntDict[neighbour];
|
145
|
matrix[keyInt, neighbourInt] = 1;
|
146
|
}
|
147
|
}
|
148
|
|
149
|
return (matrix, tagToIntDict, intToTagDict);
|
150
|
}
|
151
|
}
|
152
|
}
|