Skip to content

Commit

Permalink
Merge pull request #759 from GOURAV-SHARMA8297/deapth_first_search
Browse files Browse the repository at this point in the history
Deapth first search
  • Loading branch information
ZoranPandovski authored Nov 11, 2017
2 parents 44689d7 + d962d20 commit 6db040b
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 0 deletions.
24 changes: 24 additions & 0 deletions automorphicnumbers.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Implementing automorphic numbers in C
// run code with gcc automorphicnumbers.c -lm as I use math library
#include<stdio.h>
#include<math.h>
int main()
{
long long int n,p,count=0,q;
unsigned long long int k;
scanf("%lld",&n); //The number is given by user for checking automorphism
p=n;
while(p)
{
count++; //While here we count that our number is in which range bsically we count number of digits in our number.
p=p/10;
}
q=pow(10,count); // The ten power number just after our number for calculating mod value.
k=n*n;
k=k%q;
if(k==n)
printf("Given number is automorphic\n");
else
printf("Given number is not automorphic\n");
return 0;
}
49 changes: 49 additions & 0 deletions dfs.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Code for depth first search in c++
#include<bits/stdc++.h>

using namespace std;

const int N = int(1e5)+10;
const int INF = int(1e9)+10;
vector<int> G[N],W[N];
int vis[N];
void dfs(int u, int w)
{
if(vis[u]) return;
vis[u] = 1;
for(int i=0; i<G[u].size(); i++)
if(W[u][i] <= w && vis[G[u][i]] == 0)
dfs(G[u][i],w);
}

int main()
{
int n,m; cin>>n>>m;
vector<int> E;
for(int i=0; i<m; i++)
{
int u,v,w;
cin>>u>>v>>w;
G[u].push_back(v);
W[u].push_back(w);
E.push_back(w);
}
int src,dest; cin>>src>>dest;
sort(E.begin(),E.end());
int l=0,r=E.size()-1;
int ans=INF;
while(l<=r)
{
for(int i=1; i<=n; i++) vis[i] = 0;
int m = (l+r)/2;
dfs(src,E[m]);
if(vis[dest])
{
ans = E[m];
r = m-1;
}
else l = m+1;
}
cout<<ans<<endl;
return 0;
}
34 changes: 34 additions & 0 deletions lcs.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Program for longest common subsequence in c++
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,m;
char s[10000]; //two strings which we want to compare and find longest subsequence
char c[10000];
cin >> n >> m; // Length of two strings
cin >> s;
cin >> c;
int a[n+1][m+1];
for(int i=0;i<=n;i++)
{
for(int j=0;j<=m;j++)
{
if(i==0 || j==0)
{
a[i][j]=0;
}
else if(s[i-1]==c[j-1])
{
a[i][j]=a[i-1][j-1]+1;
}
else
{
a[i][j]=max(a[i-1][j],a[i][j-1]);
}
}
}
cout << a[n][m] << endl; // print length of longest common subsequence
return 0;
}

24 changes: 24 additions & 0 deletions linearsearch.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include<stdio.h>
int main()
{
int n,i,x,flag=0;
int a[100100];
scanf("%d",&n); //how many numbers in which you want to search
for(i=0;i<n;i++)
{
scanf("%d",&a[i]); //Taking input from user
}
scanf("%d",&x); // Number which we want to search
for(i=0;i<n;i++)
{
if(a[i]==x)
{
flag=1; //If number is found then set the flag
}
}
if(flag==1)
printf("Yes\n"); //If number is found then print yes.
else
printf("No\n"); // If number is not there then print no.
return 0;
}

0 comments on commit 6db040b

Please sign in to comment.