7

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.

templatetypedef
  • 328,018
  • 92
  • 813
  • 992
cheezone
  • 345
  • 1
  • 2
  • 9
  • Are you just having trouble with the algorithm or also with the Ada part? Do you have any references/books other then wikipedia? – hugomg Oct 17 '11 at 18:30
  • @BartKiers - Speculation on my part is that this is a class assignment. Many universities use Ada for their low-level CS courses, and this looks a lot like a typical school coding project. – T.E.D. Oct 17 '11 at 19:22
  • @T.E.D., ah, I see, thanks. I didn't know it was being used in university. Here in the Netherlands, I've never seen it used as part of a CS-course. – Bart Kiers Oct 17 '11 at 20:36
  • Try checking http://cs.fit.edu/~ryan/ada/programs/access/tree-adb.html — it should give you a hint on the tree part. As of the forest, I'm not sure Ada has resizable lists out of box, but if it is a homework, then you can probably choose a constant which will be an upper boundary for the task size. – alf Oct 17 '11 at 22:19
  • Ada doesn't generally need resizable lists... its array functionality can quite cover most cases. There's some really clever "work-arounds" on Ada's arrays. Here's one for [long] strings: http://www.adapower.com/index.php?Command=Class&ClassID=Basics&CID=202 – Shark8 Oct 18 '11 at 01:32
  • 1
    I think in this case you do need a resizable "list"; try `Ada.Containers.Vectors`. – Simon Wright Oct 18 '11 at 10:07

2 Answers2

3

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.
T.E.D.
  • 41,324
  • 8
  • 64
  • 131
2

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 :)

Fred Foo
  • 328,932
  • 68
  • 689
  • 800