Example #1
0
        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 Edge ToImmutable(this MutableEdge mutable, GraphIndex index)
 {
     return(new Edge(index.Vertices[mutable.Vertex1.Id], index.Vertices[mutable.Vertex2.Id], mutable.Label));
 }
 public static Edge ToImmutable(this MutableEdge mutable)
 {
     return(new Edge(mutable.Vertex1.ToImmutable(), mutable.Vertex2.ToImmutable(), mutable.Label));
 }