//ConstructFlowNetwork try to construct a adjacency list based graph with edge capacity,
// nodeCount and edgeCount will define V and E
// then from r to read contents for adjacency list relationship and edge attr
ConstructFlowNetwork try to construct a adjacency list based graph with edge capacity,
nodeCount and edgeCount will define V and E
then from r to read contents for adjacency list relationship and edge attr
// ConstructFlowNetwork try to construct a adjacency list based graph with edge capacity // nodeCount and edgeCount will define V and E // then from r to read contents for adjacency list relationship and edge attr
func ConstructFlowNetwork(nodeCount, edgeCount int, r io.Reader) (*FlowNetwork, error) {
	flowGraph := &FlowNetwork{
		Graph:      graph.NewAdjacencyListGraph(nodeCount, true),
		capacities: CapacityStorage{},
		source:     graph.InvalidNodeID,
		target:     graph.InvalidNodeID}
	scanner := bufio.NewScanner(r)
	scanner.Split(bufio.ScanLines)
	var count int
	for scanner.Scan() {
		count++
		var fromNode, toNode, edgeCapacity int
		fmt.Sscanf(scanner.Text(), "%d%d%d", &fromNode, &toNode, &edgeCapacity)
		fromNode--
		toNode--
		if fromNode < 0 || fromNode >= nodeCount {
			return nil, fmt.Errorf("invalid fromNode %d, nodeCount %d", fromNode, nodeCount)
		}
		if toNode < 0 || toNode >= nodeCount {
			return nil, fmt.Errorf("invalid toNode %d, nodeCount %d", toNode, nodeCount)
		}
		if edgeCapacity <= 0 {
			return nil, fmt.Errorf("invalid edgeCapacity %d", edgeCapacity)
		}
		flowGraph.AddEdge(graph.NodeID(fromNode), graph.NodeID(toNode))
		edge := graph.EdgeID{From: graph.NodeID(fromNode), To: graph.NodeID(toNode)}
		flowGraph.capacities[edge] = EdgeFlowUnit(edgeCapacity)
		if count >= edgeCount { 
			break
		}
	}
	if err := scanner.Err(); err != nil {
		return nil, err
	}
	flowGraph.source = graph.NodeID(0)
	flowGraph.target = graph.NodeID(nodeCount - 1)
	return flowGraph, nil
}
func ConstructFlowNetwork ( nodeCount , edgeCount int , r io . Reader ) ( * FlowNetwork , error ) { flowGraph := & FlowNetwork { Graph : graph . NewAdjacencyListGraph ( nodeCount , true ) , capacities : CapacityStorage { } , source : graph . InvalidNodeID , target : graph . InvalidNodeID } scanner := bufio . NewScanner ( r ) scanner . Split ( bufio . ScanLines ) var count int for scanner . Scan ( ) { count ++ var fromNode , toNode , edgeCapacity int fmt . Sscanf ( scanner . Text ( ) , " " , & fromNode , & toNode , & edgeCapacity ) fromNode -- toNode -- if fromNode < 0 || fromNode >= nodeCount { return nil , fmt . Errorf ( " " , fromNode , nodeCount ) } if toNode < 0 || toNode >= nodeCount { return nil , fmt . Errorf ( " " , toNode , nodeCount ) } if edgeCapacity <= 0 { return nil , fmt . Errorf ( " " , edgeCapacity ) } flowGraph . AddEdge ( graph . NodeID ( fromNode ) , graph . NodeID ( toNode ) ) edge := graph . EdgeID { From : graph . NodeID ( fromNode ) , To : graph . NodeID ( toNode ) } flowGraph . capacities [ edge ] = EdgeFlowUnit ( edgeCapacity ) if count >= edgeCount { break } } if err := scanner . Err ( ) ; err != nil { return nil , err } flowGraph . source = graph . NodeID ( 0 ) flowGraph . target = graph . NodeID ( nodeCount - 1 ) return flowGraph , nil }
ConstructFlowNetwork try to construct a adjacency list based graph with edge capacity,
nodeCount and edgeCount will define V and E
then from r to read contents for adjacency list relationship and edge attr
ConstructFlowNetwork try to construct a adjacency list based graph with edge capacity nodeCount and edgeCount will define V and E then from r to read contents for adjacency list relationship and edge attr
//NOTE: input nodeID will start from 1, but in code we prefer start from 0 // got enough edges