1

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: for example this 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:

graph example

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; 
} 
george_pap
  • 23
  • 4
  • 1
    What have you tried so far? how are you representing the graph in code? – cigien Apr 19 '20 at 19:05
  • I coded the part that implements the graph given the nodes and the edges. Then printed the graph's adjacency list. Also coded the part that finds a cycle in a graph but thats not helpfull at all because not all graphs have the structure i specified above. – george_pap Apr 19 '20 at 19:13
  • Ok, but at least show the code that you already have and then describe the issues with it. – cigien Apr 19 '20 at 19:13
  • To confirm: you want to check whether there are three nodes linked in a triangle such that the graph, except for those three nodes, forms a tree? – templatetypedef Apr 19 '20 at 22:42
  • @george_pap *How can I decide given the vertices which trees should i search?* -- Does it matter? Regardless of which node you start at, you will hit the cycle if you do a depth-first search. – PaulMcKenzie Apr 19 '20 at 22:44
  • @templatetypedef not exactly. Given a graph i want to be able to tell if there are trees in this graph, the root's of those form a cycle. Also the cycle could contain more than three tree's roots and the tree could have only the root and no children. in example a graph like a square (0->1, 1->2, 2->3, 3->0) should pass. – george_pap Apr 19 '20 at 23:48
  • @PaulMcKenzie this is whre i am stuck. It does not matter if the tree's root's you find form a cycle. What i am thinking is that a possible solution could be a graph containing exactly one cycle. Since a tree could only be the root, every graph with a cylce should pass. – george_pap Apr 19 '20 at 23:57
  • You have a raw array of std::vectors? That's ... inadvisable? Also it's a non-standard VLA. None of this code attempts to do any of the stuff you're asking about. A quick google search suggests that you detect cycles by doing DFS, mark visited nodes, and if you find one then you have a cycle. QED – Kenny Ostrom Apr 20 '20 at 00:38

1 Answers1

0

Your question is how to tell whether a given graph

  1. contains exactly one cycle,
  2. where each node on the cycle is the root of a tree.

The good news is that, assuming your graph is connected, then if property (1) is true, then so is property (2)! To see why this is, imagine deleting any edge from that cycle. Now you have a connected graph with no cycles, which is a tree. That means that every node, not just the ones on the cycle, can then be thought of as the root.

The nice part about this is that there’s a really nice algorithm for determining whether a graph is connected and contains exactly one cycle. First, count the number of edges in the graph. If the graph is indeed connected and contains exactly one cycle, then the number of edges should be exactly equal to the number of nodes. (A tree has one more node than edge, and you’ve added in an edge). If that isn’t the case, then stop - the answer is no.

From there, you know you have the right number of nodes and edges. You just need to check whether the graph is connected, and you can do that with a DFS or BFS over the graph.

The nice part about this is that if you implement it correctly, the runtime will be O(n), where n is the number of nodes, independently of the number of edges. After all, if you see more than n edges, you can stop the search.

Hope this helps!

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065