简体   繁体   English

图 java 上的深度优先搜索

[英]Depth First Search on graph java

I am having a bit of a problem implementing DFS traversal in java.我在 Java 中实现 DFS 遍历时遇到了一些问题。 My problem I think is the 'dfs' method in Graph.java I coded.我认为我的问题是我编码的 Graph.java 中的“dfs”方法。 It is not returning the required output giving it a specific input.它没有返回所需的输出,给它一个特定的输入。 My code is below along with its input and desired output.我的代码及其输入和所需的输出如下。 Could someone help me solve this problem in my code.有人可以帮我在我的代码中解决这个问题。 Thanks.谢谢。

Graph.java图.java

public class Graph {
ArrayList<Vertex> Vertices=new ArrayList<Vertex>();
Stack<Integer> stack=new Stack<Integer>();
public Graph(){
    Scanner in=new Scanner(System.in);
    String sz=in.nextLine();
    int size=Integer.parseInt(sz);
    for(int i=0; i<size; i++) addVertex();
    String s=in.nextLine();
    while(!s.equals("-1")){
        String[] arr=s.split(",");
        int v1=Integer.parseInt(arr[0]);
        int v2=Integer.parseInt(arr[1]);
        addEdge(v1,v2);
        s=in.nextLine();
    }

    //Vertex v=Vertices.get(2);
    //System.out.println(dfs(v));
}

public static void main(String[] args){
    new Graph();
}
public void addVertex(){
    Vertex v=new Vertex(Vertices.size());
    Vertices.add(v);
}
public Vertex getVertex(int n){
    return Vertices.get(n);
}
public void addEdge(int n, int m){
    Vertex v1=Vertices.get(n);
    Vertex v2=Vertices.get(m);
    v1.addAdjacency(v2);
    v2.addAdjacency(v1);
}
public void dfs(Vertex obj){
    obj.marked=true;
    int k=0;
    for(Vertex v:obj.Vertices){
        Vertex d=v;
        if(!d.marked){
            d.parent=obj;
            k=d.parent.vertexNumber;
            stack.push(k);
            dfs(d);
        }
    }
}
}

Vertex.java顶点.java

public class Vertex {
int vertexNumber;
Vertex parent = null;
boolean marked = false;
LinkedList<Vertex> Vertices = new LinkedList<Vertex>();

public Vertex(int num) {
    vertexNumber = num;
}

public void addAdjacency(Vertex object) {
    Vertices.add(object);
}

public boolean isAdjacent(Vertex object) {
    if (Vertices.contains(object))
        return true;
    else
        return false;
}

public int getDegree() {
    return Vertices.size();
}

} } 在此处输入图片说明

You almost had it.你几乎拥有它。 You don't need the stack in your dfs.您不需要 dfs 中的堆栈。 Simplify it like this:把它简化成这样:

public void dfs(Vertex obj) {
    obj.marked = true;
    for (Vertex v : obj.Vertices) {
        if (!v.marked) {
            v.parent = obj;
            dfs(v);
        }
    }
}

Just print the results in your main:只需在您的主要打印结果:

public static void main(String[] args) {
    Graph g = new Graph();
    Vertex source = g.Vertices.get(0);
    g.dfs(source);

    for(Vertex v:g.Vertices){
        if (v!= source && v.marked){
            System.out.println(v.vertexNumber+":"+v.parent.vertexNumber);
        }
    }
}

You are simply calling dfs, marking anything reachable as you along and updating the parent.您只需调用 dfs,标记任何可访问的内容并更新父级。 Once you are done, just go through all vertices and print the ones that were reachable (except the source itself).完成后,只需遍历所有顶点并打印可到达的顶点(源本身除外)。

And here is the output I'm getting:这是我得到的输出:

1:0 
2:1 
3:8 
4:5 
5:6 
6:2 
7:10 
8:7 
9:5 
10:5

I also recommend you to refactor your code and move the command line reads to your main instead of Graph constructor.我还建议您重构代码并将命令行读取移动到 main 而不是Graph构造函数。 Just read the numbers and call g.addEdge in order to build your graph.只需阅读数字并调用g.addEdge即可构建图形。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM