Advertisement
Guest User

caiobm

a guest
Jan 23rd, 2009
1,343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.89 KB | None | 0 0
  1. /**
  2.  * SearchGraphAlgorithms 1.0
  3.  *
  4.  * Is a class containg search methods for graphs. Until now, only contains
  5.  * a single method that uses dijkstra algorithm.
  6.  *
  7.  */
  8.  
  9. import java.util.ArrayList;
  10. import java.util.LinkedList;
  11.  
  12. /**
  13.  *
  14.  * @author Caio Bomfim Martins
  15.  * @version 1.0
  16.  */
  17. public class SearchGraphAlgorithms {    
  18.    
  19.     /**
  20.      * Calculates the distance and paths from a vertex "v" to other
  21.      * vertices of a graph with double representation of the costs. Returns a
  22.      * SearchInfo object (inner public class) containing information about paths
  23.      * and distances.
  24.      *
  25.      * It is possible to make two types of consults in a SearchInfo object. The first
  26.      * is ask about a lowest path from vertex "v" to another one of the graph. And it is
  27.      * possible to ask about the lowest distance between vertex "v" to another vertex.
  28.      *
  29.      * The kind of the search depends of the TypeSearchEnum.
  30.      *
  31.      * @param type TypeSearchEnum is only Dijkstra
  32.      * @param v the initial vertex of the search
  33.      * @param cost the costs representation between each vertex.
  34.      *  
  35.      * @returns SearchInfo object containing data about the search results.      
  36.      */
  37.    
  38.     public SearchInfo search(TypeSearchEnum type, int v, Graph<Double,Double> cost) {
  39.         if(type == TypeSearchEnum.DIJKSTRA) {
  40.             return this.dijkstra(v, cost);
  41.         }
  42.        
  43.         return null;
  44.     }
  45.    
  46.     /**
  47.      * Calculates the lowest distance and paths from a vertex "v" to other
  48.      * vertices of a graph with double representation of the costs. Returns a
  49.      * SearchInfo object (inner public class) containing information about paths
  50.      * and distances.
  51.      *
  52.      * It is possible to make two types of consults in a SearchInfo object. The first
  53.      * is ask about a lowest path from vertex "v" to another one of the graph. And it is
  54.      * possible to ask about the lowest distance between vertex "v" to another vertex.
  55.      *
  56.      * Uses dijkstra algorithm.
  57.      *
  58.      * @param v the initial vertex of the search
  59.      * @param cost the costs representation between each vertex.
  60.      *  
  61.      * @returns SearchInfo object containing data about the search results.      
  62.      */
  63.     private SearchInfo dijkstra(int v, Graph<Double,Double> cost) {        
  64.         boolean[] visited = new boolean[cost.size()];
  65.         int[] previous = new int[cost.size()]; //
  66.         double[] DIST = new double[cost.size()];
  67.         double min = Float.MAX_VALUE;
  68.        
  69.         int i;        
  70.        
  71.         for ( i = 0; i < visited.length; i++ ) {            
  72.             DIST[i] = cost.get(v, i);
  73.         }
  74.        
  75.         previous[v] = -1;
  76.         visited[v] = true;
  77.         DIST[v] = 0;
  78.        
  79.         int u = 0;
  80.         for ( i = 0; i < DIST.length; i++ ) {
  81.             if ( min > DIST[i] && !visited[i]) {
  82.                 min = DIST[i];
  83.                 u = i;
  84.             }
  85.         }
  86.        
  87.         previous[u] = v;
  88.         int num = 2;
  89.        
  90.         while ( num < visited.length ) {
  91.             min = Double.POSITIVE_INFINITY; //Infinity
  92.            
  93.             for ( i = 0; i < DIST.length; i++ ) {
  94.                 if ( min > DIST[i] && !visited[i]) {
  95.                     min = DIST[i];
  96.                     u = i;
  97.                 }
  98.             }
  99.            
  100.             visited[u] = true;
  101.        
  102.             num++;
  103.            
  104.             for ( i = 0; i < visited.length; i++ ) {                
  105.                 if ( !visited[i] ) {                                  
  106.                     if ( DIST[i] > DIST[u] + cost.get(u, i) ) {  
  107.                         DIST[i] = DIST[u] + cost.get(u, i);                        
  108.                         previous[i] = u;
  109.                     }
  110.                 }
  111.             }            
  112.         }
  113.        
  114.         SearchInfo si = new SearchInfo(DIST);
  115.         si.buildPaths(previous);
  116.        
  117.         return si;
  118.     }
  119.    
  120.     /**
  121.      *
  122.      * @author Caio Bomfim Martins
  123.      * @version 1.0
  124.      */
  125.     public class SearchInfo {
  126.  
  127.         private ArrayList<LinkedList<Integer>> paths;
  128.  
  129.         private double[] dist;
  130.  
  131.         private SearchInfo(double[] dist) {
  132.             paths = new ArrayList<LinkedList<Integer>>(dist.length);
  133.  
  134.             for ( int i  = 0; i < dist.length; i++ ) {
  135.                 paths.add(new LinkedList<Integer>());
  136.             }
  137.  
  138.             this.dist = dist;
  139.         }
  140.  
  141.         private void buildPaths(int[] before) {
  142.             int i = 0;
  143.             int j;
  144.             while ( i < before.length ) {
  145.                 j = before[i];
  146.                 if ( j == -1 ) {
  147.                     this.paths.get(i).addFirst(i);
  148.                     i++;
  149.                     continue;
  150.                 }
  151.  
  152.                 this.paths.get(i).addFirst(i);
  153.                 this.paths.get(i).addFirst(j);
  154.  
  155.                 while ( j != -1) {
  156.                     if ( before[j] == -1) {
  157.                         break;
  158.                     }
  159.  
  160.                     this.paths.get(i).addFirst(before[j]);
  161.  
  162.                     j = before[j];
  163.                 }
  164.  
  165.                 i++;                
  166.             }
  167.         }
  168.  
  169.         /**
  170.          * Returns the lowest path from vertex "v" (from up class) to vertex "vertex";
  171.          *
  172.          * @param vertex the number of the vertex that will be returned the lowest path
  173.          *
  174.          * @returns A linkedList of Integers containing the vertex order in the  lowest path
  175.          */
  176.         public LinkedList<Integer> getPathFrom(int vertex) {
  177.             return this.paths.get(vertex);
  178.         }
  179.  
  180.        
  181.         /**
  182.          * Returns the lowest distance from vertex "v" (from up class) to vertex "vertex";
  183.          *
  184.          * @param vertex the number of the vertex that will be returned the lowest distance
  185.          *
  186.          * @returns A double containg the lowest distance
  187.          */
  188.         public double getDistFrom(int vertex) {
  189.             return this.dist[vertex];
  190.         }
  191.     }
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement