Пример #1
0
        /// <summary>
        /// Clones this graph and creates a residual graph.
        /// </summary>
        private WeightedDiGraph <T, W> createResidualGraph(IDiGraph <T> graph)
        {
            var newGraph = new WeightedDiGraph <T, W>();

            //clone graph vertices
            foreach (var vertex in graph.VerticesAsEnumberable)
            {
                newGraph.AddVertex(vertex.Key);
            }

            //clone edges
            foreach (var vertex in graph.VerticesAsEnumberable)
            {
                //Use either OutEdges or InEdges for cloning
                //here we use OutEdges
                foreach (var edge in vertex.OutEdges)
                {
                    //original edge
                    newGraph.AddOrUpdateEdge(vertex.Key, edge.TargetVertex.Key, edge.Weight <W>(), @operator);
                    //add a backward edge for residual graph with edge value as default(W)
                    newGraph.AddOrUpdateEdge(edge.TargetVertex.Key, vertex.Key, @operator.defaultWeight, @operator);
                }
            }

            return(newGraph);
        }