macrofish

ggggggpath

Aug 3rd, 2014
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.44 KB | None | 0 0
  1. public void findPath()
  2.     {
  3.         System.out.println("finding path from "+ start.getName() + " to "+ stop.getName());
  4.         if(start.equals(stop))
  5.         {
  6.             System.out.println("start = stop, path is 0 units long");
  7.             return;
  8.         }
  9.        
  10.         int listsToAdd = 0;
  11.         List <Node>l = new ArrayList<Node>();
  12.         l.add(start);
  13.         listOfLists.add(l);
  14.      
  15.         listOfLengths.add(0);
  16.         listOfBools.add(false);
  17.        
  18.             for(int i = 0; i < listOfLists.size();i++)//pro kazdy list v seznamu listu
  19.             {
  20.                 for(int j =0;j<edges.size();j++)//pro vsechny hrany ktere sousedi s danym bodem
  21.                 {
  22.                     //System.out.println(edges.get(j).getFrom().getName());
  23.                     //System.out.println(listOfLists.get(i).s);
  24.                    
  25.                     int size = listOfLists.get(i).size();
  26.                     if((edges.get(j).getFrom().getName()==listOfLists.get(i).get(size-1).getName()) &&
  27.                             (listOfLists.get(i).contains(edges.get(j).getTo())==false))
  28.                     {
  29.                         List<Node> list = new ArrayList<>(listOfLists.get(i));
  30.                         list.add(edges.get(j).getTo());                      
  31.                         listOfLists.add(list);
  32.                                                
  33.                        
  34.                         int len = listOfLengths.get(i);
  35.                         len+=edges.get(j).getValue();
  36.                         listOfLengths.add(len);
  37.                        
  38.                         if(edges.get(j).getTo()==stop)
  39.                         {
  40.                             listOfBools.add(true);
  41.                         }
  42.                         else
  43.                         {
  44.                             listOfBools.add(false);
  45.                         }
  46.                     }
  47.                    
  48.                     if((edges.get(j).getTo().getName()==listOfLists.get(i).get(size-1).getName()) &&
  49.                             (listOfLists.get(i).contains(edges.get(j).getFrom())==false))
  50.                     {
  51.                         List<Node> list = new ArrayList<>(listOfLists.get(i));
  52.                         list.add(edges.get(j).getFrom());
  53.                         listOfLists.add(list);
  54.  
  55.                         int len = listOfLengths.get(i);
  56.                         len+=edges.get(j).getValue();
  57.                         listOfLengths.add(len);
  58.                        
  59.                         if(edges.get(j).getFrom()==stop)
  60.                         {
  61.                             listOfBools.add(true);
  62.                         }
  63.                         else
  64.                         {
  65.                             listOfBools.add(false);
  66.                         }
  67.                     }
  68.                 }
  69.             }
  70.        
  71.        
  72.        
  73. //        for(int i =0;i<listOfLists.size();i++)
  74. //        {          
  75. //            List <Node> llll =listOfLists.get(i);
  76. //            for(int j = 0;j < llll.size();j++)
  77. //            {
  78. //                System.out.print(llll.get(j).getName());
  79. //            }
  80. //            System.out.println();
  81. //            System.out.println(listOfLengths.get(i));
  82. //        }
  83.        
  84.  
  85.        
  86.         for(int i =0;i<listOfLists.size();i++)
  87.         {
  88.             path = listOfLists.get(i);
  89.             for(int j = 0; j <path.size();j++)
  90.             {
  91.                 System.out.print(path.get(j).getName()+"-");
  92.             }
  93.             System.out.print("  size: "+listOfLengths.get(i)+" " + listOfBools.get(i));
  94.             System.out.println();
  95.  
  96.         }
  97.        
  98.         int shortest = Integer.MAX_VALUE;
  99.         this.length = shortest;
  100.        
  101.         for(int i =0;i<listOfLists.size();i++)
  102.         {          
  103.            
  104.             if(listOfBools.get(i))
  105.             {
  106.                
  107.                 if(listOfLengths.get(i)<shortest)
  108.                 {
  109.                     shortest = listOfLengths.get(i);
  110.                     path = listOfLists.get(i);
  111.                 }
  112.             }
  113.         }
  114.        
  115.         System.out.println();
  116.         System.out.println();
  117.        
  118.         System.out.println("The path is "+ shortest +" units long");
  119.         System.out.print("The nodes are: ");
  120.         for(int i =0;i<path.size();i++)
  121.         {
  122.             System.out.print(path.get(i).getName()+"-");
  123.         }
  124.         System.out.println();
  125.        
  126.     }
Advertisement
Add Comment
Please, Sign In to add comment