Package dsa

Class DiGraph

java.lang.Object
dsa.DiGraph

public class DiGraph extends Object
A data type to represent a directed graph.
  • Constructor Summary

    Constructors
    Constructor
    Description
    DiGraph(int V)
    Constructs an empty digraph with V vertices and 0 edges.
    DiGraph(stdlib.In in)
    Constructs a digraph from the input stream in.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    addEdge(int v, int w)
    Adds the directed edge v->w to this digraph.
    adj(int v)
    Returns the vertices adjacent from vertex v in this digraph.
    int
    E()
    Returns the number of edges in this digraph.
    int
    inDegree(int v)
    Returns the in-degree of vertex v in this digraph.
    static void
    main(String[] args)
    Unit tests the data type.
    int
    outDegree(int v)
    Returns the out-degree of vertex v in this digraph.
    Returns a string representation of this digraph.
    int
    V()
    Returns the number of vertices in this digraph.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • DiGraph

      public DiGraph(int V)
      Constructs an empty digraph with V vertices and 0 edges.
      Parameters:
      V - number of vertices.
    • DiGraph

      public DiGraph(stdlib.In in)
      Constructs a digraph from the input stream in.
      Parameters:
      in - the input stream.
  • Method Details

    • V

      public int V()
      Returns the number of vertices in this digraph.
      Returns:
      the number of vertices in this digraph.
    • E

      public int E()
      Returns the number of edges in this digraph.
      Returns:
      the number of edges in this digraph.
    • addEdge

      public void addEdge(int v, int w)
      Adds the directed edge v->w to this digraph.
      Parameters:
      v - the from vertex.
      w - the to vertex.
    • adj

      public Iterable<Integer> adj(int v)
      Returns the vertices adjacent from vertex v in this digraph.
      Parameters:
      v - the vertex.
      Returns:
      the vertices adjacent from vertex v in this digraph.
    • outDegree

      public int outDegree(int v)
      Returns the out-degree of vertex v in this digraph.
      Parameters:
      v - the vertex.
      Returns:
      the out-degree of vertex v in this digraph.
    • inDegree

      public int inDegree(int v)
      Returns the in-degree of vertex v in this digraph.
      Parameters:
      v - the vertex.
      Returns:
      the in-degree of vertex v in this digraph.
    • toString

      public String toString()
      Returns a string representation of this digraph.
      Overrides:
      toString in class Object
      Returns:
      a string representation of this digraph.
    • main

      public static void main(String[] args)
      Unit tests the data type.
      Parameters:
      args - the command-line arguments.