Advertisement
Aldin_SXR

Edge.java

May 26th, 2020
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.11 KB | None | 0 0
  1. package ds.edge.weighted.graph;
  2.  
  3. public class Edge implements Comparable<Edge> {
  4.    
  5.     private int v;              // one vertex  
  6.     private int w;              // the other vertex
  7.     private double weight;      // edge weight
  8.    
  9.     /* Create an Edge object */
  10.     public Edge(int v, int w, double weight) {
  11.         this.v = v;
  12.         this.w = w;
  13.         this.weight = weight;
  14.     }
  15.    
  16.     /* Retrieve the weight of the edge */
  17.     public double weight() {
  18.         return weight;
  19.     }
  20.    
  21.     /* Return one of the vertices; in this case, v */
  22.     public int either() {
  23.         return v;
  24.     }
  25.    
  26.     /* Return the other vertex */
  27.     public int other(int vertex) {                             
  28.         if (vertex == v) {          // if provided the first vertex,
  29.             return w;               // return the second one
  30.         } else if (vertex == w) {   // if provided the other vertex,
  31.             return v;               // return the first one
  32.         } else {                    // throw an error for a wrong vertex
  33.             throw new RuntimeException("Inconsistent edge");
  34.         }
  35.     }
  36.    
  37.     /* Compare two Edges */
  38.     public int compareTo(Edge e) {
  39.         double diff = this.weight - e.weight();
  40.         if (diff > 0) {
  41.             return 1;
  42.         } else if (diff < 0) {
  43.             return -1;
  44.         } else {
  45.             return 0;
  46.         }
  47.     }
  48.    
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement