Skip to content

Commit 0508a11

Browse files
committed
graphs | new question added
1 parent 04249d3 commit 0508a11

File tree

3 files changed

+157
-2
lines changed

3 files changed

+157
-2
lines changed

README.md

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,13 +186,28 @@ it gets converted to i-1,j-1 or we take min or max of i,j-1 and i-1,j
186186
## Graphs
187187

188188
- Most of the graph questions revolves around finding the adjacent nodes and doing operations on it. Finding adjacent nodes is easy as you can get it from the matrix easily by seeing if the value corresponding to that vertex and some i is 1 and that it has to be unvisited. So basically we either do DFS or BFS.
189-
- When path between two vertices is to be found, they need not be connected directly
189+
- When path between two vertices is to be found, they need not be connected directly. There may be some vertices/ nodes in between.
190190

191-
## Advanced algos
191+
## Advanced data
192192

193193
- Use KMP to find if a pattern exists in a text or not
194194
- For Graphs, Topological sort is possible only on directed acyclic graphs (one with no cycles). Useful when one task is dependent on other.
195195

196+
## Advanced data structures
197+
198+
- Disjoint sets: If two nodes are connected or belong to the same category they will be a part of the same set. Disjoint sets can be represented in two ways.
199+
1) Linked list
200+
2) Trees
201+
202+
There are three kind of operations that can be applied on such sets.
203+
1) Find
204+
2) Union
205+
3) Create
206+
207+
Implementation using trees is better as we can apply union by rank and path compression to make sure find happens in logn or constant time, create anyway happens in constant time only.
208+
In case of linked list find takes O(n) time and union also takes O(n) time and create takes O(1) time
209+
210+
196211
# Topic0: Programming Questions
197212

198213
## Note:
@@ -427,6 +442,7 @@ it gets converted to i-1,j-1 or we take min or max of i,j-1 and i-1,j
427442
- [Implement a graph using adjacency matrix and do BFT in a different way for custom value of nodes](/graphs/question2.c)
428443
- [Write a program to do topological sort in a graph](/graphs/question3.c)
429444
- [Find if there is a path between two vertices Vi and Vj in a directed graph](/graphs/question4.c)
445+
- [Given an undirected graph, find if it has a cycle or not](/graphs/question5.c)
430446

431447

432448
### Pattern Matching

graphs/question5.c

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
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+

nextquestions.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,6 @@ TODO:
104104
- trees question20 all methods (3) to be done GFG or R
105105
- geeks for geeks for DP
106106
- Graph topological sort method2 and matrix implementation of topo
107+
- Write a program to detect a cycle in a graph (question5) method2
108+
- breadth and depth first traversal in a tree
107109

0 commit comments

Comments
 (0)