Package edu.princeton.cs.algs4
Class FlowNetwork
- Object
-
- edu.princeton.cs.algs4.FlowNetwork
-
public class FlowNetwork extends Object
TheFlowNetwork
class represents a capacitated network with vertices named 0 through V - 1, where each directed edge is of typeFlowEdge
and has a real-valued capacity and flow. It supports the following two primary operations: add an edge to the network, iterate over all of the edges incident to or from a vertex. It also provides methods for returning the number of vertices V and the number of edges E. Parallel edges and self-loops are permitted.This implementation uses an adjacency-lists representation, which is a vertex-indexed array of
Bag
objects. All operations take constant time (in the worst case) except iterating over the edges incident to a given vertex, which takes time proportional to the number of such edges.For additional documentation, see Section 6.4 of Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.
- Author:
- Robert Sedgewick, Kevin Wayne
-
-
Constructor Summary
Constructors Constructor Description FlowNetwork(int V)
Initializes an empty flow network withV
vertices and 0 edges.FlowNetwork(int V, int E)
Initializes a random flow network withV
vertices and E edges.FlowNetwork(In in)
Initializes a flow network from an input stream.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addEdge(FlowEdge e)
Adds the edgee
to the network.Iterable<FlowEdge>
adj(int v)
Returns the edges incident on vertexv
(includes both edges pointing to and fromv
).int
E()
Returns the number of edges in the edge-weighted graph.Iterable<FlowEdge>
edges()
static void
main(String[] args)
Unit tests theFlowNetwork
data type.String
toString()
Returns a string representation of the flow network.int
V()
Returns the number of vertices in the edge-weighted graph.
-
-
-
Constructor Detail
-
FlowNetwork
public FlowNetwork(int V)
Initializes an empty flow network withV
vertices and 0 edges.- Parameters:
V
- the number of vertices- Throws:
IllegalArgumentException
- ifV < 0
-
FlowNetwork
public FlowNetwork(int V, int E)
Initializes a random flow network withV
vertices and E edges. The capacities are integers between 0 and 99 and the flow values are zero.- Parameters:
V
- the number of verticesE
- the number of edges- Throws:
IllegalArgumentException
- ifV < 0
IllegalArgumentException
- ifE < 0
-
FlowNetwork
public FlowNetwork(In in)
Initializes a flow network from an input stream. The format is the number of vertices V, followed by the number of edges E, followed by E pairs of vertices and edge capacities, with each entry separated by whitespace.- Parameters:
in
- the input stream- Throws:
IllegalArgumentException
- if the endpoints of any edge are not in prescribed rangeIllegalArgumentException
- if the number of vertices or edges is negative
-
-
Method Detail
-
V
public int V()
Returns the number of vertices in the edge-weighted graph.- Returns:
- the number of vertices in the edge-weighted graph
-
E
public int E()
Returns the number of edges in the edge-weighted graph.- Returns:
- the number of edges in the edge-weighted graph
-
addEdge
public void addEdge(FlowEdge e)
Adds the edgee
to the network.- Parameters:
e
- the edge- Throws:
IllegalArgumentException
- unless endpoints of edge are between0
andV-1
-
adj
public Iterable<FlowEdge> adj(int v)
Returns the edges incident on vertexv
(includes both edges pointing to and fromv
).- Parameters:
v
- the vertex- Returns:
- the edges incident on vertex
v
as an Iterable - Throws:
IllegalArgumentException
- unless0 <= v < V
-
toString
public String toString()
Returns a string representation of the flow network. This method takes time proportional to E + V.
-
main
public static void main(String[] args)
Unit tests theFlowNetwork
data type.- Parameters:
args
- the command-line arguments
-
-