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 8913705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:31:44+00:00 2026-06-15T04:31:44+00:00

I am trying to discover all nodes reachable from a start node. It seems

  • 0

I am trying to discover all nodes reachable from a start node.

It seems like the base case is never reached, I am not sure why.

My code:

class Vertex {
    public final String name;
    public int found;
    public Edge[] adjacencies;
    public Vertex(String argName) { name = argName; found = 0;}
}

class Edge {
    public final Vertex target;
    public Edge(Vertex argTarget, double argWeight) { target = argTarget; weight = argWeight; }
}

ArrayList<Vertex> merge(ArrayList<Vertex> a, ArrayList<Vertex> b) {
    if(a == null) return b;
    if(b == null) return a;
    for(int x = 0; x < a.size(); x++)
        b.add(a.get(x));
        return b;
}

ArrayList<Vertex> span(Vertex b) {
    System.out.println("hello");
    if(b.found == 1) {
        return null;
    }
    ArrayList<Vertex> t = new ArrayList<Vertex>();
    t.add(b);
    b.found = 0;
    if(b.adjacencies == null) return t;
    for(int x = 0; x < b.adjacencies.length; x++) {
        ArrayList<Vertex> result = span(b.adjacencies[x].target);
        t = merge(t, result);
    }
    return t;
}

I am getting a stack-overflow error, it seems like it is stuck in recursion, but I am not sure why.

Note: This is the graph that I am running it on.

Vertex v0 = new Vertex("Redvile");
Vertex v1 = new Vertex("Blueville");
Vertex v2 = new Vertex("Greenville");
Vertex v3 = new Vertex("Orangeville");
Vertex v4 = new Vertex("Purpleville");

v0.adjacencies = new Edge[]{ new Edge(v1, 5), new Edge(v2, 10), new Edge(v3, 8) };
v1.adjacencies = new Edge[]{ new Edge(v0, 5), new Edge(v2, 3), new Edge(v4, 7) };
v2.adjacencies = new Edge[]{ new Edge(v0, 10), new Edge(v1, 3) };
v3.adjacencies = new Edge[]{ new Edge(v0, 8), new Edge(v4, 2) };
v4.adjacencies = new Edge[]{ new Edge(v1, 7), new Edge(v3, 2) };
  • 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-15T04:31:45+00:00Added an answer on June 15, 2026 at 4:31 am

    You never set found to 1, you only set it to 0 while you probably meant 1.
    Also, use a boolean for found.
    Also, use addAll for ArrayLists, and return Collections.emptyList, instead of null.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to discover opencv library at the moment, but it seems a bit
I am trying to use wireshark to discover why visual studio is not downloading
I'm trying to discover the most-derived class of an object, within a constructor of
I am trying to discover items that a pubsub service provides. When I log
I am trying to discover how the ExecutionContext actually works in version 4.0 and
Greetings, I was trying to discover a proper way to send captured errors or
I was profiling my .net windows service. I was trying to discover OutOfMemoryException and
Trying to build out an exception if move.UserId does not equal currentUserId then Redirect
Trying to make this jQuery filter that uses .find case-insensitive. For example, when the
Trying to figure out how I can do this properly. The print_r looks like

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.