Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.15 KB | None | 0 0
  1. //kompilować z opcją -lm, tzn. np. gcc algorytm.c -lm
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5. #include <math.h>
  6. #define MLD 1000000000.0
  7. /////////////////////////////////////////////
  8. // PROCEDURY POMOCNICZE //
  9. /////////////////////////////////////////////
  10. void utworz_MACIERZ(int n, int ***M){
  11. // alokuj� pami�� na tablic� rozmiaru nxn
  12. // i wpisuje losowe warto�ci 0/1 w macierzy
  13. int i,j;
  14. (*M) = (int **)malloc(n*sizeof(int *));
  15. for(i=0;i<n;i++){
  16. (*M)[i]=(int *)malloc(n*sizeof(int));
  17. }
  18. for(i=0;i<n;i++){
  19. for(j=0;j<n;j++){
  20. (*M)[i][j]=rand()% 2;
  21. }
  22. }
  23. }
  24. /////////////////////////////////////////////
  25. void utworz_MACIERZ_x(int n, int ***M, int x){
  26. // alokuj� pami�� na tablic� rozmiaru nxn
  27. // i wpisuje do macierzy wsz�dzie warto�ci x
  28. int i,j;
  29. (*M) = (int **)malloc(n*sizeof(int *));
  30. for(i=0;i<n;i++){
  31. (*M)[i]=(int *)malloc(n*sizeof(int));
  32. }
  33. for(i=0;i<n;i++){
  34. for(j=0;j<n;j++){
  35. (*M)[i][j]=x;
  36. }
  37. }
  38. }
  39. /////////////////////////////////////////////
  40. void wypisz_MACIERZ(int n, int **M){
  41. // wypisuje warto�ci macierzy
  42. int i,j;
  43.  
  44. for(i=0;i<n;i++){
  45. for(j=0;j<n;j++)
  46. printf("%d ",M[i][j]);
  47. printf("\n");
  48. }
  49.  
  50. }
  51. /////////////////////////////////////////////
  52. void zwolnij_MACIERZ(int n, int **M){
  53. // zwalania pami�� zarezerwowan� dla macierzy
  54. int i;
  55. for(i=0;i<n;i++)
  56. {
  57. free(M[i]);
  58. }
  59. free(M);
  60. }
  61. /////////////////////////////////////////////
  62. // ALGORYTM PIERWSZY //
  63. /////////////////////////////////////////////
  64. int ALGO_NAIWNY(int n, int **M){
  65. int x1,y1,x2,y2,x,y;
  66. int max=0;
  67. int local_max=0;
  68.  
  69. for(x1=0;x1<n;x1++)
  70. for(y1=0;y1<n;y1++)
  71. for(x2=n-1;x2>x1-1;x2--)
  72. for(y2=n-1;y2>y1-1;y2--){
  73. local_max=0;
  74. for(x=x1;x<x2+1;x++)
  75. for(y=y1;y<y2+1;y++)
  76. local_max+=M[x][y];
  77. if ((local_max==(x2-x1+1)*(y2-y1+1)) && (local_max>max)) max=local_max;
  78. }
  79. return max;
  80. }
  81. /////////////////////////////////////////////
  82. // ALGORYTM DRUGI //
  83. /////////////////////////////////////////////
  84. int REKURENCJA(int **M,int x1, int y1, int x2, int y2){
  85. if ((x2==x1)&&(y2==y1)) return M[x1][y1];
  86. else if ((x2-x1)>(y2-y1))
  87. return REKURENCJA(M,x1,y1,(int)(x1+x2)/2,y2)*REKURENCJA(M,(int)(x1+x2+1)/2,y1,x2,y2);
  88. else return REKURENCJA(M,x1,y1,x2,(int)(y1+y2)/2)*REKURENCJA(M,x1,(int)(y1+y2+1)/2,x2,y2);
  89. }
  90. /////////////////////////////////////////////
  91. int ALGO_REKURENCYJNY(int n, int **M){
  92. int x1,y1,x2,y2;
  93. int max=0;
  94. int local_max;
  95.  
  96. for(x1=0;x1<n;x1++)
  97. for(y1=0;y1<n;y1++)
  98. for(x2=x1;x2<n;x2++)
  99. for(y2=y1;y2<n;y2++){
  100. local_max=REKURENCJA(M,x1,y1,x2,y2)*(x2-x1+1)*(y2-y1+1);
  101. if (local_max>max) max=local_max;
  102. }
  103. return max;
  104. }
  105. /////////////////////////////////////////////
  106. // ALGORYTM TRZECI //
  107. /////////////////////////////////////////////
  108. int ALGO_DYNAMICZNY(int n, int **M){
  109. int x1,x2,y;
  110. int max=0;
  111. int iloczyn;
  112. int **MM;
  113.  
  114. utworz_MACIERZ_x(n,&MM,0);
  115.  
  116. for(y=0;y<n;y++)
  117. for(x1=0;x1<n;x1++){
  118. iloczyn=1;
  119. for(x2=x1;x2<n;x2++){
  120. iloczyn*=M[x2][y];
  121. MM[x1][x2]=iloczyn*(x2-x1+1+MM[x1][x2]);
  122. if (MM[x1][x2]>max) max=MM[x1][x2];
  123. }
  124. }
  125. return max;
  126. }
  127. /////////////////////////////////////////////
  128. // ALGORYTM CZWARTY //
  129. /////////////////////////////////////////////
  130. int ALGO_CZULY(int n, int **M){
  131. int x1,y1,x2,y2,ymax;
  132. int max=0;
  133. int local_max=0;
  134.  
  135. for(x1=0;x1<n;x1++)
  136. for(y1=0;y1<n;y1++){
  137. local_max=0;
  138. x2=x1;
  139. ymax=n-1;
  140. while ((x2<n)&&(M[x2][y1]==1)){
  141. y2=y1;
  142. while((y2<ymax+1)&&(M[x2][y2]==1)){
  143. y2++;
  144. }
  145. ymax=y2-1;
  146. local_max=(x2-x1+1)*(ymax-y1+1);
  147. if (local_max>max) max=local_max;
  148. x2++;
  149. }
  150. }
  151. return max;
  152. }
  153. /////////////////////////////////////////////
  154. /////////////////////////////////////////////
  155. /////////////////////////////////////////////
  156. int main(){
  157. int n=20, x; //wymiar macierzy/
  158. int **Macierz;
  159. struct timespec tp0, tp1;
  160. double Tn,Fn;
  161. srand(time(NULL));
  162.  
  163. utworz_MACIERZ(n,&Macierz);
  164. //utworz_MACIERZ_x(n,&Macierz,0);
  165. //utworz_MACIERZ_x(n,&Macierz,1);
  166. wypisz_MACIERZ(n,Macierz);
  167.  
  168. //ALGORYTM NAIWNY//////////////////////////////////////////////////////////////////////////
  169. printf("\n\n----------------------------ALGORYTM NAIWNY----------------------------\n\n");
  170. for(x=n;x>0;x--){
  171. clock_gettime(CLOCK_PROCESS_CPUTIME_ID,&tp0);
  172. ALGO_NAIWNY(x,Macierz);
  173. clock_gettime(CLOCK_PROCESS_CPUTIME_ID,&tp1);
  174. Fn = x*x*x*x*x*x; //oszacowanie (dla wszystkich takie samo)
  175. Tn=(tp1.tv_sec+tp1.tv_nsec/MLD)-(tp0.tv_sec+tp0.tv_nsec/MLD);
  176. printf("n: %5d \tczas: %3.10lf \twspolczynnik: %3.5lf\n",x,Tn, Fn/Tn);
  177. }
  178.  
  179.  
  180. //ALGORYTM REKURENCYJNY////////////////////////////////////////////////////////////////////
  181. printf("\n\n-------------------------ALGORYTM REKURENCYJNY-------------------------\n\n");
  182. for(x=n;x>0;x--){
  183. clock_gettime(CLOCK_PROCESS_CPUTIME_ID,&tp0);
  184. ALGO_REKURENCYJNY(x,Macierz);
  185. clock_gettime(CLOCK_PROCESS_CPUTIME_ID,&tp1);
  186. Fn = n*n*n*n*log2(n); //oszacowanie (dla wszystkich takie samo)
  187. Tn=(tp1.tv_sec+tp1.tv_nsec/MLD)-(tp0.tv_sec+tp0.tv_nsec/MLD);
  188. printf("n: %5d \tczas: %3.10lf \twspolczynnik: %3.5lf\n",x,Tn, Fn/Tn);
  189. }
  190.  
  191. //ALGORYTM DYNAMICZNY/////////////////////////////////////////////////////////////////////
  192. printf("\n\n--------------------------ALGORYTM DYNAMICZNY--------------------------\n\n");
  193. for(x=n;x>0;x--){
  194. clock_gettime(CLOCK_PROCESS_CPUTIME_ID,&tp0);
  195. ALGO_DYNAMICZNY(x,Macierz);
  196. clock_gettime(CLOCK_PROCESS_CPUTIME_ID,&tp1);
  197. Fn = n*n*n; //oszacowanie (dla wszystkich takie samo)
  198. Tn=(tp1.tv_sec+tp1.tv_nsec/MLD)-(tp0.tv_sec+tp0.tv_nsec/MLD);
  199. printf("n: %5d \tczas: %3.10lf \twspolczynnik: %3.5lf\n",x,Tn, Fn/Tn);
  200. }
  201.  
  202. //ALGORYTM CZUŁY///////////////////////////////////////////////////////////////////////////
  203. printf("\n\n-----------------------------ALGORYTM CZUŁY----------------------------\n\n");
  204. for(x=n;x>0;x--){
  205. clock_gettime(CLOCK_PROCESS_CPUTIME_ID,&tp0);
  206. ALGO_CZULY(x,Macierz);
  207. clock_gettime(CLOCK_PROCESS_CPUTIME_ID,&tp1);
  208. Fn = n*n*n*n; //oszacowanie dla losowych liczb oraz dla macierzy samych jedynek
  209. //Fn = n*n; //oszacowanie dla macierzy samych zer
  210. Tn=(tp1.tv_sec+tp1.tv_nsec/MLD)-(tp0.tv_sec+tp0.tv_nsec/MLD);
  211. printf("n: %5d \tczas: %3.10lf \twspolczynnik: %3.5lf\n",x,Tn, Fn/Tn);
  212. }
  213. printf("\n");
  214.  
  215. //////////////////////////////////////////////////////////////////////////////
  216.  
  217. printf("Wynik algorytmu czułego = %d \n",ALGO_NAIWNY(n,Macierz));
  218. printf("Wynik algorytmu rekurencyjnego = %d \n",ALGO_REKURENCYJNY(n,Macierz));
  219. printf("Wynik algorytmu dynamicznego = %d \n",ALGO_DYNAMICZNY(n,Macierz));
  220. printf("Wynik algorytmu czułego = %d \n",ALGO_CZULY(n,Macierz));
  221.  
  222.  
  223. zwolnij_MACIERZ(n,Macierz);
  224.  
  225. return 1;
  226. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement