Posts

Showing posts with the label DFS appln

Cut tree in two halfs (dfs implementation )

Cut the tree (dfs application) https://www.hackerrank.com/challenges/cut-the-tree In  this question , we should cut the tree such that the difference between two halfs would be smallest as possible . firstly ,total weight is added as sum. after that:- It is implemented using recursive dfs. Here each child gives his lower weight to their parents. #include<bits/stdc++.h> using namespace std; int sum=0; int a[10000000],vis[1000000]; int ans=INT_MAX; vector<int> v[1000000]; int  dfs(int node) {   vis[node]=1;   int curr=a[node];  for(int i=0;i<v[node].size();i++)  {      if(vis[v[node][i]]==0)      curr+=dfs(v[node][i]);  }  ans=min(ans,abs(sum-curr-curr));  //cout<<curr<<" "<<ans<<endl;  return curr;  vis[node]=0; } int main() {  int n,i,j,k,l,p,q;  cin>>n;  for(i=1;i<=n;i++)  { ...

Counting removing edges to make it forest of even nodes. (dfs applin)

https://www.hackerrank.com/challenges/even-tree Even Tree (dfs application ) In this question we have to count what is the maximum number of forests (of even nodes)  that can be made by removal of minimum edges. Here child returns its parent the no. of nodes ,if the number of nodes(including parent itself ) is even then ans=ans+1; #include<bits/stdc++.h> using namespace std; int ans=0,visited[100000]; vector<int> v[100000]; int dfs(int nod) {     visited[nod]=1;    int  total=1;                  ///self count    if(v[nod].size()==1 && nod!=1)     return 1;                /// the leaf child gives 1 to parent   for(int i=0;i<v[nod].size();i++)   {      if(visited[v[nod][i]]==0)       total+=dfs(v[nod][i]);   }   if(total%2==0)   ...

Minimum Steps to reach a destination (Recursive method )

http://www.geeksforgeeks.org/minimum-steps-to-reach-a-destination/ GeeksforGeeks. It is a ""branch and bound"" type of recursive problem, in which we have two options of going either left or Right in a number line.  And the target is to reach a particular point. Starting Point is Zero. #include<bits/stdc++.h> using namespace std; int fi(int dest,int steps,int cu) {  if(cu==dest)     return steps;  if(abs(cu)>dest)     return INT_MAX;   return min(fi(dest,steps+1,steps+cu+1),fi(dest,steps+1,cu-steps-1)); } int main() { int dest,i,j; cin>>dest; cout<<fi(dest,0,0)<<endl; return 0; }

Program to find total number of Paths between two points in a graph.( Recursive method )

If the "source" Pointer will become "destination" (after the cursion calls ) the path array will get printed. #include<bits/stdc++.h> using namespace std; int fin(vector<int> a[],int src,int dest,vector<int> path,int visited[]) {     path.push_back(src);     if(src==dest)     {       for(int i=0;i<path.size();i++)              cout<<path[i]<<" ";                cout<<endl;                return 1;     }     visited[src]=1;     int ct=0;     for(int i=0;i<a[src].size();i++)         if(visited[a[src][i]]==0)             ct+=fin(a,a[src][i],dest,path,visited);      visited[src]=0;      return ct; } int main() {  vector<int> v[...