-
Notifications
You must be signed in to change notification settings - Fork 1
/
SuperGraph.cs
89 lines (76 loc) · 3.12 KB
/
SuperGraph.cs
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
using System.Collections.Generic;
using System.Linq;
namespace Cuthill
{
class SuperGraph
{
public List<Vertex> Vertices { get; set; }
public List<Vertex> ViewedVertices { get; set; }
public List<Graph> Graphs { get; set; }
public SuperGraph()
{
Graphs = new List<Graph>();
Vertices = new List<Vertex>();
ViewedVertices = new List<Vertex>();
}
public SuperGraph(int[,] matrix)
{
int rank = matrix.GetLength(0);
Graphs = new List<Graph>();
Vertices = new List<Vertex>();
ViewedVertices = new List<Vertex>();
for (int i = 1; i <= rank; i++)
{
Vertex vertex = new Vertex(i);
Vertices.Add(vertex);
}
for (int i = 0; i < rank; i++)
for (int j = 0; j < rank; j++)
if (matrix[i, j] == 1 && i != j)
Vertices.ElementAt(i).ConnectedVertices.Add(Vertices.ElementAt(j));
foreach (var vertex in Vertices) vertex.CalculateBonds();
while (Vertices.Any(vertex => !ViewedVertices.Contains(vertex)))
{
int startID = 0;
for (int i = 1; i <= Vertices.Count; i++)
if (ViewedVertices.All(ch => ch.ID != i))
startID = i;
Vertex start = Vertices.Where(item => item.ID == startID).ElementAt(0);
List<List<Vertex>> newOrder = new List<List<Vertex>>() { new List<Vertex>() { start } };
int level = 0;
start.View();
while (level < newOrder.Count)
{
foreach (var item in newOrder.ElementAt(level).OrderBy(u => u.Bonds))
{
if (item.ConnectedVertices.Where(u => !u.IsViewed()).Count() == 0)
continue;
newOrder.Add(item.ConnectedVertices.Where(u => u.NewID == 0 && !u.IsViewed()).OrderBy(u => u.Bonds).ToList());
item.ConnectedVertices.ForEach(u => u.View());
}
level++;
}
Graph tempGraph = new Graph();
foreach (var row in newOrder)
{
foreach (var vx in row)
{
ViewedVertices.Add(vx);
tempGraph.Vertices.Add(vx);
}
}
Graphs.Add(tempGraph);
}
foreach (var v in Vertices)
v.Reset();
}
public SuperGraph(List<Graph> list) { Graphs = list; }
public void Add(Graph graph) { Graphs.Add(graph); }
public bool CheckConnection(int first, int second, Graph.Mode mode)
{
return (mode == Graph.Mode.Old) ?
Vertices.Find(ch => ch.ID == first).ConnectedVertices.Any(vx => vx.ID == second):
Vertices.Find(ch => ch.NewID == first).ConnectedVertices.Any(vx => vx.NewID == second);
}
}
}