Skip to content

Commit 04249d3

Browse files
committed
graph new question added
1 parent e658c12 commit 04249d3

File tree

3 files changed

+233
-1
lines changed

3 files changed

+233
-1
lines changed

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,8 @@ it gets converted to i-1,j-1 or we take min or max of i,j-1 and i-1,j
185185

186186
## Graphs
187187

188-
- 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
188+
- 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
189190

190191
## Advanced algos
191192

@@ -425,6 +426,7 @@ it gets converted to i-1,j-1 or we take min or max of i,j-1 and i-1,j
425426
- [Implement a graph using adjacency matrix and list, implement DFS and BFS, BST and DST](/graphs/question1.c)
426427
- [Implement a graph using adjacency matrix and do BFT in a different way for custom value of nodes](/graphs/question2.c)
427428
- [Write a program to do topological sort in a graph](/graphs/question3.c)
429+
- [Find if there is a path between two vertices Vi and Vj in a directed graph](/graphs/question4.c)
428430

429431

430432
### Pattern Matching

graphs/question4.c

+155
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
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+

misc/question2.c

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
#include <stdbool.h>
5+
6+
int hash[256];
7+
8+
int cal(char *str){
9+
10+
int k;
11+
for(k=0;k<256;k++){
12+
hash[k] = 0;
13+
}
14+
15+
int length = strlen(str);
16+
17+
int allEqual = true;
18+
19+
int mid = length/2;
20+
21+
int max = 0;
22+
23+
int i = 0;
24+
int j = mid;
25+
26+
int count = 0;
27+
28+
while(i<mid && j<length){
29+
30+
if(str[i] == str[j]){
31+
i++;
32+
j++;
33+
count++;
34+
if(max < count){
35+
max = count;
36+
}
37+
}else{
38+
if(i == 0){
39+
j++;
40+
}
41+
i = 0;
42+
count = 0;
43+
max = 0;
44+
}
45+
}
46+
47+
int z;
48+
49+
for(z=0;z<length;z++){
50+
hash[str[z]]++;
51+
}
52+
53+
if(hash[str[0]] == length){
54+
return length-1;
55+
}
56+
57+
return max;
58+
}
59+
60+
int main(){
61+
int cases;
62+
scanf("%d", &cases);
63+
64+
int i;
65+
66+
char str[100000];
67+
68+
for(i=0;i<cases;i++){
69+
scanf("%s",str);
70+
int ans = cal(str);
71+
printf("%d\n", ans);
72+
}
73+
74+
return 0;
75+
}

0 commit comments

Comments
 (0)