i want to determine if a given graph has the structure i want. The structure i want is that if the given graph's tree's roots form a cycle then the output is true, else is false.
Here’s an example graph:
It has 3 trees and the roots 1,5,4 form a cycle.
Also this is an example that should not pass because it does not contain tree's which root's form a cycle:
How can I decide given the vertices which trees should i search?
This is the code so far, printing the adjacency list of a given graph.
#include <iostream>
#include <vector>
using namespace std;
void addEdge(vector<int> vec[], int u, int v)
{
vec[u].push_back(v);
}
void printGraph(vector<int> vec[], int j)
{
cout << "Graph's adjacent list: \n";
for (int v = 0; v < j; ++v)
{
if (vec[v].size() == 0) continue;
cout << "Head(" << v << ")";
for (auto x = vec[v].begin(); x != vec[v].end(); x++)
cout << " -> " << *x;
cout << "\n" ;
}
}
int main()
{
int V = 10;
vector<int> vec[V];
addEdge(vec, 6, 3);
addEdge(vec, 7, 1);
addEdge(vec, 8, 9);
addEdge(vec, 6, 4);
addEdge(vec, 5, 1);
addEdge(vec, 1, 9);
addEdge(vec, 2, 5);
addEdge(vec, 1, 4);
addEdge(vec, 5, 4);
printGraph(vec, V);
return 0;
}