Esempio n. 1
0
  @Override
  public int checkFitness(
      Graph<ColorableVertex, ?> graph,
      List<ColorableVertex> gVertices,
      Colony<ColorableVertex> colony) {
    Collections.sort(
        colony,
        new Comparator<ColorableVertex>() {
          @Override
          public int compare(ColorableVertex o1, ColorableVertex o2) {
            return o1.getId() - o2.getId();
          }
        });

    for (int i = 0; i < gVertices.size(); i++) {
      if (gVertices.get(i).getId() != colony.get(i).getId())
        throw new IllegalStateException("DUPA");
      gVertices.get(i).setColor(colony.get(i).getColor());
    }

    // Sprawdzenie czy rozwiązanie jest poprawne
    int bad = 0;
    for (ColorableVertex v : graph.getVertices()) {
      for (ColorableVertex c : graph.getNeighbors(v)) {
        if ((c.getColor() == v.getColor()) && (c != v)) bad++;
      }
    }

    return bad;
  }
Esempio n. 2
0
  private void getCandidateDomVerMap() throws ExceedLongMaxException {
    candidateDomVerMap = new LinkedHashMap<String, List<Integer>>();

    // the vertex cover's complement set will be an independent set
    Collection<Integer> vertices = g.getVertices();
    Collection<Integer> independentSet = CollectionUtils.subtract(vertices, vertexCover);

    /*
     * the vertex cover size will be the parameter k used in the fpt
     * algorithm
     */
    int vertexCoverSize = vertexCover.size();
    byte[] comparedRuler = new byte[vertexCoverSize];
    Arrays.fill(comparedRuler, AlgorithmUtil.MARKED);

    for (Integer isv : independentSet) {
      Collection<Integer> neighOfIsv = g.getNeighbors(isv);
      byte ruler[] = new byte[vertexCoverSize];
      // initilze the array with 0
      Arrays.fill(ruler, AlgorithmUtil.UNMARKED);
      for (Integer neig : neighOfIsv) {
        /*
         * the position of the dominate vertex in the vertex cover will
         * set 1
         */
        int pos = vertexCover.indexOf(neig);
        if (pos != -1) {
          ruler[pos] = AlgorithmUtil.MARKED;
        }
      }

      // if (Arrays.equals(comparedRuler, ruler)) {
      // log.debug(isv + " dominates all vc");
      // }

      String rulerStr = AlgorithmUtil.arrayToString(ruler);

      List<Integer> candidateDomVerSet = candidateDomVerMap.get(rulerStr);

      if (candidateDomVerSet == null) {
        candidateDomVerSet = new ArrayList<Integer>();
      }
      // candidateDomVerSet.add(isv);
      AlgorithmUtil.addElementToList(candidateDomVerSet, isv);
      candidateDomVerMap.put(rulerStr, candidateDomVerSet);
    }

    /*
     * because the vertices in the vertex cover can also dominate other
     * vertices in the vertex cover,they are also considered
     */
    for (Integer vcv : vertexCover) {
      Collection<Integer> neighOfVcv = g.getNeighbors(vcv);

      byte ruler[] = new byte[vertexCoverSize];
      Arrays.fill(ruler, AlgorithmUtil.UNMARKED);

      // the vertex can dominated by itself
      int pos = vertexCover.indexOf(vcv);
      ruler[pos] = AlgorithmUtil.MARKED;

      for (Integer neig : neighOfVcv) {
        /*
         * the position of the dominate vertex in the vertex cover will
         * set 1
         *
         * a neighbour of the vertex is likely not to be in the vertex
         * cover
         */
        pos = vertexCover.indexOf(neig);
        if (pos != -1) {
          ruler[pos] = AlgorithmUtil.MARKED;
        }
      }

      // if (Arrays.equals(comparedRuler, ruler)) {
      // log.debug(vcv + " dominates all vc");
      // }
      String rulerStr = AlgorithmUtil.arrayToString(ruler);

      List<Integer> candidateDomVerSet = candidateDomVerMap.get(rulerStr);

      if (candidateDomVerSet == null) {
        candidateDomVerSet = new ArrayList<Integer>();
      }
      // candidateDomVerSet.add(vcv);
      AlgorithmUtil.addElementToList(candidateDomVerSet, vcv);
      candidateDomVerMap.put(rulerStr, candidateDomVerSet);
    }

    applySubsetRule(candidateDomVerMap);
  }
Esempio n. 3
0
  /**
   * Searches the graph and searches for the shortest path from a given start node to the
   * destination. Returns {@code null} if there are no vertices, edges, or path to the goal,
   * otherwise a list with the order of vertices on the shortest path.
   *
   * @param graph The graph to search
   * @param source The vertex to start the search from
   * @param destination The goal vertex
   * @return A list with the order of vertices on the shortest path, null if no path exists in the
   *     graph.
   */
  public List<V> search(Graph<V, E> graph, V source, V destination) {

    // Check if it is even possible to find a path, return null
    // if the graph has no vertices or edges
    if (graph.getVertexCount() == 0) {
      System.out.println("No nodes in the graph, " + "no shortest path can be found");
      return null;
    } else if (graph.getEdgeCount() == 0) {
      System.out.println("No edges in graph, no path " + "can be found");
      return null;
    }

    // Keep record of distance to each vertex, map each vertex
    // in the graph to it's distance
    HashMap<V, Number> distanceTable = new HashMap<>();

    // Unvisited node queue, uses a pair <Vertex, Double> and ordered
    // by the distance to the vertex
    PriorityQueue<Pair<V, Number>> queue = new PriorityQueue<>(new QueueComparator());

    // Map of nodes on the path, parents is value, key is child
    HashMap<V, V> parent = new HashMap<>();

    Number maxValue;
    E edgeTest = graph.getEdges().iterator().next();

    // This is so ugly, I hate Java Numbers
    int numberType = 0;
    if (edgeTest.getWeight() instanceof Integer) {
      numberType = 1;
    } else if (edgeTest.getWeight() instanceof Double) {
      numberType = 2;
    }
    // Place each vertex in the map, initialize distances and put
    // the pairings into the queue.
    for (V vertex : graph.getVertices()) {
      if (numberType == 1) {
        maxValue = Integer.MAX_VALUE;
        if (vertex.equals(source)) {
          distanceTable.put(source, 0);
          queue.add(new Pair<>(vertex, 0));
        } else {
          distanceTable.put(vertex, Integer.MAX_VALUE);
          queue.add(new Pair<>(vertex, maxValue));
        }
      } else if (numberType == 2) {
        maxValue = Double.MAX_VALUE;
        if (vertex.equals(source)) {
          distanceTable.put(source, 0.0);
          queue.add(new Pair<>(vertex, 0.0));
        } else {
          distanceTable.put(vertex, Double.MAX_VALUE);
          queue.add(new Pair<>(vertex, maxValue));
        }
      }
    }

    parent.put(source, null);

    while (!queue.isEmpty()) {

      Pair<V, Number> topPair = queue.remove();
      V vertex = topPair.getLeft();

      // Goal test, return the list of nodes on the path
      // if we reach the destination
      if (vertex.equals(destination)) {
        return tracePath(parent, destination);
      }

      Collection<V> neighbours = graph.getNeighbors(vertex);

      for (V neighbour : neighbours) {

        E edge = graph.findEdge(vertex, neighbour);
        assert (edge != null);

        // Test for type of number used for weight, work accordingly
        // Did I mention I hate the Java Number class.
        if (numberType == 1) {

          Integer alternateDistance = (Integer) edge.getWeight();

          if (alternateDistance < (Integer) distanceTable.get(neighbour)) {
            distanceTable.put(neighbour, alternateDistance);
            parent.put(neighbour, vertex);
            queue.add(new Pair<>(neighbour, alternateDistance));
          }
        } else if (numberType == 2) {
          Double alternateDistance = (Double) edge.getWeight();

          if (alternateDistance < (Double) distanceTable.get(neighbour)) {
            distanceTable.put(neighbour, alternateDistance);
            parent.put(neighbour, vertex);
            queue.add(new Pair<>(neighbour, alternateDistance));
          }
        }
      }
    }
    // Exhausted all possible paths from source, could not find a path
    // to the goal.
    return null;
  }