//Fisher–Yates shuffle https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle protected void Randomize(List <CWVertex> Vertices) { Random Rand = new Random(); int n = Vertices.Count; while (n > 1) { n--; int k = Rand.Next(n + 1); CWVertex value = Vertices[k]; Vertices[k] = Vertices[n]; Vertices[n] = value; } }
protected CWGraph CreateGraph(IEnumerable <T> Collection, Func <T, T, double> WeightFunction, Func <T, T, double, bool> ConnectionFunction) { CWGraph Graph = new CWGraph(); if (Collection is ICollection <T> ) { Graph.Vertices = new List <CWVertex>(((ICollection <T>)Collection).Count); } else { Graph.Vertices = new List <CWVertex>(); } foreach (T Source in Collection) { CWVertex Vertex = new CWVertex() { Source = Source }; Vertex.Cluster = Vertex.GetHashCode(); Graph.Vertices.Add(Vertex); } for (int i = 0; i < Graph.Vertices.Count; i++) { for (int j = i + 1; j < Graph.Vertices.Count; j++) { double Weight = WeightFunction(Graph.Vertices[i].Source, Graph.Vertices[j].Source); if (ConnectionFunction == null || ConnectionFunction(Graph.Vertices[i].Source, Graph.Vertices[j].Source, Weight)) { CWEdge Edge = new CWEdge() { Vertex1 = Graph.Vertices[i], Vertex2 = Graph.Vertices[j], Weight = Weight }; Graph.Vertices[i].Edges.Add(Edge); Graph.Vertices[j].Edges.Add(Edge); } } } return(Graph); }
public CWVertex GetVertex(CWVertex Vertex) { return((Vertex == Vertex1) ? Vertex2 : Vertex1); }