Advertisement
Guest User

Graph Ford Fulkerson

a guest
Nov 24th, 2017
988
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.16 KB | None | 0 0
  1. package ford_fulkerson;
  2.  
  3. import java.util.List;
  4. import java.util.ArrayList;
  5.  
  6. public class Graph {
  7.     private int vCount;
  8.     private float[][] adj;
  9.  
  10.     public int getvCount() {
  11.         return vCount;
  12.     }
  13.  
  14.     public float[][] getAdj() {
  15.         return adj;
  16.     }
  17.  
  18.     public Graph(int vCount) {
  19.         this.vCount = vCount;
  20.         adj = new float[vCount][vCount];
  21.         for (int i = 0; i < vCount; i++) {
  22.             for (int j = 0; j < vCount; j++) {
  23.                 adj[i][j] = 0;
  24.             }
  25.         }
  26.     }
  27.  
  28.     public void addEdge(int i, int j, float weight) {
  29.         adj[i][j] = weight;
  30.     }
  31.  
  32.     public void removeEdge(int i, int j) {
  33.         adj[i][j] = 0;
  34.     }
  35.  
  36.     public boolean hasEdge(int i, int j) {
  37.         if (adj[i][j] != 0) {
  38.             return true;
  39.         }
  40.         return false;
  41.     }
  42.  
  43.     public List<Integer> neighbours(int vertex) {
  44.         List<Integer> edges = new ArrayList<Integer>();
  45.         for (int i = 0; i < vCount; i++)
  46.             if (hasEdge(vertex, i))
  47.                 edges.add(i);
  48.         return edges;
  49.     }
  50.  
  51.     public void printGraph() {
  52.         for (int i = 0; i < vCount; i++) {
  53.             List<Integer> edges = neighbours(i);
  54.             System.out.print(i + ": ");
  55.             for (int j = 0; j < edges.size(); j++) {
  56.                 System.out.print(edges.get(j) + " ");
  57.             }
  58.             System.out.println();
  59.         }
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement