I'm given a graph, where the points are connected by edges given by numbers like:
1 3
2 19
8 12
etc.
The method prescribed to find the graph is to set a vector<vector<int> > adj, where adj[1][3] = 1 and adj[3][1] =1, ie, adjacency is true for vertices 1 and 3, and so on.
the condition is that the starting and ending vertex is 1.
My method is to:
1. read the file
2. push the data into two vectors of 100 entries. Also, define an adjacency vector adj where all adjacent edges give 1. This is required from the question.
3. declare a starting number, z =1
4. use a for loop to find a number for which adj[i][z] = 1 or adj[z][i] = 1.
5. update z.
However, I have a few problems.
1. This is clearly the wrong code.
2. I don't have a method to end at 1.
3. The if expression that you see there only meets the adj[i][z]=1 condition but can't account for the other condition.
Can someone help, please? Is there a better way to do this without the additional vectors a and b??
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
int main()
{
vector<vector<int> > adj(101);
vector<int> a;
vector<int> b;
for(int i=1;i<=100;i++)
adj[i].resize(101);
ifstream in("data.txt");
int x,y;
while(in >> x >> y)
{
a.push_back(x);
b.push_back(y);
adj[x][y]=1;
adj[x][y]=1;
}
int z;
z =1;
cout << z << " ";
for(int i=0;i<100;i++)
{
if(adj[z][i] == 1 && b[i-1]==z && a[i-1] != z)
{
cout << a[i-1] << " ";
start = a[i-1];
}
}
}
|