簡體   English   中英

如何在圖形中查找所有前向和交叉邊緣

[英]How to find all forward and cross edges in a graph

我知道什么是前沿和交叉邊緣。 但是我發現很難在程序中實現它們以在給定圖中找到所有前向和交叉邊緣。

在這方面的任何幫助將不勝感激。

您可以使用DFS橫向對所有圖形邊緣進行分類:

DFS-Visit(u)         ▷ with edge classification. G must be a directed graph

1.        color[u] ← GRAY
2.        time ← time + 1
3.        d[u] ← time
4.        for each vertex v adjacent to u
5.            do if color[v] ← BLACK
6.                then if d[u] < d[v]
7.                            then Classify (u, v) as a forward edge
8.                            else Classify (u, v) as a cross edge
9.                        if color[v] ← GRAY
10.                            then Classify (u, v) as a back edge
11.                       if color[v] ← WHITE
12.                            then π[v] ← u
13.                                 Classify (u, v) as a tree edge
14.                                 DFS-Visit(v)
15.        color[u] ← BLACK
16.        time ← time + 1
17.        f[u] ← time

正如你可以看到在這里

您也可以使用時鍾代替顏色。 看起來比較容易。 這是達到此目的的方法:但是我會在c中發布答案,因為我還不了解c ++,因此您必須將其轉換為c ++。

void DFS(Graph* G){             
int v;

for (v=0; v < G->n; v++)
  visited[v]=FALSE;

for (v=0; v < G->n; v++){
  if (!visited[v])
    Traverse(G, v);
 }

}

void Traverse(Graph* Gr, int v){                
int w;
Edge *current;

start[v]=clock;                             //We are going to classify the edges by the time they were visited
clock++;
visited[v]=TRUE;

current=Gr->stpoint[v];

while (current){
    w=current->vertex;

    if (!visited[w]) {                          
        printf("%d %d: Tree edge\n",v,w);       //If w is not visited then mark v-w as a tree edge
        if(!current)
            Traverse(Gr, w);
    }
    else{

        if((start[v] > start[w]) && (end[v] < end[w]))      //v was visited after w
            printf("%d %d: Back edge\n",v,w);
        else if ((start[v] < start[w]) && (end[v] > end[w]))    //v was visited before w
            printf("%d %d: Forward edge\n",v,w);
        else if ((start[v] > start[w]) && (end[v] > end[w]))    //
            printf("%d %d: Cross edge\n",v,w);

    }
    end[v]=clock;
    clock++;
    current=current->next;
 }

}

暫無
暫無

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

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