Advertisement
UniQuet0p1

Untitled

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