Class GraphAlgorithms

java.lang.Object
com.javaholics.GraphAlgorithms
All Implemented Interfaces:
GraphAlgorithmsInterface<String>

public class GraphAlgorithms extends Object implements GraphAlgorithmsInterface<String>
Provides graph algorithms for traversing a graph. This class implements the GraphAlgorithmsInterface and provides methods for performing breadth-first and depth-first traversals of a graph.
Author:
Lindsay Kislingbury, Lea Wiranatha
  • Constructor Details

    • GraphAlgorithms

      public GraphAlgorithms(Graph graph)
      Constructs a new GraphAlgorithms object for the given graph.
      Parameters:
      graph - The graph to traverse.
  • Method Details

    • getBreadthFirstTraversal

      public QueueInterface<String> getBreadthFirstTraversal(String origin)
      Performs a breadth-first traversal of this graph, starting at the given origin vertex. This method uses a queue to keep track of the vertices to visit next.
      Specified by:
      getBreadthFirstTraversal in interface GraphAlgorithmsInterface<String>
      Parameters:
      origin - A string that labels the origin vertex of the traversal.
      Returns:
      A queue containing the labels of the vertices in the order they were visited
    • getDepthFirstTraversal

      public QueueInterface<String> getDepthFirstTraversal(String origin)
      Performs a depth-first traversal of this graph, starting at the given origin vertex. This method uses a stack to keep track of the vertices to visit next. This method returns a queue containing the labels of the vertices in the order they were visited.
      Specified by:
      getDepthFirstTraversal in interface GraphAlgorithmsInterface<String>
      Parameters:
      origin - A string that labels the origin vertex of the traversal.
      Returns:
      A queue containing the labels of the vertices in the order they were visited