Java 如何使图遍历算法适用于大型图?
我正在编写一个算法,在给定源和目标时遍历图形。算法应该遍历并在图中显示从源到目标的所有可能路径。我使用广度优先搜索来实现这一点,它在一个小图形中工作。但是,当提供一个巨大的图形(有1000多个顶点)时,程序似乎被冻结,并且在很长一段时间后不会显示任何输出。我能知道怎么解决这个问题吗 我进行的一个简单的测试(小图) 我的广度优先搜索算法Java 如何使图遍历算法适用于大型图?,java,data-structures,breadth-first-search,infinite,graph-traversal,Java,Data Structures,Breadth First Search,Infinite,Graph Traversal,我正在编写一个算法,在给定源和目标时遍历图形。算法应该遍历并在图中显示从源到目标的所有可能路径。我使用广度优先搜索来实现这一点,它在一个小图形中工作。但是,当提供一个巨大的图形(有1000多个顶点)时,程序似乎被冻结,并且在很长一段时间后不会显示任何输出。我能知道怎么解决这个问题吗 我进行的一个简单的测试(小图) 我的广度优先搜索算法 // SUBMODULE: bfs // IMPORT: src (String), dest (String) // EXPORT: T (String) p
// SUBMODULE: bfs
// IMPORT: src (String), dest (String)
// EXPORT: T (String)
public String bfs( String src, String dest )
{
String T = "";
DSAQueue Q = new DSAQueue(); // The queue will store linked list
DSALinkedList path = null; // This is the list for queue
DSALinkedList adj = null; // Adjacency for src
adj = getAdjacent( src );
// Make adjacent to linked list first and store in queue
for( Object o : adj )
{
DSALinkedList ll = new DSALinkedList(); // Creating list for every iteration
DSAGraphVertex vertex = (DSAGraphVertex) o;
ll.insertLast( vertex ); // Every adjacent vertex is ele of list
Q.enqueue( ll ); // Store the list to queue
}
// ASSERTION: We Iterate until Q is empty, Q will store all L[V]
while( !Q.isEmpty() )
{
path = (DSALinkedList) Q.dequeue(); // Dequeue a linked list
// Get the tail of list
DSAGraphVertex v = (DSAGraphVertex) path.peekLast();
// We found the complete list path when the tail is dest
if( v.getLabel().equals(dest) )
{
T += src + " -> ";
for( Object o : path )
{
DSAGraphVertex pathVertex = (DSAGraphVertex) o;
T += pathVertex.getLabel() + " -> ";
}
T += "(END)\n";
}
else
{
// If v.getLabel() is not dest, we need to do bfs from adj of tail
DSALinkedList tailAdj = v.getAdjacent();
// Check the number of paths in this vertex
if( v.getSize() == 1 )
{
/* If only one path found, simply traverse to the only one path */
DSAGraphVertex headVertex = (DSAGraphVertex) tailAdj.peekFirst(); // head of the tailAdj
path.insertLast( headVertex );
Q.enqueue( path );
headVertex = null;
}
else
{
/* If the vertex has more than one path, copy all the existing paths for
every single connection, then insert the new node to each list */
for( Object o : tailAdj )
{
DSALinkedList newList = new DSALinkedList();
newList = copyList( path ); // Copy the existing
// Then add the new node into the copied list
DSAGraphVertex currVertex = (DSAGraphVertex) o;
newList.insertLast( currVertex );
// The queue now has a new list of paths
Q.enqueue( newList );
newList = null;
}
}
tailAdj = null;
}
path = null;
}
return T;
}
1000个节点并不是一个很大的图。我想象你的代码中有一个无限循环。让我们看看这里有一个无限循环的可能位置:
while(!Q.isEmpty())
啊,是的,我没有看到任何东西阻止您向队列添加节点。队列应该只处理每个节点一次。您需要保留已添加到队列且不允许再次添加的节点列表
它不会很快停止,因为您从未停止向队列中添加内容,所以它永远不会为空
这一天的教训是:当你有一个循环只有在满足某个条件时才结束时,要加倍确保该条件能够满足
// SUBMODULE: bfs
// IMPORT: src (String), dest (String)
// EXPORT: T (String)
public String bfs( String src, String dest )
{
String T = "";
DSAQueue Q = new DSAQueue(); // The queue will store linked list
DSALinkedList path = null; // This is the list for queue
DSALinkedList adj = null; // Adjacency for src
adj = getAdjacent( src );
// Make adjacent to linked list first and store in queue
for( Object o : adj )
{
DSALinkedList ll = new DSALinkedList(); // Creating list for every iteration
DSAGraphVertex vertex = (DSAGraphVertex) o;
ll.insertLast( vertex ); // Every adjacent vertex is ele of list
Q.enqueue( ll ); // Store the list to queue
}
// ASSERTION: We Iterate until Q is empty, Q will store all L[V]
while( !Q.isEmpty() )
{
path = (DSALinkedList) Q.dequeue(); // Dequeue a linked list
// Get the tail of list
DSAGraphVertex v = (DSAGraphVertex) path.peekLast();
// We found the complete list path when the tail is dest
if( v.getLabel().equals(dest) )
{
T += src + " -> ";
for( Object o : path )
{
DSAGraphVertex pathVertex = (DSAGraphVertex) o;
T += pathVertex.getLabel() + " -> ";
}
T += "(END)\n";
}
else
{
// If v.getLabel() is not dest, we need to do bfs from adj of tail
DSALinkedList tailAdj = v.getAdjacent();
// Check the number of paths in this vertex
if( v.getSize() == 1 )
{
/* If only one path found, simply traverse to the only one path */
DSAGraphVertex headVertex = (DSAGraphVertex) tailAdj.peekFirst(); // head of the tailAdj
path.insertLast( headVertex );
Q.enqueue( path );
headVertex = null;
}
else
{
/* If the vertex has more than one path, copy all the existing paths for
every single connection, then insert the new node to each list */
for( Object o : tailAdj )
{
DSALinkedList newList = new DSALinkedList();
newList = copyList( path ); // Copy the existing
// Then add the new node into the copied list
DSAGraphVertex currVertex = (DSAGraphVertex) o;
newList.insertLast( currVertex );
// The queue now has a new list of paths
Q.enqueue( newList );
newList = null;
}
}
tailAdj = null;
}
path = null;
}
return T;
}