// @brief Print path from s to v with BFS iteration // @status finished public LinkedList<Integer> HasPathBFS(int s, int v) { LinkedList<Integer> ret = new LinkedList<Integer>(); int[] edgeTo = new int[G.V()]; boolean[] marked = new boolean[G.V()]; Queue<Integer> queue = new LinkedList<Integer>(); queue.add(s); marked[s] = true; while (!queue.isEmpty()) { int node = queue.poll(); LinkedList<Integer> neighbors = G.adj(node); for (int n : neighbors) { if (!marked[n]) { edgeTo[node] = n; marked[n] = true; queue.add(n); } } } if (marked[v] == false) { System.out.println("Can not find a path from " + s + " to " + v + "!\n"); return ret; } for (int n = v; n != s; n = edgeTo[n]) ret.addFirst(n); printPath(s, v, ret); return ret; }
// This method takes a Graph g and creates a new graph // in which all edges joining a pair of even vertices // are deleted; the new graph is returned. public static Graph deleteEvenEven(Graph g) { Graph result = new Graph(g.V()); for (int i = 0; i < g.V(); i++) { for (int j : g.adj(i)) { if (i < j && (i % 2 == 1 || j % 2 == 1)) result.addEdge(i, j); } } return result; }
/** * Initializes a new graph that is a deep copy of <tt>G</tt>. * * @param G the graph to copy */ public Graph(Graph G) { this(G.V()); this.E = G.E(); for (int v = 0; v < G.V(); v++) { // reverse so that adjacency list is in same order as original Stack<Integer> reverse = new Stack<Integer>(); for (int w : G.adj[v]) { reverse.push(w); } for (int w : reverse) { adj[v].add(w); } } }
public Iterable<Integer> eulerTour() { Stack<Integer> tour = new Stack<Integer>(); if (!hasEulerTour()) return tour; marked = new boolean[G.V()]; return tour; }
/** @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Graph g = new Graph(7); g.addEdge(0, 1); g.addEdge(0, 2); g.addEdge(1, 3); g.addEdge(2, 4); g.addEdge(4, 5); g.addEdge(0, 6); BreadthFirstPaths test = new BreadthFirstPaths(g, 0); for (int w = 0; w < g.V(); w++) { System.out.println(test.distTo(g, w)); } }
/** Unit tests the <tt>DepthFirstPaths</tt> data type. */ public static void main(String[] args) { In in = new In(args[0]); Graph G = new Graph(in); int s = Integer.parseInt(args[1]); DepthFirstPaths dfs = new DepthFirstPaths(G, s); for (int v = 0; v < G.V(); v++) { if (dfs.hasPathTo(v)) { StdOut.printf("%d to %d: ", s, v); for (int x : dfs.pathTo(v)) { if (x == s) StdOut.print(x); else StdOut.print("-" + x); } StdOut.println(); } else { StdOut.printf("%d to %d: not connected\n", s, v); } } }
public EulerTour(Graph G) { this.G = G; marked = new boolean[G.V()]; dfs(0); }
public boolean hasEulerTour() { for (boolean b : marked) if (!b) return false; for (int v = 0; v < G.V(); v++) if (G.degree(v) % 2 != 0) return false; return true; }
/** * Computes a path between <tt>s</tt> and every other vertex in graph <tt>G</tt>. * * @param G the graph * @param s the source vertex */ public DepthFirstPaths(Graph G, int s) { this.s = s; edgeTo = new int[G.V()]; marked = new boolean[G.V()]; dfs(G, s); }
public void DirectedPath(Graph G, int s) { this.marked = new boolean[G.V()]; this.edgeTo = new int[G.V()]; this.s = s; dfs(G, s); }
public static int maxDegree(Graph G) { int max = 0; for (int v = 0; v < G.V(); v++) if (degree(G, v) > max) max = degree(G, v); return max; }
public static int numberOfSelfLoops(Graph G) { int count = 0; for (int v = 0; v < G.V(); v++) for (int w : G.adj(v)) if (v == w) count++; return count / 2; }
public static int avgDegree(Graph G) { return 2 * G.E() / G.V(); }
public BreadthFirstPaths(Graph G, int s) { marked = new boolean[G.V()]; edgeTo = new int[G.V()]; this.s = s; bfs(G, s); }