public static Graph ConstructKRegularGraph(int n, int k) // n - liczba wierzcholkow, k - liczba krawedzi jakie maja wychodzic z jednego wierzcholka { Graph finalGraph = new Graph(); List <int> GraphList = new List <int>(); for (int i = 0; i < n; ++i) { GraphList.Add(k); } if (GraphStringValidator.IsGraphString(GraphList)) { finalGraph = GraphCreator.CreateGraphFromNodesDegrees(GraphList); if (k < 3) { GraphCreator.RandomizeGraph(finalGraph, 1); // With special thanks to P.Augustyn } else { GraphCreator.RandomizeGraph(finalGraph, (k / 3)); // With special thanks to P.Augustyn } } return(finalGraph); }
//Tworzenie grafu ze stopni wierzchołków public static Graph CreateGraphFromNodesDegrees(List <int> graphicalStringGraph) { // Wprowadzana lista to wartosci stopni wierzcholkow Graph graphString = new Graph(); int counter = graphicalStringGraph.Count; if (!GraphStringValidator.IsGraphString(graphicalStringGraph)) { return(new Graph()); } for (int i = 0; i < counter; i++) { graphString.Nodes.Add(new Node() { ID = i, GraphicalStringConnections = graphicalStringGraph[i] }); } graphString.Nodes.Sort((x, y) => x.GraphicalStringConnections.CompareTo(y.GraphicalStringConnections)); while (graphString.Nodes.FindLastIndex(x => x.GraphicalStringConnections == 0) < graphString.Nodes.Count - 1) { var current = graphString.Nodes[counter - 1]; var indexGoingDown = 2; while (current.GraphicalStringConnections > 0) { Connection addedConnection = new Connection { Node1 = current, Node2 = graphString.Nodes[counter - indexGoingDown] }; graphString.AddConnection(addedConnection); graphString.Nodes[counter - indexGoingDown++].GraphicalStringConnections -= 1; current.GraphicalStringConnections--; } graphString.Nodes.Sort((x, y) => x.GraphicalStringConnections.CompareTo(y.GraphicalStringConnections)); } graphString.Nodes.Sort((x, y) => x.ID.CompareTo(y.ID)); return(graphString); }