Skip to content

Commit e658c12

Browse files
committed
misc questions
1 parent 9f2d928 commit e658c12

File tree

4 files changed

+159
-2
lines changed

4 files changed

+159
-2
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,11 @@ it gets converted to i-1,j-1 or we take min or max of i,j-1 and i-1,j
430430
### Pattern Matching
431431
- [Given a text and a pattern, find all occurences of a pattern in a given text.](/pattern-matching/question1.c)
432432

433+
### MISC
434+
435+
- [Test whether given two strings are isomorphic or not](/misc/question1.c)
436+
- [Find the length of the longest suffix which is also a prefix](/misc/question2.c)
437+
433438
## Some important concepts to solve algos better
434439

435440
- For extreme values refer to limits.h constants given by C

graphs/question3.c

Lines changed: 80 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ Now each time if we traverse the array it will take us O(v) time, rather than do
3838
iteration only while setting the values, we push all the nodes having value zero onto a queue. Each time
3939
we update values in the array we push values in the queue and will printing we pop. This way
4040
topological sort can be done
41+
42+
43+
METHOD2:
44+
While doing DFT we can push elements on stack and then pop them once DFT is done. The order in which
45+
elements are popped is the topological sort
4146
*/
4247

4348
#include <stdio.h>
@@ -49,6 +54,8 @@ int incidents[MAX];
4954
int queue[MAX];
5055
int rear = -1, front = 0;
5156

57+
int adjMatrix[MAX][MAX];
58+
5259
int vertexCount = 0;
5360

5461
int queueItemCount = 0;
@@ -108,6 +115,9 @@ void addEdge(int start, int end){
108115
printf("there is no head\n");
109116
newGraph->arr[start].head = temp;
110117
}
118+
119+
//making a matrix as well
120+
adjMatrix[start][end] = 1;
111121
}
112122

113123
void initArray(vertices){
@@ -139,7 +149,7 @@ void findNodeWithZeroIncidents(){
139149

140150
}
141151

142-
void topologicalSort(){
152+
void topologicalSortList(){
143153

144154
findNodeWithZeroIncidents();
145155

@@ -160,6 +170,55 @@ void topologicalSort(){
160170
}
161171
}
162172

173+
void findNodeWithZeroIncidentsMatrix(int vertices){
174+
int i,j;
175+
176+
bool noIncident = true;
177+
int count = 0;
178+
179+
for(j=0;j<vertices;j++){
180+
for(i=0;i<vertices;i++){
181+
182+
if(adjMatrix[i][j] != 0){
183+
count++;
184+
noIncident = false;
185+
}
186+
}
187+
incidents[j] = count;
188+
if(noIncident){
189+
// printf("enqueueing %d\n", j);
190+
enqueue(j);
191+
printf("%c ", customNodes[j].label);
192+
}
193+
noIncident = true;
194+
count = 0;
195+
}
196+
}
197+
198+
void topologicalSortMatrix(int vertices){
199+
200+
int j;
201+
202+
findNodeWithZeroIncidentsMatrix(vertices);
203+
204+
while(!isQueueEmpty()){
205+
int tempVertex = dequeue();
206+
// printf("dequeue %d\n", tempVertex);
207+
incidents[tempVertex]=-1;
208+
209+
for(j=0;j<vertices;j++){
210+
if(adjMatrix[tempVertex][j] == 1){
211+
incidents[j]--;
212+
adjMatrix[tempVertex][j] = 0;
213+
if(incidents[j] == 0){
214+
// printf("enqueueing %d\n", j);
215+
enqueue(j);
216+
}
217+
}
218+
}
219+
}
220+
}
221+
163222
void printAdjList(struct Graph *newGraph, int vertices){
164223
int i;
165224

@@ -175,10 +234,23 @@ void printAdjList(struct Graph *newGraph, int vertices){
175234
}
176235
}
177236

237+
void initMatrix(int vertices){
238+
int i,j;
239+
240+
for(i=0;i<vertices;i++){
241+
for(j=0;j<vertices;j++){
242+
adjMatrix[i][j] = 0;
243+
}
244+
}
245+
}
246+
178247
int main(){
179248
int vertices = 6;
180249

181250
initArray(vertices);
251+
252+
initMatrix(vertices);
253+
182254
//intializing the number of custom nodes as well the adjList basis number of vertices
183255
customNodes = (struct CustomNode *)malloc(sizeof(struct CustomNode)*vertices);
184256
newGraph = (struct Graph *)malloc(sizeof(struct Graph));
@@ -202,7 +274,13 @@ int main(){
202274

203275
printAdjList(newGraph, vertices);
204276

205-
topologicalSort();
277+
printf("list:\n");
278+
topologicalSortList();
279+
280+
initArray(vertices);
281+
printf("\n");
282+
printf("matrix:\n");
283+
topologicalSortMatrix(vertices);
206284

207285
return 0;
208286
}

misc/question1.c

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
#define MAX 1000
5+
6+
int hash1[256], hash2[256];
7+
8+
9+
int cal(char *str1, char *str2){
10+
11+
int j;
12+
13+
for(j=0;j<256;j++){
14+
hash1[j] = 0;
15+
hash2[j] = 0;
16+
}
17+
18+
int len1 =strlen(str1);
19+
int len2 = strlen(str2);
20+
21+
if(len1 != len2){
22+
return 0;
23+
}
24+
int i;
25+
for(i=0;i<len1;i++){
26+
27+
if(hash1[str1[i]] == 0){
28+
29+
hash1[str1[i]] = str2[i];
30+
31+
}else{
32+
if(hash1[str1[i]] != str2[i]){
33+
34+
return 0;
35+
}
36+
37+
}
38+
39+
if(hash2[str2[i]] == 0){
40+
hash2[str2[i]] = str1[i];
41+
}else{
42+
if(hash2[str2[i]] != str1[i]){
43+
return 0;
44+
}
45+
}
46+
47+
}
48+
49+
return 1;
50+
51+
}
52+
53+
int main(){
54+
55+
int cases;
56+
scanf("%d",&cases);
57+
58+
int i;
59+
60+
char str1[MAX], str2[MAX];
61+
62+
for(i=0;i<cases;i++){
63+
scanf("%s",str1);
64+
scanf("%s",str2);
65+
66+
int isIsomorphic = cal(str1, str2);
67+
68+
printf("%d\n",isIsomorphic);
69+
70+
}
71+
72+
return 0;
73+
}

nextquestions.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,5 @@ TODO:
103103
- watch DP second last video
104104
- trees question20 all methods (3) to be done GFG or R
105105
- geeks for geeks for DP
106+
- Graph topological sort method2 and matrix implementation of topo
106107

0 commit comments

Comments
 (0)