Beispiel #1
0
        /**
         * This method gives the actual flow value by adding all flow values of the
         * out leading edges of the source.
         *
         * @param flow A HashMap of the form like getMaxFlow produces them
         * @param g The directed Graph
         * @param source The object identifying the source node of the flow
         * @return The value of the given flow
         */
        // 10
        public int getFlowSize(Dictionary <EdgeF, int> flow, DirectedGraph g,
                               Object source)
        {
            //-------------------

            int  maximumFlow = 0; af++; ffLines++;
            Node sourceNode  = g.getNode(source); af++; ffLines++;

            //-------------------
            compF++; af++;
            ffLines++;
            for (int i = 0; i < sourceNode.getOutLeadingOrder(); i++)
            {
                ffLines += 2;
                compF++;
                maximumFlow += flow[sourceNode.getEdge(i)]; af++; ffLines++;
            }
            ffLines++;
            return(maximumFlow);
            //-------------------
        }
Beispiel #2
0
        /**
         * Simple breadth first search in the directed graph
         *
         * @param g The directed Graph
         * @param start The object that identifying the start node of the search
         * @param target The object that identifying the target node of the search
         * @param flow A HashMap of the form like getMaxFlow produces them. If an
         * edge has a value > 0 in it, it will also be used in the opposite
         * direction. Also edges that have a value equal to its capacity will be
         * ignored.
         * @return A list of all edges of the found path in the order in which they
         * are used, null if there is no path. If the start node equals the target
         * node, an empty list is returned.
         */
        public LinkedList <EdgeF> bfs(DirectedGraph g, Object start, Object target,
                                      Dictionary <EdgeF, int> flow)
        {
            //-------------------

            // The edge by which a node was reached.
            Dictionary <Object, EdgeF> parent = new Dictionary <Object, EdgeF>(); af++; ffLines++;
            // All outer nodes of the current search iteration.
            LinkedList <Object> fringe = new LinkedList <Object>(); af++; ffLines++;

            //-------------------

            // We need to put the start node into those two.
            parent.Add(start, null); af++; ffLines++;
            fringe.AddLast(start); af++; ffLines++;
            // The actual algorithm
            bool stop = false; af++; ffLines++;

            //-------------------

            compF++; ffLines++;
            while (!fringe.Count.Equals(0))
            {
                ffLines++;
                compF++;
                // This variable is needed to prevent the JVM from having a
                // concurrent modification
                //-------------------

                LinkedList <Object> newFringe = new LinkedList <Object>(); af++; ffLines++;

                //-------------------

                // Iterate through all nodes in the fringe.
                compF++;
                ffLines++;
                foreach (Object nodeID in fringe)
                {
                    ffLines++;
                    compF++;
                    Node nodes = g.getNode(nodeID); af++; ffLines++;
                    // Iterate through all the edges of the node.
                    compF++; af++; ffLines++;
                    for (int i = 0; i < nodes.getOutLeadingOrder(); i++)
                    {
                        compF++; af++; ffLines++;
                        EdgeF e = nodes.getEdge(i); af++; ffLines++;
                        //-------------------

                        // Only add the node if the flow can be changed in an out
                        // leading direction. Also break, if the target is reached.
                        //-------------------

                        compF += 3; ffLines += 3;
                        if (e.getStart().Equals(nodeID) &&
                            !parent.ContainsKey(e.getTarget()) &&
                            flow[e] < e.getCapacity())
                        {
                            parent.Add(e.getTarget(), e); af++; ffLines++;
                            compF++; ffLines++;
                            if (e.getTarget().Equals(target))
                            {
                                stop = true; af++; ffLines++;
                                break;
                            }
                            newFringe.AddLast(e.getTarget()); af++; ffLines++;
                        }
                        else if (e.getTarget().Equals(nodeID) &&
                                 !parent.ContainsKey(e.getStart()) &&
                                 flow[e] > 0)
                        {
                            ffLines++;
                            parent.Add(e.getStart(), e); af++; ffLines++;
                            compF++;
                            ffLines++;
                            if (e.getStart().Equals(target))
                            {
                                stop = true; af++; ffLines++;
                                break;
                            }
                            newFringe.AddLast(e.getStart()); af++; ffLines++;
                        }
                        ffLines++;
                        compF += 3;
                    }
                    ffLines++;
                    compF++;
                    if (stop)
                    {
                        break;
                    }
                }
                compF++; ffLines++;
                if (stop)
                {
                    break;
                }
                // Replace the fringe by the new one.
                fringe = newFringe; af++; ffLines++;
            }
            // Return null, if no path was found.
            compF++; ffLines++;
            if (fringe.Count.Equals(0))
            {
                return(null);
            }
            // If a path was found, reconstruct it.
            Object             node = target; af++; ffLines++;
            LinkedList <EdgeF> path = new LinkedList <EdgeF>(); af++; ffLines++;

            compF++;
            ffLines++;
            while (!node.Equals(start))
            {
                ffLines++;
                compF++;
                EdgeF e = parent[node]; af++; ffLines++;
                path.AddFirst(e); af++; ffLines++;
                compF++;
                ffLines++;
                if (e.getStart().Equals(node))
                {
                    node = e.getTarget(); af++; ffLines++;
                }
                else
                {
                    ffLines++;
                    node = e.getStart(); af++; ffLines++;
                }
            }
            ffLines++;
            // Return the path.
            return(path);
        }