Prim’s Algorithm
At each stage, include the least cost edge whose addition to the selected edges forms a tree.
The approach takes O(|E| log |V |) time for a graph G = (V, E). The time requirement is driven by the algorithm used for selecting the edges to be added in each stage.
- Assume a priority queue Q for the nodes of the graph that have not been chosen yet.
- For the priority evaluation, assign each node in Q the least cost of adding it into V - Q (i.e., the nodes already selected for inclusion the spanning tree T).
- Initially, the priority queue is a complete tree, and each node carries a priority value larger than the cost of all the edges.
- Upon removing a node u from the queue Q, and adding it into the spanning tree T, the priority value is modified for each node v in Q that is adjacent to u. The changing of the priority value of a node may require the shifting of the up or down in the tree.
The initialization of the the priority queue takes O(|V |) time. Each deletion of a node from the queue takes O(log |V |) time, and so O(|V | log |V |) time takes to empty the queue. A modification of a priority value of a node in the queue takes O(log |V |) time, and |E| such changes are needed.
Consequently, the algorithm takes O(|V |) + O(|V | log |V |) + O(|E| log |V |) = O(|E| log |V |) time.
Kruskal’s Algorithm
A greedy algorithm: Visit the edges in order of increasing cost. Include just those edges that don’t create cycles.
Kruskal’s algorithm requires O(|E| log |E|) time for sorting the edges, O(|E|) time to traversing them, and O(|V | log |V |) time for checking the existence of cycles (employing the union-find algorithm below). Hence, the algorithm is of time complexity O(|E| log |E|).
Union-Find Algorithm
Kruskal’s algorithm relies on a union-find algorithm for checking cycles. The “union” operation unites equivalent sets of elements, and the “find” operation determines the sets in which the elements reside.
No comments:
Post a Comment