1. 程式人生 > >Depth First Search(給出臨接表輸出dfs遍歷的順系)

Depth First Search(給出臨接表輸出dfs遍歷的順系)

滴答滴答---題目連結 

Depth-first search (DFS) follows the strategy to search ”deeper” in the graph whenever possible. In DFS, edges are recursively explored out of the most recently discovered vertex vv that still has unexplored edges leaving it. When all of vv's edges have been explored, the search ”backtracks” to explore edges leaving the vertex from which vv was discovered.

This process continues until all the vertices that are reachable from the original source vertex have been discovered. If any undiscovered vertices remain, then one of them is selected as a new source and the search is repeated from that source.

DFS timestamps each vertex as follows:

  • d[v]d[v] records when vv is first discovered.
  • f[v]f[v] records when the search finishes examining vv’s adjacency list.

Write a program which reads a directed graph G=(V,E)G=(V,E) and demonstrates DFS on the graph based on the following rules:

  • GG is given in an adjacency-list. Vertices are identified by IDs 1,2,...n1,2,...n respectively.
  • IDs in the adjacency list are arranged in ascending order.
  • The program should report the discover time and the finish time for each vertex.
  • When there are several candidates to visit during DFS, the algorithm should select the vertex with the smallest ID.
  • The timestamp starts with 1.

Input

In the first line, an integer nn denoting the number of vertices of GG is given. In the next nn lines, adjacency lists of uu are given in the following format:

uu kk v1v1 v2v2 ... vkvk

uu is ID of the vertex and kk denotes its degree. vivi are IDs of vertices adjacent to uu.

Output

For each vertex, print idid, dd and ff separated by a space character in a line. idid is ID of the vertex, dd and ffis the discover time and the finish time respectively. Print in order of vertex IDs.

Constraints

  • 1≤n≤1001≤n≤100

Sample Input 1

4
1 1 2
2 1 4
3 0
4 1 3

Sample Output 1

1 1 8
2 2 7
3 4 5
4 3 6

Sample Input 2

6
1 2 2 3
2 2 3 4
3 1 5
4 1 6
5 1 6
6 0

Sample Output 2

1 1 12
2 2 11
3 3 8
4 9 10
5 4 7
6 5 6

This is example for Sample Input 2 (discover/finish)

Reference

Introduction to Algorithms, Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. The MIT Press.

//用遞迴實現dfs
#include<bits/stdc++.h>
using namespace std;
const int maxn=101;
int e[maxn][maxn];
int vis[maxn],d[maxn],f[maxn],tt;
int nt[maxn];
int n;
void dfs_visit(int u)
{
    vis[u]=1;
    d[u]=++tt;
    for(int v=0; v<n; v++)
    {
        if(e[u][v]==0)
            continue;
        if(vis[v]==0)
            dfs_visit(v);
    }
    vis[u]=2;
    f[u]=++tt;
}
void dfs()
{
    for(int i=0; i<n; i++)
    {
        vis[i]=0;
        nt[i]=0;
    }
    tt=0;
    for(int u=0; u<n; u++)
        if(vis[u]==0)
            dfs_visit(u);
    for(int i=0; i<n; i++)
        printf("%d %d %d\n",i+1, d[i],f[i]);
}
int main()
{
    int u,k,v;
    scanf("%d",&n);
    for(int i=0; i<n; i++)
        for(int j=0; j<n; j++)
            e[i][j]=0;
    for(int i=0; i<n; i++)
    {
        cin>>u>>k;
        u--;
        for(int j=0; j<k; j++)
        {
            cin>>v;
            v--;
            e[u][v]=1;

        }
    }
    dfs();
    return 0;
}

//用棧實現dfs

#include<bits/stdc++.h>
using namespace std;
const int maxn=101;
int e[maxn][maxn];
int vis[maxn],d[maxn],f[maxn],tt;
int nt[maxn];
int n;
int next(int u)
{
    for(int v=nt[u]; v<n; v++)
    {
        nt[u]=v+1;
        if(e[u][v])
            return v;
    }
    return -1;
}
void dfs_visit(int r)
{
    for(int i=0; i<n; i++)
        nt[i]=0;
    stack<int>s;
    s.push(r);
    vis[r]=1;
    d[r]=++tt;
    while(!s.empty())
    {
        int u=s.top();
        int v=next(u);
        if(v!=-1)
        {
            if(vis[v]==0)
            {
                vis[v]=1;
                d[v]=++tt;
                s.push(v);

            }

        }
        else
        {
            s.pop();
            vis[u]=2;
            f[u]=++tt;
        }
    }
}
void dfs()
{
    for(int i=0; i<n; i++)
    {
        vis[i]=0;
        nt[i]=0;
    }
    tt=0;
    for(int u=0; u<n; u++)
        if(vis[u]==0)
            dfs_visit(u);
    for(int i=0; i<n; i++)
        printf("%d %d %d\n",i+1, d[i],f[i]);
}
int main()
{
    int u,k,v;
    scanf("%d",&n);
    for(int i=0; i<n; i++)
        for(int j=0; j<n; j++)
            e[i][j]=0;
    for(int i=0; i<n; i++)
    {
        cin>>u>>k;
        u--;
        for(int j=0; j<k; j++)
        {
            cin>>v;
            v--;
            e[u][v]=1;

        }
    }
    dfs();
    return 0;
}