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

  • SEARCH
  • Home
  • 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 7943989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T00:25:48+00:00 2026-06-04T00:25:48+00:00

Two simple questions and my brain’s not working. I’m using Floyd’s Algorithm and trying

  • 0

Two simple questions and my brain’s not working. I’m using Floyd’s Algorithm and trying to reconstruct the path taken from vertex U to vertex V.
Here’s my code to reconstruct the path.

public List<Integer> findCheapestPath(int u, int v)
{
    if (u >= adjMatrix.length || v >= adjMatrix.length || u < 0 || v < 0)
    {
        throw new IllegalArgumentException("Error--Illegal Arugment Exception: One of the verticies are not valid.");
    }
    int intermediate;

    intermediate = path[u][v];
    if (intermediate == -1)
    {
        pathList.add(u);
        return pathList;
    } else
    {
        findCheapestPath(u, intermediate);
        findCheapestPath(intermediate, v);
    }
    return pathList;
}

For one example, let u = 0 and v = 3, and let the shortest path from 0 to 3 be 0,1,2,3.
However I have two problems. My pathList is an instance variable of the class and:

  1. I want the list to return “0,1,2,3” but it only returns “0,1,2”, or if i replace pathList.add(u) with pathList.add(v), then it returns
    only “1,2,3” I could not figure out how to make it return the entire
    path including both end vertices. trying to put pathList.add(other
    vertex) will cause it to duplicate every intermediate vertex.
  2. When I call the method again, letting u = 3 and v = 0, and let the shortest path from 3 to 0 be “3,0” (already the shortest path), it
    just adds onto my previous list making it with my error from above
    “0,1,2,3” or “1,2,3,0” when it’s supposed to be just “3,0”

Any help?

  • 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-04T00:25:50+00:00Added an answer on June 4, 2026 at 12:25 am

    Don’t use a class variable, and make a wrapper function.

    In more detail, change your function to this signature:

    private List<Integer> findCheapestPathInner(int u, int v, pathList)
    

    (obviously also changing the recursive call inside it to fit the new signature), and create another function:

    public List<Integer> findCheapestPath(int u, int v) {
      List<Integer> pathList = new ArrayList<Integer>();
      pathList.add(u);
      return findCheapestPathInner(u, v, pathList);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Two questions. First question is I'm trying to create a simple form that when
This should be simple enough, but somehow my brain stopped working. I have two
I'm trying to publish webpage using org-mode. Two questions: Is there a way to
I actually have two questions, both are probably simple, but for some odd reason
I have two questions. First one is simple: Is there any way to control
I'll cut to the chase. I have two questions about switch that are simple,
Hopefully two simple questions relating to creating a server application: Is there a theoretical/practical
I have got two simple questions How can I tell what server is a
I am a definite beginner to iPhone programming. I have two simple questions. 1)
I'm still learning about DDD and I have these two (probably simple) questions: If

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.