Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.42 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.         //ArrayList<Vertex> pred = new ArrayList<Vertex>();
  62.        
  63.         Vertex u = q.get(w1index);
  64.         u.distance = 0;
  65.         q.remove(u);
  66.        
  67.         for (Vertex v : this.vertices) {
  68.             if (Graph.areVerticesAdjacent(u, v))
  69.                 v.distance = dist_between(u, v);
  70.         }
  71.            
  72.         ArrayList<Vertex> S = new ArrayList<Vertex>();
  73.         S.add(u);
  74.        
  75.         while (!q.isEmpty()) {
  76.            
  77.             Vertex v = getSmallest(q);
  78.             S.add(v);
  79.             q.remove(v);
  80.            
  81.             for (Vertex w : q) {
  82.                 int dist = dist_between(v, w);
  83.                 w.distance = dist;
  84.             }
  85.            
  86.         }
  87.        
  88.     }
  89.    
  90.     private static Vertex getSmallest(ArrayList<Vertex> list) {
  91.        
  92.         Vertex v = null;
  93.         int s = Integer.MAX_VALUE;
  94.        
  95.         for (Vertex w : list) {
  96.            
  97.             if (w.distance < s) {
  98.                 v = w;
  99.                 s = w.distance;
  100.             }
  101.            
  102.         }
  103.        
  104.         list.remove(v);
  105.        
  106.         return v;
  107.     }
  108.    
  109.     private static int dist_between(Vertex u, Vertex v) {
  110.         String word1 = u.getWord();
  111.         String word2 = v.getWord();
  112.         char a = 0;
  113.         char b = 0;
  114.        
  115.         for (int i = 0; i < word1.length(); i++) {
  116.             a = word1.charAt(i);
  117.             b = word2.charAt(i);
  118.            
  119.             if (a < b)
  120.                 return b - a;
  121.             else if (a > b)
  122.                 return a - b;
  123.         }
  124.        
  125.         return 0;
  126.     }
  127.    
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement