I have written a code for this but it gives segmentation fault for disconnected graphs. It works fine for graphs that are connected.
How can I overcome this error?
vector<int> getPathBFS(int V, int** edges,int v1, int v2, int* visited, unordered_map<int,int> t)
{
queue<int> q;
q.push(v1);
visited[v1]=1;
int done=0;
while(!q.empty() && done==0)
{
for(int i=0;i<V;i++)
{
int front=q.front();
q.pop();
if(edges[front][i]==1 && visited[i]!=1)
{
q.push(i);
t[i]=front;
visited[i]=1;
if(i==v2)
{
done=1;
break;
}
}
}
}
vector<int> a;
if(done==0)
return a;
else
{
int k=v2;
a.push_back(v2);
while(k!=v1)
{
k=t[k];
a.push_back(k);
}
return a;
}
}
int main()
{
int V, E;
cin >> V >> E;
int** edges=new int*[V];
for(int i=0;i<V;i++)
{
edges[i]=new int[V];
for(int j=0;j<V;j++)
{
edges[i][j]=0;
}
}
for(int i=0;i<E;i++)
{
int f,s;
cin>>f>>s;
edges[f][s]=1;
edges[s][f]=1;
}
int v1,v2;
cin>>v1>>v2;
int* visited=new int[V];
for(int i=0;i<V;i++)
visited[i]=0;
unordered_map<int,int> t;
t[v2]=0;
vector<int> ans=getPathBFS(V,edges,v1,v2,visited,t);
for(int i=0;i<ans.size();i++ && !ans.empty())
{
cout<<ans[i]<<" ";
}
delete [] visited;
for(int i=0;i<V;i++)
{
delete [] edges[i];
}
delete [] edges;
return 0;
}
I did a dry run of the code. It will first create adjacency matrix edges and mark all the edges in it. Visited array is used to keep track of all the vertices that have been visited till now so that there is no infinite loop.
For the test case given below it will work till the queue contains 1 then it will pop 1 and the loop will end because there is no edge left that is connected to 1 and is not visited. After this the while loop should ideally break and as done==0 it should return an empty vector. I can't understand why the segmentation fault is coming.
The map is being used to keep track of which vertex was put in the queue by which vertex.
Doesn't work for the test case:
6 3
5 3
0 1
3 4
0 3
Below is the image of the graph for the above test case:
Here we need to find the path from vertex 0 to 3.
The input format is :
Number of Vertices in the graph, Number of edges
Edges between the vertices (for E lines),
Vertices between which we need to find the path.
You are popping the BFS queue incorrectly. Instead of the inner for loop, which is executed |V| times for each entry in the queue, you should pop the queue in the outer loop, which is executed once for each element in the queue.
vector<int> getPathBFS(int V, int** edges,int v1, int v2, int* visited, unordered_map<int,int> t)
{
queue<int> q;
q.push(v1);
visited[v1]=1;
int done=0;
while(!q.empty() && done==0)
{
int front=q.front();
q.pop();
for(int i=0;i<V;i++)
{
if(edges[front][i]==1 && visited[i]!=1)
{
q.push(i);
t[i]=front;
visited[i]=1;
if(i==v2)
{
done=1;
break;
}
}
}
}
vector<int> a;
if(done==0)
return a;
else
{
int k=v2;
a.push_back(v2);
while(k!=v1)
{
k=t[k];
a.push_back(k);
}
return a;
}
}
Also, in main function of your code, there is a redundant expression !ans.empty() in the for loop where you are printing the answer(s).
Related
Hi I am try to implement a graph using adjacency list using following code.
#include<iostream>
#include<list>
#include<vector>
#include<unordered_map>
using namespace std;
class graph{
public:
vector<int> adj[10000];
void insert(int u,int v, bool direction) {
adj[u].push_back(v);
if(direction==1) {
adj[v].push_back(u);
}
}
void print(int n) {
for(int i=0;i<n+1;i++) {
cout<<i<<"->";
for(auto j : adj[i]) {
cout<<j<<",";
}
cout<<endl;
}
}
};
int main( ) {
int n;
cout<<"Enter no of node"<<endl;
cin>>n;
cout<<"enter edges "<<endl;
int m;
cin>>m;
graph g;
for(int i=0;i<m;i++) {
int u, v;
cin>>u>>v;
g.insert(u,v,1);
}
g.print(n);
return 0;
}
But the problem with this code is that it will give correct answer only in the case when my node start from 0 in a continuous manner(0,1,2,3). But when I try to print adjacency list of this graph:
Then it is giving this output:
Can somebody tell me where am I wrong?
The edges you are adding aren't the same as the graph i picture, you are inputting edge 1, 3 instead of edge 1, 5.
It's printing the 0 because you started that for loop from i = 0 and it doesn't print node 5 for the same reason (the loop ends at 4 because you will have i < 4 + 1.
void print(int n) {
//↓↓↓ HERE
for(int i=0;i<n+1;i++) {
cout<<i<<"->";
for(auto j : adj[i]) {
cout<<j<<",";
}
cout<<endl;
}
}
Here is how I would change your code:
First, I changed the print() function a little (added the if() to see if the current row is empty and I changed the int n parameter to int maximum which will hold the highest value node so we know when to stop the for).
void print(int maximum)
{
for(int i=0; i<=maximum; i++)
{
if(!adj[i].empty())
{
cout<<i<<"->";
for(auto j : adj[i])
{
cout<<j<<",";
}
cout<<endl;
}
}
}
Then, in main() I added the maximum and aux variables in order to store the aforementioned highest value node. And I also changed the g.print(n) to g.print(maximum).
int main( )
{
int n, maximum = 0, aux;
cout<<"Enter no of node"<<endl;
cin>>n;
cout<<"enter edges "<<endl;
int m;
cin>>m;
graph g;
for(int i=0; i<m; i++)
{
int u, v;
cin>>u>>v;
g.insert(u,v,1);
aux = max(u, v);
maximum = max(maximum, aux);
}
g.print(maximum);
return 0;
}
However, I might not be Terry A. Davis, but I know that if you say you have 4 nodes, those 4 nodes will be 1 2 3 and 4. And I also know that any graph related problem will have nodes starting from 1, therefore every for loop would start from i = 1, or at least that's how I was taught. The way you did it might be correct too, but I am not sure.
This my code for DFS and it should give output like this:
Following is Depth First Traversal:
0 1 3 2 4
but it is giving the output:
Following is Depth First Traversal:
0 2 3 4 1 1 1
I am not visiting the visited element again still it is not working.
#include<bits/stdc++.h>
using namespace std;
void addEdge(vector<int> adj[], int u, int v)
{
adj[u].push_back(v);
adj[v].push_back(u);
}
void DFS(vector<int> adj[], int V, int s)
{
stack<int> st;
bool visited[V];
for(int i=0; i<V;i++)
visited[i] = false;
visited[s] = true;
st.push(s);
while(st.empty()==false)
{
int n=st.top();
st.pop();
visited[n] =true;
cout<<n<<" ";
for(int v:adj[n])
{
if(visited[v]==false)
st.push(v);
}
}
}
int main()
{
int V=5;
vector<int> adj[V];
addEdge(adj,0,1);
addEdge(adj,0,2);
addEdge(adj,2,3);
addEdge(adj,1,3);
addEdge(adj,1,4);
addEdge(adj,3,4);
cout << "Following is Depth First Traversal: "<< endl;
DFS(adj,V,0);
return 0;
}
Unless there is a good reason to use an explicit stack, I would recommend to use recursion(implicit stack). However I am going to fix it least changes to your code.
There are 3 things to fix and I left comments below.
void DFS(vector<int> adj[], int V, int s)
{
stack<int> st;
vector<bool> visited(V, false); // 1. Don't use VLA as it is not standard
// 2. Remove redundant first element visit marking
st.push(s);
while(st.empty()==false)
{
int n=st.top();
st.pop();
// 2. Check if visited since some elements may have added multiple times
// (Some are pushed in the stack many times but not never visited yet)
if (visited[n])
continue;
visited[n] =true;
cout<<n<<" ";
// 3. Reverse the order of iteration
for(auto v = adj[n].rbegin(); v != adj[n].rend(); ++v)
{
if(visited[*v]==false)
st.push(*v);
}
}
}
https://godbolt.org/z/Kz16GT
Adding some more about no. 3 - Actually 0 2 3 4 1 is a valid DFS order too. But it traverses from the reverse order of adj[n] due to the nature of Stack. So iterating reverse way will make the iteration result be 0 1 3 2 4.
I am implementing bfs (Breadth First Search ) for the graph , but I am getting an error while I pass the starting value of the vector to an integer, for the dfs function to perform, as in the dfs function I have passed the source of the vector, i.e the first element of the vector.
error is on the line where start is declared to v[i]
Here is the complete code
#include <iostream>
#include <vector>
#include <queue>
#include <stdio.h>
using namespace std;
vector<int> v[10];
bool visited[10];
int level[10];
int a = 0;
int arr[10];
void dfs(int s) //function should run only one time
{
queue<int> q;
q.push(s);
visited[s] = true;
level[s] = 0;
while (!q.empty())
{
int p = q.front();
arr[a] = p;
a++;
q.pop();
for (int i = 0; i < v[p].size(); i++)
{
if (visited[v[p][i]] == false) {
level[v[p][i]] = level[p] + 1;
q.push(v[p][i]);
visited[v[p][i]] = true;
}
}
}
}
int main()
{
char c;
int start; // starting element of the vector
int i = 0; // for keeping track of the parent
int countt = 0; // keep track of the no of parents
bool check;
printf("Child or Parent ?");
scanf("%c", &c);
while (countt <= 10) {
if (c == 'c') {
check = true;
int j = 0;
while (check) {
// to keep the track of the child;
scanf("%d", &v[i][j]);
j++;
}
}
if (c == 'p')
{
scanf("%d", &v[i]);
if (i == 0)
{
start = v[i];
}
i++;
countt++;
}
}
printf(" Vector input completed");
dfs(start);
printf("DFS completed, printing the dfs now ");
for (int g = 0; g <= 10; g++)
{
printf("%d", &arr[g]);
}
}
In your current code, v is an array of size 10 containing vector's. However, start is an int, so there is nothing strange in getting an error when trying to assign one to another.
I believe that you wanted v to be either an array of ints or vector of ints. In such a case you just have to declare v properly: int v[10] or vector<int> v(10).
This is general syntax: if you want to declare a vector with known size then you have to put it in (), not in []. Note that you can also fill the vector with some initial values (say zeroes) by writing vector<int> v(10, 0).
In case got you wrong and you wanted to store a graph as vector of vectors, then you can write vector<vector<int>> v(10).
Given a unweighted and undirected tree with N nodes and N-1 edges I need to find minimum distance between source S and destination D.
Code :
vector<vector<int> >G(110);
bool check(int node,vector<int>path)
{
for(int i=0;i<path.size();++i)
{
if(path[i]==node)
return false;
}
return true;
}
int findMinpath(int source,int target,int totalnode,int totaledge)
{
vector<int>path;
path.push_back(source);
queue<vector<int> >q;
q.push(path);
while(!q.empty())
{
path=q.front();
q.pop();
int lastNode=path[path.size()-1];
if(lastNode==target)
{
return path.size()-1;
}
for(int i=0;i<G[lastNode].size();++i){
if(check(G[lastNode][i],path)){
vector<int>new_path(path.begin(),path.end());
new_path.push_back(G[lastNode][i]);
q.push(new_path);
}}}
return 1;
}
And then in main :
int N,S,E;
cin>>N>>S>>E;
for(int i=1;i<=N-1;++i)
{
int u,v;
cin>>u>>v;
G[u].push_back(v);
G[v].push_back(u);
}
cout<<findpaths(S,E,N,N-1)<<"\n";
Can it be further optimised as I need just minimum distance between S and E
You seem to be pushing vectors to your queue, each vector containing the actual path so far. But you can get away with only pushing nodes, since you only use the last node in those vectors anyway, and instead store the distance to each node from the source.
This will be much faster because you won't be copying vectors at each step.
Keep track of the distances in an array and also use them to make sure you don't visit a node multiple times.
Untested, but should get the point across:
int distance[110 + 1]; // d[i] = distance from source to i, initialize with a large number
int findMinpath(int source,int target,int totalnode,int totaledge)
{
for (int i = 0; i <= totalnode; ++i)
{
distance[i] = 2000000000;
}
queue<int> q;
q.push(source);
distance[source] = 0;
while(!q.empty())
{
node=q.front();
q.pop();
if(node==target)
{
return distance[node];
}
for(int i=0;i<G[node].size();++i){
if(distance[node] + 1 < distance[ G[node][i] ]){
distance[ G[node][i] ] = distance[node] + 1
q.push(G[node][i]);
}}}
return 1;
}
Hi guys so I am making function for my graphs (adjacency matrices) to return number of connected components using breadth first search algorithm.
It almost works properly. It returns proper value if number of components is equal to number of vertices, but if number of components is smaller than number of vertices it returns (proper value +1). I have no idea how to fix it, so if you could give it a look and tell me I would be glad. Heres the link to the code it looks more decent then the one below http://wklej.org/id/861341/
int Graph::getNumberOfConnectedComponents()
{
int components=0;
queue<int> S;
int n = getVerticesCount();//as name indicates it returns number of vertices in graph
bool* visited = new bool[n];
for(int i=1;i<n;i++)
visited[i]=false;
visited[0]=true;
S.push(0);
while(!S.empty())
{
int v = S.front();
S.pop();
list<int> x = getNeighbors(v);//as name indicates this function returns list of neighbours of given vertice
if(!x.empty())
{
list<int>::iterator it;
for (it=x.begin(); it!=x.end(); it++)
{
if(visited[*it]==false)
{
S.push(*it);
visited[*it]=true;
}
}
}
if(S.empty())
{
components++;
for(int i=1;i<n;i++)
{
if(visited[i]==false)
{
S.push(i);
visited[i]=true;
break;
}
}
}
}
return components;
}
I explained what those functions do in comments, hope you will be able to help me :/ . Btw if you change place of components++; and put it into if(visited[i]==false) it gives proper value for all graphs except those which number of components = number of vertices (for those this value is "proper value-1").
#Edit 1 , here is this function john
list<int> AdjacencyMatrixGraph::getNeighbors(int v)
{
list<int> x;
if(v==0||v>=n)
return x;
for(int j=0;j<n;j++)
{
if(matrix[v][j]!=0)
x.push_front(j);
}
return x;
}
list<int> AdjacencyMatrixGraph::getNeighbors(int v)
{
list<int> x;
if(v==0||v>=n)
return x;
should be
list<int> AdjacencyMatrixGraph::getNeighbors(int v)
{
list<int> x;
if(v<0||v>=n)
return x;
vertex 0 can have neighbors as far as I can tell.