use of chapter3.section5.HashSet in project algorithms-sedgewick-wayne by reneargento.
the class Exercise51_RealWorldDAG method randomRealDAG.
private Digraph randomRealDAG(int randomVerticesToChoose, int randomEdgesToChoose) {
String filePath = Constants.FILES_PATH + Constants.CITATION_DIGRAPH_FILE;
String separator = " ";
In in = new In(filePath);
Digraph fullDigraph = new Digraph();
SeparateChainingHashTable<Integer, Integer> realDAGToFullDAGMap = new SeparateChainingHashTable<>();
while (in.hasNextLine()) {
String[] connection = in.readLine().split(separator);
int paper1 = Integer.parseInt(connection[0]);
int paper2 = Integer.parseInt(connection[1]);
if (paper1 == paper2 || paper1 > paper2) {
// and maintain the digraph as a DAG
continue;
}
if (!realDAGToFullDAGMap.contains(paper1)) {
int paperVertex1Id = realDAGToFullDAGMap.size();
realDAGToFullDAGMap.put(paper1, paperVertex1Id);
}
if (!realDAGToFullDAGMap.contains(paper2)) {
int paperVertex2Id = realDAGToFullDAGMap.size();
realDAGToFullDAGMap.put(paper2, paperVertex2Id);
}
int paperVertex1Id = realDAGToFullDAGMap.get(paper1);
int paperVertex2Id = realDAGToFullDAGMap.get(paper2);
fullDigraph.addEdge(paperVertex1Id, paperVertex2Id);
}
DirectedCycle directedCycle = new DirectedCycle(fullDigraph);
if (directedCycle.hasCycle()) {
throw new IllegalArgumentException("Digraph is not a DAG");
}
Digraph randomSubDigraph = new Digraph();
SeparateChainingHashTable<Integer, Integer> digraphToSubDigraphMap = new SeparateChainingHashTable<>();
List<DirectedEdge> allSubDigraphEdges = new ArrayList<>();
for (int vertex = 0; vertex < randomVerticesToChoose; vertex++) {
// Randomly choose a vertex between 1 and vertices
int randomVertexId = StdRandom.uniform(fullDigraph.vertices) + 1;
if (digraphToSubDigraphMap.contains(randomVertexId)) {
continue;
}
int subDigraphVertexId = digraphToSubDigraphMap.size();
digraphToSubDigraphMap.put(randomVertexId, subDigraphVertexId);
randomSubDigraph.addVertex(subDigraphVertexId);
for (int neighbor : fullDigraph.adjacent(randomVertexId)) {
allSubDigraphEdges.add(new DirectedEdge(subDigraphVertexId, neighbor));
}
}
// Randomly choose E directed edges from the subdigraph induced by the random vertices
if (randomEdgesToChoose > allSubDigraphEdges.size()) {
throw new IllegalArgumentException("Not enough edges to choose");
}
DirectedEdge[] allSubDigraphEdgesArray = new DirectedEdge[allSubDigraphEdges.size()];
int allSubDigraphEdgesArrayIndex = 0;
HashSet<Integer> edgesChosen = new HashSet<>();
for (DirectedEdge directedEdge : allSubDigraphEdges) {
allSubDigraphEdgesArray[allSubDigraphEdgesArrayIndex++] = directedEdge;
}
for (int edge = 0; edge < randomEdgesToChoose; edge++) {
// Randomly choose an edge
int randomEdgeId = StdRandom.uniform(allSubDigraphEdgesArray.length);
if (edgesChosen.contains(randomEdgeId)) {
continue;
}
edgesChosen.add(randomEdgeId);
DirectedEdge randomEdge = allSubDigraphEdgesArray[randomEdgeId];
if (!digraphToSubDigraphMap.contains(randomEdge.toVertex)) {
int subDigraphNeighborVertexId = digraphToSubDigraphMap.size();
digraphToSubDigraphMap.put(randomEdge.toVertex, subDigraphNeighborVertexId);
}
int subDigraphNeighborVertexId = digraphToSubDigraphMap.get(randomEdge.toVertex);
randomSubDigraph.addEdge(randomEdge.fromVertex, subDigraphNeighborVertexId);
}
return randomSubDigraph;
}
Aggregations