private void initAdjacencyCountArray(int maxAdjCount) { adjCounts = new ArrayList[maxAdjCount + 1]; for (CoverVertex<L, R> qv : qVerticesMap.values()) { List<String> qvWithAdjCount = adjCounts[qv.getAdjacentCount()]; if (qvWithAdjCount == null) { qvWithAdjCount = new ArrayList<String>(); adjCounts[qv.getAdjacentCount()] = qvWithAdjCount; } qvWithAdjCount.add(qv.getVertex().getLabel()); } }
private void removeAdjacentVertx(CoverVertex<L, R> qv, String adjLabel) { int oldAdjCount = qv.getAdjacentCount(); qv.removeAdjLabel(adjLabel); // adjacency count has changed, remove it from the original bin this.adjCounts[oldAdjCount].remove(qv.getVertex().getLabel()); // if no adjacent vertices are left, remove this vertex if (qv.getAdjacentCount() == 0) { qVerticesMap.remove(qv.getVertex().getLabel()); return; } // put this in the appropriate adjacency count bin List<String> adjList = this.adjCounts[qv.getAdjacentCount()]; // the bin might not exist yet, we my need to create a new one. if (adjList == null) { adjList = new ArrayList<String>(); adjCounts[qv.getAdjacentCount()] = adjList; } adjList.add(qv.getVertex().getLabel()); }
private void addVerticesWithUniqAdjacent(List<L> setCover) { Iterator<CoverVertex<L, R>> iter = qVerticesMap.values().iterator(); while (iter.hasNext()) { CoverVertex<L, R> qv = iter.next(); if (qv.getUniqAdjacentcount() > 0) { setCover.add(qv.getVertex()); } } // remove the vertices already added (and their adjacent vertices) from the graph for (L v : setCover) { CoverVertex<L, R> qv = qVerticesMap.get(v.getLabel()); // get all the labels of all vertices adjacent to this vertex // and remove them from the graph. for (String rLabel : qv.getAdjRLabels()) { Set<String> lLabels = rToLMap.get(rLabel); // other L vertices adjacent to this R vertex for (String label : lLabels) { CoverVertex<L, R> qvl = qVerticesMap.get(label); if (qvl == qv) continue; removeAdjacentVertx(qvl, rLabel); } } } // remove these vertices from the adjacency count list // This should be done AFTER the previous step (removing all R vertices for vertices in set // cover // and updating L vertices that matched to them). for (L v : setCover) { CoverVertex<L, R> qv = qVerticesMap.get(v.getLabel()); List<String> qvWithAdjCount = adjCounts[qv.getAdjacentCount()]; Iterator<String> iterL = qvWithAdjCount.iterator(); boolean removed = false; while (iterL.hasNext()) { String label = iterL.next(); if (label.equals(qv.getVertex().getLabel())) { iterL.remove(); removed = true; break; } } if (!removed) throw new IllegalArgumentException("Vertex not removed; " + qv.getVertex().getLabel()); } }