图的遍历

#include <iostream>
#include <vector>
#include <queue>
#include <string>
#include <stdio.h>
const int Max = 26;


using namespace std;


class Graph{
private:
    struct VertexNode{//节点信息
        bool isVisited;
        string data;
        vector<int> vecNode;
        VertexNode():isVisited(false) {}
    };
    struct Edge{
        int beginNode;
        int endNode;
    };


    int VertexNum;//节点个数;
    int EdgeNum;//边的个数;
    vector<VertexNode> vecVertex;//存储节点的容器
public:
    Graph(int v):VertexNum(v),EdgeNum(0){//初始化
        char tmp[Max];
        VertexNode node;
        for(int i =0;i<v;i++){
            sprintf(tmp,"v%d",i+1);
            node.data = tmp;
            vecVertex.push_back(node);
        }
    }
    void insertEdge(int b,int e){
        Edge edge;
        edge.beginNode = b;
        edge.endNode = e;
        vecVertex.at(b).vecNode.push_back(e);
        EdgeNum++;
    }
    void showGraph(){
        cout<<"show Graph:\n";
        for(int i =0;i<VertexNum;i++){
            cout<<"node "<<i<<"("<<vecVertex.at(i).data<<")";
            for(int j =0;j<vecVertex.at(i).vecNode.size();j++)
                cout<<"->"<<vecVertex.at(i).vecNode.at(j);
             cout<<endl;
        }
    }


    void depthFirstSearch(){
        cout<<"depthFirstSearch: \n";
        for(int i =0;i<VertexNum;i++){
            if(!vecVertex.at(i).isVisited)
                depthFirstSearch(i);
        }
    }
    void depthFirstSearch(int v){
        int iAdjacent = 0;
        VertexNode node = vecVertex.at(v);
        vecVertex.at(v).isVisited = true;
        cout<<node.data<<endl;
        for(int i =0;i<node.vecNode.size();i++){
            iAdjacent = node.vecNode.at(i);
            if(!vecVertex.at(iAdjacent).isVisited)
                depthFirstSearch(iAdjacent);
        }
    }


    void breadFirstSearch(){
        int count = 0;
        queue<int> bfs_queue;
        cout<<"breadFirstSearch:\n";
        for(int i = 0;i<VertexNum;i++){
            vecVertex.at(i).isVisited = false;
        }
        for(int i =0;i<VertexNum;i++){
            if(!vecVertex.at(i).isVisited){
                vecVertex.at(i).isVisited = true;
                cout<<vecVertex.at(i).data<<endl;
            }
            bfs_queue.push(i);
            while(!bfs_queue.empty()){
                VertexNode node = vecVertex.at(bfs_queue.front());
                bfs_queue.pop();
                for(int i =0;i<node.vecNode.size();i++){
                    if(!(vecVertex.at(node.vecNode.at(i)).isVisited)){
                        vecVertex.at(node.vecNode.at(i)).isVisited = true;
                        cout<<vecVertex.at(node.vecNode.at(i)).data<<endl;
                        bfs_queue.push(node.vecNode.at(i));
                    }
                }
            }
        }
    }
};



int main()
{
    Graph graph(4);
    graph.insertEdge(graph.makeEdge(0,3));
    graph.insertEdge(graph.makeEdge(0,2));
    graph.insertEdge(graph.makeEdge(1,0));
    graph.insertEdge(graph.makeEdge(3,1));
    graph.insertEdge(graph.makeEdge(3,0));
    graph.showGraph();
    graph.depthFirstSearch();
    graph.breadFirstSearch();
    cin.get();
    cout << "Hello world!" << endl;
    return 0;

}

//转自http://blog.csdn.net/wobuaishangdiao/article/details/8044916


//转自http://blog.csdn.net/wobuaishangdiao/article/details/8044916
//转自http://blog.csdn.net/wobuaishangdiao/article/details/8044916

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
void CreateGraph Graph graph 的创建 { ENode p q e; int i; cout<<"请输入连通无向的顶点数和边数 例如 3 3: n"; scanf "%d %d" &graph >numberOfVerts &graph >numberOfEerts ; for i 1;i< graph >numberOfVerts;i++ { cout<<"请输入第"<<i<<"个顶点的信息: n"; cin>>graph >amlist [i] data; graph >amlist [i] number i; graph >amlist[i] firstedge NULL; graph >amlist [i] mark 0; } for i 1;i< graph >numberOfEerts;i++ { p ENode malloc sizeof ENode ; cout<<"请输入每条边的信息(编号小的在前 例如1 3回车1 2回车2 3) n"; cin>>p >ivex>>p >jvex; p >ilink p >jlink NULL; if graph >amlist [p >ivex ] firstedge NULL graph >amlist [p >ivex ] firstedge p; else { q graph >amlist [p >ivex ] firstedge ; while q NULL { e q; if q >ivex p >ivex q q >ilink ; else q q >jlink ; } if e >ivex p >ivex e >ilink p; else e >jlink p; } if graph >amlist [p >jvex ] firstedge NULL graph >amlist [p >jvex ] firstedge p; else { q graph >amlist [p >jvex ] firstedge ; while q NULL { e q; if q >ivex p >ivex q q >ilink ; else q q >jlink ; } if e >ivex p >ivex e >ilink p; else e >jlink p; } } } void SetMark Graph graph 设置访问标记 { int i; for i 1;i< graph >numberOfVerts ;i++ graph >amlist [i] mark 0; } void DFS Graph graph int v 深度遍 { ENode p; cout<<v<<" "; graph >amlist [v] mark 1; p graph >amlist [v] firstedge ; while p NULL { if p >ivex v { if graph >amlist [p >jvex ] mark 0 { cout<<"<"<<p >ivex<<" "<<p >jvex<<">"<<endl; DFS graph p >jvex ; } p p >ilink ; } else { if graph >amlist [p >ivex] mark 0 { cout<<"<"<<p >jvex<<" "<<p >ivex<<">"<<endl; DFS graph p >ivex ; } p p >jlink ; } } }">void CreateGraph Graph graph 的创建 { ENode p q e; int i; cout<<"请输入连通无向的顶点数和边数 例如 3 3: n"; scanf "%d %d" &graph >numberOfVerts &graph >numberOfEerts ; for i 1;i< graph >numberOfVerts;i++ { cou [更多]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值