|
| 1 | +/* |
| 2 | +Find if there is a path between two vertices Vi and Vj in a directed graph |
| 3 | +
|
| 4 | +Here path means, they there may or may not be some other vertices as well in the path. |
| 5 | +
|
| 6 | +METHOD: |
| 7 | +We can use DFS or BFS and basically if the graph is connected containing these two vertices, if you |
| 8 | +start from one, you will definately get the other one. |
| 9 | +
|
| 10 | +Time complexity: O(V+E); |
| 11 | +*/ |
| 12 | + |
| 13 | +#include <stdio.h> |
| 14 | +#include <stdlib.h> |
| 15 | +#include <stdbool.h> |
| 16 | +#define MAX 100 |
| 17 | + |
| 18 | +int queue[MAX]; |
| 19 | +int rear = -1, front = 0; |
| 20 | + |
| 21 | +int queueElementCount = 0; |
| 22 | + |
| 23 | +int visited[MAX]; |
| 24 | + |
| 25 | +struct AdjListNode{ |
| 26 | + int data; |
| 27 | + struct AdjListNode *next; |
| 28 | +}; |
| 29 | + |
| 30 | +struct AdjList{ |
| 31 | + struct AdjListNode *head; |
| 32 | +}; |
| 33 | + |
| 34 | +struct Graph{ |
| 35 | + int vertices, edges; |
| 36 | + struct AdjList *map; |
| 37 | +} *newGraph; |
| 38 | + |
| 39 | +int dequeue(){ |
| 40 | + queueElementCount--; |
| 41 | + return queue[front++]; |
| 42 | +} |
| 43 | + |
| 44 | +void enqueue(int data){ |
| 45 | + queueElementCount++; |
| 46 | + queue[++rear] = data; |
| 47 | +} |
| 48 | + |
| 49 | +bool isQueueEmpty(){ |
| 50 | + return queueElementCount == 0; |
| 51 | +} |
| 52 | + |
| 53 | +struct AdjListNode *createNode(int data){ |
| 54 | + struct AdjListNode *temp = (struct AdjListNode *)malloc(sizeof(struct AdjListNode)); |
| 55 | + temp->data = data; |
| 56 | + temp->next = NULL; |
| 57 | + return temp; |
| 58 | +} |
| 59 | + |
| 60 | +void addEdge(int start, int end){ |
| 61 | + struct AdjListNode *newNode = createNode(end); |
| 62 | + if(newGraph->map[start].head){ |
| 63 | + newNode->next = newGraph->map[start].head; |
| 64 | + newGraph->map[start].head = newNode; |
| 65 | + }else{ |
| 66 | + newGraph->map[start].head = newNode; |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +void displayAdjList(){ |
| 71 | + int i; |
| 72 | + |
| 73 | + for(i=0;i<newGraph->vertices;i++){ |
| 74 | + struct AdjListNode *temp = newGraph->map[i].head; |
| 75 | + printf("%d --> ", i); |
| 76 | + while(temp){ |
| 77 | + printf("%d --> ", temp->data); |
| 78 | + temp = temp->next; |
| 79 | + } |
| 80 | + printf("\n"); |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +int checkDirectlyIfConnected(int v1, int v2){ |
| 85 | + struct AdjListNode *temp = newGraph->map[v1].head; |
| 86 | + |
| 87 | + while(temp){ |
| 88 | + if(temp->data == v2){ |
| 89 | + return 1; |
| 90 | + } |
| 91 | + temp = temp->next; |
| 92 | + } |
| 93 | + return 0; |
| 94 | +} |
| 95 | + |
| 96 | +int breadthFirstSearch(int v1, int v2){ |
| 97 | + |
| 98 | + if(checkDirectlyIfConnected(v1, v2)){ |
| 99 | + return 1; |
| 100 | + } |
| 101 | + |
| 102 | + visited[v1] = 1; |
| 103 | + enqueue(v1); |
| 104 | + |
| 105 | + while(!isQueueEmpty()){ |
| 106 | + int tempVertex = dequeue(); |
| 107 | + |
| 108 | + struct AdjListNode *temp = newGraph->map[tempVertex].head; |
| 109 | + |
| 110 | + while(temp){ |
| 111 | + if(temp->data == v2){ |
| 112 | + return 1; |
| 113 | + }else{ |
| 114 | + enqueue(temp->data); |
| 115 | + visited[temp->data] = 1; |
| 116 | + } |
| 117 | + temp = temp->next; |
| 118 | + } |
| 119 | + |
| 120 | + } |
| 121 | + |
| 122 | + return 0; |
| 123 | + |
| 124 | +} |
| 125 | + |
| 126 | +int main(){ |
| 127 | + |
| 128 | + int vertices = 6; |
| 129 | + |
| 130 | + newGraph = (struct Graph *)malloc(sizeof(struct Graph)); |
| 131 | + newGraph->vertices = vertices; |
| 132 | + newGraph->map = (struct AdjList *)calloc(sizeof(struct AdjList),vertices); |
| 133 | + |
| 134 | + addEdge(0,2); |
| 135 | + addEdge(2,3); |
| 136 | + addEdge(3,4); |
| 137 | + addEdge(0,1); |
| 138 | + addEdge(5,1); |
| 139 | + |
| 140 | + displayAdjList(); |
| 141 | + |
| 142 | + printf("basis the graph check enter two vertices to see if they are connected or not\n"); |
| 143 | + int v1, v2; |
| 144 | + scanf("%d %d", &v1, &v2); |
| 145 | + |
| 146 | + int isConnected = breadthFirstSearch(v1,v2); |
| 147 | + if(isConnected){ |
| 148 | + printf("YES they are connected\n"); |
| 149 | + }else{ |
| 150 | + printf("NO they are not connected\n"); |
| 151 | + } |
| 152 | + |
| 153 | + return 0; |
| 154 | +} |
| 155 | + |
0 commit comments