Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8425003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T04:09:28+00:00 2026-06-10T04:09:28+00:00

So I wrote a Graph class and I can’t seem to do a depth

  • 0

So I wrote a Graph class and I can’t seem to do a depth first search on it properly depending on the sequencing of nodes. Here’s what I mean:

If my graph looks like this:

A-B-D
|/
C

The DFS returns: “ABC”

But when it looks like this:

A-B
| |
D C
|
E

It will print ABCDE correctly.

The problem I’ve found lies in my getUnvisitedAdjacentNode() function. Here is the function:

    public int getUnvisitedAdjacentNode(int n) {
    for (int i = 0; i < this.nodeList.size(); i++) {
        if (this.edges[n][i] == 1 && this.nodeList.get(i).wasVisited == false) {
            return i;
        }
    }
    return -1;
}

The problem, I’ve found is because it goes in “order” (just a for loop) , it will never get traverse D in the first situation because B gets visited and after C gets visited, B simply get’s popped off of the stack. Maybe this isn’t the problem.

Here’s the code for my actual DFS traversal.

   public void depthFirstTraverse() {
    Stack<Node> stack = new Stack<Node>();

    nodeList.get(0).wasVisited = true;
    System.out.println(nodeList.get(0).item);
    stack.push(nodeList.get(0));

    while (!stack.isEmpty()) {
        int nextNode = this.getUnvisitedAdjacentNode(stack.peek().index);

        if (nextNode == -1) {
            stack.pop();
        } else {
            nodeList.get(nextNode).wasVisited = true;
            System.out.println(nodeList.get(nextNode).item);
            stack.push(nodeList.get(nextNode));
        }
    }
    for (int i = 0; i < nodeList.size(); i++) {
        nodeList.get(i).wasVisited = false;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T04:09:30+00:00Added an answer on June 10, 2026 at 4:09 am

    Fortunately I found my own mistake, the code above is all correct, except it was in the code that I hadn’t pasted.

    In case anybody cares, the problem lied in the fact that I completely disregarded the fact that ArrayLists have an “IndexOf()” method (stupid, I know) and decided to hack my own “index” field into my Node class. When dealing with my own indices, I had a minor bug which screwed up the traversal.

    So the old line in my DFS algorithm looks like this:

    int nextNode = this.getUnvisitedAdjacentNode(stack.peek().index);
    

    But it should be:

    int nextNode = this.getUnvisitedAdjacentNode(this.nodeList.indexOf(stack.peek()));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a graph class I can use in Android(I'm aware pre-made
Suppose we have an arbitrary graph represented by nodes and pointers like this: class
I wrote a little graph visualizer class: public void simpleGraph(SparseMultigraph<Vertex,SEdge> graph, String name) {
I have a graph, each node have 4 child nodes. I wrote a algorithm
First a little intro: Last year i wrote this http://dragan.yourtree.org/code/canvas-3d-graph/ Now, i want to
I have a graph representing users and some articles they wrote. I need to
I wrote a symfony task to fill a database of sample data. Here's a
I am trying to explore a graph here but I am not sure what
In Python, I have a Graph class that has a dictionary of vertex objects.
I am working on Graphs and I created a Graph class and added #include

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.