我正在尝试使用LinkedList的ArrayList在java中创建一个图形。我已经实现了自己的清单。但是,当我尝试在图形的顶点之间添加连接时,我遇到了无限循环。我正在调试,我意识到这发生在我试图在LinkedList的末尾添加元素时。我是初学者,我没有看到我的List实现有什么问题。有人可以帮忙吗?
import java.util.Stack;
// traverse the graph
public class GraphTraversal {
public static void main(String[] args)
{
Graph graph=new Graph();
initializeGraph(graph);
graph.breadthFirstSearch();
}
public static void initializeGraph(Graph graph)
{
Node_Graph node1=new Node_Graph(1, false);
Node_Graph node2=new Node_Graph(2, false);
Node_Graph node3=new Node_Graph(3, false);
Node_Graph node4=new Node_Graph(4, false);
Node_Graph node5=new Node_Graph(5, false);
Node_Graph node6=new Node_Graph(6, false);
Node_Graph node7=new Node_Graph(7, false);
Node_Graph node8=new Node_Graph(8, false);
graph.addNode(node1);
graph.addNode(node2);
graph.addNode(node3);
graph.addNode(node4);
graph.addNode(node5);
graph.addNode(node6);
graph.addNode(node7);
graph.addNode(node8);
graph.makeConnection(node1, node2);
graph.makeConnection(node1, node3);
graph.makeConnection(node3, node4);
graph.makeConnection(node3, node5);
graph.makeConnection(node4, node5);
graph.makeConnection(node4, node6);
graph.makeConnection(node4, node8);
graph.makeConnection(node4, node2);
graph.makeConnection(node6, node5);
graph.makeConnection(node8, node7);
graph.makeConnection(node7, node2);
}
}
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Stack;
//Class for graph data structure
public class Graph {
public ArrayList<List> nodes=new ArrayList<List>();
public void addNode(Node_Graph n)
{
List new_node=new List();
new_node.add(n);
nodes.add(new_node);
}
public void makeConnection(Node_Graph node1, Node_Graph node2)
{
for(List list:nodes)
{
if(list.head.getId()==node1.getId())
{
list.add(node2);
break;
}
}
}
public void breadthFirstSearch()
{
Stack<Node_Graph> traverse=new Stack<Node_Graph>();
Node_Graph start=(nodes.get(0)).head;
start.setVisited(true);
traverse.push(start);
while(traverse.empty())
{
Node_Graph popped=traverse.pop();
System.out.println(popped.getId());
List nextList= nodes.get(popped.getId());
Node_Graph newElement=nextList.head;
while(newElement.getNext()!=null)
{
newElement=newElement.getNext();
if(!newElement.getVisited())
{
newElement.setVisited(true);
traverse.push(newElement);
}
}
if(!newElement.getVisited())
traverse.push(newElement);
}
}
}
//linked list implementation
public class List{
public Node_Graph head;
public int size;
public List()
{
head=null;
size=0;
}
public void add(Node_Graph element)
{
if(head==null)
{
head=element;
}
else
{
Node_Graph last=head;
while(last.getNext()!=null)
{
last=last.getNext();
}
last.setNext(element);
}
}
}
//node of a graph
public class Node_Graph {
private int id;
private boolean visited;
private Node_Graph next;
public Node_Graph(int id,boolean visited)
{
this.id=id;
this.visited=visited;
}
public void setId(int id)
{
this.id=id;
}
public int getId()
{
return id;
}
public void setVisited(boolean visited)
{
this.visited=visited;
}
public boolean getVisited()
{
return visited;
}
public void setNext(Node_Graph next)
{
this.next=next;
}
public Node_Graph getNext()
{
return next;
}
}
行graph.makeConnection(node4, node6);
导致无限循环,因为节点4的下一个变量无限连接到节点5
答案 0 :(得分:1)
我注意到的第一件事是graph.makeConnection(node3, node5);
行导致4连接到5,它不应该是。
我在列表和node_graph类中添加了toString方法,以便更容易理解正在进行的操作;他们在这里你可以尝试一下:
列表:
public String toString(){
Node_Graph h = head;
String s = "";
while(h != null){
s += "[" + h.toString() + "] ";
h = h.next;
}
return s;
}
Node_Graph:
public String toString(){
String s = id + "";
if(next != null)
s += ", " + next.toString();
return s;
}
追踪错误。让我们从这一行开始:
graph.makeConnection(node1, node3);
导致通话:
{1 -> 2,2 -> null}.add(3)
到目前为止一切都很好。
另外,您可以找到列表的最后一个元素:{2},并将其设置在{3}旁边。因此,列表现在看起来像{1 - &gt; 2 - &gt; 3,2-&gt; 3,3},当它应该是{1 - > 2 - &gt; 3,2,3}。第一个列表(不正确)表示1连接到2和3,2连接到3,而2不应连接到3,如第二个列表所示。在您当前的方案中,这是不可能的,因为&#39; 2实际上是具有相同的单一next
字段的相同对象。它不能在1个元素中为{3}而在自身中为{null}。
总的来说,你需要区分两个&#34; nexts&#34;。我相信你的目标是node_graph中的下一个字段代表该节点所连接的节点,而列表中的下一个字段代表列表中的下一个节点,无论是否存在连接。你试图用一块石头和下一个单一的石头一起捡到两只鸟,它会回来咬你无限的递归。
有更简洁的方法来实现图形 - 哈希图(节点 - &gt;邻居节点列表)更加清晰,使您无需处理下一个业务。如果您的主要目标是优化图形算法,例如bfs / dfs-ing,您可能只想使用它。
如果您真的想使用列表实现图形,那么您需要做一些解决。我建议完全从Node_Graph类中删除next
字段; Node_Graph类应该只担心它自己的数据,而不是维护列表不变量。然后让列表类有一个内包装类,它包含一个&#34;这个&#34; (一个Node_Graph实例)和一个&#34; next&#34; (Node_Wrapper)实例。完成所有操作后,您可以为Node_Graph提供List类型的邻居字段,该字段将包含所有可访问的邻居。
这是一个遵循您的模式的基本HashMap图形实现。您也不需要列表实现。没有必要的包装/联系:
public class Node{
public final Graph graph; //The graph this Node belongs to
private int id;
private boolean visited;
/** Constructs a Node with the given inputs.
* Also adds itself to g as part of construction */
public Node(Graph g, int i, boolean v){
graph = g;
id = i;
visited = v;
graph.addNode(this);
}
public int getId(){
return id;
}
public void setVisited(boolean v){
visited = v;
}
//Getters for boolean fields usually follow the is<Field> pattern
public boolean isVisited(){
return visited;
}
/** Looks up the neighbors of this in the graph */
public Set<Node> getNeighbors(){
return graph.neighborsOf(this);
}
}
public class Graph{
private HashMap<Node, HashSet<Node>> graph; //The actual graph. Maps a node -> its neighbors
public Graph(){
graph = new HashMap<Node, HashSet<Node>>();
}
/** Adds the node to this graph.
If n is already in this graph, doesn't overwrite */
public void addNode(Node n) throws IllegalArgumentException{
if(n.graph != this)
throw new IllegalArgumentException(n + " belongs to " + n.graph ", not " + this);
if(! graph.contains(n))
graph.put(n, new HashSet<Node>());
}
/** Returns the neighbors of the given node.
* Returns null if the node isn't in this graph */
public Set<Node> neighborsOf(Node n){
if(! graph.contains(n))
return null;
return graph.get(n);
}
/** Connects source to sink. Also adds both to graph if they aren't there yet */
public void makeConnection(Node source, Node sink){
//Make sure source and sink belong to this graph first
addNode(source);
addNode(sink);
//Make the connection by adding sink to source's associated hashset
graph.get(source).add(sink);
}
}