// check that algorithm computes either the topological order or finds a directed cycle private void dfs(EdgeWeightedDigraph G, int v) { onStack[v] = true; marked[v] = true; for (DirectedEdge e : G.adj(v)) { int w = e.to(); // short circuit if directed cycle found if (cycle != null) return; // found new vertex, so recur else if (!marked[w]) { edgeTo[w] = e; dfs(G, w); } // trace back directed cycle else if (onStack[w]) { cycle = new Stack<DirectedEdge>(); while (e.from() != w) { cycle.push(e); e = edgeTo[e.from()]; } cycle.push(e); } } onStack[v] = false; }
public void relax(EdgeWeightedDigraph G , int v){//vertex relaxation for(DirectedEdge e: G.adj(v)){//没有遍历树,只是遍历了从v指向的所有边(edges) int w = e.to(); if(distTo[w]>distTo[v]+e.weight){ distTo[w]=distTo[v]+e.weight; edgeTo[w]=e; } } }
private void relax(EdgeWeightedDigraph G, int v) { for (DirectedEdge edge : G.adj(v)) { int w = edge.to(); if (distTo[w] < distTo[v] + edge.weight()) { distTo[w] = distTo[v] + edge.weight(); edgeTo[w] = edge; } } }
/** * Método recursivo usado para la obtención de todos los caminos posibles dentro de un árbol. Si * un nodo tiene un outdegree igual a 0, es una hoja, y el camino ha terminado. De lo contrario, * debe continuar buscando caminos entre los nodos adyacentes al primer nodo mencionado. * * @param G Grafo dirigido y con pesos sobre el cual se están buscando caminos. * @param node Nodo que se está visitando actualmente. * @param weight Peso actual del camino. * @param path Representaoión como String del camino que se ha seguido hasta el nodo actual. */ private void getPaths(EdgeWeightedDigraph G, Integer node, double weight, String path) { if (G.outdegree(node) == 0) this.paths.put(weight, path); else for (DirectedEdge edge : G.adj(node)) this.getPaths( G, edge.to(), weight + edge.weight(), path + "| " + edge.from() + " -> " + edge.to() + " | "); }
private void relax(EdgeWeightedDigraph G ,int v){ for(DirectedEdge e:G.adj(V)){ int w = e.to(); if(distTo[w]>distTo[v]+e.weight()){ distTo[w] = distTo[v]+e.weight(); edgeTo[w] = e; if(pq.contains(w)) pq.change(w,distTo[w]); else pq.insert(w,distTo[w]); } } }
public AcyclicLP(EdgeWeightedDigraph G, int s) { distTo = new double[G.V()]; edgeTo = new DirectedEdge[G.V()]; for (int v = 0; v < G.V(); v++) distTo[v] = Double.NEGATIVE_INFINITY; distTo[s] = 0.0; // relax vertices in toplogical order Topological topological = new Topological(G); for (int v : topological.order()) { for (DirectedEdge e : G.adj(v)) relax(e); } }
// run DFS in edge-weighted digraph G from vertex v and compute preorder/postorder private void dfs(EdgeWeightedDigraph G, int v) { marked[v] = true; pre[v] = preCounter++; preorder.enqueue(v); for (DirectedEdge e : G.adj(v)) { int w = e.to(); if (!marked[w]) { dfs(G, w); } } postorder.enqueue(v); post[v] = postCounter++; }
private void relax(EdgeWeightedDigraph G ,int v){ for(DirectedEdge e:G.adj(v)){ int w = e.to(); if(distTo[w]>distTo[v]+e.weight()){ distTo[w] = distTo[v]+e.weight(); edgeTo[w] = e; if(!onQ[w]){ q.enqueue(w); onQ[w] = true; } } if(cost++ %G.V() == 0 ) findNegativeCycle(); } }