简体   繁体   English

这是DFS,BFS的Graph的正确实现吗

[英]Is this a proper implementation of a Graph for DFS, BFS

I'm learning about search algorithms BFS and DFS. 我正在学习搜索算法BFS和DFS。 I plan to implement both but before I do that, I need to implement my graph structure. 我计划实现两者,但在此之前,我需要实现我的图形结构。 Here's my idea: 这是我的主意:

A graph of connecting cities: Each city is represented by a Node. 连接城市的图形:每个城市都由一个节点表示。 Our graph will simply be an ArrayList of Nodes added as they're created, and each Node will have a list of it's neighbors, and a parent which will let us know where we came from (for path retrieval). 我们的图将只是创建时添加的Node的ArrayList,每个Node将具有其邻居的列表,以及一个父节点,该父节点将让我们知道我们来自哪里(用于路径检索)。 I haven't coded anything up yet, I wanted to get some feedback on my ideas before spending the time writing up something that won't work. 我还没有编写任何代码,所以我想在花时间编写不起作用的内容之前获得一些想法方面的反馈。 Here's some pseudocode-ish, code. 这是一些伪代码的代码。 One potential problem I can see is how we're going to deal with Nodes that we can get to from multiple places (multiple parents). 我看到的一个潜在问题是,我们将如何处理可以从多个地方(多个父节点)到达的节点。 If anyone has any suggestions on dealing with that, feel free to share. 如果有人对此有任何建议,请随时分享。

 public class Node{
     String name;
     Node parent;
     ArrayList<Node> neighbors; 

    public addNeighbor(Node n);
    public setParent(Node n);
    public getNeighbors()
    ...
 }

 public static void main(String[] args){
     ArrayList<Node> graph = new ArrayList<Node>(); 
     //build node
     Node node = new Node(String name);

     //add neighbors
     node.addNeighbor(neighbor1);
     node.addNeighbor(neighbor2);

     //set parent
     node.setParent(parent1);

     //add to graph
     graph.add(node);

     path = dfs(graph, startNode, goalNode);
     System.out.print(path);
 }

Edit: I know I could look online and find implementations of this pretty easily, but I'd prefer to come up with my own solutions. 编辑:我知道我可以在网上查找并轻松找到此实现,但是我更愿意提出自己的解决方案。

Your implementation look good. 您的实现看起来不错。 It's the classic implentation of a graph structure (a node with a list of neighbors). 这是图结构(带有邻居列表的节点)的经典实现。 Some points: 一些要点:

  • You can use backtracking to deal with multiples paths that reach the same node. 您可以使用回溯处理到达同一节点的多个路径。 If the dfs method have a recursive implementation, you need to avoid the recursive call if the Node has already a parent. 如果dfs方法具有递归实现,则在Node已具有父节点的情况下,需要避免递归调用。 But if the new path is better that the old one, then you discard the old parent, and set the new one. 但是,如果新路径比旧路径更好,那么您将丢弃旧的父路径,然后设置新路径。
  • Your implementation is a directional graph. 您的实现是有向图。 In other words, you can build a graph that has a path from A to B, but has no path from B to A. I don't know if this is ok for you. 换句话说,您可以构建一个图,该图具有从A到B的路径,但是没有从B到A的路径。我不知道这是否适合您。
  • I recommend you encapsulate the building of the graph inside a wrapper, that build both paths automatically, whith a unique call to a method. 我建议您将图形的构建封装在一个包装器中,该包装器将自动构建两条路径,并通过对方法的唯一调用。 That way, always you build bidirectional paths. 这样,您总是可以构建双向路径。
  • You can use a Set to store the neighbors. 您可以使用Set来存储邻居。 That way, there is no duplicates. 这样,就不会重复。 Of course, you need to implements the "equals" method in the Node class. 当然,您需要在Node类中实现“等于”方法。

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

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