Guest User

Untitled

a guest
May 27th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.97 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <math.h>
  5.  
  6. #define MAX_HOSTS_NUM 200
  7.  
  8. #define MAX_LINE_LENGTH 100000
  9. #define MAX_HOSTNAME_LENGTH 200
  10.  
  11. int readHostNames( FILE *fpin, char *host_names[]);
  12.  
  13. void initStructures( int Oij[][MAX_HOSTS_NUM], int Oj[],
  14. int Iij[][MAX_HOSTS_NUM+1], float PR[],
  15. int hosts_num);
  16.  
  17. void readHostGraph_ComputeOij( FILE *fpin, int Oij[][MAX_HOSTS_NUM],
  18. int hosts_num);
  19.  
  20. void computeOj( int Oj[], int Oij[][MAX_HOSTS_NUM], int hosts_num);
  21.  
  22. void computeIij( int Iij[][MAX_HOSTS_NUM+1], int Oij[][MAX_HOSTS_NUM],
  23. int hosts_num);
  24.  
  25. void computePR( float PR[], int Oij[][MAX_HOSTS_NUM],
  26. int Oj[], int Iij[][MAX_HOSTS_NUM+1], int hosts_num);
  27.  
  28. void printOij( int Oij[][MAX_HOSTS_NUM], int hosts_num);
  29. void printOj( int Oj[], int hosts_num);
  30. void printIij( int Iij[][MAX_HOSTS_NUM+1], int hosts_num);
  31. void printPR( float PR[], int hosts_num, char *host_names[]);
  32.  
  33. float euclideanDistance( float PR[], float next_PR[], int length);
  34.  
  35. void strRemoveNewLineChar( char str[]);
  36. FILE *openFile( char *filename, char *mode);
  37.  
  38.  
  39. int main(int argc, char *argv[])
  40. {
  41. FILE *fpin;
  42.  
  43. char *host_names[MAX_HOSTS_NUM];
  44.  
  45. int Oij[MAX_HOSTS_NUM][MAX_HOSTS_NUM],
  46. Oj[MAX_HOSTS_NUM],
  47. Iij[MAX_HOSTS_NUM][MAX_HOSTS_NUM+1],
  48. hosts_num;
  49.  
  50. float PR[MAX_HOSTS_NUM];
  51.  
  52. if (argc != 3){
  53. printf("Wrong number of arguments.\n");
  54. printf("Usage: %s hostnames_file hostgraph_file\n", argv[0]);
  55. exit(1);
  56. }
  57.  
  58.  
  59. fpin= openFile(argv[1], "r");
  60. hosts_num= readHostNames(fpin, host_names);
  61. fclose(fpin);
  62.  
  63. initStructures(Oij, Oj, Iij, PR, hosts_num);
  64.  
  65. fpin= openFile(argv[2], "r");
  66. readHostGraph_ComputeOij(fpin, Oij, hosts_num);
  67. fclose(fpin);
  68.  
  69. computeOj(Oj, Oij, hosts_num);
  70. computeIij(Iij, Oij, hosts_num);
  71. computePR(PR, Oij, Oj, Iij, hosts_num);
  72.  
  73. printOij(Oij, hosts_num);
  74. printOj(Oj, hosts_num);
  75. printIij(Iij, hosts_num);
  76. printPR(PR, hosts_num, host_names);
  77.  
  78. return 0;
  79. }
  80.  
  81.  
  82.  
  83. int readHostNames( FILE *fpin, char *host_names[])
  84. {
  85. /* Reads host names and host ids from file fpin.
  86. * Computes host_names[].
  87. *
  88. * The structure of each line of fpin is:
  89. * id name
  90. *
  91. * Host with id=i and name=host_name is stored in
  92. * host_names[i]==host_name.
  93. *
  94. * It assumes hosts ids start from 0 and are incremented by 1
  95. * (but they can be in any order in file fpin, e.g.
  96. * host with id=2 first, host with id=0 second,
  97. * host with id=1 third etc.
  98. *
  99. * Returns the number of hosts read.
  100. */
  101.  
  102. char line[MAX_LINE_LENGTH], line_copy[MAX_LINE_LENGTH],
  103. *delimeter=" ", *host_name, *token;
  104.  
  105. int host_id, hosts_num, line_num;
  106.  
  107.  
  108. hosts_num= 0;
  109. line_num= 0;
  110.  
  111. while (fgets(line, MAX_LINE_LENGTH, fpin) != NULL){
  112. line_num++;
  113. strcpy(line_copy, line);
  114.  
  115. strRemoveNewLineChar(line);
  116.  
  117. /* We tokenize each line of file fpin,
  118. based on the characters of delimeter string. */
  119.  
  120. token= strtok(line, delimeter);
  121.  
  122. if (token == NULL){
  123. printf("\n* Input file has bad structure \
  124. at the beggining of line: %d.\n",line_num);
  125. printf("* Line itself follows (without the ||):\n");
  126. printf("|%s|\n", line_copy);
  127. printf("* Exiting.\n");
  128. exit(3);
  129. }
  130.  
  131. /* First token is the host id. */
  132. host_id= atoi(token);
  133.  
  134.  
  135. token= strtok(NULL, delimeter);
  136.  
  137. if (token == NULL){
  138. printf("\n* Input file has bad structure \
  139. at line: %d.\n", line_num);
  140. printf("* Line itself follows (without the ||):\n");
  141. printf("|%s|\n", line_copy);
  142. printf("* Exiting.\n");
  143. exit(3);
  144. }
  145.  
  146. /* Second token is the host name.
  147. * Note that we have to allocate memory. */
  148. host_name= (char *)malloc( strlen(token) * sizeof(char) );
  149.  
  150. if (host_name == NULL){
  151. printf("\n* Error in allocating memory in readHostNames.\n");
  152. printf("Exiting.\n");
  153. exit(3);
  154. }
  155.  
  156. strcpy(host_name, token);
  157.  
  158. /* Note: strcpy(host_names[host_id], host_name)
  159. * would be wrong (why?). */
  160.  
  161. host_names[host_id]= host_name;
  162.  
  163. hosts_num++;
  164. } /* end while */
  165.  
  166. return hosts_num;
  167. }
  168.  
  169.  
  170.  
  171. void initStructures( int Oij[][MAX_HOSTS_NUM], int Oj[],
  172. int Iij[][MAX_HOSTS_NUM+1],
  173. float PR[], int hosts_num)
  174. {
  175. /* Initialises our array structures. */
  176.  
  177. int i, j;
  178.  
  179.  
  180. for (i= 0; i<= hosts_num-1; i++){
  181. Oj[i]= 0;
  182. PR[i]= 1.0;
  183.  
  184. for (j=0; j<= hosts_num-1; j++){
  185. Oij[i][j]= 0;
  186. Iij[i][j]= 0;
  187. }
  188. }
  189.  
  190. return;
  191. }
  192.  
  193.  
  194.  
  195. void readHostGraph_ComputeOij( FILE *fpin, int Oij[][MAX_HOSTS_NUM],
  196. int hosts_num){
  197.  
  198. /* Reads Oij information from fpin, and computes Oij[]][].
  199. *
  200. * The structure of each line of fpin is:
  201. * src -> dest1:nlinks1 dest2:nlinks2 ... destk:nlinksk
  202. *
  203. * Oij[i][j] is the number of links from
  204. * host with id=j towards host with id=i,
  205. * and is already initialised to 0.
  206. */
  207.  
  208. char line[MAX_LINE_LENGTH], line_copy[MAX_LINE_LENGTH],
  209. *delimeter_start=" ->", delimeter_following= ':',
  210. *token, *char_ptr;
  211.  
  212. int host_i, host_j, links_num, line_num;
  213.  
  214. line_num= 0;
  215.  
  216. while (fgets(line, MAX_LINE_LENGTH, fpin) != NULL){
  217.  
  218. line_num++;
  219. strcpy(line_copy, line);
  220.  
  221. strRemoveNewLineChar(line);
  222.  
  223. /* We tokenize each line of file fpin,
  224. * based on the characters of delimeter_start string. */
  225.  
  226. token= strtok(line, delimeter_start);
  227.  
  228. if (token == NULL){
  229. printf("\n* Input file has bad structure \
  230. at the beggining of line: %d.\n", line_num);
  231. printf("* Line itself follows (without the ||):\n");
  232. printf("|%s|\n", line_copy);
  233. printf("* Exiting.\n");
  234. exit(3);
  235. }
  236.  
  237. /* First token is host_j id. */
  238. host_j= atoi(token);
  239.  
  240.  
  241. /* We tokenize the rest of each line of file fpin,
  242. * based on the characters of delimeter_start string.
  243. * Space character of delimeter_start will now be used.*/
  244.  
  245. while ( (token= strtok(NULL, delimeter_start)) != NULL){
  246. /* Each next token has the form host_i_id:nlinksi, where
  247. * nlinksi is the number of links from host_j to host_i. */
  248.  
  249. /* delimeter_following is char ':'. */
  250. char_ptr= strchr(token, delimeter_following);
  251.  
  252. if (char_ptr == NULL){
  253. printf("\n* Input file has bad structure at some part \
  254. of line: %d.\n", line_num);
  255. printf("* Line itself follows (without the ||):\n");
  256. printf("|%s|\n", line_copy);
  257. printf("* Exiting.\n");
  258.  
  259. exit(3);
  260. }
  261.  
  262. /* Now char_ptr points to ':' of the token
  263. * of the form host_i_id:nlinksi, so,
  264. * to retrieve the nlinksi part, we use char_ptr+1. */
  265.  
  266. links_num= atoi(char_ptr + 1);
  267.  
  268. /* We don't need the nlinksi part of token any more,
  269. * so we discard it in order to retrieve
  270. * the first host_i_id part of the token. */
  271.  
  272. *char_ptr='\0';
  273. host_i= atoi(token);
  274. Oij[host_i][host_j]+= links_num;
  275.  
  276. } /* end while */
  277. } /* end while */
  278.  
  279. return;
  280. }
  281.  
  282.  
  283.  
  284. void computeOj( int Oj[], int Oij[][MAX_HOSTS_NUM], int hosts_num)
  285. {
  286. /* Computes Oj[], using Oij[][].
  287. *
  288. * Oj[j] is the total number of links starting from
  289. * host with id=j, and is already initialised to 0.
  290. */
  291.  
  292. int i, j;
  293.  
  294.  
  295. /* We just have to run through Oij[][] by columns. */
  296. for (j= 0; j<= hosts_num-1; j++)
  297. for (i= 0; i<= hosts_num-1; i++)
  298. Oj[j]+= Oij[i][j];
  299.  
  300. return;
  301. }
  302.  
  303.  
  304.  
  305. void computeIij( int Iij[][MAX_HOSTS_NUM+1], int Oij[][MAX_HOSTS_NUM],
  306. int hosts_num)
  307. {
  308. /* Computes Iij[][], using Oij[][].
  309. *
  310. * The structure of each line i of Iij[i][], for host i, is:
  311. * Iij[i][0] contains the total number of hosts pointing
  312. * to host i (hosts_pointingto_i). Each of the
  313. * following cells Iij[i][j], 1<= j <=hosts_pointingto_i,
  314. * contains the host_id hj of host hj pointing to host i.
  315. *
  316. * Iij[][] is already initialised to 0.
  317. */
  318.  
  319. int i, j, hosts_pointingto_i;
  320.  
  321.  
  322. hosts_pointingto_i= 0;
  323.  
  324. /* We run through Oij[][] by rows. */
  325.  
  326. for (i= 0; i<= hosts_num-1; i++){
  327.  
  328. for (j= 0; j<= hosts_num-1; j++){
  329. if (Oij[i][j] != 0){
  330. hosts_pointingto_i++;
  331. Iij[i][hosts_pointingto_i]= j;
  332. }
  333. }
  334. Iij[i][0]= hosts_pointingto_i;
  335. hosts_pointingto_i= 0;
  336. }
  337. return;
  338. }
  339.  
  340.  
  341. void computePR( float PR[], int Oij[][MAX_HOSTS_NUM],
  342. int Oj[], int Iij[][MAX_HOSTS_NUM+1], int hosts_num){
  343.  
  344. /* Computes array PR[], using arrays Oij[][], Oj[] and Iij[][].
  345. *
  346. * PR[i] is the rank of host with id=i,
  347. * and is already initialised.
  348. */
  349.  
  350. const float d= 0.85;
  351. const float limit= 0.01;
  352.  
  353. float *next_PR, first_factor, second_factor, sum_factor,
  354. euclidean_distance;
  355.  
  356. int i, j, hj, hosts_pointingto_i;
  357.  
  358. /* Allocate memory for next_PR[hosts_num]. */
  359.  
  360. next_PR= (float *)calloc(hosts_num, sizeof(float));
  361.  
  362. if (next_PR == NULL){
  363. printf("*Out of memory, trying to allocate next_PR.\nExiting.\n");
  364. exit(2);
  365. }
  366.  
  367. /* Initial values. */
  368. first_factor= (1-d)/hosts_num;
  369. sum_factor= 0.0;
  370.  
  371. int loop= 1;
  372.  
  373. while (1){
  374. /* We calculate the next_PR[] vector. */
  375. for (i= 0; i<= hosts_num-1; i++){
  376.  
  377. hosts_pointingto_i= Iij[i][0];
  378.  
  379. for (j= 1; j<= hosts_pointingto_i; j++){
  380.  
  381. hj= Iij[i][j];
  382. if (Oj[hj] != 0){
  383. sum_factor+= (PR[hj] * Oij[i][hj]) / Oj[hj];
  384. }
  385. else{
  386. printf("\n*!* This cannot happen in function computePR");
  387. printf("\nExiting.\n");
  388. exit(4);
  389. }
  390. }
  391. second_factor= d * sum_factor;
  392. next_PR[i]= first_factor + second_factor;
  393.  
  394. sum_factor= 0.0;
  395. }
  396.  
  397. euclidean_distance= euclideanDistance(PR, next_PR, hosts_num);
  398.  
  399. loop++;
  400.  
  401.  
  402. if (euclidean_distance < limit){
  403. free(next_PR);
  404. break;
  405. }
  406. else{
  407. for (i= 0; i<= hosts_num-1; i++){
  408. PR[i]= next_PR[i];
  409. }
  410. }
  411. }
  412. return;
  413. }
  414.  
  415.  
  416.  
  417. float euclideanDistance( float PR[], float next_PR[], int length)
  418. {
  419. /* Calculates the Euclidean distance of two vectors PR[]
  420. * and next_PR of length length.
  421. *
  422. * The Euclidean distance of two vectors X, Y, where
  423. * X=(x1, x2, ..., xn) and Y=(y1, y2, ..., yn), is given by
  424. * the square root of the sum of the squares of the differences
  425. * of their elements, i.e. sqrt(sum((xi - yi)^2)) */
  426.  
  427. float eucl_dist, diff, sum;
  428. int i;
  429.  
  430. sum= 0.0;
  431.  
  432. for (i= 0; i<= length-1; i++){
  433. diff= PR[i] - next_PR[i];
  434. sum+= diff * diff;
  435. }
  436.  
  437. eucl_dist= sqrt(sum);
  438.  
  439. return eucl_dist;
  440. }
  441.  
  442.  
  443.  
  444. void printOij( int Oij[][MAX_HOSTS_NUM], int hosts_num)
  445. {
  446. int i, j, links_num;
  447.  
  448.  
  449. printf("\n+++ Start of printing structure Oij +++\n");
  450. printf("The structure of each line is:\n");
  451. printf("src -> dest1:nlinks1 dest2:nlinks2 ... destk:nlinksk\n\n");
  452.  
  453. for (j= 0; j<= hosts_num-1; j++){
  454.  
  455. printf("%d ->", j);
  456.  
  457. for (i= 0; i<= hosts_num-1; i++){
  458. links_num= Oij[i][j];
  459. if (links_num > 0){
  460. printf(" %d:%d", i, links_num);
  461. }
  462. }
  463.  
  464. printf("\n");
  465.  
  466. }
  467.  
  468. printf("\n--- End of printing structure Oij ---\n");
  469. return;
  470. }
  471.  
  472.  
  473.  
  474. void printOj( int Oj[], int hosts_num)
  475. {
  476. int j;
  477.  
  478. printf("\n\n+++ Start of printing structure Oj +++\n");
  479. printf("The structure of each line is:\n");
  480. printf("src -=> links_total\n\n");
  481.  
  482. for (j= 0; j<= hosts_num-1; j++){
  483. printf("%d -=> %d\n", j, Oj[j]);
  484. }
  485.  
  486. printf("\n--- End of printing structure Oj ---\n");
  487.  
  488. return;
  489. }
  490.  
  491.  
  492.  
  493. void printIij( int Iij[][MAX_HOSTS_NUM+1], int hosts_num)
  494. {
  495. int i, j, hosts_pointingto_i;
  496.  
  497.  
  498. printf("\n\n+++ Start of printing structure Iij +++\n");
  499. printf("The structure of each line is:\n");
  500. printf("dest <== hosts_total hostid1 hostid2 ... hostidk\n\n");
  501.  
  502. for (i= 0; i<= hosts_num-1; i++){
  503.  
  504. hosts_pointingto_i= Iij[i][0];
  505.  
  506. printf("%d <== %d", i, hosts_pointingto_i);
  507. for (j= 1; j<= hosts_pointingto_i; j++){
  508. printf(" %d", Iij[i][j]);
  509. }
  510. printf("\n");
  511. }
  512.  
  513. printf("\n--- End of printing structure Iij ---\n");
  514. return;
  515. }
  516.  
  517.  
  518. void printPR( float PR[], int hosts_num, char *host_names[])
  519. {
  520. const float UNDEF= -1;
  521. float *norm_PR, sum_PR;
  522. int i;
  523.  
  524.  
  525. /* Allocate memory for norm_PR[hosts_num]. */
  526.  
  527. norm_PR= (float *)calloc(hosts_num, sizeof(float));
  528.  
  529. if (norm_PR == NULL){
  530. printf("*Out of memory, trying to allocate next_PR.\nExiting.\n");
  531. exit(2);
  532. }
  533.  
  534. /* Compute normalised PR, norm_PR[]= PR[i] / sum(PR[i]). */
  535.  
  536. sum_PR= 0.0;
  537.  
  538. for (i= 0; i<= hosts_num-1; i++){
  539. sum_PR+= PR[i];
  540. }
  541.  
  542. for (i= 0; i<= hosts_num-1; i++){
  543.  
  544. if (sum_PR != 0){
  545. norm_PR[i]= PR[i] / sum_PR;
  546. }
  547. else{
  548. norm_PR[i]= UNDEF;
  549. }
  550. }
  551.  
  552.  
  553. printf("\n\n+++ Start of printing PR +++\n");
  554. printf("\nhost_id\thost_name\thost_rank\tnorm_host_rank\n");
  555.  
  556. for (i= 0; i<= hosts_num-1; i++){
  557. printf("%d\t%s\t%.3f\t%.2f\n", i, host_names[i],
  558. PR[i], norm_PR[i]);
  559. }
  560.  
  561. printf("\n--- End of printing PR ---\n");
  562.  
  563. free(norm_PR);
  564. return;
  565. }
  566.  
  567.  
  568.  
  569. void strRemoveNewLineChar( char str[])
  570. {
  571. char new_line_char= '\n', carriage_return_char='\r', *position;
  572.  
  573. /* There is the DOS/Unix CR-LF matter
  574. * (CarriageReturn - LineFeed, LF is the new line character. */
  575.  
  576. /* Set position to point to carriage_return_char of str. */
  577. position= strchr(str, carriage_return_char);
  578.  
  579. /* If there is no carriage_return_char in str. */
  580. if (position == NULL){
  581. /* Set position to point to new_line_char of str. */
  582. position= strchr(str, new_line_char);
  583. }
  584.  
  585. /* If there is carriage_return_char or new_line_char in str. */
  586. if (position != NULL){
  587. *position= '\0';
  588. }
  589.  
  590. return;
  591. }
  592.  
  593.  
  594.  
  595.  
  596. FILE *openFile( char *filename, char *mode)
  597. {
  598. FILE *fp;
  599.  
  600.  
  601. fp= fopen(filename, mode);
  602.  
  603. if (fp==NULL){
  604. printf("Cannot open file: %s.\nExiting.\n", filename);
  605. exit(1);
  606. return NULL;
  607. }
  608. else{
  609. return fp;
  610. }
  611. }
Add Comment
Please, Sign In to add comment