Skip to content

Commit

Permalink
misc questions
Browse files Browse the repository at this point in the history
  • Loading branch information
arorarahul committed Jul 17, 2017
1 parent 9f2d928 commit e658c12
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 2 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
### Pattern Matching
- [Given a text and a pattern, find all occurences of a pattern in a given text.](/pattern-matching/question1.c)

### MISC

- [Test whether given two strings are isomorphic or not](/misc/question1.c)
- [Find the length of the longest suffix which is also a prefix](/misc/question2.c)

## Some important concepts to solve algos better

- For extreme values refer to limits.h constants given by C
Expand Down
82 changes: 80 additions & 2 deletions graphs/question3.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ Now each time if we traverse the array it will take us O(v) time, rather than do
iteration only while setting the values, we push all the nodes having value zero onto a queue. Each time
we update values in the array we push values in the queue and will printing we pop. This way
topological sort can be done
METHOD2:
While doing DFT we can push elements on stack and then pop them once DFT is done. The order in which
elements are popped is the topological sort
*/

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

int adjMatrix[MAX][MAX];

int vertexCount = 0;

int queueItemCount = 0;
Expand Down Expand Up @@ -108,6 +115,9 @@ void addEdge(int start, int end){
printf("there is no head\n");
newGraph->arr[start].head = temp;
}

//making a matrix as well
adjMatrix[start][end] = 1;
}

void initArray(vertices){
Expand Down Expand Up @@ -139,7 +149,7 @@ void findNodeWithZeroIncidents(){

}

void topologicalSort(){
void topologicalSortList(){

findNodeWithZeroIncidents();

Expand All @@ -160,6 +170,55 @@ void topologicalSort(){
}
}

void findNodeWithZeroIncidentsMatrix(int vertices){
int i,j;

bool noIncident = true;
int count = 0;

for(j=0;j<vertices;j++){
for(i=0;i<vertices;i++){

if(adjMatrix[i][j] != 0){
count++;
noIncident = false;
}
}
incidents[j] = count;
if(noIncident){
// printf("enqueueing %d\n", j);
enqueue(j);
printf("%c ", customNodes[j].label);
}
noIncident = true;
count = 0;
}
}

void topologicalSortMatrix(int vertices){

int j;

findNodeWithZeroIncidentsMatrix(vertices);

while(!isQueueEmpty()){
int tempVertex = dequeue();
// printf("dequeue %d\n", tempVertex);
incidents[tempVertex]=-1;

for(j=0;j<vertices;j++){
if(adjMatrix[tempVertex][j] == 1){
incidents[j]--;
adjMatrix[tempVertex][j] = 0;
if(incidents[j] == 0){
// printf("enqueueing %d\n", j);
enqueue(j);
}
}
}
}
}

void printAdjList(struct Graph *newGraph, int vertices){
int i;

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

void initMatrix(int vertices){
int i,j;

for(i=0;i<vertices;i++){
for(j=0;j<vertices;j++){
adjMatrix[i][j] = 0;
}
}
}

int main(){
int vertices = 6;

initArray(vertices);

initMatrix(vertices);

//intializing the number of custom nodes as well the adjList basis number of vertices
customNodes = (struct CustomNode *)malloc(sizeof(struct CustomNode)*vertices);
newGraph = (struct Graph *)malloc(sizeof(struct Graph));
Expand All @@ -202,7 +274,13 @@ int main(){

printAdjList(newGraph, vertices);

topologicalSort();
printf("list:\n");
topologicalSortList();

initArray(vertices);
printf("\n");
printf("matrix:\n");
topologicalSortMatrix(vertices);

return 0;
}
73 changes: 73 additions & 0 deletions misc/question1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 1000

int hash1[256], hash2[256];


int cal(char *str1, char *str2){

int j;

for(j=0;j<256;j++){
hash1[j] = 0;
hash2[j] = 0;
}

int len1 =strlen(str1);
int len2 = strlen(str2);

if(len1 != len2){
return 0;
}
int i;
for(i=0;i<len1;i++){

if(hash1[str1[i]] == 0){

hash1[str1[i]] = str2[i];

}else{
if(hash1[str1[i]] != str2[i]){

return 0;
}

}

if(hash2[str2[i]] == 0){
hash2[str2[i]] = str1[i];
}else{
if(hash2[str2[i]] != str1[i]){
return 0;
}
}

}

return 1;

}

int main(){

int cases;
scanf("%d",&cases);

int i;

char str1[MAX], str2[MAX];

for(i=0;i<cases;i++){
scanf("%s",str1);
scanf("%s",str2);

int isIsomorphic = cal(str1, str2);

printf("%d\n",isIsomorphic);

}

return 0;
}
1 change: 1 addition & 0 deletions nextquestions.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,5 @@ TODO:
- watch DP second last video
- trees question20 all methods (3) to be done GFG or R
- geeks for geeks for DP
- Graph topological sort method2 and matrix implementation of topo

0 comments on commit e658c12

Please sign in to comment.