Advertisement
UniQuet0p1

Untitled

Nov 14th, 2021
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.49 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. /** Container class to different classes, that makes the whole
  4. * set of classes one class formally.
  5. */
  6. public class GraphTask {
  7.  
  8. /** Main method. */
  9. public static void main (String[] args) {
  10. GraphTask a = new GraphTask();
  11. a.run();
  12. throw new RuntimeException ("Nothing implemented yet!"); // delete this
  13. }
  14.  
  15. /** Actual main method to run examples and everything. */
  16. public void run() {
  17. Graph g = new Graph ("G");
  18. g.createRandomSimpleGraph (6, 9);
  19. System.out.println (g);
  20.  
  21. // TODO!!! Your experiments here
  22. }
  23.  
  24. // TODO!!! add javadoc relevant to your problem
  25. class Vertex {
  26.  
  27. private String id;
  28. private Vertex next;
  29. private Arc first;
  30. private int info = 0;
  31. // You can add more fields, if needed
  32.  
  33. Vertex (String s, Vertex v, Arc e) {
  34. id = s;
  35. next = v;
  36. first = e;
  37. }
  38.  
  39. Vertex (String s) {
  40. this (s, null, null);
  41. }
  42.  
  43. @Override
  44. public String toString() {
  45. return id;
  46. }
  47.  
  48. // TODO!!! Your Vertex methods here!
  49. }
  50.  
  51.  
  52. /** Arc represents one arrow in the graph. Two-directional edges are
  53. * represented by two Arc objects (for both directions).
  54. */
  55. class Arc {
  56.  
  57. private String id;
  58. private Vertex target;
  59. private Arc next;
  60. private int info = 0;
  61. // You can add more fields, if needed
  62.  
  63. Arc (String s, Vertex v, Arc a) {
  64. id = s;
  65. target = v;
  66. next = a;
  67. }
  68.  
  69. Arc (String s) {
  70. this (s, null, null);
  71. }
  72.  
  73. @Override
  74. public String toString() {
  75. return id;
  76. }
  77.  
  78. // TODO!!! Your Arc methods here!
  79. }
  80.  
  81.  
  82. class Graph {
  83.  
  84. private String id;
  85. private Vertex first;
  86. private int info = 0;
  87. // You can add more fields, if needed
  88.  
  89. Graph (String s, Vertex v) {
  90. id = s;
  91. first = v;
  92. }
  93.  
  94. Graph (String s) {
  95. this (s, null);
  96. }
  97.  
  98. @Override
  99. public String toString() {
  100. String nl = System.getProperty ("line.separator");
  101. StringBuffer sb = new StringBuffer (nl);
  102. sb.append (id);
  103. sb.append (nl);
  104. Vertex v = first;
  105. while (v != null) {
  106. sb.append (v.toString());
  107. sb.append (" -->");
  108. Arc a = v.first;
  109. while (a != null) {
  110. sb.append (" ");
  111. sb.append (a.toString());
  112. sb.append (" (");
  113. sb.append (v.toString());
  114. sb.append ("->");
  115. sb.append (a.target.toString());
  116. sb.append (")");
  117. a = a.next;
  118. }
  119. sb.append (nl);
  120. v = v.next;
  121. }
  122. return sb.toString();
  123. }
  124.  
  125. public Vertex createVertex (String vid) {
  126. Vertex res = new Vertex (vid);
  127. res.next = first;
  128. first = res;
  129. return res;
  130. }
  131.  
  132. public Arc createArc (String aid, Vertex from, Vertex to) {
  133. Arc res = new Arc (aid);
  134. res.next = from.first;
  135. from.first = res;
  136. res.target = to;
  137. return res;
  138. }
  139.  
  140. /**
  141. * Create a connected undirected random tree with n vertices.
  142. * Each new vertex is connected to some random existing vertex.
  143. * @param n number of vertices added to this graph
  144. */
  145. public void createRandomTree (int n) {
  146. if (n <= 0)
  147. return;
  148. Vertex[] varray = new Vertex [n];
  149. for (int i = 0; i < n; i++) {
  150. varray [i] = createVertex ("v" + String.valueOf(n-i));
  151. if (i > 0) {
  152. int vnr = (int)(Math.random()*i);
  153. createArc ("a" + varray [vnr].toString() + "_"
  154. + varray [i].toString(), varray [vnr], varray [i]);
  155. createArc ("a" + varray [i].toString() + "_"
  156. + varray [vnr].toString(), varray [i], varray [vnr]);
  157. } else {}
  158. }
  159. }
  160.  
  161. /**
  162. * Create an adjacency matrix of this graph.
  163. * Side effect: corrupts info fields in the graph
  164. * @return adjacency matrix
  165. */
  166. public int[][] createAdjMatrix() {
  167. info = 0;
  168. Vertex v = first;
  169. while (v != null) {
  170. v.info = info++;
  171. v = v.next;
  172. }
  173. int[][] res = new int [info][info];
  174. v = first;
  175. while (v != null) {
  176. int i = v.info;
  177. Arc a = v.first;
  178. while (a != null) {
  179. int j = a.target.info;
  180. res [i][j]++;
  181. a = a.next;
  182. }
  183. v = v.next;
  184. }
  185. return res;
  186. }
  187.  
  188. /**
  189. * Create a connected simple (undirected, no loops, no multiple
  190. * arcs) random graph with n vertices and m edges.
  191. * @param n number of vertices
  192. * @param m number of edges
  193. */
  194. public void createRandomSimpleGraph (int n, int m) {
  195. if (n <= 0)
  196. return;
  197. if (n > 2500)
  198. throw new IllegalArgumentException ("Too many vertices: " + n);
  199. if (m < n-1 || m > n*(n-1)/2)
  200. throw new IllegalArgumentException
  201. ("Impossible number of edges: " + m);
  202. first = null;
  203. createRandomTree (n); // n-1 edges created here
  204. Vertex[] vert = new Vertex [n];
  205. Vertex v = first;
  206. int c = 0;
  207. while (v != null) {
  208. vert[c++] = v;
  209. v = v.next;
  210. }
  211. int[][] connected = createAdjMatrix();
  212. int edgeCount = m - n + 1; // remaining edges
  213. while (edgeCount > 0) {
  214. int i = (int)(Math.random()*n); // random source
  215. int j = (int)(Math.random()*n); // random target
  216. if (i==j)
  217. continue; // no loops
  218. if (connected [i][j] != 0 || connected [j][i] != 0)
  219. continue; // no multiple edges
  220. Vertex vi = vert [i];
  221. Vertex vj = vert [j];
  222. createArc ("a" + vi.toString() + "_" + vj.toString(), vi, vj);
  223. connected [i][j] = 1;
  224. createArc ("a" + vj.toString() + "_" + vi.toString(), vj, vi);
  225. connected [j][i] = 1;
  226. edgeCount--; // a new edge happily created
  227. }
  228. }
  229.  
  230. // TODO!!! Your Graph methods here! Probably your solution belongs here.
  231. }
  232.  
  233. }
  234.  
  235.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement