Skip to content

Commit

Permalink
Merge pull request Crio-Bytes#38 from TheShubham-K/main
Browse files Browse the repository at this point in the history
[C++] Data Structures
  • Loading branch information
Kevinpaulose05 authored Oct 31, 2020
2 parents cf59281 + 1156cba commit 8f2e948
Show file tree
Hide file tree
Showing 4 changed files with 268 additions and 0 deletions.
26 changes: 26 additions & 0 deletions Getting Started with DSA/src/C++/Data Structures/Booyre_Moore.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <iostream>
#include <bits/stdc++.h>

using namespace std;


void badCharHeuristic(string str, int size, int badchar[NO_OF_CHARS])
{
for(int i = 0;i<NO_OF_CHARS;i++)
badchar[i] = -1;

for (int i = 0; i<size;i++){
badchar[(int) str[i]] = i;
}
}
void search(string txt, string pat)
{
int m = pat.size;
int n = txt.size;
}

int main()
{
cout << "Hello world!" << endl;
return 0;
}
154 changes: 154 additions & 0 deletions Getting Started with DSA/src/C++/Data Structures/Dijkstras_Algorithm.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
#include <stdio.h>
#include <stdlib.h>

int v,e,s,D[20],P[20],adjMat[25][25];

struct node
{
int vertex,distance;
struct node *llink,*rlink;
};
typedef struct node *NODE;

NODE initialize(NODE queue)
{
int i;
NODE cur;
for(i=1;i<=v;i++)
{
D[i] = 9999;
P[i] = 0;
NODE temp = (NODE)malloc(sizeof(struct node));
temp->vertex = i;
temp->distance = 99999;
temp->rlink = NULL;
if(queue==NULL)
{
queue=temp;
queue->llink=NULL;
cur=queue;
}
else
{
cur->rlink = temp;
temp->llink = cur;
cur=cur->rlink;
}
}
return queue;
}

int deleteMin(NODE *queue)
{
int del, m = 9999;
NODE cur=(*queue),prev,min=NULL;
while(cur!=NULL)
{
if(cur->distance<m)
{
min=cur;
m=cur->distance;
}
cur=cur->rlink;
}
if(min==(*queue))
{
(*queue)=min->rlink;
if(*queue!= NULL)
(min->rlink)->llink=NULL;
}
else
{
prev=min->llink;
prev->rlink=min->rlink;
if(min->rlink != NULL)
(min->rlink)->llink = prev;
}
del = min->vertex;
free(min);
return del;
}

void Decrease(NODE queue, int v, int d)
{
NODE cur;
cur=queue;
while(cur!=NULL)
{
if(cur->vertex==v)
{
cur->distance=d;
return;
}
cur=cur->rlink;
}
}

void Dijikstras(NODE queue)
{
int i,j,u,VT[20],V_VT[20];
queue=initialize(queue);
for(i=1;i<=v;i++)
{
VT[i]=0;
V_VT[i]=1;
}
VT[s]=1;
V_VT[s]=0;
D[s]=0;
Decrease(queue,s,D[s]);
for(i=1;i<=v;i++)
{
u=deleteMin(&queue);
VT[u]=1;
V_VT[u]=0;
for(j=1; j<=v; j++)
if(VT[u]==1 && V_VT[j]==1 && adjMat[u][j]<9999)
if(D[u]+adjMat[u][j] < D[j])
{
D[j]=D[u]+adjMat[u][j];
Decrease(queue,j,D[j]);
P[j]=u;
}
}
}

int main()
{
int i,j, v1,v2,w;
NODE queue = NULL;
printf("Enter the number of Vertices : ");
scanf("%d", &v);
printf("Enter the number of edges : ");
scanf("%d", &e);
printf("Enter %d edges : \n",e);
for(i=1;i<=e;i++)
{
for(j=1;j<=v;j++)
{
if(i==j)
adjMat[i][j]=0;
else
adjMat[i][j]=9999;
}
}
for(i=1;i<=e;i++)
{
printf("Edges - %d : ",i);
scanf("%d%d",&v1,&v2);
printf("Enter the weight of edge %d --> %d : ", v1,v2);
scanf("%d", &w);
adjMat[v1][v2]=adjMat[v2][v1]=w;
}
printf("Enter the Source vertex : ");
scanf("%d",&s);
Dijikstras(queue);
printf("\nSingle source (vertex - %d)Shortest distance : \n",s);
for(i=1;i<=v;i++)
{
if(D[i]==0)
continue;
printf("Distance %d --> %d : %d\tPrevious Vertex : %d\n",s,i,D[i],P[i]);
}
return 0;
}
26 changes: 26 additions & 0 deletions Getting Started with DSA/src/C++/Data Structures/Eucler's_Method.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <stdio.h>
#include <stdlib.h>

int main()
{
int n,m,G;
printf("Enter two integer number:\n");
scanf("%d %d",&m,&n);
G=gcd(m,n);
printf("GCD of %d and %d is %d:\n",m,n,G);
return 0;
}



int gcd(int m,int n)
{
int r;
while(n!=0)
{
r=m%n;
m=n;
n=r;
}
return m;
}
62 changes: 62 additions & 0 deletions Getting Started with DSA/src/C++/Data Structures/dfs_traversal.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include <stdio.h>
#include <stdlib.h>

int count=0,v,e,visited[20],mat[20][20];
void dfs(int w)
{
int j;
count++;
visited[w]=count; /**Mark vertex w as visited*/
printf("%d(%d)\t",w ,visited[w]);
for(j=1;j<=v;j++)
{
if(mat[w][j]==1&&visited[j]==0)
{
dfs(j);
}
}
}
void DFS() /**TO ensure all the vertices are visited*/
{
int i;
for(i=1;i<=v;i++)
{
if(visited[i]==0)
{
dfs(i);
}
}
}

int main()
{
int i, v1, v2, ch;
printf("Select the type of Graph:\n\t> 1.Directed Graph\n\t>2.Undirected Graph\n");
scanf("%d",&ch);
if(ch!=1&&ch!=2)
{
printf("Invalid option !!\n");
return 0;
}
printf("Enter the number of vertices: \n");
scanf("%d",&v);
printf("Enter the number of edges: \n");
scanf("%d",&e);
printf("Enter %d edges one by one: \n",e);
for(i=1;i<=e;i++)
{
printf("Edge--%d: \n",i);
scanf("%d%d",&v1,&v2);
if(ch==1)
{
mat[v1][v2]=1; /**Directed Graph*/
}
else
{
mat[v1][v2]=mat[v2][v1]=1; /**Undirected Graph*/
}
}
printf("\nOrder of vertices processed:\n");
DFS();
return 0;
}

0 comments on commit 8f2e948

Please sign in to comment.