Skip to content

Update find a node #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 38 additions & 93 deletions Graph 1/BFS Traversal
Original file line number Diff line number Diff line change
Expand Up @@ -5,103 +5,48 @@ E is the number of edges present in graph G.
Note : 1. Take graph input in the adjacency matrix.
2. Handle for Disconnected Graphs as well


#include <iostream>
#include <vector>
#include<unordered_map>
#include <queue>
#include<iostream>
#include<vector>
#include<queue>
using namespace std;


vector<int> * getpath( int ** graph , int v , int s ,int e , bool * visited)
{
queue<int> pn;
unordered_map<int ,int> m;
pn.push(s);
visited[s]=true;

while(!pn.empty())
{
int t= pn.front();

pn.pop();


for(int i=0 ; i<v ;i++)
{
if(graph[t][i]==1 && !visited[i])
{
pn.push(i);
m[i]=t;

visited[i]=true;
if(i==e)
{
vector<int> * ans = new vector<int>();

ans->push_back(e);
int x =e;
while(m[x]!=s)
{
ans->push_back(m[x]);
x=m[x];
}
ans->push_back(s);

return ans;
}
}
}


}
return NULL;
}


int main()
{
int V, E;
cin >> V >> E;

int** graph = new int * [V];
for( int i=0 ;i<V ;i++)
{
graph[i]= new int[V];
for( int j=0 ;j<V ;j++)
graph[i][j]=0;

}
for (int i=0 ;i<E ;i++)
{
int x ,y ;
cin>>x>>y ;
graph[x][y]=1;
graph[y][x]=1;
}
bool * visited = new bool[V];
for ( int i=0 ;i<V ;i++)
visited[i]=false;

int s ,e ;
cin>>s>>e;

vector<int> * ans = getpath(graph , V,s,e,visited);
if(ans)
{
for(int i=0 ;i< ans->size(); i++)
cout<< ans->at(i) << " ";
int main() {
// Write your code here
int v,e;
cin >> v >> e;

vector<vector<bool>> edges(v, vector<bool>(v, false));

for(int i = 0; i < e; ++i) {
int first;
int second;
cin >> first >> second;
edges[first][second] = true;
edges[second][first] = true;
}

delete ans;
vector<bool> visited(v, false);

for(int i=0 ;i<V ;i++)
{
delete [] graph[i];
}
delete [] graph;
queue<int> q;

delete [] visited;
// If the graph is a connected component graph
// We Call BFs for each Component
for(int i = 0; i < v ; i++) {
if(!visited[i]) {
q.push(i);
visited[i] = true;
while(!q.empty()) {
int front = q.front();
q.pop();
cout << front << " ";

for(int i = 0; i < v; i++) {
if(edges[front][i] and !visited[i]) {
q.push(i);
visited[i] = true;
}
}
}
}
}

return 0;
}
35 changes: 17 additions & 18 deletions binary trees/find a node
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,22 @@ Given a Binary Tree and an integer x, check if node with data x is present in th


bool isNodePresent(BinaryTreeNode<int>* root, int x) {


if(root->data==x)
ans =true;

bool ans=false;
if(root->left)
{ bool leftans= isNodePresent(root->left,x);
if(leftans)
ans=leftans;
}

if(root->right)
{ bool rightans= isNodePresent(root->right,x);
if(rightans)
ans=rightans;
}
return ans;
// Write your code here

if (root == NULL)
return false;
if (root->left != NULL) {
bool val = isNodePresent(root->left, x);
if (val == true)
return true;
}
if (root->right != NULL) {
bool val = isNodePresent(root->right, x);
if (val == true)
return true;
}
if (root->data == x)
return true;
else
return false;
}