コード例 #1
0
ファイル: Graph.java プロジェクト: karthikrk1/IADSA
  /**
   * Method to verify if the Input tour is a valid tour.
   *
   * @param g : Input graph for which the tour is to be verified.
   * @param tour : The tour output provided by findEulerTour() method.
   * @param start : The start vertex from where the tour begins.
   * @return : Returns true if tour is valid or false if the tour is rejected.
   */
  static boolean verifyTour(Graph<Vertex> g, List<Edge> tour, Vertex start) {
    Edge ePrevious = null;
    boolean previousHead = true;
    int count = 0;
    System.out.println("Size " + g.verts.size());
    for (Edge e : tour) {
      e.To.seen = false;
      e.From.seen = false;
      e.visited = false;
    }
    for (Edge e : tour) {
      count++;
      if (count == 2) {
        if (ePrevious.From.name == e.From.name || ePrevious.To.name == e.From.name) {
          previousHead = false;
          System.out.println("01");

        } else if (ePrevious.To.name == e.To.name || ePrevious.From.name == e.To.name) {
          previousHead = true;
          System.out.println("02");
        } else {
          System.out.println("03" + count);
          System.out.println(ePrevious.To.name);
          System.out.println(e.From.name);
          return false;
        }

      } else if (previousHead && ePrevious != null && count > 2) {
        if (ePrevious.From.name == e.From.name) {
          previousHead = false;
          System.out.println("1");
        } else if (ePrevious.From.name == e.To.name) {
          previousHead = true;
          System.out.println("2");
        } else {
          System.out.println("3" + count);
          return false;
        }
      } else if (!previousHead && ePrevious != null && count > 2) {
        if (ePrevious.To.name == e.From.name) {
          System.out.println("4");
          previousHead = false;
        } else if (ePrevious.To.name == e.To.name) {
          System.out.println("5");
          previousHead = true;
        } else {
          System.out.println("6" + count);
          return false;
        }
      }
      e.From.seen = true;
      e.To.seen = true;
      if (e.visited) {
        System.out.println("Edge is visited twice");
        return false;
      } else {
        e.visited = true;
      }
      ePrevious = e;
    }
    Iterator<Vertex> iterator = g.iterator();
    Vertex vertex;

    while (iterator.hasNext()) /*
											 * declaring the vertices as not yet
											 * processed
											 */ {
      vertex = (Vertex) iterator.next();

      if (!vertex.seen) {
        System.out.println("9");
        return false;
      }
      for (Edge edge : vertex.Adj) {
        if (!edge.visited) {
          System.out.println("10");
          return false;
        }
      }
    }
    return true;
  }
コード例 #2
0
ファイル: Graph.java プロジェクト: midiablo/CoarseWordNet
 public static Graph merge(Graph g1, Graph g2) {
   int commit = 0;
   WeightedArcSet list = new WeightedArcSet();
   Constructor[] cons = WeightedArc.class.getDeclaredConstructors();
   for (int i = 0; i < cons.length; i++) cons[i].setAccessible(true);
   ArcLabelledNodeIterator it1 = g1.graph.nodeIterator();
   while (it1.hasNext()) {
     Integer aux1 = it1.nextInt();
     ArcLabelledNodeIterator.LabelledArcIterator suc = it1.successors();
     Integer aux2 = null;
     while ((aux2 = suc.nextInt()) != null && aux2 >= 0 && (aux2 < g1.graph.numNodes()))
       try {
         WeightedArc arc = (WeightedArc) cons[0].newInstance(aux1, aux2, suc.label().getFloat());
         list.add(arc);
         if (commit++ % COMMIT_SIZE == 0) {
           list.commit();
         }
       } catch (Exception ex) {
         throw new Error(ex);
       }
   }
   ArcLabelledNodeIterator it2 = g2.graph.nodeIterator();
   while (it2.hasNext()) {
     Integer aux1 = it2.nextInt();
     ArcLabelledNodeIterator.LabelledArcIterator suc = it2.successors();
     Integer aux2 = null;
     while ((aux2 = suc.nextInt()) != null && aux2 >= 0 && (aux2 < g2.graph.numNodes()))
       try {
         int aaux1 = aux1 + g1.numNodes();
         int aaux2 = aux2 + g1.numNodes();
         if (g1.nodes.get(aux1) != null && g1.nodes.get(aux1).equals(g2.nodes.get(aux1)))
           aaux1 = g1.nodesReverse.get(g2.nodes.get(aux1));
         if (g1.nodes.get(aux2) != null && g1.nodes.get(aux2).equals(g2.nodes.get(aux2)))
           aaux2 = g1.nodesReverse.get(g2.nodes.get(aux2));
         WeightedArc arc = (WeightedArc) cons[0].newInstance(aux1, aux2, suc.label().getFloat());
         list.add(arc);
         if (commit++ % COMMIT_SIZE == 0) {
           list.commit();
         }
       } catch (Exception ex) {
         throw new Error(ex);
       }
   }
   Graph result = new Graph(list.toArray(new WeightedArc[0]));
   result.nodes.clear();
   result.nodesReverse.clear();
   for (Integer n : g1.nodes.keySet()) {
     result.nodesReverse.put(g1.nodes.get(n), n);
     result.nodes.put(n, g1.nodes.get(n));
     if (commit++ % COMMIT_SIZE == 0) {
       result.commit();
     }
   }
   for (Integer n : g2.nodes.keySet()) {
     int nn = n + g1.numNodes();
     if (g1.nodes.get(n) != null && g1.nodes.get(n).equals(g2.nodes.get(n)))
       nn = g1.nodesReverse.get(g2.nodes.get(n));
     result.nodesReverse.put(g2.nodes.get(n), nn);
     result.nodes.put(nn, g2.nodes.get(n));
     if (commit++ % COMMIT_SIZE == 0) {
       result.commit();
     }
   }
   result.iterator = result.nodeIterator();
   return result;
 }