Testing for a circuit when implementing Kruskalls algorithm

谁说胖子不能爱 提交于 2019-12-05 03:40:50

Kruskall's algorithm won't searches for cycles, because It's not performance efficient; But tries to create a components which are tree, and then connect them to each other. As you know if you connect two different trees with one new edge you will create new tree and there is no need to check for cycles.

If you look at wiki page algorithm is as follow:

1. create a forest F (a set of trees), where each vertex in the graph is a separate tree
2. create a set S containing all the edges in the graph
3. while S is nonempty and F is not yet spanning
    a. remove an edge with minimum weight from S
    b. if that edge connects two different trees, then add it to the forest, combining 
       two trees into a single tree
    c. otherwise discard that edge.

You should use Disjoint Set Data Structure for this. again from wiki:

first sort the edges by weight using a comparison sort in O(E log E) time; this allows the step "remove an edge with minimum weight from S" to operate in constant time. Next, we use a disjoint-set data structure (Union&Find) to keep track of which vertices are in which components. We need to perform O(E) operations, two 'find' operations and possibly one union for each edge. Even a simple disjoint-set data structure such as disjoint-set forests with union by rank can perform O(E) operations in O(E log V) time. Thus the total time is O(E log E) = O(E log V).


Creating Disjoint Forests

Now you can take a look at Boost Graph Library-Incremental Components part. You should implement some methods: MakeSet, Find, Union, After that you can implement kruskall's algorithm. All you doing is working with some sets, and simplest possible way to do so is using linked list.

Each set has one element named as representative element which is first element in the set.

1- First implement MakeSet by linked lists:

This prepares the disjoint-sets data structure for the incremental connected components algorithm by making each vertex in the graph a member of its own component (or set).

You should just initialize each vertex (element) as a representative element of new set, you can do this by setting them as themselves parent:

 function MakeSet(x)
   x.parent := x

2- Implement Find method:

Find representative element of set which contains vertex x:

 function Find(x)
 if x.parent == x
    return x
 else
    return Find(x.parent)

The if part checks the element is representative element or not. we set all representative elements of sets as their first element by setting them as themselves parent.

3- And finally when you got all previous things simple part is implementing Union method:

function Union(x, y)
 xRoot := Find(x) // find representative element of first element(set)
 yRoot := Find(y) // find representative element of second element(set)
 xRoot.parent := yRoot // set representative element of first set 
                       // as same as representative element of second set

Now how you should run Kruskall?

First put all nodes in n disjoint sets by MakeSet method. In each step after finding desired edge (not checked and minimal one), find related sets of its endpoint vertices by Find method (their representative elements), if they are same, drop this edge out because this edge causes a cycle, but If they are in different sets, use Union method to merge these sets. Because each set is tree union of them is tree.

You can optimize this by choosing better data structure for disjoint sets, but for now I think is enough. If you are interested in more advanced data structures, you can implement rank base way, you will find it in wiki, It's easy but I didn't mention it to prevent from bewilderment.

If the vertices are labelled in some way all you need to do is check if both vertices of the edge selected has been visited previously which will indicate a loop.

So if its implemented with integers you could use a boolean array to mark which vertices have been selected.

boolean[] visited = new boolean[vertex-count-1];

Or if the vertices are labelled as strings you could add them to a Map for instance and check they have not already been added.

To check for circuits, you'll want to use a union-find data structure. The easiest way to do it is just with linked lists, but there are more efficient implementations. This link can help you if you want to implement one yourself.

http://en.wikipedia.org/wiki/Disjoint-set_data_structure

Or here's a link to a java implementation:

http://www.koders.com/java/fid125D835ECD1C1C701008C456A93A7179FA06D196.aspx

Basically, a union-find data structure allows you to track disjoint sets of elements, and supports two operations:

1) Find, which takes an element as an argument and tells you which set it is in
2) Union, which takes 2 disjoint sets and merges them

Let's say your array of edges that will form the MST is S. The idea is that you can make a set, C, using Union-Find, that tracks which vertices of the graph are connected by the edges in S. When C contains all the vertices in the graph, then you're finished, and checking to see if the addition of an edge will create a cycle amounts to checking whether the two vertices that it connects are already in C (by using two Find operations).

So if E is the set of all the edges in the graph, your update operation can proceed like so:

    Remove edge, e from E, with minimum weight (say that it connects vertices v1 and v2)
    Find(v1) and Find(v2)
    If v1 and v2 are both in C then continue
    Else, Union(C,{v1,v2}) and append e to S

And you stop the update once C contains every vertex of the graph.

If you check for cycle, using DFS it will be very inefficient. But you can use Disjoint Set. When connecting, you'll only need to check if your nodes are in same connected component.

Also note, that you have to check that your original is connected, because Kruskall algorithm will find spanning forest and not a spanning tree in that case.

Garen

The most powerful if not most common way to detect cycles is via Tarjan's SCC (Strongly Connected Components) algorithm. In any case, this question has already been answered:

Finding all cycles in a directed graph

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!