Package edu.princeton.cs.algs4
Class AdjMatrixEdgeWeightedDigraph
 Object

 edu.princeton.cs.algs4.AdjMatrixEdgeWeightedDigraph

public class AdjMatrixEdgeWeightedDigraph extends Object
TheAdjMatrixEdgeWeightedDigraph
class represents an edgeweighted digraph of vertices named 0 through V  1, where each directed edge is of typeDirectedEdge
and has a realvalued weight. It supports the following two primary operations: add a directed edge to the digraph and iterate over all of edges incident from a given vertex. It also provides methods for returning the number of vertices V and the number of edges E. Parallel edges are disallowed; selfloops are permitted.This implementation uses an adjacencymatrix representation. All operations take constant time (in the worst case) except iterating over the edges incident from a given vertex, which takes time proportional to V.
For additional documentation, see Section 4.4 of Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.
 Author:
 Robert Sedgewick, Kevin Wayne


Constructor Summary
Constructors Constructor Description AdjMatrixEdgeWeightedDigraph(int V)
Initializes an empty edgeweighted digraph withV
vertices and 0 edges.AdjMatrixEdgeWeightedDigraph(int V, int E)
Initializes a random edgeweighted digraph withV
vertices and E edges.

Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addEdge(DirectedEdge e)
Adds the directed edgee
to the edgeweighted digraph (if there is not already an edge with the same endpoints).Iterable<DirectedEdge>
adj(int v)
Returns the directed edges incident from vertexv
.int
E()
Returns the number of edges in the edgeweighted digraph.static void
main(String[] args)
Unit tests theAdjMatrixEdgeWeightedDigraph
data type.String
toString()
Returns a string representation of the edgeweighted digraph.int
V()
Returns the number of vertices in the edgeweighted digraph.



Constructor Detail

AdjMatrixEdgeWeightedDigraph
public AdjMatrixEdgeWeightedDigraph(int V)
Initializes an empty edgeweighted digraph withV
vertices and 0 edges. Parameters:
V
 the number of vertices Throws:
IllegalArgumentException
 ifV < 0

AdjMatrixEdgeWeightedDigraph
public AdjMatrixEdgeWeightedDigraph(int V, int E)
Initializes a random edgeweighted digraph withV
vertices and E edges. Parameters:
V
 the number of verticesE
 the number of edges Throws:
IllegalArgumentException
 ifV < 0
IllegalArgumentException
 ifE < 0


Method Detail

V
public int V()
Returns the number of vertices in the edgeweighted digraph. Returns:
 the number of vertices in the edgeweighted digraph

E
public int E()
Returns the number of edges in the edgeweighted digraph. Returns:
 the number of edges in the edgeweighted digraph

addEdge
public void addEdge(DirectedEdge e)
Adds the directed edgee
to the edgeweighted digraph (if there is not already an edge with the same endpoints). Parameters:
e
 the edge

adj
public Iterable<DirectedEdge> adj(int v)
Returns the directed edges incident from vertexv
. Parameters:
v
 the vertex Returns:
 the directed edges incident from vertex
v
as an Iterable  Throws:
IllegalArgumentException
 unless0 <= v < V

toString
public String toString()
Returns a string representation of the edgeweighted digraph. This method takes time proportional to V^{2}.

main
public static void main(String[] args)
Unit tests theAdjMatrixEdgeWeightedDigraph
data type. Parameters:
args
 the commandline arguments

