算法导论22(基本的图算法)

1. 广度优先搜索(BFS)

#include<iostream> 
#include<queue>
using namespace std;

const int N=100;
bool visited[N];
int d[N],pred[N];

struct Node
{
    int v;
    Node *next;
    Node(int x):v(x),next(0){}
};

struct Graph    
{
    int VNum,ENum;    
    Node *Adj[N]; 
};    

//无向图,图的顶点的编号为0..VNum-1
void createGraph(Graph &G)    
{    
    cin>>G.VNum>>G.ENum;   
    for(int i=0;i<G.VNum;++i)G.Adj[i]=0;  
    for(int i=0;i<G.ENum;++i)    
    {
        int u,v;    
        cin>>u>>v;    
        Node *p=new Node(v);
        p->next=G.Adj[u];  
        G.Adj[u]=p;
        p=new Node(u);  
        p->next=G.Adj[v];  
        G.Adj[v]=p;
    }    
}    

void BFS(Graph G,int s)
{
    for(int i=0;i<G.VNum;++i)
    {
        visited[i]=false;
        d[i]=INT_MAX;
        pred[i]=-1;
    }
    visited[s]=true;
    d[s]=0;
    queue<int> q;
    q.push(s);
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        for(Node *p=G.Adj[u];p;p=p->next)
        {
            int v=p->v;
            if(!visited[v])
            {
                visited[v]=true;
                d[v]=d[u]+1;
                pred[v]=u;
                q.push(v);
            }
        }
    }
}

void printPath(Graph G,int s,int v)
{
    if(v==s)cout<<s<<' ';
    else 
    {
        printPath(G,s,pred[v]);
        cout<<v<<' ';
    }
}

//输入
//8 10
//0 1
//0 4
//1 5
//2 3
//2 5
//2 6
//3 6
//3 7
//5 6
//6 7
//
//输出
//1 0
//1 
//1 5 2
//1 5 2 3
//1 0 4
//1 5
//1 5 6
//1 5 6 7

int main()      
{  
    Graph G;  
    createGraph(G);
    int s=1;
    BFS(G,s);
    for(int i=0;i<G.VNum;++i)
    {
        printPath(G,s,i);
        cout<<endl;
    }
    return 0;      
}

2. 深度优先搜索(DFS)

#include<iostream>
using namespace std;

const int N=100;   
bool visited[N];
int d[N],pred[N],f[N],time;  

struct Node    
{    
    int v;  
    Node *next;
    Node(int x):v(x),next(0){}  
};

struct Graph    
{       
    int VNum,ENum;
    Node *Adj[N];     
};    

//有向图,图的顶点的编号为0..VNum-1
void createGraph(Graph &G)    
{    
    cin>>G.VNum>>G.ENum;   
    for(int i=0;i<G.VNum;++i)G.Adj[i]=0;    
    for(int i=0;i<G.ENum;++i)    
    {
        int u,v;    
        cin>>u>>v;    
        Node *p=new Node(v);
        p->next=G.Adj[u];  
        G.Adj[u]=p;
    }    
}    

void DFSVisit(Graph G,int u)  
{  
    d[u]=++time;  
    visited[u]=true;  
    for(Node *p=G.Adj[u];p;p=p->next)  
    {
        int v=p->v;
        if(!visited[v])  
        {  
            pred[v]=u;  
            DFSVisit(G,v);  
        }   
    }  
    f[u]=++time;  
}  

void DFS(Graph G)  
{  
    for(int i=0;i<G.VNum;++i)  
    {  
        visited[i]=false;  
        pred[i]=-1;  
    }
    time=0;
    for(int i=0;i<G.VNum;++i)  
    {  
        if(!visited[i])DFSVisit(G,i);  
    }  
}  

//输入
//6 8
//0 1
//0 3
//1 4
//2 4
//2 5
//3 1
//4 3
//5 5
//
//输出
//1 8
//3 6
//9 12
//2 7
//4 5
//10 11

int main()      
{  
    Graph G;  
    createGraph(G);  
    DFS(G);
    for(int i=0;i<G.VNum;++i)
    {
        cout<<d[i]<<' '<<f[i]<<endl;
    }
    return 0;      
}    

3. 拓扑排序

#include<iostream>
#include<vector>
#include<stack>
using namespace std;

struct Node
{
    int v,w;
    Node *next;
    Node(int x,int y):v(x),w(y),next(0){}
};

struct Graph
{
    int VNum,ENum;
    vector<Node *> Adj;
};

void createGraph(Graph &G)
{
    cin>>G.VNum<<G.ENum;
    for(int i=0;i<G.VNum;++i)Adj.push_back(0);
    for(int i=0;i<G.ENum;++i)
    {
        int u,v,w;
        cin>>u>>v>>w;
        Node *p=new Node(v,w);
        p->next=G.Adj[u];
        G.Adj[u]=p;
    }
}

vector<int> GetIndegree(Graph G)
{
    vector<int> indegree(G.VNum,0);
    for(int i=0;i<G.VNum;++i)
    {
        for(Node *p=G.Adj[i];p;p=p->next)
        {
            int v=p->v;
            ++indegree[v];
        }
    }
    return indegree;
}

vector<int> TopoSort(Graph G)
{
    vector<int> res;
    vector<int> indegree=GetIndegree(G);
    stack<int> S;
    for(int i=0;i<G.VNum;++i)
    {
        if(indegree[i]==0)S.push(i);
    }
    while(!S.empty())
    {
        int u=S.top();
        S.pop();
        res.push_back(u);
        for(Node *p=G.Adj[u];p;p=p->next)
        {
            int v=p->v;
            if(--indegree[v]==0)S.push(v);
        }
    }
    return res;
}

int main()
{
    Graph G;
    createGraph(G);
    vector<int> res=TopoSort(G);
    for(int i=0;i<res.size();++i)cout<<res[i]<<' ';
    cout<<endl;
    return 0;
}

你可能感兴趣的:(算法导论22(基本的图算法))