Exemplo n.º 1
1
  /**
   * Method to find the Euler Tour based on the Hierholzer's algorithm.
   *
   * @param g : Input graph for which the tour is to be found.
   * @return : Returns a list of edges that comprises of the Euler Tour.
   */
  public static List<Edge> findEulerTour(Graph<Vertex> g) {
    Vertex start = g.verts.get(1);
    Stack<Edge> forward = new Stack<Edge>();
    Stack<Edge> backtrack = new Stack<Edge>();
    Edge e = getUnvisitedEdge(start);
    while (e != null) {
      e.visited = true;
      forward.push(e);
      e = getUnvisitedEdge(e.To);
    }

    while (!(forward.isEmpty())) {
      e = forward.pop();
      backtrack.push(e);
      e = getUnvisitedEdge(e.From);
      while (e != null) {
        e.visited = true;
        forward.push(e);
        e = getUnvisitedEdge(e.To);
      }
    }

    List<Edge> path = new LinkedList<Edge>();
    while (!backtrack.isEmpty()) {
      Edge edge = backtrack.pop();
      path.add(edge);
    }
    return path;
  }
Exemplo n.º 2
0
 /**
  * Method to return the unvisited edge for the Graph
  *
  * @param n : Input vertex for which the unvisited Edges are to be determined.
  * @return : Returns the unvisited Edges for the input Vertex.
  */
 public static Edge getUnvisitedEdge(Vertex n) {
   for (Edge e : n.Adj) {
     if (e.visited == false && e.From.equals(n)) {
       e.visited = true;
       return e;
     }
   }
   return null;
 }
Exemplo n.º 3
0
 /**
  * Utility Method to determine if the Vertex has unvisited children
  *
  * @param n : Input vertex for which the children are to be determined.
  * @return : Returns the unvisited child Vertex for the input Vertex.
  */
 public static Vertex getUnvisitedChildren(Vertex n) {
   for (Edge e : n.Adj) {
     if (e.visited == false && e.From.equals(n)) {
       e.visited = true;
       return e.To;
     }
   }
   return null;
 }
Exemplo n.º 4
0
  /**
   * 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;
  }