Advertisement
NikitaM

Untitled

May 24th, 2017
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.51 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <malloc.h>
  4.  
  5. struct AdjListNode
  6. {
  7. int dest, id;
  8. struct AdjListNode* next;
  9. };
  10.  
  11. struct AdjList
  12. {
  13. struct AdjListNode *head;
  14. };
  15.  
  16. struct Graph
  17. {
  18. int V;
  19. int *killedEdges, *killedVertices;
  20. struct AdjList* array;
  21. };
  22.  
  23. struct AdjListNode* newAdjListNode(int dest, int id)
  24. {
  25. struct AdjListNode* newNode =
  26. (struct AdjListNode*) malloc(sizeof(struct AdjListNode));
  27. newNode->dest = dest;
  28. newNode->id=id
  29. ;
  30. newNode->next = NULL;
  31. return newNode;
  32. }
  33.  
  34. struct Graph* createGraph(int V)
  35. {
  36. struct Graph* graph = (struct Graph*) malloc(sizeof(struct Graph));
  37. graph->V = V;
  38. graph->array = (struct AdjList*) malloc(V * sizeof(struct AdjList));
  39.  
  40. graph->killedEdges = (int*) calloc(V*(V-1)/2, sizeof(int));
  41. graph->killedVertices = (int*) calloc(V*(V-1)/2, sizeof(int));
  42. int i;
  43. for (i = 0; i < V; ++i)
  44. graph->array[i].head = NULL;
  45.  
  46. return graph;
  47. }
  48.  
  49. void addEdge(struct Graph* graph, int src, int dest, int id)
  50. {
  51.  
  52. struct AdjListNode* newNode = newAdjListNode(dest, id);
  53. newNode->next = graph->array[src].head;
  54. graph->array[src].head = newNode;
  55.  
  56. newNode = newAdjListNode(src, id);
  57. newNode->next = graph->array[dest].head;
  58. graph->array[dest].head = newNode;
  59. }
  60.  
  61.  
  62.  
  63. void printGraph(struct Graph* graph)
  64. {
  65. int v;
  66. for (v = 0; v < graph->V; ++v)
  67. {
  68. if (graph->killedVertices[v])
  69. continue;
  70. struct AdjListNode* pCrawl = graph->array[v].head;
  71.  
  72. printf("\n Adjacency list of vertex %d\n head ", v);
  73. while (pCrawl)
  74. {
  75. if(!graph->killedEdges[pCrawl->id] && !graph->killedVertices[pCrawl->dest] )
  76. printf("-> %d", pCrawl->dest);
  77. pCrawl = pCrawl->next;
  78. }
  79. printf("\n");
  80. }
  81. }
  82.  
  83.  
  84.  
  85. void getAdjMatrix(struct Graph* graph, char **matrix)
  86. {
  87. int v;
  88. for (v = 0; v < graph->V; ++v)
  89. {
  90. if (graph->killedVertices[v])
  91. continue;
  92. struct AdjListNode* pCrawl = graph->array[v].head;
  93.  
  94. while (pCrawl)
  95. {
  96. if(!graph->killedEdges[pCrawl->id] && !graph->killedVertices[pCrawl->dest])
  97. matrix[v][ pCrawl->dest] =1;
  98. pCrawl = pCrawl->next;
  99. }
  100. }
  101. }
  102. void deleteEdge(struct Graph* graph, int id)
  103. {
  104. graph->killedEdges[id]=1;
  105. }
  106.  
  107. void deleteVertex(struct Graph* graph, int id)
  108. {
  109. graph->killedVertices[id]=1;
  110. }
  111.  
  112. int main()
  113. {
  114. int V = 7;
  115. struct Graph* graph = createGraph(V);
  116. addEdge(graph, 0, 1, 5);
  117. addEdge(graph, 1, 5, 9);
  118. addEdge(graph, 1, 3, 12);
  119. addEdge(graph, 2, 0, 8);
  120. addEdge(graph, 2, 4, 8);
  121. addEdge(graph, 2, 6, 2);
  122. addEdge(graph, 2, 5, 4);
  123. addEdge(graph, 4, 3, 3);
  124. addEdge(graph, 4, 6, 7);
  125. addEdge(graph, 3, 5, 6);
  126. char choice;
  127. printf("If you want to delete vertex No. 5 press 'Y' key and Enter key, otherwise press 'N' key followed by Enter\n");
  128. scanf("%c",&choice);
  129. if(choice=='Y')
  130.  
  131. deleteVertex(graph,5);
  132. printf("If you want to delete edges No. i press 'Y' key and Enter, otherwise press 'N' key followed by Enter\n");
  133. scanf(" %c",&choice);
  134. if(choice=='Y')
  135. deleteEdge(graph,8);
  136. printGraph(graph);
  137. char **matrix=(char**)malloc(V*sizeof(char*));
  138. int i=0;
  139. for (i=0; i<V; ++i)
  140. matrix[i]=(char*)calloc(V,sizeof(char));
  141.  
  142. getAdjMatrix(graph,matrix);
  143. return 0;
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement