Fleury(弗羅萊)算法求歐拉路徑


上面是摘自圖論書上的定義。

算法在運行過程中刪除了所有已走的路徑,也就是說途中殘留了所有沒有行走的邊。根據割邊的定義,如果在搜索過程中遇到割邊意味着當前的搜索路徑需要改進,即提前輸出某一個聯通子集的訪問序列,這樣就能夠保證訪問完其中聯通子圖中后再通過割邊訪問后一個聯通子圖,最后再沿原路輸出一開始到達該點的路徑。如果只有割邊可以擴展的話,只需要考慮先輸出割邊的另一部分聯通子集訪問序列。

樣例圖:

代碼如下:

#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
/*
弗羅萊算法
*/

int stk[1005];
int top;
int N, M, ss, tt;
int mp[1005][1005];

void dfs(int x) {
    stk[top++] = x;
    for (int i = 1; i <= N; ++i) {
        if (mp[x][i]) {
            mp[x][i] = mp[i][x] = 0; // 刪除此邊
            dfs(i);
            break;
        }    
    }
}
/*
9 12
1 5
1 9
5 3
5 4
5 8
2 3
2 4
4 6
6 7
6 8
7 8
8 9

path:
4 5 8 7 6 8 9 1 5 3 2 4 6
*/

void fleury(int ss) {
    int brige;
    top = 0;
    stk[top++] = ss; // 將起點放入Euler路徑中
    while (top > 0) {
        brige = 1;
        for (int i = 1; i <= N; ++i) { // 試圖搜索一條邊不是割邊(橋) 
            if (mp[stk[top-1]][i]) {
                brige = 0;
                break;
            }
        }
        if (brige) { // 如果沒有點可以擴展,輸出並出棧
            printf("%d ", stk[--top]);
        } else { // 否則繼續搜索歐拉路徑
            dfs(stk[--top]);
        }
    }
}

int main() {
    int x, y, deg, num;
    while (scanf("%d %d", &N, &M) != EOF) {
        memset(mp, 0, sizeof (mp));
        for (int i = 0; i < M; ++i) {
            scanf("%d %d", &x, &y);
            mp[x][y] = mp[y][x] = 1;
        }
        for (int i = 1; i <= N; ++i) {
            deg = num = 0;
            for (int j = 1; j <= N; ++j) {
                deg += mp[i][j];    
            }
            if (deg % 2 == 1) {
                ss = i, ++num;
                printf("%d\n", i);
            }
        }
        if (num == 0 || num == 2) {
            fleury(ss);
        } else {
            puts("No Euler path");
        }
    }
    return 0;    
} 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM