Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <malloc.h>
- struct AdjListNode
- {
- int dest, id;
- struct AdjListNode* next;
- };
- struct AdjList
- {
- struct AdjListNode *head;
- };
- struct Graph
- {
- int V;
- int *killedEdges, *killedVertices;
- struct AdjList* array;
- };
- struct AdjListNode* newAdjListNode(int dest, int id)
- {
- struct AdjListNode* newNode =
- (struct AdjListNode*) malloc(sizeof(struct AdjListNode));
- newNode->dest = dest;
- newNode->id=id
- ;
- newNode->next = NULL;
- return newNode;
- }
- struct Graph* createGraph(int V)
- {
- struct Graph* graph = (struct Graph*) malloc(sizeof(struct Graph));
- graph->V = V;
- graph->array = (struct AdjList*) malloc(V * sizeof(struct AdjList));
- graph->killedEdges = (int*) calloc(V*(V-1)/2, sizeof(int));
- graph->killedVertices = (int*) calloc(V*(V-1)/2, sizeof(int));
- int i;
- for (i = 0; i < V; ++i)
- graph->array[i].head = NULL;
- return graph;
- }
- void addEdge(struct Graph* graph, int src, int dest, int id)
- {
- struct AdjListNode* newNode = newAdjListNode(dest, id);
- newNode->next = graph->array[src].head;
- graph->array[src].head = newNode;
- newNode = newAdjListNode(src, id);
- newNode->next = graph->array[dest].head;
- graph->array[dest].head = newNode;
- }
- void printGraph(struct Graph* graph)
- {
- int v;
- for (v = 0; v < graph->V; ++v)
- {
- if (graph->killedVertices[v])
- continue;
- struct AdjListNode* pCrawl = graph->array[v].head;
- printf("\n Adjacency list of vertex %d\n head ", v);
- while (pCrawl)
- {
- if(!graph->killedEdges[pCrawl->id] && !graph->killedVertices[pCrawl->dest] )
- printf("-> %d", pCrawl->dest);
- pCrawl = pCrawl->next;
- }
- printf("\n");
- }
- }
- void getAdjMatrix(struct Graph* graph, char **matrix)
- {
- int v;
- for (v = 0; v < graph->V; ++v)
- {
- if (graph->killedVertices[v])
- continue;
- struct AdjListNode* pCrawl = graph->array[v].head;
- while (pCrawl)
- {
- if(!graph->killedEdges[pCrawl->id] && !graph->killedVertices[pCrawl->dest])
- matrix[v][ pCrawl->dest] =1;
- pCrawl = pCrawl->next;
- }
- }
- }
- void deleteEdge(struct Graph* graph, int id)
- {
- graph->killedEdges[id]=1;
- }
- void deleteVertex(struct Graph* graph, int id)
- {
- graph->killedVertices[id]=1;
- }
- int main()
- {
- int V = 7;
- struct Graph* graph = createGraph(V);
- addEdge(graph, 0, 1, 5);
- addEdge(graph, 1, 5, 9);
- addEdge(graph, 1, 3, 12);
- addEdge(graph, 2, 0, 8);
- addEdge(graph, 2, 4, 8);
- addEdge(graph, 2, 6, 2);
- addEdge(graph, 2, 5, 4);
- addEdge(graph, 4, 3, 3);
- addEdge(graph, 4, 6, 7);
- addEdge(graph, 3, 5, 6);
- char choice;
- printf("If you want to delete vertex No. 5 press 'Y' key and Enter key, otherwise press 'N' key followed by Enter\n");
- scanf("%c",&choice);
- if(choice=='Y')
- deleteVertex(graph,5);
- printf("If you want to delete edges No. i press 'Y' key and Enter, otherwise press 'N' key followed by Enter\n");
- scanf(" %c",&choice);
- if(choice=='Y')
- deleteEdge(graph,8);
- printGraph(graph);
- char **matrix=(char**)malloc(V*sizeof(char*));
- int i=0;
- for (i=0; i<V; ++i)
- matrix[i]=(char*)calloc(V,sizeof(char));
- getAdjMatrix(graph,matrix);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement