簡體   English   中英

如果圖中只有一個循環,如何找到無向圖中哪些頂點構成了循環?

[英]How to find of what vertices is made cycle in undirected graph if there is only one cycle in graph?

如果圖中只有一個循環,如何找到無向圖中哪些頂點構成了循環?

我有用於在圖形中查找循環的代碼,但是現在我需要用於查找形成哪些頂點循環的代碼。

這是用於查找循環的代碼(在C ++中):

bool dfs(int x)
{
    state[x] = 1;
    for(int j = 0; j < ls[x].size(); j++)
    {
        if(state[ls[x][j]] == 1 and parent[x] != ls[x][j])
        {
            t = 0; // Graph contains cycle.
            return t;
        }
        if(state[ls[x][j]] == 0)
        {
            parent[ls[x][j]] = x;
            dfs(ls[x][j]);
        }
    }
}

void detect_cycle()
{
    memset(state, 0, sizeof state);
    memset(parent, 0, sizeof parent);
    for(int i = 1; i <= n; i++)
        if(state[i] == false)
            dfs(i);
}

謝謝。

這是最終代碼。 多謝你們。

bool dfs(int x)
{
    state[x] = 1;
    for(int j = 0; j < ls[x].size(); j++)
    {
        if(state[ls[x][j]] == 1 and parent[x] != ls[x][j])
        {
            if(t)
            {
                printf("Cycle entry: %d\n", ls[x][j]);
                printf("Cycle contains: %d, %d ", ls[x][j], x);
                int cycleNode = parent[x];
                while(cycleNode != ls[x][j])
                {
                    printf("%d ", cycleNode);
                    cycleNode = parent[cycleNode];
                }
            }
            t = 0;
            return t;
        }
        if(state[ls[x][j]] == 0)
        {
            parent[ls[x][j]] = x;
            dfs(ls[x][j]);
        }
    }
}

天真的方法-丟棄所有度數為1的節點,直到所有節點度數為2。這是圖中的循環。

運行dfs時,如果在dfs()之前標記了頂點,則必須有一個循環。

     A
   /
  B
 |  \
 C  E
  \ /
   D

如果圖中只有一個循環,則無論dfs的起始頂點是什么,前面標記的頂點都是循環的入口,如下面的B所示。 在你的代碼中

if(state[ls[x][j]] == 1 and parent[x] != ls[x][j])
{
    t = 0; // Graph contains cycle.
    return t;
}

在第一個if() {} ,parent和t是不必要的,請更改為:

if(state[ls[x][j]] == 1 and parent[x] != ls[x][j])
{
    cout<<"cycle's entry:"<<j<<endl;
     // Graph contains cycle.
    return false;
}

此外,您的代碼需要return true; dfs()for之外。

如果我是對的,則parent []是一個數組(parent [i]是您訪問第i個節點之前直接存活的節點的編號)。

然后,您知道如果圖形包含循環(您訪問了已經訪問過的節點),則知道循環中至少有一個節點(假設其第k個節點)。 在這種情況下,parent [k]節點也屬於該循環,而parent [parent [k]]等也屬於該循環。

這樣,我們得到下一個代碼:

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <map>
using namespace std;

vector <int> state;
vector <vector <int> > ls; //graph
vector <int> parent;
bool t = 1; 
int theNodeInTheCycle;

void dfs(int x)
{
    state[x] = 1;
    for(int j = 0; j < ls[x].size(); j++)
    {
            if(state[ls[x][j]] == 1 && parent[x] != ls[x][j])
            {
                    parent[ls[x][j]] = x;
                    theNodeInTheCycle = ls[x][j]; //ls[x][j] belongs to the cycle since state[ls[x][j]]==1
                    t = 0;
            }
            if(state[ls[x][j]] == 0)
            {
                    parent[ls[x][j]] = x;
                    dfs(ls[x][j]);
            }
    }
}

vector <int> GetCycle ()
{
    vector <int> cycle;
    int firstNodeInTheCycle = theNodeInTheCycle;
    do 
    {
            theNodeInTheCycle = parent[theNodeInTheCycle];
            cycle.push_back (theNodeInTheCycle);
    } while (theNodeInTheCycle != firstNodeInTheCycle);

    reverse (cycle.begin (), cycle.end ()); //to get them in the right order
    return cycle;
}

int main()
{
    int n; cin>>n; //the number of nodes (from 0 to n-1)
    int m; cin>>m; //the number of edges

    state.resize (n);
    ls.resize (n);
    parent.resize (n);

    for (int i = 0; i < m; ++i)
    {
            int a, b; cin>>a>>b;
            ls[a].push_back(b);
            ls[b].push_back(a);
    }

    for (int i = 0; i<n; ++i)
            if (state[i]==0)
                    dfs(i);

    if (t==0) 
    {
            vector <int> cycle = GetCycle ();
            for (int i = 0; i < cycle.size (); ++i)
                    cout<<cycle[i]<<" ";
            cout<<"\n";
    }
    else cout<<"No cycle\n";
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM