Esempio n. 1
0
  public void createGraph() {

    int N = 100;
    int T = 10;
    int x = 0;
    int y = 0;

    Random rando = new Random();

    ArrayList<String> usedPositions = new ArrayList();

    // Create vertices
    for (int i = 0; i < N; i++) {

      do {
        x = rando.nextInt(100) + 1;
        y = rando.nextInt(100) + 1;
      } while (usedPositions.contains(x + "," + y));

      usedPositions.add(x + "," + y);
      graph.insertVertex(new Node(x, y));
    }

    // Create edges
    // n^2
    for (Graph.Vertex v1 : graph.getVertices()) {

      for (Graph.Vertex v2 : graph.getVertices()) {

        Node n1 = (Node) v1.getElement();
        Node n2 = (Node) v2.getElement();

        if (n1.equals(n2)) {
          continue;
        }

        double distance = Math.sqrt(Math.pow((n1.x - n2.x), 2) + Math.pow((n1.y - n2.y), 2));

        if (distance <= T) {
          if (graph.insertEdge(distance, v1, v2)) {
            // plotter.connectCircles(plotter.getCircles().get(n1.index),
            // plotter.getCircles().get(n2.index));
            plotter.drawLine(n1.x, n1.y, n2.x, n2.y);
            // System.out.println("(" + n1.x + "," + n1.y + ")" + "," + "(" + n2.x + "," + n2.y +
            // ")");
          }
        }
      }
    }
  }
Esempio n. 2
0
 private void populateLists(List<Vertex> verticies, List<Edge> edges) {
   for (Vertex v : graph.getVertices()) {
     verticies.add(v);
   }
   for (Edge e : graph.getEdges()) {
     edges.add(e);
   }
 }
  public boolean findCycle(Graph graph) {
    if (graph.isEmpty()) throw new RuntimeException("Graph is empty.");

    /** Unvisited vertices * */
    List<Integer> whiteSet = new ArrayList<>();
    whiteSet.addAll(graph.getVertices());

    /** Under-process vertices * */
    List<Integer> graySet = new ArrayList<>();

    /** Visited including adjacent vertices & children * */
    List<Integer> blackSet = new ArrayList<>();

    /** Call DSF of white set element * */
    while (!whiteSet.isEmpty())
      /** Call dfs to explore vertex and find loop * */
      if (dfs(whiteSet.get(0), whiteSet, graySet, blackSet, graph)) return true;

    return false;
  }
  /**
   * Helper method for parseQuery. Writes the results of the queries to the output file.
   *
   * @param output initialized BufferedWriter for the output file
   * @param g initialized graph of edges and vertices
   * @param u1 one vertex used in commands
   * @param u2 another vertex used in commands
   * @param command command to execute
   */
  private static void printResults(
      BufferedWriter output, Graph g, Vertex u1, Vertex u2, String command) {
    final String commonInfluencers = "commonInfluencers";
    final String numRetweets = "numRetweets";

    final String VERTEX_NOT_FOUND_ERROR =
        "ERROR: One or more vertices does not exist in the graph.";
    final String INVALID_COMMAND_ERROR = "\tError: invalid command ";
    final String PATH_NOT_FOUND_ERROR = "\tPath not found.";

    List<Vertex> allVertices = new ArrayList<Vertex>(g.getVertices());

    try {
      output.write("query: " + command + " " + u1.toString() + " " + u2.toString());
      output.newLine();
      output.write("<result>");
      output.newLine();

      // check if vertices exist in graph
      if (!allVertices.contains(u1) || !allVertices.contains(u2)) {
        output.write(VERTEX_NOT_FOUND_ERROR);
        output.newLine();
        output.write("</result>");
        output.newLine();
        output.newLine();
        return;
      }
      // if query is commonInfluencers
      if (command.equals(commonInfluencers)) {
        List<Vertex> commonFollowers =
            new LinkedList<Vertex>(Algorithms.commonDownstreamVertices(g, u1, u2));
        for (Vertex v : commonFollowers) {
          output.write("\t" + v.toString());
          output.newLine();
        }
      }

      // if query is numRetweets
      else if (command.equals(numRetweets)) {
        // note switch in u1 and u2; this is because tweets go upstream
        int distance = Algorithms.shortestDistance(g, u2, u1);

        if (distance == -1) {
          output.write(PATH_NOT_FOUND_ERROR);
        } else {
          // implicitly convert distance to string as printing out ints somehow didn't work
          output.write("" + distance);
          // System.out.println(distance);
        }

        output.newLine();
      } else {
        output.write(INVALID_COMMAND_ERROR + command);
        output.newLine();
      }
      output.write("</result>");
      output.newLine();
      output.newLine();
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }