Package dsa

Class Graph


  • public class Graph
    extends Object
    A data type to represent an undirected graph.
    • Constructor Summary

      Constructors 
      Constructor Description
      Graph​(int V)
      Constructs an empty graph with V vertices and 0 edges.
      Graph​(stdlib.In in)
      Constructs a graph from the input stream in.
    • Constructor Detail

      • Graph

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

        public Graph​(stdlib.In in)
        Constructs a graph from the input stream in.
        Parameters:
        in - the input stream.
    • Method Detail

      • V

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

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

        public void addEdge​(int v,
                            int w)
        Adds an undirected edge between vertices v and w in this graph.
        Parameters:
        v - one vertex in the edge.
        w - the other vertex in the edge.
      • adj

        public Iterable<Integer> adj​(int v)
        Returns the vertices adjacent to vertex v in this graph.
        Parameters:
        v - the vertex.
        Returns:
        the vertices adjacent to vertex v in this graph.
      • degree

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

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

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