private static Graph RandomConnectedGraphInternal(int vertexCount, int edgeCount, Random random) { var ids = Enumerable.Range(0, vertexCount); var graph = new MutableGraph(); graph.Vertices.AddRange(VerticesInIdRange(0, vertexCount)); var vertices = graph.Vertices.ToDictionary(v => v.Id); // making graph connected foreach (var id in ids) { var otherId = random.Next(id); if (id > 0) { var edge = new MutableEdge { Vertex1 = vertices[id], Vertex2 = vertices[otherId], Label = $"e{id}:{otherId}" }; graph.Edges.Add(edge); } } // adding remaining vertices foreach (var edgeId in Enumerable.Range(vertexCount, edgeCount - vertexCount + 1)) { var id1 = random.Next(vertexCount); var id2 = random.Next(vertexCount); var edge = new MutableEdge { Vertex1 = vertices[id1], Vertex2 = vertices[id2], Label = $"e{id1}:{id2}" }; graph.Edges.Add(edge); } return(graph.ToImmutable()); }
public static Graph ToImmutable(this MutableGraph mutable) { var vertices = mutable.Vertices.Select(v => v.ToImmutable()).ToImmutableSortedSet(); var index = new Graph(vertices, ImmutableSortedSet.Create <Edge>()).Index(); var edges = mutable.Edges.Select(e => e.ToImmutable(index)).ToImmutableSortedSet(); return(new Graph(vertices, edges)); }
/// <summary> /// Adds to <paramref name="graph"/> new edges connecting each of <paramref name="vertices"/> with every other. /// </summary> /// <param name="graph">Graph to add edges to.</param> /// <param name="vertices">Vertices to connect with new edges.</param> public static void ConnectAll(this MutableGraph graph, IEnumerable <MutableVertex> vertices) { graph.ConnectAll(vertices.ToArray()); }
/// <summary> /// Adds to <paramref name="graph"/> new edges connecting each of <paramref name="vertices"/> with every other. /// </summary> /// <param name="graph">Graph to add edges to.</param> /// <param name="vertices">Vertices to connect with new edges.</param> public static void ConnectAll(this MutableGraph graph, params MutableVertex[] vertices) { graph.Edges.AddRange(vertices.SelectMany(v1 => vertices.Select(v2 => v1.Id != v2.Id ? v1.Connecting(v2) : null).Where(e => e != null))); }