Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.33 KB | None | 0 0
  1. import java.util.ArrayList;
  2.  
  3. public class Graph {
  4.    
  5.     private ArrayList<Vertex> vertices;
  6.    
  7.     public Graph() {
  8.         this.vertices = new ArrayList<Vertex>();
  9.     }
  10.    
  11.     public int size() {
  12.         return this.vertices.size();
  13.     }
  14.    
  15.     public Vertex getVertex(int i) {
  16.         if (i >= this.size())
  17.             throw new IndexOutOfBoundsException("Specified vertex index is out of range.");
  18.        
  19.         return this.vertices.get(i);
  20.     }
  21.    
  22.     public int addVertex(String w) {
  23.         this.vertices.add(
  24.                 new Vertex(this.vertices.size(), w));
  25.        
  26.         return (this.vertices.size() - 1);
  27.     }
  28.    
  29.     public void initAdjacencyLists() {
  30.         for (Vertex v : this.vertices)
  31.             for (Vertex w : this.vertices)
  32.                 if (Graph.areVerticesAdjacent(v, w))
  33.                     v.addToAdjacencyList(w);
  34.     }
  35.    
  36.     private static boolean areVerticesAdjacent(Vertex v, Vertex w) {
  37.         String vWord = v.getWord();
  38.         String wWord = w.getWord();
  39.        
  40.         if (vWord.length() != wWord.length())
  41.             return false;
  42.        
  43.         if (vWord.equals(wWord))
  44.             return false;
  45.        
  46.         int differenceCount = 0;
  47.        
  48.         for (int i = 0; i < vWord.length(); i++)
  49.             if (vWord.charAt(i) != wWord.charAt(i))
  50.                 differenceCount++;
  51.        
  52.         if (differenceCount != 1)
  53.             return false;
  54.        
  55.         return true;
  56.     }
  57.    
  58.     public void doBusiness(int w1index, int w2index) {
  59.        
  60.         ArrayList<Vertex> q = this.vertices;
  61.        
  62.         Vertex u = q.get(w1index);
  63.         u.distance = 0;
  64.        
  65.         for (Vertex v : this.vertices) {
  66.             if (Graph.areVerticesAdjacent(u, v))
  67.                 v.distance = dist_between(u, v);
  68.         }
  69.            
  70.         ArrayList<Vertex> S = new ArrayList<Vertex>();
  71.         S.add(u);
  72.        
  73.         while (!q.isEmpty()) {
  74.            
  75.             Vertex v = getSmallest(q);
  76.             S.add(v);
  77.             q.remove(v);
  78.            
  79.             for (Vertex w : q) {
  80.                 w.distance = dist_between(v, w);
  81.             }
  82.            
  83.         }
  84.        
  85.     }
  86.    
  87.     private static Vertex getSmallest(ArrayList<Vertex> list) {
  88.        
  89.         Vertex v = null;
  90.         int s = Integer.MAX_VALUE;
  91.        
  92.         for (Vertex w : list) {
  93.            
  94.             if (w.distance < s) {
  95.                 v = w;
  96.                 s = w.distance;
  97.             }
  98.            
  99.         }
  100.        
  101.         list.remove(v);
  102.        
  103.         return v;
  104.     }
  105.    
  106.     private static int dist_between(Vertex u, Vertex v) {
  107.         String word1 = u.getWord();
  108.         String word2 = v.getWord();
  109.         char a = 0;
  110.         char b = 0;
  111.        
  112.         for (int i = 0; i < word1.length(); i++) {
  113.             a = word1.charAt(i);
  114.             b = word2.charAt(i);
  115.            
  116.             if (a < b)
  117.                 return b - a;
  118.             else if (a > b)
  119.                 return a - b;
  120.         }
  121.        
  122.         return 0;
  123.     }
  124.    
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement