有向图邻接矩阵实现

来源:互联网 发布:生活 知乎 编辑:程序博客网 时间:2024/04/30 03:10

DFS写了递归和非递归方法:

#ifndef GRAPH_H_#define GRAPH_H_#define MAXSIZE 100#include <iostream>#include <queue>#include <stack>using namespace std;template<class T>class Graph{public:    Graph(T a[],int n,int e);//构造函数    void DFS(int v);//从v开始深搜 (连通图)    void BFS(int v);//从v开始广搜 (连通图)    void Display();//打印这个图   #代表有边 .代表无边    bool visited[MAXSIZE];//是否访问过的标记private:    T vertex[MAXSIZE];//顶点    int arc[MAXSIZE][MAXSIZE];//边    int vnum;//顶点数    int arcnum;//边数};template<class T>Graph<T>::Graph(T a[],int n,int e){    vnum=n;    arcnum=e;    int i,j;    for(i=0; i<vnum; i++)    {        vertex[i]=a[i];//初始化顶点        for(j=0; j<vnum; j++)        {            arc[i][j]=0;//初始化边        }    }    cout<<"The Graph is created with matrix."<<endl;    cout<<"Please input the row and rank of the node in matrix one by one."<<endl<<endl;    for(int k=0; k<arcnum; k++)    {        cin>>i>>j;        arc[i][j]=1;        //arc[j][i]=a[i][j];//无向图    }}template <class T>void Graph<T>::Display(){    int i,j;    for(i=0; i<vnum; i++)    {        for(j=0; j<vnum; j++)        {            if(arc[i][j]==1)            {                cout<<"#";            }            else            {                cout<<".";            }        }        cout<<endl;    }}/*//递归template<class T>//连通图的深搜void Graph<T>::DFS(int v){    cout<<vertex[v];    visited[v]=1;    for(int j=0;j<vnum;j++)    {        if(arc[v][j]==1 && visited[j]==0)        {            DFS(j);        }    }}*///非递归template <class T>void Graph<T>::DFS(int v){    int j;    for(int i=0; i<vnum; i++)    {        visited[i]=0;    }    stack<T> S;    cout<<vertex[v];    visited[v]=1;    S.push(v);    while(!S.empty())    {        int temp=S.top();        for(j=0; j<vnum; j++)        {            if(arc[temp][j]==1 && visited[j]==0)            {                cout<<vertex[j];                visited[j]=1;                S.push(vertex[j]);                break;            }        }        if(j==vnum) S.pop();    }    //处理非连通的有向图,孤立点也要输出    for(int k=0; k<vnum; k++)    {        if(visited[k]==0)        {            cout<<vertex[k];        }    }}template<class T>void Graph<T>::BFS(int v){    for(int i=0; i<vnum; i++)    {        visited[i]=0;    }    queue<T> Q;    cout<<vertex[v];    visited[v]=1;    Q.push(v);    while(!Q.empty())    {        int temp=Q.front();        Q.pop();        for(int j=0; j<vnum; j++)        {            if(arc[temp][j]==1 && visited[j]==0)            {                cout<<vertex[j];                visited[j]=1;                Q.push(vertex[j]);            }        }    }    //处理非连通的有向图,孤立点也要输出    for(int k=0; k<vnum; k++)    {        if(visited[k]==0)        {            cout<<vertex[k];        }    }}#endif

测试:

#include <iostream>#include "Graph.h"#include <cstdlib>#include <cstdio>using namespace std;int main(){    //for test input    //10 10    //0 1 0 2 0 5 2 3 3 4 2 4 5 6 6 7 3 8 8 9    //0    int n,e;    int i;    cout<<"Please input the node_num and edge_num of the Graph:"<<endl;    cin>>n>>e;    int a[n];    for(i=0; i<n; i++)    {        a[i]=i;    }    Graph<int> hehe(a,n,e);    hehe.Display();    cout<<"Please input a node_num to start:   (a integer from 0 to n-1)"<<endl;    int num;    cin>>num;    cout<<endl<<"BFS:"<<endl;    hehe.BFS(num);    cout<<endl<<endl;    cout<<"DFS:"<<endl;    hehe.DFS(num);    system("pause");    return 0;}
原创粉丝点击