Пример #1
0
  /**
   * @param vertexValues is the list of vertex values.
   * @param adjacencyMatrix is the adjacency matrix with the connections from the graph.
   * @param vertexIndex is the index of the vertex value to build the connection list.
   * @return a list of vertex values based on the connectivity of the given index on the adjacency
   *     matrix.
   */
  private static List<String> makeConnections(
      String[] vertexValues, boolean[][] adjacencyMatrix, int vertexIndex) {
    Preconditions.checkArgument(vertexValues != null, "The vertex values must be provided.");
    Preconditions.checkArgument(adjacencyMatrix != null, "The adjacency matrix must be provided.");
    Preconditions.checkArgument(
        vertexIndex >= 0, "The vertex index must be greater than or equals to 0.");

    List<String> connections = new SinglyLinkedList<>();
    for (int i = 0; i < adjacencyMatrix.length; i++) {
      if (adjacencyMatrix[vertexIndex][i]) {
        connections.add(vertexValues[i]);
      }
    }
    return connections;
  }
Пример #2
0
 @Override
 public String toString() {
   StringBuilder builder = new StringBuilder();
   for (int i = 0; i < vertexes.length; i++) {
     if (i == rootIndex) {
       builder.append("-> ");
     }
     builder.append(vertexes[i].getValue());
     builder.append(": ");
     List<Type> connections = vertexes[i].getConnections();
     Iterator<Type> it = connections.iterator();
     while (it.hasNext()) {
       Type value = it.next();
       builder.append(value);
       if (it.hasNext()) {
         builder.append(", ");
       }
     }
     builder.append("\n");
   }
   return builder.toString();
 }