Пример #1
0
 // @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;
 }
  private void dfs(Graph<V> graph, V vertex) {
    visited.add(vertex);
    ids.put(vertex, count);
    size[count]++;

    for (V adj : graph.adj(vertex)) if (!visited.contains(adj)) dfs(graph, adj);
  }
Пример #3
0
 // depth first search from v
 private void dfs(Graph G, int v) {
   marked[v] = true;
   for (int w : G.adj(v)) {
     if (!marked[w]) {
       edgeTo[w] = v;
       dfs(G, w);
     }
   }
 }
Пример #4
0
 // 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;
 }
Пример #5
0
 public void dfs(Graph G, int s) {
   marked[s] = true;
   for (int w : G.adj(s)) {
     if (!marked[w]) {
       marked[w] = true;
       edgeTo[w] = s;
       dfs(G, w);
     }
   }
 }
Пример #6
0
 public static void test(String[] args) {
   String filename = args[0];
   String delimiter = args[1];
   SymbolGraph sg = new SymbolGraph(filename, delimiter);
   Graph G = sg.G();
   while (StdIn.hasNextLine()) {
     String source = StdIn.readLine();
     int s = sg.index(source);
     for (int v : G.adj(s)) {
       StdOut.println("   " + sg.name(v));
     }
   }
 }
Пример #7
0
 private void bfs(Graph G, int s) {
   Queue<Integer> queue = new LinkedList<Integer>();
   marked[s] = true; // 标记起点
   queue.add(s); // 将它加入队列
   while (!queue.isEmpty()) {
     int v = queue.peek(); // 从队列中删去下一顶点
     for (int w : G.adj(v)) {
       if (!marked[w]) { // 对于每个未被标记的相邻顶点
         edgeTo[w] = v; // 保存最短路径的最后一条边
         marked[w] = true; // 标记她,因为最短路径已知
         queue.add(w); // 并将它添加到队列中
       }
     }
   }
 }
Пример #8
0
  public void bfs(Graph graph, int i, int explored[]) {
    visited[i] = true;
    queue.enqueue(i);
    explored[i] = i;

    while (!queue.isEmpty()) {
      int vertex = queue.dequeue();
      for (Object v : graph.adj(vertex))
        if (visited[(int) v] == false) {
          queue.enqueue((int) v);
          visited[(int) v] = true;
          explored[(int) v] = i;
        }
    }
  }
Пример #9
0
  public int shortestPath(Graph graph, int start, int dest) {
    int distance[] = new int[graph.vertices()];
    visited[start] = true;
    queue.enqueue(start);
    distance[start] = 0;

    while (!queue.isEmpty()) {
      int vertex = queue.dequeue();
      for (Object v : graph.adj(vertex))
        if (!visited[(int) v]) {
          queue.enqueue((int) v);
          visited[(int) v] = true;
          distance[(int) v] = distance[vertex] + 1;
        }
    }

    return distance[dest];
  }
Пример #10
0
 private void dfs(int v) {
   marked[v] = true;
   for (int w : G.adj(v)) if (!marked[w]) dfs(w);
 }
Пример #11
0
 public static int degree(Graph G, int v) {
   int degree = 0;
   for (int w : G.adj(v)) degree++;
   return degree;
 }
Пример #12
0
 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;
 }