@Check(CheckType.FAST) public void vertexNotReachable(final Vertex vertex) { if (!(vertex instanceof Entry)) { final Set<Object> stateScopeSet = new HashSet<Object>(); for (EObject obj : EcoreUtil2.eAllContents(vertex)) { stateScopeSet.add(obj); } stateScopeSet.add(vertex); final List<Object> externalPredecessors = new ArrayList<Object>(); DFS dfs = new DFS() { @Override public Iterator<Object> getElementLinks(Object element) { List<Object> elements = new ArrayList<Object>(); if (element instanceof org.yakindu.sct.model.sgraph.State) { if (!stateScopeSet.contains(element)) { externalPredecessors.add(element); } else { elements.addAll(((org.yakindu.sct.model.sgraph.State) element).getRegions()); elements.addAll( ((org.yakindu.sct.model.sgraph.State) element).getIncomingTransitions()); } } else if (element instanceof Region) { elements.addAll(((Region) element).getVertices()); } else if (element instanceof Entry) { if (!stateScopeSet.contains(element)) { externalPredecessors.add(element); } else { elements.addAll(((Entry) element).getIncomingTransitions()); } } else if (element instanceof Vertex) { elements.addAll(((Vertex) element).getIncomingTransitions()); } else if (element instanceof Transition) { elements.add(((Transition) element).getSource()); } return elements.iterator(); } }; dfs.perform(vertex); if (externalPredecessors.size() == 0) { error(ISSUE_NODE_NOT_REACHABLE, vertex, null, -1); } } }
/** * Imprime por pantalla el radio de un arbol, es decir, el peso del camino más largo y el camino. * * @param G Grafo dirigido y con pesos sobre el cual se desea aplicar el algoritmo para la * obtención de su radio. */ public void radiusOfATree(EdgeWeightedDigraph G) { this.paths = new HashMap<>(); DFS dfs = new DFS(G, 0); Iterable<Integer> limitVertex = dfs.getInitialVertex(); for (Integer node : limitVertex) this.getPaths(G, node, 0, ""); TreeSet<Double> tree = new TreeSet<>(this.paths.keySet()); // Matar una mosca con un arbol rojo-negro :D System.out.println("\nPeso del camino más largo : " + tree.last()); System.out.println("Camino más largo : " + this.paths.get(tree.last())); }
public static void main(String[] args) { System.out.println("DFS"); // create a graph and connect some vertices Graph g = new Graph(8); g.addEdge(1, 3); g.addEdge(3, 4); g.addEdge(4, 6); // create a dfs structure based on a subject vertex int subjV = 3; DFS dfs = new DFS(g, subjV); System.out.println("subject vertex: " + subjV); System.out.println("# of vertices visited: " + dfs.numVisited()); // show the dfs structure based on the subject vertex for (int v = 0; v < g.numVertices(); v++) { if (dfs.visited(v)) { System.out.print(v + " "); } } }
private static void getpos() { int k = 0; for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { int change_row = 0; if (Graph[i][j] != '.') { Goal_pos[k][change_row] = i; Goal_pos[k][change_row + 1] = j; k++; } } } DFS.setGoal_pos(Goal_pos); }
public static void filehandler() { try { int i = 0; File file = new File("map1.txt"); FileInputStream fis = new FileInputStream(file); BufferedInputStream bis = new BufferedInputStream(fis); BufferedReader d = new BufferedReader(new InputStreamReader(bis)); String line = d.readLine(); line = line.trim(); String temp[] = line.split(" "); row = Integer.parseInt(temp[0]); DFS.setMax_Row(row - 1); col = Integer.parseInt(temp[1]); DFS.setMax_Col(col - 1); line = d.readLine(); line = line.trim(); temp = line.split(" "); int startx = Integer.parseInt(temp[0]); DFS.setStartx(startx); int starty = Integer.parseInt(temp[1]); DFS.setStarty(starty); int j = i = 0; while (i < row) { line = d.readLine(); char ch[] = line.toCharArray(); for (int k = 0; k < col; k++) { Graph[i][k] = ch[k]; if (ch[k] != '.') { Message_Obtained.add(j); j++; } } i++; } DFS.setGraph(Graph); DFS.setGoals(Message_Obtained); getpos(); i = 0; while (i < row) { line = d.readLine(); line = line.trim(); temp = line.split(" +"); for (j = 0; j < col; j++) { cost_map[i][j] = Double.parseDouble(temp[j]); } i++; } DFS.setMap_cost(cost_map); } catch (IOException e) { } }