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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T02:48:05+00:00 2026-05-21T02:48:05+00:00

I know the title is a bit messy, but I don’t know how to

  • 0

I know the title is a bit messy, but I don’t know how to explain it better.

What I’m trying to do:

Using a graph found in a text file, find and print the shortest path (minimum amount of vertices) from vertex A to vertex B.

Note: using breadth-first search, not Dijkstra’s.

What I’ve got:

A working algorithm that applies BFS on the graph, but no good way of actually printing out the shortest path.

I’m having a hard time distinguishing a vertex in the shortest path from one that is simply run through the algorithm, but not in the shortest path.

For example: Find the shortest path between 0 and 4.
0 connects to 1,2 and 3. 1 connects to 4.
My path turns out to be [0,1,2,3,4] instead of [0,1,4].

I haven’t been able to find any threads asking the same question, or any walk-through of BFS that includes this, so I’m not sure if I’m making this out to be way harder than it is?

Edit: code for those who may be interested (not sure at all if I’m avoiding circles?)

Edit 2: Changed the way I store my path to a Stack.

public String findPath(int v, int w) {
    Queue<Integer> q = new LinkedList<Integer>();
    boolean[] visited = new boolean[g.numVertices()];

    q.add(v);
    Stack<Integer> path = new Stack<Integer>();
    while(q.peek() != null) {
        runBFS(q.poll(),w,visited,q,path);
    }
    return path.toString();
} 

private void runBFS(int v, int w, boolean[] visited, Queue<Integer> q, Stack<Integer> path) {
    if(visited[v]) {
    }
    else if(v == w) {
        path.add(v);
        q.clear();
    }
    else {
        path.add(v);
        visited[v] = true;
        VertexIterator vi = g.adjacentVertices(v);
        while(vi.hasNext()) {
                q.add(vi.next());
        }
    }
}

Some explanation of variables and methods:

v = vertex of origin

w = target vertex

g = graph

vi = a normal iterator that iterates over the neighbours of v

Thanks for reading!

  • 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-05-21T02:48:05+00:00Added an answer on May 21, 2026 at 2:48 am

    You will have to have specific path field for each vertex. That way you can keep track of the paths you’ve chosen, hence the short path found. I will use an String array, just like you used the Boolean array for storing visited vertices.

    public String findPath(int v, int w) {
        Queue<Integer> q = new LinkedList<Integer>();
        boolean[] visited = new boolean[g.numVertices()];
        String[] pathTo = new String[g.numVertices()];
    
        q.add(v);
        pathTo[v] = v+" ";
        while(q.peek() != null) {
            if(runBFS(q.poll(),w,visited,q,pathTo))
            break;
        }
        return pathTo[w];
    }
    
    private boolean runBFS(int v, int w, boolean[] visited, Queue<Integer> q, String[] pathTo) {
        if(visited[v]) {
        }
        else if(v == w)
            return true; 
        }
        else {
            visited[v] = true;
            VertexIterator vi = g.adjacentVertices(v);
            while(vi.hasNext()) {
                int nextVertex = vi.next();
                pathTo[nextVertex] = pathTo[v] + nextVertex + " ";
                q.add(nextVertex);
            }
        }
        return false;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know the title's a bit wordy, but I don't know how else to
I know the title of the question is a bit odd. But I don't
Excuse my title if it's confusing, but I don't know how else to describe
I know the title of this question is a bit confusing, but here it
I know the title is a little bit strange, but here is what the
i know, the title is a bit crampy, but it's a weird problem. i'm
I know the title is bit confusing for my question. Let me explain:- There
I know the title is bit confusing for my question. Let me explain:- There
I really didn't know what title to give this question, but I'll explain here:
OK, the question title is a bit crappy, but I didn't really know how

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.