Advertisement
UniQuet0p1

Untitled

Nov 21st, 2021
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.98 KB | None | 0 0
  1. //https://russianblogs.com/article/474724617/
  2. //https://russianblogs.com/article/6212218691/
  3. //https://codeforces.com/blog/entry/2692?locale=ru
  4. //https://www.geeksforgeeks.org/add-and-remove-edge-in-adjacency-list-representation-of-a-graph/
  5. //https://algorithms.tutorialhorizon.com/given-graph-remove-a-vertex-and-all-edges-connect-to-the-vertex/
  6. //https://stackoverflow.com/questions/59045309/is-it-possible-to-also-delete-certain-edges-as-well-in-a-weighted-graph
  7. //https://www.baeldung.com/java-graphs
  8. //https://www.softwaretestinghelp.com/java-graph-tutorial/
  9. //https://www.quora.com/How-do-I-efficiently-delete-an-edge-in-an-undirected-graph
  10. //https://www.freecodecamp.org/news/graph-algorithms-and-data-structures-explained-with-java-and-c-examples/
  11.  
  12. import java.util.ArrayList;
  13. import java.util.Arrays;
  14. import java.util.HashSet;
  15. import java.util.List;
  16.  
  17. /**
  18. * Container class to different classes, that makes the whole
  19. * set of classes one class formally.
  20. */
  21. public class GraphTask {
  22.  
  23. /**
  24. * Main method.
  25. */
  26. public static void main(String[] args) throws Exception {
  27. GraphTask a = new GraphTask();
  28. a.run();
  29. }
  30.  
  31. /**
  32. * Actual main method to run examples and everything.
  33. */
  34. public void run() throws Exception {
  35. testSimpleGraph();
  36. testBiggerGraph();
  37. testRandomGraph();
  38. }
  39.  
  40.  
  41. /**
  42. * @throws Exception if program does not work with small graph
  43. */
  44. public void testSimpleGraph() throws Exception {
  45. Graph g = new Graph("G");
  46.  
  47. Vertex a = new Vertex("A");
  48. Vertex b = new Vertex("B");
  49. Vertex c = new Vertex("C");
  50.  
  51. g.firstVertex = a;
  52. g.firstVertex.nextVertex = b;
  53. g.firstVertex.nextVertex.nextVertex = c;
  54.  
  55. g.info = 3;
  56.  
  57. a.firstArc = new Arc("AB", b, new Arc("AC", c, null));
  58. b.firstArc = new Arc("BA", a, new Arc("BC", c, null));
  59. c.firstArc = new Arc("CA", a, new Arc("CB", b, null));
  60.  
  61. g.simplify();
  62.  
  63. if (a.firstArc.nextArc != null || b.firstArc.nextArc != null || c.firstArc.nextArc != null) {
  64. throw new Exception("Program is not working correctly");
  65. }
  66. }
  67.  
  68. public void testBiggerGraph() throws Exception {
  69. Graph g = new Graph("G");
  70.  
  71. Vertex a = new Vertex("A");
  72. Vertex b = new Vertex("B");
  73. Vertex c = new Vertex("C");
  74. Vertex d = new Vertex("D");
  75.  
  76. g.firstVertex = a;
  77. g.firstVertex.nextVertex = b;
  78. g.firstVertex.nextVertex.nextVertex = c;
  79. g.firstVertex.nextVertex.nextVertex.nextVertex = d;
  80.  
  81. g.info = 4;
  82.  
  83. a.firstArc = new Arc("AB", b, new Arc("AC", c, null));
  84. b.firstArc = new Arc("BA", a, new Arc("BC", c, null));
  85. c.firstArc = new Arc("CA", a, new Arc("CB", b, new Arc("CD", d, null)));
  86. d.firstArc = new Arc("DC", c, null);
  87.  
  88. System.out.println(g);
  89. g.simplify();
  90. System.out.println(g);
  91.  
  92. if (a.firstArc.nextArc != null || b.firstArc.nextArc != null || c.firstArc.nextArc.nextArc != null || d.firstArc == null) {
  93. throw new Exception("Program is not working correctly");
  94. }
  95. }
  96.  
  97.  
  98. /**
  99. * visual test with random graph
  100. */
  101. public void testRandomGraph() {
  102. Graph g = new Graph("G");
  103. g.createRandomSimpleGraph(6, 9);
  104. System.out.println(g.createSimpleMatrix());
  105. System.out.println(g);
  106. g.simplify();
  107. System.out.println(g.createSimpleMatrix());
  108. System.out.println(g);
  109. }
  110.  
  111. class Vertex {
  112.  
  113. private String id;
  114. private Vertex nextVertex;
  115. private Arc firstArc;
  116. private int info = 0;
  117.  
  118. Vertex(String s, Vertex v, Arc e) {
  119. id = s;
  120. nextVertex = v;
  121. firstArc = e;
  122. }
  123.  
  124. Vertex(String s) {
  125. this(s, null, null);
  126. }
  127.  
  128. @Override
  129. public String toString() {
  130. return id;
  131. }
  132. }
  133.  
  134.  
  135. /**
  136. * Arc represents one arrow in the graph. Two-directional edges are
  137. * represented by two Arc objects (for both directions).
  138. */
  139. class Arc {
  140.  
  141. private String id;
  142. private Vertex target;
  143. private Arc nextArc;
  144. private int info = 0;
  145.  
  146. Arc(String s, Vertex v, Arc a) {
  147. id = s;
  148. target = v;
  149. nextArc = a;
  150. }
  151.  
  152. Arc(String s) {
  153. this(s, null, null);
  154. }
  155.  
  156. @Override
  157. public String toString() {
  158. return id;
  159. }
  160. }
  161.  
  162.  
  163. class Graph {
  164.  
  165. private String id;
  166. private Vertex firstVertex;
  167. private int info = 0;
  168.  
  169. Graph(String s, Vertex v) {
  170. id = s;
  171. firstVertex = v;
  172. }
  173.  
  174. Graph(String s) {
  175. this(s, null);
  176. }
  177.  
  178. @Override
  179. public String toString() {
  180. String nl = System.getProperty("line.separator");
  181. StringBuffer sb = new StringBuffer(nl);
  182. sb.append(id);
  183. sb.append(nl);
  184. Vertex v = firstVertex;
  185. while (v != null) {
  186. sb.append(v.toString());
  187. sb.append(" -->");
  188. Arc a = v.firstArc;
  189. while (a != null) {
  190. sb.append(" ");
  191. sb.append(a.toString());
  192. sb.append(" (");
  193. sb.append(v.toString());
  194. sb.append("->");
  195. sb.append(a.target.toString());
  196. sb.append(")");
  197. a = a.nextArc;
  198. }
  199. sb.append(nl);
  200. v = v.nextVertex;
  201. }
  202. return sb.toString();
  203. }
  204.  
  205. public Vertex createVertex(String vid) {
  206. Vertex res = new Vertex(vid);
  207. res.nextVertex = firstVertex;
  208. firstVertex = res;
  209. return res;
  210. }
  211.  
  212. public Arc createArc(String aid, Vertex from, Vertex to) {
  213. Arc res = new Arc(aid);
  214. res.nextArc = from.firstArc;
  215. from.firstArc = res;
  216. res.target = to;
  217. return res;
  218. }
  219.  
  220. /**
  221. * Create a connected undirected random tree with n vertices.
  222. * Each new vertex is connected to some random existing vertex.
  223. *
  224. * @param n number of vertices added to this graph
  225. */
  226. public void createRandomTree(int n) {
  227. if (n <= 0)
  228. return;
  229. Vertex[] varray = new Vertex[n];
  230. for (int i = 0; i < n; i++) {
  231. varray[i] = createVertex("v" + String.valueOf(n - i));
  232. if (i > 0) {
  233. int vnr = (int) (Math.random() * i);
  234. createArc("a" + varray[vnr].toString() + "_"
  235. + varray[i].toString(), varray[vnr], varray[i]);
  236. createArc("a" + varray[i].toString() + "_"
  237. + varray[vnr].toString(), varray[i], varray[vnr]);
  238. }
  239. }
  240. }
  241.  
  242. /**
  243. * Create an adjacency matrix of this graph.
  244. * Side effect: corrupts info fields in the graph
  245. *
  246. * @return adjacency matrix
  247. */
  248. public int[][] createAdjMatrix() {
  249. info = 0;
  250. Vertex v = firstVertex;
  251. while (v != null) {
  252. v.info = info++;
  253. v = v.nextVertex;
  254. }
  255. int[][] res = new int[info][info];
  256. v = firstVertex;
  257. while (v != null) {
  258. int i = v.info;
  259. Arc a = v.firstArc;
  260. while (a != null) {
  261. int j = a.target.info;
  262. res[i][j]++;
  263. a = a.nextArc;
  264. }
  265. v = v.nextVertex;
  266. }
  267. return res;
  268. }
  269.  
  270. /**
  271. * Create a connected simple (undirected, no loops, no multiple
  272. * arcs) random graph with n vertices and m edges.
  273. *
  274. * @param n number of vertices
  275. * @param m number of edges
  276. */
  277. public void createRandomSimpleGraph(int n, int m) {
  278. if (n <= 0)
  279. return;
  280. if (n > 2500)
  281. throw new IllegalArgumentException("Too many vertices: " + n);
  282. if (m < n - 1 || m > n * (n - 1) / 2)
  283. throw new IllegalArgumentException
  284. ("Impossible number of edges: " + m);
  285. firstVertex = null;
  286. createRandomTree(n); // n-1 edges created here
  287. Vertex[] vert = new Vertex[n];
  288. Vertex v = firstVertex;
  289. int c = 0;
  290. while (v != null) {
  291. vert[c++] = v;
  292. v = v.nextVertex;
  293. }
  294. int[][] connected = createAdjMatrix();
  295. int edgeCount = m - n + 1; // remaining edges
  296. while (edgeCount > 0) {
  297. int i = (int) (Math.random() * n); // random source
  298. int j = (int) (Math.random() * n); // random target
  299. if (i == j)
  300. continue; // no loops
  301. if (connected[i][j] != 0 || connected[j][i] != 0)
  302. continue; // no multiple edges
  303. Vertex vi = vert[i];
  304. Vertex vj = vert[j];
  305. createArc("a" + vi.toString() + "_" + vj.toString(), vi, vj);
  306. connected[i][j] = 1;
  307. createArc("a" + vj.toString() + "_" + vi.toString(), vj, vi);
  308. connected[j][i] = 1;
  309. edgeCount--; // a new edge happily created
  310. }
  311. }
  312.  
  313.  
  314. /**
  315. * @return matrix containing directions
  316. */
  317. public String createSimpleMatrix() {
  318. String nl = System.getProperty("line.separator");
  319. StringBuffer sb = new StringBuffer(nl);
  320. sb.append(" ");
  321. int w = 0;
  322. Vertex[] vv = getVertexArray();
  323. Arrays.stream(vv).forEach(a -> {
  324. sb.append(a.toString());
  325. sb.append(" ");
  326. });
  327. sb.append(nl);
  328. Vertex v = firstVertex;
  329. while (v != null) {
  330. sb.append(v.toString());
  331. int i = 0;
  332. while (i != info) {
  333. Arc a = v.firstArc;
  334. sb.append(" ");
  335. while (a != null) {
  336. if (a.target == vv[i]) {
  337. sb.append(1);
  338. break;
  339. }
  340. a = a.nextArc;
  341. }
  342. if (a == null) {
  343. sb.append(0);
  344. }
  345. i += 1;
  346. }
  347. sb.append(nl);
  348. v = v.nextVertex;
  349. }
  350. return sb.toString();
  351. }
  352.  
  353.  
  354. /**
  355. * @return array containing all vertexs in this graph
  356. */
  357. public Vertex[] getVertexArray() {
  358. Vertex vert = firstVertex;
  359. Vertex[] vertArray = new Vertex[info];
  360. int index = 0;
  361. while (vert != null) {
  362. vertArray[index] = vert;
  363. index++;
  364. vert = vert.nextVertex;
  365. }
  366. return vertArray;
  367. }
  368.  
  369.  
  370. /**
  371. * deletes all arcs not mentioned in findMinimumArcs() from this graph.
  372. */
  373. public void simplify() {
  374. List<Arc> arcList = findTheMinimumArcs();
  375. Vertex vert = firstVertex;
  376. Arc a;
  377. Arc b;
  378. while (vert != null) {
  379. a = vert.firstArc;
  380. b = a;
  381. while (a != null) {
  382. if (arcList.contains(a)) {
  383. b = a;
  384. } else {
  385. if (vert.firstArc == a) {
  386. vert.firstArc = a.nextArc;
  387. }
  388. }
  389. b.nextArc = a.nextArc;
  390. a = a.nextArc;
  391. }
  392. vert = vert.nextVertex;
  393. }
  394. }
  395.  
  396.  
  397. /**
  398. * finds the ringway of arcs and vertexs in the graph
  399. *
  400. * @return list of arcs that are essential for the ringway
  401. */
  402. public List<Arc> findTheMinimumArcs() {
  403. List<Arc> arcs = new ArrayList<>();
  404. List<Vertex> vertexList = new ArrayList<>();
  405. HashSet<Vertex> vertexs = new HashSet<>();
  406.  
  407. vertexList.add(firstVertex);
  408. vertexs.add(firstVertex);
  409.  
  410. Arc first = firstVertex.firstArc;
  411. while (vertexs.size() != info || vertexList.get(0) != first.target) {
  412.  
  413. // check if there are already a lot of arcs
  414. if (arcs.size() > info) {
  415.  
  416. // check whether algorithm is looping between two vertexs
  417. if (first == arcs.get(arcs.size() - 2) && first == arcs.get(arcs.size() - 4)) {
  418. if (first.nextArc != null) {
  419. first = first.nextArc;
  420. } else {
  421. vertexList = vertexList.subList(0, vertexList.size() - 1);
  422. while (arcs.get(arcs.size() - 1).nextArc == null) {
  423. vertexList = vertexList.subList(0, vertexList.size() - 1);
  424. arcs = arcs.subList(0, arcs.size() - 1);
  425. }
  426. first = arcs.get(arcs.size() - 1).nextArc;
  427. arcs = arcs.subList(0, arcs.size() - 1);
  428. }
  429. }
  430.  
  431. } else if (!vertexs.contains(first.target) || !vertexList.contains(first.target)) {
  432.  
  433. // if not used vertex found go there
  434. vertexList.add(first.target);
  435. vertexs.add(first.target);
  436. arcs.add(first);
  437. first = first.target.firstArc;
  438. } else if (first.nextArc != null) {
  439.  
  440. // if this vertex is already used, then next arc
  441. first = first.nextArc;
  442. } else if (first == vertexList.get(vertexList.size() - 1).firstArc) {
  443.  
  444. // if this is the only one arc going out of this vertex then use it
  445. vertexList.add(first.target);
  446. vertexs.add(first.target);
  447. arcs.add(first);
  448. first = first.target.firstArc;
  449. } else if (vertexs.size() == info && vertexList.containsAll(vertexs)) {
  450.  
  451. // if we have already been everywhere and now have to find the start point
  452. vertexList.add(first.target);
  453. arcs.add(first);
  454. if (vertexList.get(0) == first.target.firstArc.target) {
  455. vertexList.add(first.target.firstArc.target);
  456. arcs.add(first.target.firstArc);
  457. break;
  458. }
  459. first = first.target.firstArc;
  460. } else {
  461.  
  462. // going some steps back and switching the current arc to next if possible
  463. vertexList = vertexList.subList(0, vertexList.size() - 1);
  464. while (arcs.get(arcs.size() - 1).nextArc == null) {
  465. vertexList = vertexList.subList(0, vertexList.size() - 1);
  466. arcs = arcs.subList(0, arcs.size() - 1);
  467. }
  468. first = arcs.get(arcs.size() - 1).nextArc;
  469. arcs = arcs.subList(0, arcs.size() - 1);
  470. }
  471.  
  472. // if all the vertexs are used and now we can finish the loop
  473. if (vertexList.get(0) == first.target && vertexList.containsAll(vertexs) && vertexs.size() == info) {
  474. vertexList.add(first.target);
  475. arcs.add(first);
  476. break;
  477. }
  478. }
  479. return arcs;
  480. }
  481. }
  482. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement