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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:02:47+00:00 2026-05-31T18:02:47+00:00

I have to implement a search algorithm in java for a school project. In

  • 0

I have to implement a search algorithm in java for a school project. In this algorithm i need to find, in an undirected graph, a path that goes through each link only once and ends in the start node. I’m trying to use a DFS with backtracking to solve this problem, but i’m having trouble implementating it. Here’s my code:

import java.util.*;

public class Graph {

    private Map<Integer, LinkedHashSet<Integer>> map = 
        new HashMap<Integer, LinkedHashSet<Integer>>();
    private int startNode;
    private int numLinks;



    public Graph(int startNode, int numLinks) {
        super();
        this.startNode = startNode;
        this.numLinks = numLinks;
    }

    public void addEdge(int source, int destiny) {
        LinkedHashSet<Integer> adjacente = map.get(source);
        if(adjacente==null) {
            adjacente = new LinkedHashSet<Integer>();
            map.put(source, adjacente);
        }
        adjacente.add(destiny);
    }

    public void addLink(int source, int destiny) {
        addEdge(source, destiny);
        addEdge(destiny, source);
    }

    public LinkedList<Integer> adjacentNodes(int last) {
        LinkedHashSet<Integer> adjacente = map.get(last);
        System.out.println("adjacentes:" + adjacente);
        if(adjacente==null) {
            return new LinkedList<Integer>();
        }
        return new LinkedList<Integer>(adjacente);
    }

public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

    int numVertices = input.nextInt();
    int numLinks = input.nextInt();
    int startNode = input.nextInt();
    int endNode = startNode;

    Graph mapa = new Graph(startNode, numLinks);

    for(int i = 0; i<numLinks; i++){
        mapa.addLink(input.nextInt(), input.nextInt());

    }

    List<ArrayList<Integer>> paths = new ArrayList<ArrayList<Integer>>();
    Integer currentNode = startNode;
    List<Integer> visited = new ArrayList<Integer>();
    visited.add(startNode);
    mapa.findAllPaths(mapa, visited, paths, currentNode);

    for(ArrayList<Integer> path : paths){
        for (Integer node : path) {
            System.out.print(node);
            System.out.print(" ");
        }
        System.out.println();
    }

}

private void findAllPaths(Graph mapa, List<Integer> visited,
        List<ArrayList<Integer>> paths, Integer currentNode) {


    if (currentNode.equals(startNode)) { 
        paths.add(new ArrayList<Integer>(visited));
        return;
    }

    else {
        LinkedList<Integer> nodes = mapa.adjacentNodes(currentNode);    
        for (Integer node : nodes) {
            if (visited.contains(node)) {
                continue;
            } 
            List<Integer> temp = new ArrayList<Integer>();
            temp.addAll(visited);
            temp.add(node);          
            findAllPaths(mapa, temp, paths, node);
        }
    }

} 

}

The program is supposed to receive integers on his input, where the first one is the number of nodes, the second one is the number of links, the third is the start node(wich is also the end node), all the integers that come after represent the links between nodes.

The goal is to, in the end, print a single line with integers. This integers represent the order i visit each node to complete the path. Currently testing, it only prints a single integer, wich represents the first node.

I think my problem is either populating the graph, populating the adjacent list. Can somebody help me?

  • 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-31T18:02:48+00:00Added an answer on May 31, 2026 at 6:02 pm

    The problem is that when you call mapa.findAllPaths(mapa, visited, paths, currentNode);, you don’t actually find all paths. You only find one path (i.e. the current node) and you return:

    private void findAllPaths(Graph mapa, List<Integer> visited,
            List<ArrayList<Integer>> paths, Integer currentNode) {
    
        if (currentNode.equals(startNode)) { 
            paths.add(new ArrayList<Integer>(visited));
            return;// <--- WRONG!!!
        } else {
            // The else is never executed!
        }
    } 
    

    You should either have a loop or recursively call the findAllPaths until you find all the paths.

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

Sidebar

Related Questions

I have to implement a search algorithm for a school assignment. Right now, i'm
I am trying to implement an efficient algorithm for nearest-neighbour search problem. I have
I have an app that includes a search feature. This feature is implemented by
I'm tryin to implement a simple random search algorithm in Java here's a piece
I'm solving some problem that involves Rabin–Karp string search algorithm. This algorithm requires rolling
The Problem : I am trying to implement a search algorithm that shows the
I have a pretty good idea how to implement fulltext search with MySQL. I
I need to know about the uniform cost search algorithm. In the uniform cost
I'm currently looking into quick graph because I need to implement Job Shop Scheduling.
I'm trying to implement an algorithm to search multiple XML files for a certain

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.