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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T20:30:36+00:00 2026-05-31T20:30:36+00:00

I have to implement a search algorithm for a school assignment. Right now, i’m

  • 0

I have to implement a search algorithm for a school assignment. Right now, i’m having problems with sole java implementation. Here’s the code i have at the moment (i have been basing myself in some code i found here in stackoverflow for the dfs search, then i have to add verifications to meet the project criteria):

import java.util.*;

public class dfs<Grafo> {

public static void main(String[] args) {

    class Grafo{
         private Map<Integer, LinkedHashSet<Integer>> map = new HashMap();

         public void addEdge(int source, int destiny) {
                LinkedHashSet<Integer> adjacente = map.get(source);
                if(adjacente==null) {
                    adjacente = new LinkedHashSet();
                    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);
                if(adjacente==null) {
                    return new LinkedList();
                }
                return new LinkedList<Integer>(adjacente);
            }
    }

    Scanner input = new Scanner(System.in);

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

    Grafo mapa = new Grafo();

    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);
    new dfs().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(Grafo mapa, List<Integer> visited,
        List<ArrayList<Integer>> paths, Integer currentNode) {

    if (currentNode.equals(startNode)) { 
        paths.add(new ArrayList(Arrays.asList(visited.toArray())));
        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 problem i have with this, is that i can only turn in a single java file, so i’m putting everything in this single file.
When i get to the findAllPaths function he does not recognise the “startNode” constant (eclipse says that it cannot be be resolved to a variable) and he says “adjacentNodes” function is not defined for the type Grafo.

Is there anyway i can solve this problem or do i have to rethink the way i’m doing this, if so, what’s a good way to implement this?

  • 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-31T20:30:38+00:00Added an answer on May 31, 2026 at 8:30 pm

    Others seem to have addressed some of your coding issues, so I just wanted to illustrate how to work with the single file constraint.

    Suppose you had 3 classes, Edge, Node, and Graph that you developed in three separate files. Here’s how you could combine them:

    // in file GraphSearchHomework.java
    public class GraphSearchHomework {
      public static class Edge {
        // ... Edge code here ...
        // You can reference Nodes and Graphs
        // just as if this was in a separate file
      }
    
      public static class Node {
        // ... Node code here ...
        // You can reference Edges and Graphs
        // just as if this was in a separate file
      }
    
      public static class Graph {
        // ... Graph code here ...
        // You can reference Edges and Nodes
        // just as if this was in a separate file
      }
    
      public static void main(String args[]) {
        // This main can instantiate Graphs, Nodes, Edges
        // Keep this simple, though. Most code belongs outside of main.
      }
    }
    

    You seem to have made some mistakes trying to force this into one file. If it helps you, go ahead and develop each class separately and then combine them.

    • 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 in java for a school project. In
I'm tryin to implement a simple random search algorithm in Java here's a piece
I am having a Search text-box for which I have to implement a search
I have to implement the VinPower application. They offer a Java version, a C
I have to implement MPI system in a cluster. If anyone here has any
I'm trying to implement an algorithm to search multiple XML files for a certain
I have simple map-reduce type algorithm, which I want to implement in python and
I'm looking to implement fuzzy search for a small PHP/MySQL application. Specifically, I have
I have problem designing pseudo-code for these problems. It is not an assignment problem.
I am trying to implement the depth first search algorithm into my game. I

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.