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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:18:26+00:00 2026-05-27T04:18:26+00:00

With a set of random inputs like this (20k lines): A B U Z

  • 0

With a set of random inputs like this (20k lines):

A B
U Z
B A
A C
Z A
K Z
A Q
D A
U K
P U
U P
B Y
Y R
Y U
C R
R Q
A D
Q Z

Find all the paths from A to Z.

  1. A – B – Y – R – Q – Z
  2. A – B – Y – U – Z
  3. A – C – R – Q – Z
  4. A – Q – Z
  5. A – B – Y – U – K – Z

enter image description here

A location cannot appear more than once in the path, hence A - B - Y - U - P - U - Z is not valid.

Locations are named AAA to ZZZ (presented here as A – Z for simplicity) and the input is random in such a way that there may or may not be a location ABC, all locations may be XXX (unlikely), or there may not be a possible path at all locations are “isolated”.

Initially I’d thought that this is a variation of the unweighted shortest path problem, but I find it rather different and I’m not sure how does the algorithm there apply here.

My current solution goes like this:

  1. Pre-process the list such that we have a hashmap which points a location (left), to a list of locations (right)

  2. Create a hashmap to keep track of “visited locations”. Create a list to store “found paths”.

  3. Store X (starting-location) to the “visited locations” hashmap.

  4. Search for X in the first hashmap, (Location A will give us (B, C, Q) in O(1) time).

  5. For-each found location (B, C, Q), check if it is the final destination (Z). If so store it in the “found paths” list. Else if it doesn’t already exist in “visited locations” hashmap, Recurl to step 3 now with that location as “X”. (actual code below)

With this current solution, it takes forever to map all (not shortest) possible routes from “BKI” to “SIN” for this provided data.

I was wondering if there’s a more effective (time-wise) way of doing it. Does anyone know of a better algorithm to find all the paths from an arbitrary position A to an arbitrary position Z ?

Actual Code for current solution:

import java.util.*;
import java.io.*;

public class Test {
    private static HashMap<String, List<String>> left_map_rights;

    public static void main(String args[]) throws Exception {
        left_map_rights = new HashMap<>();
        BufferedReader r = new BufferedReader(new FileReader("routes.text"));
        String line;
        HashMap<String, Void> lines = new HashMap<>();
        while ((line = r.readLine()) != null) {
            if (lines.containsKey(line)) { // ensure no duplicate lines
                continue;
            }
            lines.put(line, null);
            int space_location = line.indexOf(' ');
            String left = line.substring(0, space_location);
            String right = line.substring(space_location + 1);
            if(left.equals(right)){ // rejects entries whereby left = right
                continue;
            }
            List<String> rights = left_map_rights.get(left);
            if (rights == null) {
                rights = new ArrayList<String>();
                left_map_rights.put(left, rights);
            }
            rights.add(right);
        }
        r.close();
        System.out.println("start");
        List<List<String>> routes = GetAllRoutes("BKI", "SIN");
        System.out.println("end");
        for (List<String> route : routes) {
            System.out.println(route);
        }
    }

    public static List<List<String>> GetAllRoutes(String start, String end) {
        List<List<String>> routes = new ArrayList<>();
        List<String> rights = left_map_rights.get(start);
        if (rights != null) {
            for (String right : rights) {
                List<String> route = new ArrayList<>();
                route.add(start);
                route.add(right);
                Chain(routes, route, right, end);
            }
        }
        return routes;
    }

    public static void Chain(List<List<String>> routes, List<String> route, String right_most_currently, String end) {
        if (right_most_currently.equals(end)) {
            routes.add(route);
            return;
        }
        List<String> rights = left_map_rights.get(right_most_currently);
        if (rights != null) {
            for (String right : rights) {
                if (!route.contains(right)) {
                    List<String> new_route = new ArrayList<String>(route);
                    new_route.add(right);
                    Chain(routes, new_route, right, end);
                }
            }
        }
    }
}
  • 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-27T04:18:27+00:00Added an answer on May 27, 2026 at 4:18 am

    As I understand your question, Dijkstras algorithm cannot be applied as is, since shortest path problem per definition finds a single path in a set of all possible paths. Your task is to find all paths per-se.

    Many optimizations on Dijkstras algorithm involve cutting off search trees with higher costs. You won’t be able to cut off those parts in your search, as you need all findings.

    And I assume you mean all paths excluding circles.

    Algorithm:

    • Pump network into a 2dim array 26×26 of boolean/integer. fromTo[i,j].
      Set a 1/true for an existing link.

    • Starting from the first node trace all following nodes (search links for 1/true).

    • Keep visited nodes in a some structure (array/list). Since maximal
      depth seems to be 26, this should be possible via recursion.

    • And as @soulcheck has written below, you may think about cutting of paths you have aleady visted. You may keep a list of paths towards the destination in each element of the array. Adjust the breaking condition accordingly.

    • Break when

      • visiting the end node (store the result)
      • when visiting a node that has been visted before (circle)
      • visiting a node for which you have already found all paths to the destination and merge your current path with all the existing ones from that node.

    Performance wise I’d vote against using hashmaps and lists and prefer static structures.

    Hmm, while re-reading the question, I realized that the name of the nodes cannot be limited to A-Z. You are writing something about 20k lines, with 26 letters, a fully connected A-Z network would require far less links. Maybe you skip recursion and static structures 🙂

    Ok, with valid names from AAA to ZZZ an array would become far too large. So you better create a dynamic structure for the network as well. Counter question: regarding performance, what is the best data structure for a less popuplate array as my algorithm would require? I’ vote for an 2 dim ArrayList. Anyone?

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

Sidebar

Related Questions

How do I pick a random element from a set? I'm particularly interested in
I want to put random output from my result set (about 1.5 mil rows)
Is it possible to print a random number in C++ from a set of
I am using this code to generate random text : from collections import defaultdict,
So right now I have this code that generates random letters in set increments
I need to find the most effecient way to find a random element from
I am doing a project involving taking millions of random samples from a set
I'm looking for a way to generate a set of random sided, but regular,
I have a txt file and each line has a set of random strings,
:set ic ignores the case. How do you unset this?

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.