Пример #1
0
  public AbstractChooser(IndexGraph g) {
    this.G = g;
    this.LEFT = new ArrayList<IndexVertex>();
    this.RIGHT = new HashSet<IndexVertex>();
    this.N_LEFT = new HashSet<IndexVertex>();
    this.N_v_RIGHT = new ArrayList<VSubSet>(G.numVertices());
    this.UB = 2;
    IndexedSet<IndexVertex> groundSet;
    groundSet = new IndexedSet<IndexVertex>(G.vertices());
    // degInLeft = new DegreeList(g);

    // System.out.println("Inside abstract class constructor");
    for (IndexVertex v : G.vertices()) {
      RIGHT.add(v);
    }

    for (int i = 0; i < G.numVertices(); i++) N_v_RIGHT.add(new VSubSet(groundSet));

    // create for all v in LEFT N(v) in RIGHT
    for (IndexVertex x : G.vertices()) {
      VSubSet temp = new VSubSet(groundSet);

      for (IndexVertex y : G.neighbours(x)) {
        if (RIGHT.contains(y)) temp.add(y);
      }
      // System.out.println("temp:"+temp);
      N_v_RIGHT.set(x.id(), temp);
    }
  }
Пример #2
0
  // Input  : None
  // Output : next vertex chosen from N_LEFT or RIGHT
  // has to be override
  public IndexVertex next() {
    IndexVertex nextVertex = choose();
    // System.out.println("choosen "+nextVertex);
    // Update N_LEFT
    // Remove v from N_LEFT
    if (N_LEFT.contains(nextVertex)) {
      N_LEFT.remove(nextVertex);
    }
    // Add all neighbours of v in RIGHT to N_LEFT
    for (IndexVertex x : G.neighbours(nextVertex)) {

      if (RIGHT.contains(x)) // && !(N_Left.contains(x)))
      {
        N_LEFT.add(x);
      }
      // update N_v_RIGHT -remove nextvertex from all its neighbours neighbour list in RIGHT
      N_v_RIGHT.get(x.id()).remove(nextVertex);
    }

    // move v from right to left
    LEFT.add(nextVertex);
    RIGHT.remove(nextVertex);
    return nextVertex;
  }
  public static int linIS(IndexGraph g, List<IndexVertex> order) {
    // sizes is a list which for each neighbourhood stores the optimal size of an independent Set
    // can easily be changed to store sets instead of sizes
    Map<VSubSet, Integer> sizes = new HashMap<VSubSet, Integer>();

    // groundset is the vertices of the graph, solutions will be subsets of the groundset
    IndexedSet<IndexVertex> groundset = new IndexedSet<IndexVertex>(g.vertices());

    // list of the neighbourhoods to the remaining part of the graph
    ArrayList<VSubSet> neighbours = new ArrayList<VSubSet>(g.numVertices());
    for (int i = 0; i < g.numVertices(); i++) {
      VSubSet nset = new VSubSet(groundset);
      nset.addAll(g.neighbours(g.getVertex(i)));
      neighbours.add(nset);
    }

    // add the empty neighbourhood with size 0
    sizes.put(new VSubSet(groundset), 0);

    // store the vertices you have seen so far (while you iterate through order) starting with an
    // empty set
    VSubSet seen = new VSubSet(groundset);
    // and the vertices you have not seen
    VSubSet unseen = new VSubSet(groundset);
    for (IndexVertex v : g.vertices()) unseen.add(v);

    // will store the optimal solution
    int maxIS = 0;
    int maxListSize = 0;

    // go through the vertices in the given order
    for (IndexVertex v : order) {
      maxListSize = Math.max(maxListSize, sizes.size());
      // System.out.println("Considering: "+v);
      // System.out.println("The list has size: "+sizes.size());
      // add v to the already seen vertices (left side of the cut)
      seen.add(v);
      unseen.remove(v);
      // update the neighbourhoods
      for (IndexVertex u : g.neighbours(v)) {
        neighbours.get(u.id()).remove(v);
      }
      // System.out.println("Have added "+v);

      // make a new map for the next cut (after v is moved from right to left)
      Map<VSubSet, Integer> newsizes = new HashMap<VSubSet, Integer>();

      // go through all neighbourhoods
      for (Entry<VSubSet, Integer> e : sizes.entrySet()) {
        VSubSet set = e.getKey();
        int size = e.getValue();

        // If v is in the neighbourhood we can not add it to the Independent set, otherwise we may.
        if (set.contains(v)) {
          // update the size (This will never change maxIS and should be removed)
          maxIS = Math.max(maxIS, size);
          // remove v from the neighbourhood
          set.remove(v);
          // if the neighbourhood is already in the new list update, else add
          Integer temp = newsizes.get(set);
          if (temp == null || temp < size) newsizes.put(set, size);
        } else {
          Integer temp = newsizes.get(set);
          boolean usedSet = false;
          // If v has no neighbours in the set we can still keep the set as is without adding v
          if ((temp == null || temp < size) && neighbours.get(v.id()).intersects(unseen)) {
            usedSet = true;
            newsizes.put(set, size);
          }

          // make a copy of the Independent Set and increase the size of the solution (add v to the
          // solution)
          VSubSet newset;
          if (usedSet) newset = (VSubSet) set.clone();
          else newset = set;
          size++;
          maxIS = Math.max(maxIS, size);

          // update the neighbourhood by adding the neighbours of v
          newset.AddAll(neighbours.get(v.id()));

          temp = newsizes.get(newset);
          // add the new set to the map
          if (temp == null || temp < size) newsizes.put(newset, size);
        }
      }
      // set the map to the one for the current cut
      sizes = newsizes;
      // for( Entry<VSubSet, Integer> set : newsizes.entrySet())
      //	System.out.println(set.getKey()+" of size "+set.getValue());
    }

    System.out.println("Max list size used is: " + maxListSize);
    return maxIS;
  }