Advertisement
Guest User

Untitled

a guest
Mar 19th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.91 KB | None | 0 0
  1. Prim's Algorithm(modified).cpp
  2.  
  3. #include<iostream>
  4. #include<cstdlib>
  5. #define WHITE 0 //not visited
  6. #define BLACK 1 //visited
  7. #define INF -1
  8. using namespace std;
  9. void create_nodes(int nodes, int** &edges, int* &parents, int* &colors){
  10.  
  11. edges=new int*[nodes];
  12. parents=new int[nodes];
  13. colors=new int[nodes];
  14.  
  15. }
  16. void populate_nodes(int nodes, int** &edges, int* &parents, int* &colors, int high, int low){
  17.  
  18. for(int i=0; i<nodes; i++){
  19. edges[i]=new int[nodes];
  20. for(int j=0; j<nodes; j++){
  21. if(i!=j){
  22. edges[i][j]=low+rand()%(high-low+1);
  23. parents[i]=i;
  24. colors[i]= WHITE;
  25. }
  26. else{
  27. edges[i][j]=0;
  28. }
  29. }
  30. }
  31. }
  32. void display_graph(int nodes, int** edges){
  33. for(int i=0; i<nodes; i++){
  34. for(int j=0; j<nodes; j++){
  35. cout<<edges[i][j]<<" ";
  36. }
  37. cout<<endl;
  38. }
  39. }
  40. int minValue(int nodes, int* minWeightOfEdges, int* nonMstList){
  41. int minValue=INF;
  42. int minIndex;
  43. for(int i=0; i<nodes; i++){
  44. //nonMSTList is not visited && minWeightOfEdges is less than min Value
  45. if(nonMstList[i]==WHITE && minWeightOfEdges[i]<minValue){
  46. //update minValue with minWeightofEdges[i]
  47. minValue=minWeightOfEdges[i];
  48. minIndex=i;
  49. }
  50. }
  51. return minIndex;
  52. }
  53. void display_MST(int* parent,int nodes, int** edges){
  54. cout<<"Edge: "<<"Weight: "<<endl;
  55. for(int i=0; i<nodes; i++){
  56. cout<<"Edge: "<<parent[i]<<" -> "<<i<<"Weight: "<<edges[i][parent[i]];
  57. }
  58. }
  59. void create_MST(int start,int nodes, int** edges, int* parent, int* colors,
  60. int* nonMstList, int* minWeightOfEdges){
  61. nonMstList=new int[nodes];
  62. minWeightOfEdges=new int[nodes];
  63. int minIndex;
  64. for(int i=start; i<nodes; i++){
  65. minWeightOfEdges[i]=INF;
  66. nonMstList[i]=WHITE;
  67. minWeightOfEdges[0]=0;
  68. parent[0]=INF;
  69. }
  70. for(int i=start; i<nodes-1; i++){
  71. minIndex=minValue(nodes,minWeightOfEdges,nonMstList);
  72. nonMstList[minIndex]=BLACK;
  73. }
  74. for(int i=start; i<nodes; i++){
  75. if(edges[minIndex][i] && nonMstList[i]==WHITE && edges[minIndex][i]<minWeightOfEdges[i]){
  76. parent[i]=minIndex;
  77. minWeightOfEdges[i]=edges[minIndex][i];
  78. }
  79. }
  80. display_MST(parent,nodes,edges);
  81. }
  82. int main(){
  83. int nodes;
  84. int high,low;
  85. cout<<"Enter number of nodes: ";
  86. cin>>nodes;
  87. cout<<"Enter range high, low: ";
  88. cin>>high>>low;
  89. cout<<endl;
  90. int **edges;
  91. int *parents;
  92. int *colors;
  93. create_nodes(nodes,edges,parents,colors);
  94. populate_nodes(nodes,edges,parents,colors,high,low);
  95. display_graph(nodes,edges);
  96. int startNode;
  97. cout<<"Enter Starting Node: ";
  98. cin>>startNode;
  99. cout<<endl;
  100. int *nonMstList;
  101. int *minWeightOfEdges;
  102. create_MST(startNode,nodes,edges,parents,colors,
  103. nonMstList,minWeightOfEdges);
  104. return 0;
  105. }
  106.  
  107.  
  108.  
  109.  
  110. LCS.cpp
  111.  
  112. #include<iostream>
  113. using namespace std;
  114. int maximum(int num1, int num2){
  115. if(num1>num2){
  116. return num1;
  117. }
  118. else{
  119. return num2;
  120. }
  121. }
  122. int lcs(string line1, string line2, int line1Size, int line2Size){
  123. int lengthOfLcs[line1Size+1][line2Size+1];
  124. for(int i=0; i<=line1Size; i++){
  125. for(int j=0; j<=line2Size; j++){
  126. if(i==0 && j==0){
  127. lengthOfLcs[i][j]=0;
  128. }
  129. else if(line1[i-1]==line2[j-1]){
  130. lengthOfLcs[i][j]=lengthOfLcs[i-1][j-1]+1;
  131. }
  132. else{
  133. lengthOfLcs[i][j]=maximum(lengthOfLcs[i-1][j],lengthOfLcs[i][j-1]);
  134. }
  135. }
  136. }
  137. return lengthOfLcs[line1Size][line2Size];
  138. }
  139. int main(){
  140. string line1;
  141. string line2;
  142. cout<<"Enter Line 1: ";
  143. cin>>line1;
  144. cout<<"Enter Line 2: ";
  145. cin>>line2;
  146. int line1Size= line1.size();
  147. int line2Size= line2.size();
  148. cout<<"LCS: "<<lcs(line1,line2,line1Size,line2Size);
  149. return 0;
  150. }
  151.  
  152.  
  153.  
  154.  
  155. Kruskal Algorithm.cpp
  156.  
  157. #include <cstdio>
  158. #include <algorithm>
  159. #include<iostream>
  160. using namespace std;
  161. struct Edge {
  162. int u;
  163. int v;
  164. int weight;
  165. };
  166. class UF{
  167. int *id;
  168. int cnt;
  169. int *sz;
  170. public:
  171. UF(int N){
  172. cnt = N;
  173. id = new int[N];
  174. sz = new int[N];
  175. for(int i=0; i<N; i++){
  176. id[i] = i;
  177. sz[i] = 1;
  178. }
  179. }
  180. ~UF(){
  181. delete [] id;
  182. delete [] sz;
  183. }
  184. int find(int p){
  185. int root = p;
  186. while (root != id[root]){
  187. root = id[root];
  188. }
  189. while (p != root){
  190. int newp = id[p];
  191. id[p] = root;
  192. p = newp;
  193. }
  194. return root;
  195. }
  196. void merge(int x, int y){
  197. int i = find(x);
  198. int j = find(y);
  199. if (i == j) return;
  200. if(sz[i] < sz[j]){
  201. id[i] = j; sz[j] += sz[i];
  202. }
  203. else{
  204. id[j] = i; sz[i] += sz[j];
  205. }
  206. cnt--;
  207. }
  208. bool connected(int x, int y){
  209.  
  210. return find(x) == find(y);
  211. }
  212. int count() {
  213.  
  214. return cnt;
  215. }
  216. };
  217. bool comp(Edge *a, Edge *b){
  218. return a->weight < b->weight;
  219. }
  220. int main(){
  221. int V;
  222. int E;
  223. int i;
  224. int N;
  225. int u;
  226. int v;
  227. int cost;
  228. Edge **edges;
  229. Edge **mst;
  230. scanf("%d %d", &V, &E);
  231. edges = new Edge*[E];
  232. for(i=0; i<E; i++){
  233. edges[i] = new Edge;
  234. scanf("%d %d %d", &(edges[i]->u), &(edges[i]->v), &(edges[i]->weight));
  235. }
  236. sort(edges, edges + E, comp);
  237. UF uf(V);
  238. mst = new Edge*[V-1];
  239. for(N=i=cost=0; i<E && N<V-1; i++){
  240. u = edges[i]->u; v = edges[i]->v;
  241. if(!uf.connected(u, v)){
  242. uf.merge(u, v);
  243. mst[N++] = edges[i];
  244. cost += edges[i]->weight;
  245. }
  246. }
  247. cout<<"Total cost of MST : %d\n"<<cost;
  248. cout<<"Edges in MST :\n";
  249. for(i=0; i<N; i++){
  250. cout<<("%d %d %d\n", mst[i]->u, mst[i]->v, mst[i]->weight);
  251. }
  252. return 0;
  253. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement