0

This version of Kruskal's algorithm represents the edges with a adjacency list. How would I modify the pseudo-code to instead use a adjacency matrix?

Kruskal Pseudo-code

I was thinking you we would need to use the weight of edges for instance (i,j), as long as its not zero. Assigning the vertices to i,j. I may be a bit confused on this pseudo-code of Kruskals.

2
  • 2
    There is nothing in the pseudocode specifying which data structures have to be used. But sorting the edges by weight will be hard in a matrix without an auxiliary representation. Commented Nov 22, 2016 at 5:18
  • For what it's worth, this pseudocode closely matches that seen on Wikipedia, which in the preamble specifies use of a disjoint-set data structure. Commented May 31, 2019 at 12:16

1 Answer 1

2

As pointed out by Henry the pseudocode did not specify what concrete data structures to be used. It just appears that the adjacency list representation of graph is more convenient than the adjacency matrix representation in this case.

For adjacency matrix, you simply have to scan every entries of your matrix to sort the edges of graph G on line 4. And you are doing exactly the same thing when using the adjacency list representation.

In your case you may, for example, use a PriorityQueue to sort the edges by weight in non-decreasing order and discard entries with disconnected vertices. You can then iterate this data structure in the for-loop on line 5.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.