AdjacencyMatrixJob extends AbstractJob { @Override public int run(String[] args) throws Exception { addOption("vertices", null, "a text file containing all vertices of the graph (one per line)", true); addOption("edges", null, "text files containing the edges of the graph (vertexA,vertexB per line)", true); addOption("symmetric", null, "produce a symmetric adjacency matrix (corresponds to an undirected graph)", String.valueOf(false)); addOutputOption(); Map<String, String> parsedArgs = parseArguments(args); if (parsedArgs == null) { return -1; } Path vertices = new Path(parsedArgs.get("--vertices")); Path edges = new Path(parsedArgs.get("--edges")); boolean symmetric = Boolean.parseBoolean(parsedArgs.get("--symmetric")); log.info("Indexing vertices sequentially, this might take a while..."); int numVertices = indexVertices(vertices, getOutputPath(VERTEX_INDEX)); HadoopUtil.writeInt(numVertices, getOutputPath(NUM_VERTICES), getConf()); Preconditions.checkArgument(numVertices > 0); log.info("Found " + numVertices + " vertices, creating adjacency matrix..."); Job createAdjacencyMatrix = prepareJob(edges, getOutputPath(ADJACENCY_MATRIX), TextInputFormat.class, VectorizeEdgesMapper.class, IntWritable.class, VectorWritable.class, VectorSumReducer.class, IntWritable.class, VectorWritable.class, SequenceFileOutputFormat.class); createAdjacencyMatrix.setCombinerClass(VectorSumReducer.class); Configuration createAdjacencyMatrixConf = createAdjacencyMatrix.getConfiguration(); createAdjacencyMatrixConf.set(NUM_VERTICES_PARAM, String.valueOf(numVertices)); createAdjacencyMatrixConf.set(VERTEX_INDEX_PARAM, getOutputPath(VERTEX_INDEX).toString()); createAdjacencyMatrixConf.setBoolean(SYMMETRIC_PARAM, symmetric); createAdjacencyMatrix.waitForCompletion(true); return 0; }  static void main(String[] args); @Override int run(String[] args); static final String NUM_VERTICES; static final String ADJACENCY_MATRIX; static final String VERTEX_INDEX; }
@Test public void adjacencyMatrix() throws Exception { File verticesFile = getTestTempFile("vertices.txt"); File edgesFile = getTestTempFile("edges.txt"); File outputDir = getTestTempDir("output"); outputDir.delete(); Configuration conf = new Configuration(); writeLines(verticesFile, "12", "34", "56", "78"); writeLines(edgesFile, "12,34", "12,56", "34,34", "34,78", "56,12", "56,34", "56,56", "56,78", "78,34"); Matrix expectedAdjacencyMatrix = new DenseMatrix(new double[][] { { 0, 1, 1, 0 }, { 0, 1, 0, 1 }, { 1, 1, 1, 1 }, { 0, 1, 0, 0 } }); AdjacencyMatrixJob createAdjacencyMatrix = new AdjacencyMatrixJob(); createAdjacencyMatrix.setConf(conf); createAdjacencyMatrix.run(new String[] { "--vertices", verticesFile.getAbsolutePath(), "--edges", edgesFile.getAbsolutePath(), "--output", outputDir.getAbsolutePath() }); int numVertices = HadoopUtil.readInt(new Path(outputDir.getAbsolutePath(), AdjacencyMatrixJob.NUM_VERTICES), conf); Matrix actualAdjacencyMatrix = MathHelper.readMatrix(conf, new Path(outputDir.getAbsolutePath(), AdjacencyMatrixJob.ADJACENCY_MATRIX + "/part-r-00000"), numVertices, numVertices); StringBuilder info = new StringBuilder(); info.append("expected adjacency matrix:\n"); info.append(MathHelper.nice(expectedAdjacencyMatrix)); info.append("actual adjacency matrix:\n"); info.append(MathHelper.nice(actualAdjacencyMatrix)); log.info(info.toString()); MathHelper.assertMatrixEquals(expectedAdjacencyMatrix, actualAdjacencyMatrix); }
@Test public void symmetricAdjacencyMatrix() throws Exception { File verticesFile = getTestTempFile("vertices.txt"); File edgesFile = getTestTempFile("edges.txt"); File outputDir = getTestTempDir("output"); outputDir.delete(); Configuration conf = new Configuration(); writeLines(verticesFile, "12", "34", "56", "78"); writeLines(edgesFile, "12,34", "12,56", "34,34", "34,78", "56,34", "56,56", "56,78"); Matrix expectedAdjacencyMatrix = new DenseMatrix(new double[][] { { 0, 1, 1, 0 }, { 1, 1, 1, 1 }, { 1, 1, 1, 1 }, { 0, 1, 1, 0 } }); AdjacencyMatrixJob createAdjacencyMatrix = new AdjacencyMatrixJob(); createAdjacencyMatrix.setConf(conf); createAdjacencyMatrix.run(new String[] { "--vertices", verticesFile.getAbsolutePath(), "--edges", edgesFile.getAbsolutePath(), "--symmetric", String.valueOf(true), "--output", outputDir.getAbsolutePath() }); int numVertices = HadoopUtil.readInt(new Path(outputDir.getAbsolutePath(), AdjacencyMatrixJob.NUM_VERTICES), conf); Matrix actualAdjacencyMatrix = MathHelper.readMatrix(conf, new Path(outputDir.getAbsolutePath(), AdjacencyMatrixJob.ADJACENCY_MATRIX + "/part-r-00000"), numVertices, numVertices); StringBuilder info = new StringBuilder(); info.append("expected adjacency matrix:\n"); info.append(MathHelper.nice(expectedAdjacencyMatrix)); info.append("actual adjacency matrix:\n"); info.append(MathHelper.nice(actualAdjacencyMatrix)); log.info(info.toString()); MathHelper.assertMatrixEquals(expectedAdjacencyMatrix, actualAdjacencyMatrix); }