0

Im attempting to implement Dijkstras algorithm however somewhere in it it seems that I have created an infinite loop and I cant for the life of me find where. I first sort a "City" object into an array of lists, then attempt to run dijkstras algorithm to find the shortest path between the two cities. I know that its a lot to look at but I'm at a roadblock here.

Sort function

public static City[] store() throws FileNotFoundException{
        File input = new File("input.txt");
        Scanner in = new Scanner(input);

        int numCities = in.nextInt();
        if(numCities < 3 || numCities > 1000){
            System.out.println("Incorrect input");
            System.exit(-1);
        }
        City[] cities = new City[numCities + 1];
        for(int i = 1; i < cities.length; i++){
            cities[i] = new City(i);
        }
        int numRoads = in.nextInt();
        if(numRoads < 2 || numRoads > 10000){
            System.out.println("Incorrect input");
            System.exit(-1);
        }
        int currCity = in.nextInt();
        int count = 3;
        while(count < cities.length){
            cities[currCity].motelPrice = in.nextInt();
            if(count < cities.length - 1)
                currCity = in.nextInt();
            count++;
        }

        while(in.hasNext()){
            City activeCity;
            currCity = in.nextInt();
            int nextCity = in.nextInt();
            int weight = in.nextInt();
            if(cities[currCity].next == null){
                cities[currCity].next = duplicate(cities, nextCity, weight);
                addCity(cities, nextCity, duplicate(cities, currCity, weight));
            }
            else{
                activeCity = cities[currCity];
                while(activeCity.next != null){
                    activeCity = activeCity.next;
                }
                activeCity.next = duplicate(cities, nextCity, weight);
                addCity(cities, nextCity, duplicate(cities, currCity, weight));
            }


        }
        for( int k = 1; k < cities.length; k++){
           System.out.print("["+k+" $"+cities[k].motelPrice+"]");
           City activeCity = cities[k];
           while(activeCity.next != null){
                activeCity = activeCity.next;
               System.out.print("($"+activeCity.nextWeight+")-->["+activeCity.cityIndex+"]");
           }
           System.out.println();
        }
        return cities;
    }

dijkstra

public static int[] dijkstra(City[] cities, int source){  
       HashSet q = new HashSet();
       for(int i = 1; i < cities.length; i++){
           q.add(cities[i]);
       }

       int[] dist = new int[cities.length];
       int[] prev = new int[cities.length];
       for (int i=0; i<dist.length; i++) {
          dist[i] = Integer.MAX_VALUE;

       }

       dist[source] = 0;

       while(q.isEmpty() == false){
           City u = new City(0);
           int x;
           int min = Integer.MAX_VALUE;
           for(int j = 0; j < dist.length; j++){
               if(dist[j] < min){
                 min = dist[j];
                 if(q.contains(cities[j])){
                    u = cities[j];
                    q.remove(u);
                    City v = u;
                    while(v.next != null){
                        v = v.next;
                        int alt = dist[u.cityIndex] + v.nextWeight; 
                        if(alt < (dist[v.cityIndex])){
                            dist[v.cityIndex] = alt;
                            prev[v.cityIndex] = u.cityIndex;
                        }
                    }            
                }
            }
           }

       }
       return dist;
   }
Hunter Tipton
  • 313
  • 1
  • 4
  • 11
  • Do we need input.txt to run it? Can you provide the main code that runs the sort and dijkstra? I'd like to just load it up and execute it for myself. – Mark Cramer May 07 '16 at 03:34
  • Have you stepped through the code in your debugger? You could start the execution, wait until it goes into an infinite loop and then break in to see where it is executing. Please do this. You will almost surely figure out the problem on your own, and it will be much more educational for you than having someone tell you what is wrong. Please visit the [help] and read [ask]. – Jim Garrison May 07 '16 at 03:48

0 Answers0