|
| 1 | +/* |
| 2 | +Given an undirected graph, find if it has a cycle or not |
| 3 | +
|
| 4 | +METHOD1: |
| 5 | +We can do DFS and if any node at any given point does not have any where to go as per DFS, i.e visited of all |
| 6 | +its neighbours are 1, then it will have a cycle |
| 7 | +
|
| 8 | +Time complexity: O(V+E) //in worst case we will end up visited every node twice |
| 9 | +
|
| 10 | +METHOD2: |
| 11 | +We can use union and find algorithm |
| 12 | +*/ |
| 13 | + |
| 14 | +//METHOD1 |
| 15 | +#include <stdio.h> |
| 16 | +#include <stdlib.h> |
| 17 | +#include <stdbool.h> |
| 18 | +#define MAX 100 |
| 19 | + |
| 20 | +int queue[MAX]; |
| 21 | +int rear = -1, front = 0; |
| 22 | + |
| 23 | +int queueElementCount = 0; |
| 24 | + |
| 25 | +int visited[MAX]; |
| 26 | + |
| 27 | +struct AdjListNode{ |
| 28 | + int data; |
| 29 | + struct AdjListNode *next; |
| 30 | +}; |
| 31 | + |
| 32 | +struct AdjList{ |
| 33 | + struct AdjListNode *head; |
| 34 | +}; |
| 35 | + |
| 36 | +struct Graph{ |
| 37 | + int vertices, edges; |
| 38 | + struct AdjList *map; |
| 39 | +} *newGraph; |
| 40 | + |
| 41 | +int dequeue(){ |
| 42 | + queueElementCount--; |
| 43 | + return queue[front++]; |
| 44 | +} |
| 45 | + |
| 46 | +void enqueue(int data){ |
| 47 | + queueElementCount++; |
| 48 | + queue[++rear] = data; |
| 49 | +} |
| 50 | + |
| 51 | +bool isQueueEmpty(){ |
| 52 | + return queueElementCount == 0; |
| 53 | +} |
| 54 | + |
| 55 | +struct AdjListNode *createNode(int data){ |
| 56 | + struct AdjListNode *temp = (struct AdjListNode *)malloc(sizeof(struct AdjListNode)); |
| 57 | + temp->data = data; |
| 58 | + temp->next = NULL; |
| 59 | + return temp; |
| 60 | +} |
| 61 | + |
| 62 | +void addEdge(int start, int end){ |
| 63 | + struct AdjListNode *newNode = createNode(end); |
| 64 | + if(newGraph->map[start].head){ |
| 65 | + newNode->next = newGraph->map[start].head; |
| 66 | + newGraph->map[start].head = newNode; |
| 67 | + }else{ |
| 68 | + newGraph->map[start].head = newNode; |
| 69 | + } |
| 70 | + newNode = createNode(start); |
| 71 | + if(newGraph->map[end].head){ |
| 72 | + newNode->next = newGraph->map[end].head; |
| 73 | + newGraph->map[end].head = newNode; |
| 74 | + }else{ |
| 75 | + newGraph->map[end].head = newNode; |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +void displayAdjList(){ |
| 80 | + int i; |
| 81 | + |
| 82 | + for(i=0;i<newGraph->vertices;i++){ |
| 83 | + struct AdjListNode *temp = newGraph->map[i].head; |
| 84 | + printf("%d --> ", i); |
| 85 | + while(temp){ |
| 86 | + printf("%d --> ", temp->data); |
| 87 | + temp = temp->next; |
| 88 | + } |
| 89 | + printf("\n"); |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +int dfs(int index, int parent){ |
| 94 | + visited[index] = 1; |
| 95 | + |
| 96 | + struct AdjListNode *temp = newGraph->map[index].head; |
| 97 | + while(temp){ |
| 98 | + if(visited[temp->data] != 1){ |
| 99 | + parent = index; |
| 100 | + dfs(temp->data, parent); |
| 101 | + }else if(visited[temp->data] == 1 && temp->data != parent){ |
| 102 | + return 1; |
| 103 | + } |
| 104 | + temp = temp->next; |
| 105 | + } |
| 106 | + return 0; |
| 107 | +} |
| 108 | + |
| 109 | +int isCyclePresent(){ |
| 110 | + return dfs(0,-1); |
| 111 | +} |
| 112 | + |
| 113 | +int main(){ |
| 114 | + |
| 115 | + int vertices = 5; |
| 116 | + |
| 117 | + newGraph = (struct Graph *)malloc(sizeof(struct Graph)); |
| 118 | + newGraph->vertices = vertices; |
| 119 | + newGraph->map = (struct AdjList *)calloc(sizeof(struct AdjList),vertices); |
| 120 | + |
| 121 | + addEdge(0,1); |
| 122 | + addEdge(1,4); |
| 123 | + addEdge(0,2); |
| 124 | + addEdge(0,3); |
| 125 | + addEdge(2,3); |
| 126 | + |
| 127 | + displayAdjList(); |
| 128 | + |
| 129 | + if(isCyclePresent()){ |
| 130 | + printf("Graph contains a cycle\n"); |
| 131 | + }else{ |
| 132 | + printf("Graph does not contain a cycle\n"); |
| 133 | + } |
| 134 | + |
| 135 | + return 0; |
| 136 | +} |
| 137 | + |
0 commit comments