def main()

in cassovary-examples/src/main/scala/RenumberedGraph.scala [30:81]


  def main(args: Array[String]) {
    val numNodes = if (args.length > 0) args(0).toInt else 50
    val avgOutDegree = math.ceil(math.log(numNodes)).toInt
    val MaxNodeId = 1 << 20

    printf("Generating Erdos-Renyi random graph with n=%d nodes and log(n)=%d avg outdegree...\n", numNodes, avgOutDegree)
    // Generate mapping of each node id to a random integer in the space 0..MaxNodeId.
    val rng = new Random()
    val nodeIds = Sampling.randomSubset(numNodes, 0 to MaxNodeId, rng)

    val genGraph = TestGraphs.generateRandomGraph(numNodes,
      TestGraphs.getProbEdgeRandomDirected(numNodes, avgOutDegree))

    // Write graph to temporary file, mapping all node ids from dense to sparse representation.
    val renumGraphDirName = System.getProperty("java.io.tmpdir")
    val renumGraphFileName = "erdos_renyi_" + numNodes + ".txt"
    val renumGraphFile = new File(renumGraphDirName, renumGraphFileName)
    printf("Writing graph to temporary file %s with node ids distributed across 0..%d\n", renumGraphFile, MaxNodeId)
    val gWriter = new PrintWriter(renumGraphFile)
    genGraph.foreach { v => {
        gWriter.println(nodeIds(v.id) + " " + v.outboundCount)
        v.outboundNodes().foreach { ngh =>
          gWriter.println(nodeIds(ngh))
        }
      }
    }
    gWriter.close()

    // Read graph file into memory with renumbering.
    val readGraph = new AdjacencyListGraphReader[Int](renumGraphDirName, renumGraphFileName,
      new SequentialNodeNumberer[Int](), ParseString.toInt).toArrayBasedDirectedGraph()

    val rgComplexity = readGraph.approxStorageComplexity
    printf("A renumbered graph with %d nodes (min id: %d, max id: %d) and %d directed edges has an approx. storage complexity of %d bytes.\n",
      readGraph.nodeCount, readGraph.map{_.id}.min, readGraph.map{_.id}.max, readGraph.edgeCount, rgComplexity)
    printf("First 3 nodes of renumbered graph: %s\n", readGraph.toString(3))

    // Read graph file into memory without renumbering.
    val readGraph2 = new AdjacencyListGraphReader[Int](renumGraphDirName, renumGraphFileName,
      new SequentialNodeNumberer[Int](), _.toInt).toArrayBasedDirectedGraph()
    val rg2Complexity = readGraph2.approxStorageComplexity
    printf("An unrenumbered graph with %d nodes (min id: %d, max id: %d) and %d directed edges has an approx. storage complexity of %d bytes.\n",
      readGraph2.nodeCount, readGraph2.map{_.id}.min, readGraph2.map{_.id}.max, readGraph2.edgeCount, rg2Complexity)

    renumGraphFile.delete()

    val complexityDiff = rg2Complexity - rgComplexity
    printf("Savings for %d node graph with MaxNodeId %d: %d bytes (%.2f%%)\n", numNodes, MaxNodeId, complexityDiff, 100.0 * complexityDiff.toFloat / rgComplexity)

    printf("Finished running RenumberedGraph example.\n")

  }