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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T03:44:25+00:00 2026-05-20T03:44:25+00:00

So I have this assignment where I read in 1 line at a time

  • 0

So I have this assignment where I read in 1 line at a time separated by comma e.g.

Atlanta, Philadelphia   
New York, Philadelphia   
Philadelphia, Chicago   
Washington, Florida
.....
up to a vast amount.. (I don't know the amount)

Each line represents connectivity between the two locations (e.g. Atlanta connects to Philadelphia) creating connected nodes and nodes which are not connected like Washington and Florida is connected to each other but no one else.

What the program is suppose to do is read the file and given two city arguments its suppose to spit out Yes if its connected/ No if its not.

I finished my program and It works, however its not efficient. I’m stumped as to what I can do. Here is part of the program which makes the code inefficient.

This first input reads the file so I can determine the size of the list of different city, and it also removes any duplicate cities.

private static void createCityList() throws IOException{

        try {
            FileReader a = new FileReader(file);
            BufferedReader br = new BufferedReader(a);
            String line;
            line = br.readLine();

            while(line != null){
                StringTokenizer st = new StringTokenizer(line, ",");
                while(st.hasMoreTokens()){ 
                    String currentToken = st.nextToken();
                    if(!cityList.contains(currentToken.trim())){ 
                        cityList.add(currentToken.trim());
                    }//if
                }//while hasMoreTokens
                line = br.readLine();//read the next line
            }//while line != null
            br.close();
        }//try

        catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        length = cityList.size(); // set length to amount of unique cities

    }//createCityList

the 2nd method which does another fileread… allows me to create an adjacency matrix

private static void graph() throws IOException{ 
    cityGraph = new int[cityList.size()][cityList.size()]; 

        try {
            FileReader a = new FileReader(file);
            BufferedReader br = new BufferedReader(a);
            String line;
            line = br.readLine();


            while(line != null){
                StringTokenizer st = new StringTokenizer(line, ",");
                while(st.hasMoreTokens()){ 
                    String firstToken = st.nextToken().trim();
                    String secondToken = st.nextToken().trim();
                    cityGraph[cityList.indexOf(firstToken)][cityList.indexOf(secondToken)] = 1; 
                    cityGraph[cityList.indexOf(secondToken)][cityList.indexOf(firstToken)] = 1; 
                }//while hasMoreTokens

                line = br.readLine();//read the next line

            }//while line != null

            br.close();

        }//try

        catch (FileNotFoundException e) {
            e.printStackTrace();
        }//catch
    }//graph

And my final method runs a DFS on the 2 cities to determine if its connected

private static void isConnected(String s1, String s2){

        city1 = cityList.indexOf(s1); //set city to the index of s1 or s2 in the cityList LinkedList.
        city2 = cityList.indexOf(s2); 


        int startNode = city1;
        q.add(startNode); // start node

        while(!q.isEmpty()){
        //visit vertex
            for(int i = 0; i < length; i++){
                if(cityGraph[startNode][i] == 1){
                    if( i == city2 ){ 
                        System.out.println("yes");
                        return;
                    }//if city2 found
                    q.add(i);
                    cityGraph[startNode][i] = 0; //Set to visited
                }//if vertex exist
            }//for
            q.remove();//remove the top element and start with new node
            if(!q.isEmpty()){
                startNode = (Integer) q.element();
            }//if

        }//while q is not empty     
        System.out.println("no");
    }//isConnected

I’m trying to only have one file read, but I’m having issues making a matrix from an unknown size its only after the file read that I find out the size. Any help or suggestion would be greatly
appreciated!

  • 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-20T03:44:26+00:00Added an answer on May 20, 2026 at 3:44 am

    I have a few comments on the code:

    1) Take those lines in the first code snippet:

    while(st.hasMoreTokens()){ 
        String currentToken = st.nextToken();
        if(!cityList.contains(currentToken.trim())){ 
            cityList.add(currentToken.trim());
        }//if
    }//while hasMoreTokens
    

    The cityList.contains() method consumes linear time on the number of cities, and the while(st.hasMoreTokens()) might run O(V^2) times where V is the number of vertices, since you can have a dense graph. So, just in this one loop, you are consuming O(V^3) time, which is already worst than a DFS (O(V + E) which is O(V^2) in a dense graph). You can’t speed up the O(V^2) loop because you have to read all the edges, but you can use a more efficient data structure to hold that city list, namely a hash (O(1) lookup, O(1) insertion).

    2) On the second code snippet:

    while(st.hasMoreTokens()){ 
        String firstToken = st.nextToken().trim();
        String secondToken = st.nextToken().trim();
        cityGraph[cityList.indexOf(firstToken)][cityList.indexOf(secondToken)] = 1; 
        cityGraph[cityList.indexOf(secondToken)][cityList.indexOf(firstToken)] = 1; 
    }//while hasMoreTokens
    

    Exactly the same thing. Use a hash instead of a list.

    3) Inner loop of your DFS

    if(cityGraph[startNode][i] == 1){
        if( i == city2 ){ 
            System.out.println("yes");
            return;
        }//if city2 found
        q.add(i);
        cityGraph[startNode][i] = 0; //Set to visited
    }//if vertex exist
    

    There are two problems. One is that you are overwriting your graph representation every time you run a DFS. By setting cityGraph[startNode][i] = 0; you are actually deleting an edge of your graph. If you are reconstructing the graph for every DFS, that is a huge problem.

    Second problem is that it seems to me you are marking visited nodes in the wrong way. You are just marking visited EDGES, not nodes. If you have the path 1 -> 2 and the path 1 -> 4 -> 2, you are going to visit (and add to queue) node 2 two times.

    To solve both problems, use a boolean visited[#cities] array. Everytime you start the DFS, you set all nodes to not visited. Everytime you check an edge, you check if you have already visited that node. If not, add it to the queue.

    On a final note,

    q.remove();//remove the top element and start with new node
    if(!q.isEmpty()){
        startNode = (Integer) q.element();
    }//if
    

    This is ugly since you are already checking if the queue is empty on the while loop. Instead, you can just move this code to the beggining of the while loop, removing the if condition (because you know the queue is not empty):

    while(!q.isEmpty()){
        startNode = (Integer) q.element();
        q.remove();
    

    Hope that helps….

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

Sidebar

Related Questions

I have this string 'john smith~123 Street~Apt 4~New York~NY~12345' Using JavaScript, what is the
I have this line in a javascript block in a page: res = foo('<%=
I have this line in a useful Bash script that I haven't managed to
(this is indirectly a part of a much larger homework assignment) I have something
I have read the following from Collator's Javadoc. The exact assignment of strengths to
I have found this example on StackOverflow: var people = new List<Person> { new
Continuing on this problem , but I'll reiterate: For a homework assignment I have
I have this assignment to prove that this problem: Finite alphabet £, two strings
I have this code in jQuery, that I want to reimplement with the prototype
I have this idea for a free backup application. The largest problem I need

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.