Skip to content

Commit

Permalink
Update use of 3 arrays in prims, add strivers video link
Browse files Browse the repository at this point in the history
  • Loading branch information
nandwalritik authored Apr 26, 2022
1 parent c4f9165 commit 9fd1bc4
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions graph/prims.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
#include <bits/stdc++.h>
using namespace std;
/*
youtube :- https://www.youtube.com/watch?v=HnD676J56ak
LOGIC
1.) Maintain two sets
i.) First set contains vertices already included in MST
ii.) Second set contains vertices not yet included.
2.) At every step consider all edges that connects two sets
pick the minm weight edge from these edges.
-----------------------------------------------------------------------------------------------------------------
Use 3 arrays - keys,mstSet,parent
keys -> it contains current vertex se connected vertices ke edge ka weight (Initialized to INT_MAX)
mstSet -> boolean array used to maintain record of vertex included in min span tree
parent -> parent of vertices (It can be used to print min span tree at last)
*/
int main()
Expand Down Expand Up @@ -62,4 +67,4 @@ int main()
cout << parent[i] << " " << i << "\n";
}
return 0;
}
}

0 comments on commit 9fd1bc4

Please sign in to comment.