/** * Compute the unique decomposition of the input graph G (atoms of G). Implementation of algorithm * Atoms as described in Berry et al. (2010), DOI:10.3390/a3020197, <a * href="http://www.mdpi.com/1999-4893/3/2/197">http://www.mdpi.com/1999-4893/3/2/197</a> */ private void computeAtoms() { if (chordalGraph == null) { computeMinimalTriangulation(); } separators = new HashSet<>(); // initialize g' as subgraph of graph (same vertices and edges) UndirectedGraph<V, E> gprime = copyAsSimpleGraph(graph); // initialize h' as subgraph of chordalGraph (same vertices and edges) UndirectedGraph<V, E> hprime = copyAsSimpleGraph(chordalGraph); atoms = new HashSet<>(); Iterator<V> iterator = meo.descendingIterator(); while (iterator.hasNext()) { V v = iterator.next(); if (generators.contains(v)) { Set<V> separator = new HashSet<>(Graphs.neighborListOf(hprime, v)); if (isClique(graph, separator)) { if (separator.size() > 0) { if (separators.contains(separator)) { fullComponentCount.put(separator, fullComponentCount.get(separator) + 1); } else { fullComponentCount.put(separator, 2); separators.add(separator); } } UndirectedGraph<V, E> tmpGraph = copyAsSimpleGraph(gprime); tmpGraph.removeAllVertices(separator); ConnectivityInspector<V, E> con = new ConnectivityInspector<>(tmpGraph); if (con.isGraphConnected()) { throw new RuntimeException("separator did not separate the graph"); } for (Set<V> component : con.connectedSets()) { if (component.contains(v)) { gprime.removeAllVertices(component); component.addAll(separator); atoms.add(new HashSet<>(component)); assert (component.size() > 0); break; } } } } hprime.removeVertex(v); } if (gprime.vertexSet().size() > 0) { atoms.add(new HashSet<>(gprime.vertexSet())); } }
/** * This method will check whether the graph passed in is Eulerian or not. * * @param g The graph to be checked * @return true for Eulerian and false for non-Eulerian */ public static <V, E> boolean isEulerian(UndirectedGraph<V, E> g) { // If the graph is not connected, then no Eulerian circuit exists if (!(new ConnectivityInspector<V, E>(g)).isGraphConnected()) { return false; } // A graph is Eulerian if and only if all vertices have even degree // So, this code will check for that Iterator<V> iter = g.vertexSet().iterator(); while (iter.hasNext()) { V v = iter.next(); if ((g.degreeOf(v) % 2) == 1) { return false; } } return true; }
/** * This method will return a list of vertices which represents the Eulerian circuit of the graph. * * @param g The graph to find an Eulerian circuit * @return null if no Eulerian circuit exists, or a list of vertices representing the Eulerian * circuit if one does exist */ public static <V, E> List<V> getEulerianCircuitVertices(UndirectedGraph<V, E> g) { // If the graph is not Eulerian then just return a null since no // Eulerian circuit exists if (!isEulerian(g)) { return null; } // The circuit will be represented by a linked list List<V> path = new LinkedList<V>(); UndirectedGraph<V, E> sg = new UndirectedSubgraph<V, E>(g, null, null); path.add(sg.vertexSet().iterator().next()); // Algorithm for finding an Eulerian circuit Basically this will find an // arbitrary circuit, then it will find another arbitrary circuit until // every edge has been traversed while (sg.edgeSet().size() > 0) { V v = null; // Find a vertex which has an edge that hasn't been traversed yet, // and keep its index position in the circuit list int index = 0; for (Iterator<V> iter = path.iterator(); iter.hasNext(); index++) { v = iter.next(); if (sg.degreeOf(v) > 0) { break; } } // Finds an arbitrary circuit of the current vertex and // appends this into the circuit list while (sg.degreeOf(v) > 0) { for (Iterator<V> iter = sg.vertexSet().iterator(); iter.hasNext(); ) { V temp = iter.next(); if (sg.containsEdge(v, temp)) { path.add(index, temp); sg.removeEdge(v, temp); v = temp; break; } } } } return path; }