Advertisement
Guest User

Untitled

a guest
Sep 18th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.25 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int readGraph(int n, int m, int graph[]){ //Parse the graph. Where n is vert, and m is edge.
  5. int valid = 1;
  6. for(int i = 0; i < (m*2) && valid == 1; i=i+2){
  7. valid = scanf("%d", &graph[i]);
  8. if(valid != 1){
  9. printf("Invalid data.\n");
  10. return 1;
  11. }
  12. valid = scanf("%d", &graph[i+1]);
  13. if(valid != 1){
  14. printf("Invalid data.\n");
  15. return 1;
  16. }
  17. }
  18. if (valid != 1){
  19. printf("Invalid data.\n");
  20. return 1;
  21. }else{
  22. return 0;
  23. }
  24. }
  25.  
  26. int highestGraph(int n, int m, int graph[]){ //Check for the largest vertice in graph[]. Where n is vert, and m is edge.
  27. int chonk = 0;
  28. for(int i = 0; i < (m*2); i=i+2){
  29. if(graph[i] > chonk){
  30. chonk = graph[i];
  31. }else if(graph[i+1] > chonk){
  32. chonk = graph[i+1];
  33. }
  34. }
  35. return chonk;
  36. }
  37.  
  38. int main(int argc, char * * argv){
  39.  
  40. int verts = NULL;
  41. int edges = NULL;
  42. int chonk = NULL;
  43. int tf = NULL;
  44. int pathCur = NULL;
  45. int pathNex = NULL;
  46.  
  47. int valid = scanf("%d", &verts);
  48. if(valid != 1){
  49. printf("You nincompoop, put a valid value in.");
  50. return EXIT_FAILURE;
  51. }
  52. valid = scanf("%d", &edges);
  53. int graph[verts*verts];
  54.  
  55. if(valid != 1){
  56. printf("You nincompoop, put a valid value in.");
  57. return EXIT_FAILURE;
  58. }
  59.  
  60. tf = readGraph(verts, edges, graph);
  61. printf("Return value of: \"%d\", The array is created.\n", tf);
  62.  
  63. if(tf == 1){
  64. return EXIT_FAILURE;
  65. }
  66.  
  67. chonk = highestGraph(verts, edges, graph);
  68.  
  69. int hami[(chonk+1)*2]; //Create the array that will hold all Hamilton variables in the first section and be crossed off as they are traversed in the second section of the array.
  70. int checker = 0;
  71. for(int i = 0; i < ((chonk+1)*2); i=i+2){
  72. hami[i] = checker; //Set each position of the array to it's vertice number in the graph.
  73. hami[i+1] = 0; //Set the hami check to 0.
  74. checker++;
  75. }
  76.  
  77. printf("Once you have input all path values, end the input stream with ctrl+d.\n");
  78.  
  79.  
  80. valid = scanf("%d", &pathCur);
  81. while(valid == 1){
  82. int cont = 0; //Continue only if this is 1,which means it has gone through the vertices and found a valid edge.
  83. valid = scanf("%d", &pathNex);
  84. for(int i = 0; i< (edges*2); i=i+2){
  85. if(graph[i] == pathCur && graph[i+1] == pathNex){
  86. for(int o = 0; o < ((chonk+1)*2); o=o+2){
  87. if(pathCur == hami[o]){ //If the current position equals a vertice.
  88. if(hami[o+1] == 0){
  89. hami[o+1] = -1;
  90. }else{
  91. hami[o+1] = -2;
  92. }
  93. }
  94. }
  95. cont = 1;
  96.  
  97. }
  98. }
  99. if(cont == 0 && valid != EOF){
  100. printf("This is an invalid edge.\n");
  101. printf("This is NOT a path\nand is NOT a Hamiltonian path.\n");
  102. return EXIT_FAILURE;
  103. }
  104. pathCur = pathNex;
  105. }
  106.  
  107. for(int i = 0; i < ((chonk+1)*2); i=i+2){
  108. if(pathNex == hami[i]){ //If the current position equals the final vertice.
  109. if(hami[i+1] == 0){
  110. hami[i+1] = -1;
  111. }else{
  112. hami[i+1] = -2;
  113. }
  114. }
  115. }
  116.  
  117. int hamiV = 0;
  118. for(int i = 0; i < ((chonk+1)*2); i=i+2){
  119. if(hami[i+1] == -1){
  120. hamiV = 1;
  121. }else{
  122. hamiV = 0;
  123. break;
  124. }
  125. }
  126.  
  127. printf("This is a path\n");
  128.  
  129. if(hamiV == 1){
  130. printf("and is a Hamiltonian path.\n");
  131. }else{
  132. printf("and is NOT a Hamiltonian path.\n");
  133. }
  134.  
  135. if(valid == 1){
  136. return EXIT_SUCCESS;
  137. }else{
  138. return EXIT_FAILURE;
  139. }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement