鱼C论坛

 找回密码
 立即注册
查看: 3655|回复: 5

[技术交流] 有向图的拓扑排序

[复制链接]
发表于 2014-2-18 11:00:22 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 andalousie 于 2014-2-18 11:03 编辑

对于有向图,只需要在IsConnected里面只定义一个方向值等于一,另一个值不定义即可。graph.h
  1. #if !defined (GRAPH_H)
  2. #define GRAPH_H
  3. #include "stack.h"

  4. /************************************************************

  5. Class Graph represents a Graph [V,E] having vertices V and
  6. edges E.

  7. ************************************************************/
  8. class Graph {
  9.     private:
  10.         int    _n;  /// n is the number of vertices in the graph
  11.         int ** _A;  /// A stores the edges between two vertices
  12.         bool * visited;
  13.         IStack reverse;
  14.     public:
  15.         Graph(int size = 2);
  16.         ~Graph();
  17.         bool isConnected(int, int);
  18.         void addEdge(int x, int y);
  19.         void DFS(int);
  20.         void Toposort();
  21. };

  22. #endif
复制代码
graph.cc
  1. #include <iostream>
  2. #include "graph.h"
  3. #include "stack.h"

  4. Graph::Graph(int size) {
  5.     int i, j;
  6.     if (size < 2) _n = 2;
  7.     else _n = size;
  8.     _A = new int*[_n];
  9.     for (i = 0; i < _n; ++i)
  10.         _A[i] = new int[_n];
  11.     for (i = 0; i < _n; ++i)
  12.         for (j = 0; j < _n; ++j)
  13.             _A[i][j] = 0;
  14. }

  15. Graph::~Graph() {
  16.     for (int i = 0; i < _n; ++i)
  17.     delete [] _A[i];
  18.     delete [] _A;
  19.     delete [] visited;
  20. }

  21. /******************************************************
  22. Checks if two given vertices are connected by an edge
  23. @param u vertex
  24. @param v vertex
  25. @return true if connected false if not connected
  26. ******************************************************/
  27. bool Graph::isConnected(int x, int y) {
  28.     return (_A[x-1][y-1] == 1);
  29. }

  30. /*****************************************************
  31. adds an edge E to the graph G.
  32. @param u vertex
  33. @param v vertex
  34. *****************************************************/
  35. void Graph::addEdge(int x, int y) {
  36.     _A[x-1][y-1] = 1;
  37. }

  38. /*****************************************************
  39. performs Depth First Search
  40. @param x initial vertex
  41. *****************************************************/
  42. void Graph::DFS(int v)
  43. {
  44.     visited[v] = true;
  45.     for(int w = 1; w <= _n; ++w)
  46.     {
  47.             if(isConnected(v, w) && !visited[w]) DFS(w);
  48.     }
  49.     reverse.push(v);
  50. }

  51. void Graph::Toposort(){
  52.         visited = new bool[_n+1];
  53.         for (int i = 1; i <= _n; ++i)
  54.         visited[i] = false;
  55.     for (int v = 1; v <= _n; ++v)
  56.     {
  57.             if(!visited[v]) DFS(v);
  58.     }
  59.     while(!reverse.isEmpty())
  60.             std::cout << reverse.pop() << " ";
  61. }
复制代码
stack.h, stack.cc参照我另一篇文章里面的代码。
Graph_search.cpp
  1. #include <iostream>
  2. #include "graph.h"
  3. using namespace std;

  4. int main(){   
  5.         /** Creates a graph with 12 vertices */
  6.     Graph g1(7);

  7.     /** Adds edges to the graph */
  8.     g1.addEdge(1, 6); g1.addEdge(1, 3);
  9.     g1.addEdge(1, 2); g1.addEdge(4, 7);
  10.     g1.addEdge(4, 6); g1.addEdge(4 ,5);
  11.     g1.addEdge(6, 5); g1.addEdge(7, 5);
  12.     g1.addEdge(7, 1); g1.addEdge(4, 3);
  13.     g1.addEdge(2, 5);

  14.     /** Explores all vertices findable from vertex 1 */
  15.     g1.Toposort();
  16. }
复制代码


小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2014-8-12 08:13:29 | 显示全部楼层
谢谢楼主分享!!!!!!!!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-11-18 09:31:05 | 显示全部楼层
学习
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2014-11-26 20:16:26 | 显示全部楼层

必须得顶下
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-12-10 09:16:22 | 显示全部楼层
学习
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2014-12-17 22:54:32 | 显示全部楼层
正在学习到需要用到这方面知识,谢谢楼主分享
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-13 02:25

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表