Пример #1
0
        public static string WriteGraphViz <V, E>(this IGraph <V, E> graph, string graphName, Func <V, string> vertexLabel, Func <E, string> edgeLabel, IComparer <V> vertexComparer = null)
        {
            IDictionary <VertexDescriptor <V, E>, int> vertexIndexes = graph.GetVertexIndex(vertexComparer);

            StringBuilder sbOutput = new StringBuilder();
            StringBuilder sbEdges  = new StringBuilder();

            sbOutput.AppendFormat("graph {0} {{", graphName);
            sbOutput.AppendLine();
            foreach (VertexDescriptor <V, E> vertex in graph.GetOrderedVertices(vertexComparer))
            {
                int vertexIndex = vertexIndexes[vertex];
                foreach (EdgeDescriptor <V, E> edge in vertex.GetOrderedEdges(vertexComparer))
                {
                    VertexDescriptor <V, E> invertedVertex = graph.FindVertexDescriptor(edge.GetInvertedVertex(vertex.Vertex));
                    sbEdges.AppendFormat("{0}--{1}", vertexIndex, vertexIndexes[invertedVertex]);
                    if (edgeLabel != null)
                    {
                        sbEdges.Append(edgeLabel(edge.Edge));
                    }
                    sbEdges.AppendLine(" ;");
                }
                sbOutput.AppendFormat("{0}", vertexIndex);
                if (vertexLabel != null)
                {
                    sbOutput.Append(vertexLabel(vertex.Vertex));
                }
                sbOutput.AppendLine(";");
            }

            sbOutput.Append(sbEdges.ToString());
            sbOutput.AppendLine("}");

            return(sbOutput.ToString());
        }
Пример #2
0
        public static IEnumerable <EdgeDescriptor <V, E> > GetOrderedEdges <V, E>(this VertexDescriptor <V, E> vertexDescriptor, IComparer <V> vertexComparer = null)
        {
            var edges = vertexDescriptor.Edges.Where(d => d.IsVertex1(vertexDescriptor.Vertex));

            if (vertexComparer != null)
            {
                edges = edges.OrderBy(d => d.GetInvertedVertex(vertexDescriptor.Vertex), vertexComparer);
            }
            return(edges);
        }
Пример #3
0
        private void AddOrUpdateVertex(EdgeDescriptor <V, E> edgeDescriptor, V v)
        {
            VertexDescriptor <V, E> vertexDescriptor;

            if (!Vertices.ContainsKey(v))
            {
                vertexDescriptor = new VertexDescriptor <V, E>(v);
                vertexDescriptor.AddEdge(edgeDescriptor);
                Vertices.Add(v, vertexDescriptor);
            }
            else
            {
                Vertices[v].AddEdge(edgeDescriptor);
            }
        }