Guest User

Untitled

a guest
Jun 17th, 2015
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.45 KB | None | 0 0
  1. package com.aids.graph;
  2.  
  3. /**
  4.  * Created by spike994 on 08.05.15.
  5.  */
  6.  
  7. import java.util.LinkedList;
  8. import java.util.List;
  9. import java.util.Random;
  10.  
  11. public class MatrixGraph implements Graph {
  12.  
  13.     private int V;
  14.     private int E;
  15.     public boolean[][] adjacencyMatrix;
  16.     private Random rand = new Random();
  17.  
  18.     /**
  19.      *
  20.      * @param V amount of vertexes
  21.      * @param param density
  22.      */
  23.     public MatrixGraph(int V, double param) {
  24.         this.V = V;
  25.         this.setE((int) (((V * (V - 1)) / 2)*param));
  26.         this.adjacencyMatrix = new boolean[V][V];
  27.         for (int i = 0; i < ((V * (V - 1)) / 2)*param; i++) {
  28.             int v1 = rand.nextInt(V);
  29.             int v2 = rand.nextInt(V);
  30.             if (v1 == v2) {
  31.                 i--;
  32.             } else {
  33.                 if (!adjacencyMatrix[v1][v2]) {
  34.                     adjacencyMatrix[v1][v2] = true;
  35.                     adjacencyMatrix[v2][v1] = true;
  36.                 } else {
  37.                     i--;
  38.                 }
  39.             }
  40.         }
  41.     }
  42.     @Override
  43.     public String toString() {
  44.         StringBuilder sb = new StringBuilder();
  45.         sb.append("\n");
  46.         for (int v = 0; v < V; v++) {
  47.             sb.append("||" + v + "||");
  48.             for (int w = 0; w < V; w++) {
  49.                 if (adjacencyMatrix[v][w]) {
  50.                     sb.append(" [1] ");
  51.                 } else
  52.                     sb.append(" [0] ");
  53.             }
  54.             sb.append("\n");
  55.         }
  56.         return sb.toString();
  57.     }
  58.  
  59.     public Integer[] getAdjacentVertexes(int v) {
  60.         LinkedList<Integer> list = new LinkedList<Integer>();
  61.         for (int i = 0; i < V; i++) {
  62.             if (adjacencyMatrix[v][i]) {
  63.                 list.add(i);
  64.             }
  65.         }
  66.         return list.toArray(new Integer[1]);
  67.     }
  68.  
  69.     public ListGraph toListGraph() {
  70.         @SuppressWarnings("unchecked")
  71.         List<Integer>[] adjacencyList = new List[this.V];
  72.         for (int i = 0; i < this.V; i++) {
  73.             adjacencyList[i] = new LinkedList<Integer>();
  74.             for (int j = 0; j < this.V; j++) {
  75.                 if (adjacencyMatrix[i][j]){
  76.                     adjacencyList[i].add(j);
  77.                 }
  78.             }
  79.         }
  80.         return new ListGraph(this.V, adjacencyList);
  81.     }
  82.     public int getE() {
  83.         return E;
  84.     }
  85.     public void setE(int e) {
  86.         E = e;
  87.     }
  88.     public int getV(){
  89.         return V;
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment