/** * Method testEulerian to check if the given graph is Eulerian or not. A Eulerian graph is * connected and the degree of each vertes is even. They have a cycle, that goes through every * edge of graph only once. A connected graph with exactly two vertices of odd degree has Eulerian * Path. * * @param g: Input graph which is to be tested for satisfying Eulerian conditions */ static int testEulerian(Graph<Vertex> g) { Vertex v1 = null, v2 = null; int conn = dfsUtil(g); if (conn > 1) { System.out.println("The graph is not connected."); System.exit(0); } int odd = 0; for (Vertex u : g) { if (u.Adj.size() % 2 != 0) { odd++; if (odd == 1) { v1 = u; } else if (odd == 2) { v2 = u; } } } if (odd == 0) { return 2; } else if (odd == 2) { System.out.println(v1 + " and " + v2); return 1; } else return 0; }
/** * Method to add an edge to the graph * * @param a : int - one end of edge * @param b : int - other end of edge * @param weight : int - the weight of the edge */ void addEdge(int a, int b, int weight) { Vertex u = verts.get(a); Vertex v = verts.get(b); Edge e = new Edge(u, v, weight); u.Adj.add(e); v.Adj.add(e); }