Ejemplo n.º 1
0
 public Graph generate() {
   Timer timer = new Timer();
   Random rand = new Random(System.currentTimeMillis());
   NodeImpl[] nodes = NodeImpl.init(this.nodes());
   double p = (double) this.EDGES / (double) (this.nodes() * this.nodes());
   if (this.BIDIRECTIONAL) {
     p /= 2;
   }
   Edges edges = new Edges(nodes, (int) (this.EDGES * 1.05));
   for (int i = 0; i < nodes.length; i++) {
     for (int j = 0; j < nodes.length; j++) {
       if (i != j) {
         if (rand.nextDouble() < p) {
           if (this.BIDIRECTIONAL) {
             edges.add(nodes[i], nodes[j]);
             edges.add(nodes[j], nodes[i]);
           } else {
             edges.add(nodes[i], nodes[j]);
           }
         }
       }
     }
   }
   edges.fill();
   timer.end();
   return new Graph(this.description(), nodes, timer);
 }
  /*
   * (non-Javadoc)
   *
   * @see gtna.io.graphReader.GraphReader#read(java.lang.String)
   */
  @Override
  public Graph read(String filename) {
    String sep1 = Config.get("SNAP_SEPARATOR_1");
    String ffd = Config.get("FILESYSTEM_FOLDER_DELIMITER");
    SNAPFileReader fr = new SNAPFileReader(filename);
    try {
      String line = null;
      String name = filename.substring(filename.lastIndexOf(ffd) + 1);

      Graph graph = new Graph(name);

      Map<Integer, Integer> idMapping = new HashMap<Integer, Integer>();
      int nodecounter = 0;

      LinkedList<String> edges = new LinkedList<String>();

      graphtype type = null;

      line = fr.readLine();
      type = (line.contains("Directed graph")) ? graphtype.DIRECTED : graphtype.UNDIRECTED;

      while (!line.contains("FromNodeId") && !line.contains("ToNodeId")) {
        line = fr.readLine();
      }
      nodecounter = parseEdges(sep1, fr, idMapping, nodecounter, edges, type);

      Node[] nodes = Node.init(idMapping.size(), graph);
      Edges e = new Edges(nodes, edges.size());
      ListIterator<String> edgeIterator = edges.listIterator();
      while (edgeIterator.hasNext()) {
        String[] pair = edgeIterator.next().split("-");
        e.add(Integer.parseInt(pair[0]), Integer.parseInt(pair[1]));
      }

      e.fill();
      graph.setNodes(nodes);
      return graph;
    } catch (Exception e) {
      return null;
    } finally {
      fr.close();
    }
  }
Ejemplo n.º 3
0
  @Override
  public Graph read(String filename) {
    String delimiter = Config.get("GRAPH_WRITER_DELIMITER");
    Filereader fr = new Filereader(filename);

    String name = fr.readLine();
    int N = Integer.parseInt(fr.readLine());
    int E = Integer.parseInt(fr.readLine());

    Graph graph = new Graph(name);
    Node[] nodes = Node.init(N, graph);
    Edges edges = new Edges(nodes, E);

    String line = null;
    while ((line = fr.readLine()) != null) {
      String[] temp = line.split(delimiter);
      edges.add(Integer.parseInt(temp[0]), Integer.parseInt(temp[1]));
    }
    edges.fill();
    graph.setNodes(nodes);
    fr.close();
    return graph;
  }
Ejemplo n.º 4
0
  public Graph generate() {
    Timer timer = new Timer();
    Random rand = new Random(System.currentTimeMillis());

    int numberOfDBNodes = DeBruijn.numberOfNodes(this.BASE, this.DIMENSIONS);
    DBNode[] dbNodes = new DBNode[numberOfDBNodes];
    for (int i = 0; i < dbNodes.length; i++) {
      dbNodes[i] = new DBNode(i);
    }
    Edges dbEdges = new Edges(dbNodes, dbNodes.length * this.BASE);
    for (int i = 0; i < dbNodes.length; i++) {
      int shiftedId = (i * this.BASE) % dbNodes.length;
      for (int j = 0; j < this.BASE; j++) {
        if (i != shiftedId) {
          dbEdges.add(dbNodes[i], dbNodes[shiftedId]);
        }
        shiftedId++;
      }
    }
    dbEdges.fill();

    ODRINode[] nodes = new ODRINode[this.nodes()];
    nodes[0] = new ODRINode(0, dbNodes);
    for (int i = 1; i < nodes.length; i++) {
      while (true) {
        ODRINode bootstrap = nodes[rand.nextInt(i)];
        if (i > 100) {
          bootstrap = this.getBootstrap(rand, nodes, i);
        }
        if (bootstrap.dbNodes.length >= 2) {
          DBNode[] handOver = bootstrap.handOver();
          nodes[i] = new ODRINode(i, handOver);
          // int width = bootstrap.end - bootstrap.start;
          // if ((width % 2) == 0) {
          // int start = bootstrap.start + width / 2;
          // int end = bootstrap.end;
          // bootstrap.end = start - 1;
          // nodes[i] = new ODRINode(i, start, end);
          // } else {
          // int start = bootstrap.start + (width + 1) / 2;
          // int end = bootstrap.end;
          // bootstrap.end = start - 1;
          // nodes[i] = new ODRINode(i, start, end);
          // }
          break;
        }
      }
    }

    Edges edges = new Edges(nodes, dbNodes.length * this.BASE);
    for (int i = 0; i < dbNodes.length; i++) {
      Node[] out = dbNodes[i].out();
      for (int j = 0; j < out.length; j++) {
        edges.add(dbNodes[i].odri, ((DBNode) out[j]).odri);
      }
    }
    edges.fill();

    timer.end();
    Graph g = new Graph(this.description(), nodes, timer);
    return g;
  }
Ejemplo n.º 5
0
  public static Graph read(String filename) throws Exception {
    Timer timer = new Timer();

    RandomAccessFile file = new RandomAccessFile(new File(filename), "r");

    // seek to names
    while (!file.readLine().startsWith("names/")) {}

    // read names
    Vector<WOTNode> wotNodes = new Vector<WOTNode>();
    int index = 0;
    String line;
    while (!(line = file.readLine()).startsWith("keys/")) {
      if (!line.equals("")) {
        WOTNode node = new WOTNode(index, line);
        wotNodes.add(node);
        index += 1;
      }
    }

    // read keys
    for (index = 0; index < wotNodes.size(); index++) {
      byte[] key = new byte[4];
      file.read(key);
      WOTNode node = wotNodes.get(index);
      node.key = Util.toInt(key);
    }

    // skip the "signatures/" line
    String signaturesLine = file.readLine();
    if (!signaturesLine.startsWith("signatures/")) {
      // FIXME an IllegalArgumentException isn't really semantically
      // correct
      throw new IllegalArgumentException("A line starting with \"signatures/\" was expected.");
    }

    // read signatures
    for (index = 0; index < wotNodes.size(); index++) {
      WOTNode node = wotNodes.get(index);

      // read how many keys are signed by this key
      byte[] buffer = new byte[4];
      file.read(buffer);
      int signatureCount = Util.toInt(buffer);

      // inspect all signatures
      for (int signatureIndex = 0; signatureIndex < signatureCount; signatureIndex++) {
        byte[] signatureBuffer = new byte[4];
        file.read(signatureBuffer);

        int signature = Util.toInt(signatureBuffer);

        // get the signature type
        // TODO do something with the signature type
        // int signatureType = signature & SIGNATURE_TYPE_MASK;

        // get the target key
        int targetIndex = signature & SIGNATURE_INDEX_MASK;
        if (targetIndex >= wotNodes.size()) {
          // FIXME an IllegalArgumentException isn't really
          // semantically correct
          throw new IllegalArgumentException("A target index that matches a node was expected.");
        }

        // retrieve the target node
        WOTNode target = wotNodes.get(targetIndex);
        node.signatures.add(target);
      }
    }
    // skip the "debug/" line
    String debugLine = file.readLine();
    if (!debugLine.startsWith("debug/")) {
      // FIXME an IllegalArgumentException isn't really semantically
      // correct
      throw new IllegalArgumentException("A line starting with \"debug/was expected.");
    }

    file.close();
    timer.end();

    // prepare return graph
    Graph graph = new Graph("WOT - read from " + filename);
    Node[] nodes = Node.init(wotNodes.size(), graph);
    Edges edges = new Edges(nodes, nodes.length);
    for (WOTNode source : wotNodes) {
      for (WOTNode target : source.signatures) {
        edges.add(source.index, target.index);
      }
    }
    edges.fill();
    graph.setNodes(nodes);
    return graph;
  }