how to program Prim's and Kruskal's algorithm using adjacency lists in C

88 Views Asked by At

I have understood and implemented Prim's and Kruskal's algorithm using adjacency matrix but I am not understanding how to write a program using adjacency lists

I tried creating 2 matrices one for min weight for each edge and which is a two dimensional matrix and another matrix for the visited edges. But I couldn't proceed with that approach. Please provide an approach.

1

There are 1 best solutions below

0
gltronred On

Creating a two-dimensional matrix from the adjacency lists is not needed. It would be the same as using adjacency matrix.

You should either sort a list of all edges by their weights (in Kruskal's algorithm), or use a heap to find a minimal vertex (in Prim's algorithm).