Пример #1
0
        private static int BreadthFirstSearch(
            IWeightedGraph graph,
            Vertex source,
            Vertex target,
            IWeightedGraph legalFlows,
            out IDictionary<Vertex, Vertex> path)
        {
            path = new Dictionary<Vertex, Vertex>();
            IDictionary<Vertex, int> pathCapacity = new Dictionary<Vertex, int>();

            path[source] = null; // make sure source is not rediscovered
            pathCapacity[source] = int.MaxValue;

            Queue<Vertex> queue = new Queue<Vertex>();
            queue.Enqueue(source);

            while(queue.Count > 0)
            {
                Vertex u = queue.Dequeue();
                foreach (Vertex v in graph.Neighbors(u))
                {
                    // if there is available capacity between u and v,
                    // ... and v is not seen before in search
                    if (graph.GetCapacity(u, v) - legalFlows.GetCapacity(u, v) > 0 &&
                        path.ContainsKey(v) == false)
                    {
                        path[v] = u;
                        pathCapacity[v] = Math.Min(
                            pathCapacity[u],
                            graph.GetCapacity(u, v) - legalFlows.GetCapacity(u ,v));

                        if (!v.Equals(target)) queue.Enqueue(v);
                        else return pathCapacity[target];
                    }
                }
            }

            return 0;
        }