Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing Kruskal's algorithm in Ada, not sure where to start

With reference to Kruskal's algorithm in Ada, I'm not sure where to start.

I'm trying to think through everything before I actually write the program, but am pretty lost as to what data structures I should be using and how to represent everything.

My original thought is to represent the full tree in an adjacency list, but reading Wikipedia the algorithm states to create a forest F (a set of trees), where each vertex in the graph is a separate tree and I'm not sure how to implement this without getting really messy quickly.

The next thing it says to do is create a set S containing all the edges in the graph, but once again I'm not sure what the best way to do this would be. I was thinking of an array of records, with a to, from and weight, but I'm lost on the forest.

Lastly, I'm trying to figure out how I would know if an edge connects two trees, but again am not sure what the best way to do all of this is.

like image 976
cheezone Avatar asked Oct 17 '11 02:10

cheezone


People also ask

How is Kruskal's algorithm implemented?

The steps for implementing Kruskal's algorithm are as follows: Sort all the edges from low weight to high. Take the edge with the lowest weight and add it to the spanning tree. If adding the edge created a cycle, then reject this edge.

What algorithm technique is use in the implementation of Kruskal's solution for the MST?

Explanation: Kruskal's algorithm uses a greedy algorithm approach to find the MST of the connected weighted graph. In the greedy method, we attempt to find an optimal solution in stages.

Which edge does Kruskal's algorithm add first to the tree that it produces?

In Kruskal's algorithm, at each iteration we will select the edge with the lowest weight. So, we will start with the lowest weighted edge first i.e., the edges with weight 1.


2 Answers

I can see where their algorithm description would leave you confused as how to start. It left me the same way.

I'd suggest reading over the later Example section instead. That makes it pretty clear how to proceed, and you can probably come up with the data structures you would need to do it just from that.

It looks like the basic idea is the following:

  • Take the graph, find the shortest edge that introduces at least one new vertex, and put it in your "spanning tree".
  • Repeat the step above until you have every vertex.
like image 58
T.E.D. Avatar answered Sep 30 '22 06:09

T.E.D.


The "create a forest part" really means: implement the pseudocode from the page Disjoint-set data structure. If you can read C++, then I have a pretty straightforward implementation here. (That implementation works, I've used it to implement Kruskal's algo myself :)

like image 25
Fred Foo Avatar answered Sep 30 '22 05:09

Fred Foo