If I want to pass the comparator function in the sort function then how to write it ?
Kruscal's Code Doubt
hello @avijuneja2007
u need to write ur own compare function and then pass that function in sort function.
check this article-> https://www.geeksforgeeks.org/sort-c-stl/
In this code I have not created any edge class. So how to write the compare function for vector<vector> edges(m) ?
bool cmp( vector< int > a, vector< int > b ){
here ur comparision logic
}
what will be the comparison logic as I have not made any edge class in my code ?
u have used vector ,so u need to compare two vector.
and in vector u have stored weight at index 0 so basically u need to compare 0th index of two vector.
bool cmp( vector< int > a, vector< int > b ){
return a[0] <= b[0]; // sort in increasing order of weight
}
1 Like