• 数据结构-图的深度优先遍历方式(非递归,领接表存储)


    1. #include
    2. #include
    3. //表结点,采用邻接表存储图
    4. #include
    5. #include
    6. #include
    7. using namespace std;
    8. //表结点
    9. struct ArcNode {
    10. ArcNode * next; //下一个关联的边
    11. int adjvex; //保存弧尾顶点在顶点表中的下标
    12. };
    13. //顶点表
    14. struct Vnode {
    15. string data; //顶点名称
    16. ArcNode * firstarc; //第一个依附在该顶点边
    17. };
    18. class Graph_DG {
    19. private:
    20. int vexnum; //图的顶点数
    21. int edge; //图的边数
    22. int * indegree; //每条边的入度情况
    23. Vnode * arc; //邻接表
    24. public:
    25. Graph_DG(int, int);
    26. ~Graph_DG();
    27. //检查输入边的顶点是否合法
    28. bool check_edge_value(int,int);
    29. //创建一个图
    30. void createGraph();
    31. //打印邻接表
    32. void print();
    33. //进行拓扑排序,Kahn算法
    34. bool topological_sort();
    35. //进行拓扑排序,DFS算法
    36. bool topological_sort_by_dfs();
    37. void dfs(int n,bool * & visit, stack & result);
    38. };
    39. //初始化 vexnum:顶点个数,edge:边数
    40. Graph_DG::Graph_DG(int vexnum, int edge)
    41. {
    42. this->vexnum = vexnum;
    43. this->edge = edge;
    44. this->arc = new Vnode[this->vexnum];
    45. this->indegree = new int[this->vexnum]; //每个顶点的入度数目
    46. for (int i = 0; i < this->vexnum; i++) {
    47. this->indegree[i] = 0;
    48. this->arc[i].firstarc = NULL;
    49. this->arc[i].data = "v" + to_string(i + 1);
    50. }
    51. }
    52. void Graph_DG::createGraph() {
    53. int count = 0;
    54. int start, end;
    55. cout << "输入每条起点和终点的顶点编号(从1开始编号)" << endl;
    56. while (count != this->edge) {
    57. cin >> start;
    58. cin >> end;
    59. //检查边是否合法
    60. while (!this->check_edge_value(start, end)) {
    61. cout << "输入的顶点不合法,请重新输入" << endl;
    62. cin >> start;
    63. cin >> end;
    64. }
    65. //声明一个新的表结点
    66. ArcNode * temp = new ArcNode;
    67. temp->adjvex = end - 1;
    68. temp->next = NULL;
    69. //如果当前顶点的还没有边依附时,
    70. if (this->arc[start - 1].firstarc == NULL) {
    71. this->arc[start - 1].firstarc = temp;
    72. }
    73. else {
    74. ArcNode * now = this->arc[start - 1].firstarc;
    75. while(now->next) {
    76. now = now->next;
    77. }//找到该链表的最后一个结点
    78. now->next = temp;
    79. }
    80. ++count;
    81. }
    82. }
    83. void Graph_DG::print() {
    84. int count = 0;
    85. cout << "图的邻接矩阵为:" << endl;
    86. //遍历链表,输出链表的内容
    87. while (count != this->vexnum) {
    88. //输出链表的结点
    89. cout << this->arc[count].data<<" ";
    90. ArcNode * temp = this->arc[count].firstarc;
    91. while (temp) {
    92. cout<<"<"<< this->arc[count].data<<","<< this->arc[temp->adjvex].data<<"> ";
    93. temp = temp->next;
    94. }
    95. cout << "^" << endl;
    96. ++count;
    97. }
    98. }
    99. //非递归的方式去深度优先遍历图
    100. void DFS(Graph_DG G,int startnode)
    101. {
    102. vector<int> visit(vexnum,0);
    103. stack<int> s;
    104. s.push(startnode);
    105. visit[startnode] =1;
    106. ArcNode * temp = this->arc[startnode].firstarc;
    107. while(!s.empty())
    108. {
    109. temp =this->arc[s.top()].firstarc; //栈顶
    110. while(temp)
    111. {
    112. if(visit[temp->adjvex]==0)
    113. {
    114. visit[temp->adjvex]==1;
    115. printf("2%d");
    116. s.push(temp->adjvex);
    117. temp=this->arc[temp->adjvex].firstarc;
    118. }
    119. else //
    120. {
    121. temp =temp->next;
    122. }
    123. }
    124. if(temp==NULL)
    125. {
    126. s.pop();
    127. }
    128. }
    129. }

  • 相关阅读:
    白炽灯和led哪个护眼?分享真正适合孩子的护眼台灯
    HTTPS 加密原理
    Leetcode刷题方法总结---字符串全解
    迅为RK3588开发板编译环境Ubuntu20.04 编译配置增加交换内存
    兄弟MFC-7480D打印机墨粉清零方法(图解)
    Unity3D Shader新手入门教程:3D溶解与腐蚀特效详解
    当前视频分析【video analytics】都有哪些痛点?为什么难以落地?
    csp非零段划分
    【笔记】Nginx(5)七层负载均衡
    Spring的组件扫描配置
  • 原文地址:https://blog.csdn.net/Colin_xuan/article/details/126299981