Aldin_SXR

EdgeWeightedGraph.java

May 26th, 2020
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.22 KB | None | 0 0
  1. package ds.edge.weighted.graph;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Scanner;
  5.  
  6. public class EdgeWeightedGraph {
  7.     private int V;                      // number of vertices
  8.     private int E;                      // number of edges
  9.     private ArrayList<Edge>[] adj;      // adjacency lists
  10.    
  11.     /* Create an edge-weighted graph with V vertices and no edges */
  12.     @SuppressWarnings("unchecked")
  13.     public EdgeWeightedGraph(int V) {
  14.         this.V = V;
  15.         this.E = 0;
  16.         adj = (ArrayList<Edge>[]) new ArrayList[V]; // create an array of lists
  17.         for (int v = 0; v < V; v++) {               // initialize all lists
  18.             adj[v] = new ArrayList<Edge>();         // the lists will hold adjacent vertices
  19.         }
  20.     }
  21.    
  22.     /* Create an edge-weighted graph from an input stream (e.g. file) */
  23.     public EdgeWeightedGraph(Scanner in) {
  24.         /* The first file line is V, the number of vertices*/
  25.         this(in.nextInt());                     // construct the graph using V  
  26.         int E = in.nextInt();                   // the next line is E, the number of edges
  27.        
  28.         for (int i = 0; i < E; i++) {           // iterate over all edges
  29.             int v = in.nextInt();               // read one vertex
  30.             int w = in.nextInt();               // read the other vertex
  31.             double weight = in.nextDouble();    // read the edge weight
  32.             addEdge(new Edge(v, w, weight));    // add an edge between vertex v and w
  33.         }
  34.     }
  35.    
  36.     /* Return the number of vertices */
  37.     public int V() {
  38.         return V;
  39.     }
  40.    
  41.     /* Return the number of edges */
  42.     public int E() {
  43.         return E;
  44.     }
  45.    
  46.     /* Add a new edge to the graph */
  47.     public void addEdge(Edge e) {
  48.         int v = e.either();     // get the first vertex
  49.         int w = e.other(v);     // get the other vertex
  50.         adj[v].add(e);          // connect v to w
  51.         adj[w].add(e);          // connect w to v
  52.         E++;                    // increment the number of edges
  53.     }
  54.    
  55.     /* Return adjacent vertices for a vertex v */
  56.     public Iterable<Edge> adj(int v) {
  57.         return adj[v];
  58.     }
  59.    
  60.     /* Return all edges in the graph */
  61.     public Iterable<Edge> edges() {
  62.         ArrayList<Edge> ed = new ArrayList<Edge>(); // create a new list
  63.         for (int v = 0; v < V; v++) {               // iterate over vertices
  64.             for (Edge e: adj[v]) {                  // iterate over all adjacent vertices of v
  65.                 if (e.other(v) > v) {               // avoid self-loops
  66.                     ed.add(e);                      // add the edge to the list
  67.                 }
  68.             }
  69.         }
  70.         return ed;                                  // return all edges
  71.     }
  72. }
Add Comment
Please, Sign In to add comment