Java 基于迭代器的图深度优先搜索

Java 基于迭代器的图深度优先搜索,java,graph,depth-first-search,Java,Graph,Depth First Search,我想把这变成一个深度优先的搜索。起初它是广度优先,现在我正试图转换它。我已经为此工作了好几天,并尝试了多种不同的方法 import java.util.*; class Graph{ class Edge{ int v,w; public Edge(int v,int w){ this.v=v; this.w=w; } @Override public String toString(){ return

我想把这变成一个深度优先的搜索。起初它是广度优先,现在我正试图转换它。我已经为此工作了好几天,并尝试了多种不同的方法

        import java.util.*;


class Graph{
class Edge{
    int v,w;
    public Edge(int v,int w){
        this.v=v; this.w=w;
    }
    @Override
    public String toString(){
        return "("+v+","+w+")";
    }
}
List<Edge> G[];
public Graph(int n){
    G=new LinkedList[n];
    for(int i=0;i<G.length;i++)
        G[i]=new LinkedList<Edge>();
}
boolean isConnected(int u,int v){
    for(Edge i: G[u])
        if(i.v==v) return true;
    return false;
}
void addEdge(int u,int v,int w)
{

    G[u].add(0,new Edge(v,w));
    G[v].add(0,new Edge(u,w));
}
public int getWeight(int u, int v)
{
    for (Edge e : G[u]) 
    {
    if (e.v == v) 
        {
             return e.w ;
        }
    }throw new NoSuchElementException();
}
@Override
public String toString(){
    String result="";
    for(int i=0;i<G.length;i++)
        result+=i+"=>"+G[i]+"\n";
    return result;
}
import java.util.*;
类图{
阶级边缘{
int v,w;
公共边缘(内部v、内部w){
这个.v=v;这个.w=w;
}
@凌驾
公共字符串toString(){
返回“(“+v+”、“+w+”)”;
}
}
清单G[];
公共图(int n){
G=新链接列表[n];

for(int i=0;iDFS是一种算法,通常在没有循环或链接回其父级的树上运行。这会给图形实现带来两个非常简单的问题。问题1是每个顶点都指向其“父级”。例如,顶点1有一个边到顶点2,顶点2有一个边到顶点1。当访问边2时,它的所有子项都会放在堆栈上。当发生这种情况时,你会将已经访问过的顶点1放回堆栈上。问题2是,有时你会多次将顶点推到堆栈上,然后再进行访问已访问。例如,顶点5和3都在访问6之前将6添加到堆栈中。快速解决这两个问题的一种方法是:

  while (stack.size() != 0)
    {
        s = stack.pop();
        if(visited[s]) //add these two lines
                continue; 
        System.out.print(s+" ");
        Iterator<Edge> i = G[s].listIterator();
        if (!visited[s])
            {
                visited[s] = true;
                while (i.hasNext())
                {
                   int n = i.next().v;
                   stack.push(n);
                }
            }

    }
while(stack.size()!=0)
{
s=stack.pop();
如果(访问了[s])//添加这两行
继续;
系统输出打印(s+“”);
迭代器i=G[s].listIterator();
如果(!已访问[s])
{
已访问=正确;
while(i.hasNext())
{
int n=i.next().v;
堆栈推送(n);
}
}
}
如果您检查从堆栈弹出的元素是否已被访问,您将删除作为已访问的父节点的顶点,以及由其父节点多次推送到堆栈上的节点。只需检查是否已访问某个顶点,以及是否已继续下一次迭代t他绕了一圈

        void BFS(int s)
{
    // Mark all the vertices as not visited(By default
    // set as false)
    boolean visited[] = new boolean[G.length];

    // Create a queue for BFS
    LinkedList<Integer> queue = new LinkedList<Integer>();

    // Mark the current node as visited and enqueue it
    visited[s]=true;
    queue.add(s);

    while (queue.size() != 0)
    {
        // Dequeue a vertex from queue and print it
        s = queue.poll();
        System.out.print(s+" ");

        // Get all adjacent vertices of the dequeued vertex s
        // If a adjacent has not been visited, then mark it
        // visited and enqueue it
        Iterator<Edge> i = G[s].listIterator();
        while (i.hasNext())
        {
            int n = i.next().v;
            if (!visited[n])
            {
                visited[n] = true;
                queue.add(n);
            }
        }
    }
}
  while (stack.size() != 0)
    {
        s = stack.pop();
        if(visited[s]) //add these two lines
                continue; 
        System.out.print(s+" ");
        Iterator<Edge> i = G[s].listIterator();
        if (!visited[s])
            {
                visited[s] = true;
                while (i.hasNext())
                {
                   int n = i.next().v;
                   stack.push(n);
                }
            }

    }