View the step-by-step solution to:

Below is an incomplete definition of the Graphnode class.

Below is an incomplete definition of the Graphnode class.

class Graphnode<T> {
    private boolean visitMark;
    private List<Graphnode<T>> successors;
    public boolean getVisitMark() {
        return visitMark;
    public void setVisitMark(boolean mark) {
        visitMark = mark;
    public List<Graphnode<T>> getSuccessors() {
        return successors;

Write the hasSelfCycle method whose header is provided below. This method takes a Graphnode node and returns true iff there exists a path from node to itself (i.e., if there is a cycle in the graph that starts and ends at node). Your implementation must be based on the depth-first search algorithm (i.e., modify DFS to implement hasSelfCycle) and must exit early if possible.

public boolean hasSelfCycle( Graphnode<T> node )

You may assume that all the nodes in the graph have been marked unvisited prior to the hasSelfCycle method being called and that there are no self edges (i.e., there are no edges from a node to itself). You may not modify the Graphnode class. You may find it helpful to use auxiliary method.

Recently Asked Questions

Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.


Educational Resources
  • -

    Study Documents

    Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

    Browse Documents
  • -

    Question & Answers

    Get one-on-one homework help from our expert tutors—available online 24/7. Ask your own questions or browse existing Q&A threads. Satisfaction guaranteed!

    Ask a Question