Java - 在两个节点之间找到最短路径的最简单方法

时间:2017-11-08 23:53:07

标签: java arraylist hashmap shortest-path

我正在创建上图中所有城市及其邻居的网络/图表。

我正在尝试创建一个方法,该方法将从另一个节点找到最短路径。边缘没有重量。例如,如果我试图找到"洛杉矶"之间的最短路径。和"蒙特利尔",我应该看到以下结果:

> (find-shortest-path "los angeles" "montreal" ticket-to-ride)
 "los angeles"
 "san francisco"
 "salt lake city"
 "helena"
 "winnipeg"
 "sault ste. marie"

到目前为止,我已经创建了一个名为shortest_path的程序,它可以找到" start"我的图表中的参数和" end"参数,仅当它是其邻居之一时。如何查看每个邻近城市并找到两个节点之间的最短路径?是否可以创建一个LinkedLists数组,其中包含" start"之间的所有路径。并且"结束",以便返回最短的?为实现这一目标,最好的策略是什么?这是我从城市,邻居和图表类开始创建的程序:

城市类:

import java.util.*;  
public class city{
  String city;
  boolean visited;

  public city(String s){
    this.city = s;
    visited = false;
  }
  public String toString() {
    return this.city; 
  }
  public void setVisited(boolean b){
    this.visited = b;
  }
}

邻居类:

import java.util.*;  
public class neighbors extends ArrayList<city> {
  public neighbors (city[] n) {
    for (city v : n)
      this.add(v); 
  }
  public ArrayList<city> toArrayList() {
    return this; 
  }
}

主要方法在此课程中:

import java.util.*;

public class shortest_path{


  public static void find_shortest_path(String start, String end, Graph graph){



    city best;
    city current;

    neighbors n;
    Iterator<Map.Entry<city, neighbors>> it = graph.entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry<city, neighbors> pair = it.next();
        if(pair.getKey().toString() == start){
          //best = pair;
          best = pair.getKey();
          best.setVisited(true);

          System.out.println("Start : " + best.toString()); //THE START IS FOUND
          //Iterate through the neighbors from the start
          for (city c: best.getValue())
          {
            while(!(c.visited == true))
            {
              c.setVisited(true);
              if (!(c.toString() == end))
              {
                temp.setVisited(true);
                System.out.println("End : " + c.toString()); //THE END
              }
            }
          }
        }
    }
  }

  public static void main(String[] args){

    Graph a = new Graph(); 

    city vancouver = new city("vancouver");
    city calgary = new city("calgary");
    city winnipeg = new city("winnipeg");
    city sault_ste_marie = new city("sault ste. marie");
    city montreal = new city("montreal");
    city boston = new city("boston");
    city new_york = new city("new york");
    city toronto = new city("toronto");
    city pittsburgh = new city("pittsburgh");
    city washington = new city("washington");
    city raleigh = new city("raleigh");
    city charleston = new city("charleston");
    city miami = new city("miami");
    city atlanta = new city("atlanta");
    city nashville = new city("nashville");
    city chicago = new city("chicago");
    city saint_louis = new city("saint louis");
    city little_rock = new city("little rock");
    city new_orleans = new city("new_orleans");
    city houston = new city("houston");
    city dallas = new city("dallas");
    city oklahoma_city = new city("oklahoma city");
    city kansas_city = new city("kansas city");
    city omaha = new city("omaha");
    city duluth = new city("duluth");
    city helena = new city("helena");
    city salt_lake_city = new city("salt lake city");
    city denver = new city("denver");
    city santa_fe = new city("santa fe");
    city el_paso = new city("el paso");
    city phoenix = new city("phoenix");
    city las_vegas = new city("las vegas");
    city san_francisco = new city("san francisco");
    city los_angeles = new city("los angeles");
    city portland = new city("portland");
    city seattle = new city("seattle");

    a.put(vancouver, new neighbors( new city[] { calgary, seattle } )); 
    a.put(calgary, new neighbors( new city[] { vancouver, seattle, helena, winnipeg } )); 
    a.put(winnipeg, new neighbors( new city[] { calgary, helena, duluth, sault_ste_marie } )); 
    a.put(sault_ste_marie, new neighbors( new city[] { winnipeg, duluth, toronto, montreal } ));
    a.put(montreal, new neighbors( new city[] { boston, new_york, toronto, sault_ste_marie } ));
    a.put(boston, new neighbors( new city[] { montreal, new_york } ));
    a.put(new_york, new neighbors( new city[] { washington, pittsburgh, montreal, boston } ));
    a.put(toronto, new neighbors( new city[] { sault_ste_marie, montreal, pittsburgh, duluth, chicago } ));
    a.put(pittsburgh, new neighbors( new city[] { toronto, new_york, washington, raleigh, nashville, saint_louis, chicago } ));
    a.put(washington, new neighbors( new city[] { new_york, pittsburgh, raleigh } ));
    a.put(raleigh, new neighbors( new city[] { charleston, atlanta, nashville, pittsburgh, washington } ));
    a.put(charleston, new neighbors( new city[] { raleigh, atlanta, miami } ));
    a.put(miami, new neighbors( new city[] { charleston, atlanta, new_orleans } ));
    a.put(atlanta, new neighbors( new city[] { raleigh, charleston, miami, new_orleans, nashville } ));
    a.put(nashville, new neighbors( new city[] { saint_louis, little_rock, atlanta, raleigh, pittsburgh } ));
    a.put(chicago, new neighbors( new city[] { pittsburgh, saint_louis, toronto, duluth, omaha } ));
    a.put(saint_louis, new neighbors( new city[] { chicago, pittsburgh, nashville, little_rock, kansas_city } ));
    a.put(little_rock, new neighbors( new city[] { nashville, new_orleans, dallas, oklahoma_city, saint_louis } ));
    a.put(new_orleans, new neighbors( new city[] { houston, little_rock, atlanta, miami } ));
    a.put(houston, new neighbors( new city[] { el_paso, dallas, new_orleans } ));
    a.put(dallas, new neighbors( new city[] { little_rock, houston, el_paso, oklahoma_city } ));
    a.put(oklahoma_city, new neighbors( new city[] { denver, kansas_city, little_rock, dallas, el_paso, santa_fe } ));
    a.put(kansas_city, new neighbors( new city[] { omaha, saint_louis, oklahoma_city, denver } ));
    a.put(omaha, new neighbors( new city[] { helena, duluth, chicago, kansas_city, denver } ));
    a.put(duluth, new neighbors( new city[] { winnipeg, sault_ste_marie, toronto, chicago, omaha, helena } ));
    a.put(helena, new neighbors( new city[] { seattle, calgary, winnipeg, duluth, omaha, denver, salt_lake_city } ));
    a.put(salt_lake_city, new neighbors( new city[] { portland, helena, denver, las_vegas, san_francisco } ));
    a.put(denver, new neighbors( new city[] { salt_lake_city, helena, omaha, kansas_city, oklahoma_city, santa_fe, phoenix } ));
    a.put(santa_fe, new neighbors( new city[] { denver, oklahoma_city, el_paso, phoenix } ));
    a.put(el_paso, new neighbors( new city[] { los_angeles, phoenix, santa_fe, oklahoma_city, dallas, houston } ));
    a.put(phoenix, new neighbors( new city[] { los_angeles, denver, santa_fe, el_paso } ));
    a.put(las_vegas, new neighbors( new city[] { salt_lake_city, los_angeles } ));
    a.put(san_francisco, new neighbors( new city[] { portland, salt_lake_city, los_angeles } ));
    a.put(los_angeles, new neighbors( new city[] { san_francisco, las_vegas, phoenix, el_paso } ));
    a.put(portland, new neighbors( new city[] { seattle, salt_lake_city, san_francisco } ));
    a.put(seattle, new neighbors( new city[] { vancouver, calgary, helena, portland } ));

    find_shortest_path("omaha", "helena", a); // should work easily
    //find_shortest_path("omaha", "toronto", a); // should be difficult
  }
}

完成此任务的最简单方法是什么?任何提示或修改都会有所帮助。

0 个答案:

没有答案