有向图邻接矩阵实现

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值