-1

Let G=(V,E) be a simple undirected graph. Suggest an algorithm that finds some simple cycle in the graph and prints it (the sequence of nodes composing it). If there is no such cycle, the algorithm will not print anything.


Algorithm:

  1. Initiate an array of size n, and a parent variable for each vertex.
  2. Start DFS on a random vertex, and for each visited vertex, mark "1" in the array, and assign its parent node.
  3. If in the DFS run, the next vertex is an already marked vertex which is not its parent - there is a cycle in the graph, and print backwards all of the nodes using their parent variable.

Is the algorithm correct? Or do I need to change things?

Thanks!

Olivier
  • 13,283
  • 1
  • 8
  • 24
User
  • 1
  • 1
  • 2
    Looks good to me. – David Eisenstat Nov 06 '21 at 12:04
  • 2
    I'd add, that you need to track the count of "visited" vertices. If DFS hasn't found the cycle, but not all vertices were visited, that means that graph is not connected, and you need to start another DFS from any other unvisited vertex. – Aivean Nov 06 '21 at 20:21

1 Answers1

0

From the graph theory we know that:

  1. if the quantity of vertices of a graph is more than the quantity of edges, therefore, cycles (closed contours) are absent.
  2. if the quantity of vertices of a graph is equal to the quantity of edges, therefore, a graph has only one cycle.
  3. if the quantity of vertices of a graph is less than the quantity of edges, therefore, a graph has more than one closed contour.

enter image description here

I offer the algorithm Depth first search, that finds a simple cycles in the graph and prints them:

#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
using namespace std;
const int maximumSize=40;
vector<vector<int>> visited(maximumSize, vector<int>(maximumSize, 0));
vector<int> graph[maximumSize], closedContour, temporary;
int vertices, edges;
set<vector<int>> contours;
void showContentSetVector(set<vector<int>> input)
{
    for(auto iterator=input.begin(); iterator!=input.end(); ++iterator)
    {
        for(auto item : *iterator)
        {
            cout<<item<<", ";
        }
        cout<<endl;
    }
    return;
}
bool compare(int i,int j)
{
    return (i<j);
}
void createGraph()
{
    cin>>vertices>>edges;
    int vertex0, vertex1;
    for(int i=1; i<=edges; ++i)
    {
        cin>>vertex0>>vertex1;
        graph[vertex0].push_back(vertex1);
        graph[vertex1].push_back(vertex0);
    }
    return;
}
void depthFirstSearch(int initial, int current, int previous)
{
    if(visited[initial][current]==1)
    {
        for(int i=0; i<temporary.size(); ++i)
        {
            if(temporary[i]==current)
            {
                for(int j=i; j<temporary.size(); ++j)
                {
                    closedContour.push_back(temporary[j]);
                }
            }
        }
        sort(closedContour.begin(), closedContour.end(), compare);
        contours.insert(closedContour);
        closedContour.clear();
        return;
    }
    visited[initial][current]=1;
    temporary.push_back(current);
    for(int next : graph[current])
    {
        if(next==previous)
        {
            continue;
        }
        depthFirstSearch(initial, next, current);
    }
    temporary.pop_back();
    return;
}
void solve()
{
    createGraph();
    for(int vertex=1; vertex<=vertices; ++vertex)
    {
        temporary.clear();
        depthFirstSearch(vertex, vertex, -1);
    }
    cout<<"contours <- ";
    showContentSetVector(contours);
    return;
}
int main()
{
    solve();
    return 0;
}

Here is the result:

contours <- 
1, 2, 3, 4, 
6, 7, 8, 
Vadim Chernetsov
  • 370
  • 1
  • 2
  • 14