/** * Use this method to build the graph. It will add an edge to the graph and * also its nodes, if necessary. The node identifiers can be any object. Two * objects identify the same node, if they are equal according to their * equals function. * * @param startNodeID * Identifier object of the start node of the edge * @param endNodeID * Identifier object of the end node of the edge * @param capacity * Capacity of the edge */ public void addEdge(Object startNodeID, Object endNodeID, int capacity) { Node startNode; Node endNode; if (!this.nodes.ContainsKey(startNodeID)) { startNode = new Node(); this.nodes.Add(startNodeID, startNode); } else { startNode = this.nodes[startNodeID]; } if (!this.nodes.ContainsKey(endNodeID)) { endNode = new Node(); this.nodes.Add(endNodeID, endNode); } else { endNode = this.nodes[endNodeID]; } EdgeF edge = new EdgeF(startNodeID, endNodeID, capacity); startNode.addEdge(edge); endNode.addEdge(edge); this.edges.AddLast(edge); }
/** * 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); }
public void addEdge(EdgeF edge) { this.edges.Add(edge); }