最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

判斷給定的圖是不是有向無環(huán)圖實(shí)例代碼

 更新時(shí)間:2013年05月14日 14:43:18   作者:  
判斷給定的圖是不是是有向無環(huán)圖,方法是應(yīng)用拓?fù)渑判?,代碼如下

復(fù)制代碼 代碼如下:

#include<iostream>
#include<list>
#include<stack>
using namespace std;

class Graph {
 int vertexNum;
 list<int> *adjacents;
public:
 Graph(int _vertexNum) {
  vertexNum = _vertexNum;
  adjacents = new list<int>[vertexNum];
 }
 void findIndegree(int *indegree, int n);
 bool topologicalSort();
 void addEdge(int v, int w);
};

void Graph::addEdge(int v, int w) {
 adjacents[v].push_back(w);
}

void Graph::findIndegree(int *indegree, int n) {
 int v;
 list<int>::iterator iter;
 for(v = 0; v < vertexNum; v++) {
  for (iter = adjacents[v].begin(); iter != adjacents[v].end(); iter++)
   indegree[*iter]++;
 }
}

bool Graph::topologicalSort() {
 int ver_count = 0;
 stack<int> m_stack;
 int *indegree = new int[vertexNum];
 memset(indegree, 0, sizeof(int) * vertexNum);
 findIndegree(indegree, vertexNum);
 int v;
 for (v = 0; v < vertexNum; v++)
  if (0 == indegree[v])
   m_stack.push(v);
 while (!m_stack.empty()) {
  v = m_stack.top();
  m_stack.pop();
  cout << v << " ";
  ver_count++;
  for (list<int>::iterator iter = adjacents[v].begin(); iter != adjacents[v].end(); iter++) {
   if (0 == --indegree[*iter])
    m_stack.push(*iter);
  }
 }
 cout << endl;
 if (ver_count < vertexNum)
  return false;
 return true;
}

int main(int argc, char *argv[]) {
 Graph g(6);
 g.addEdge(5, 2);
    g.addEdge(5, 0);
    g.addEdge(4, 0);
    g.addEdge(4, 1);
    g.addEdge(2, 3);
    g.addEdge(3, 1);
 if (g.topologicalSort())
  cout << "it is a topological graph" << endl;
 else
  cout << "it is not a topological graph" << endl;
 cin.get();
 return 0;
}

相關(guān)文章

最新評(píng)論

遂溪县| 治多县| 乐东| 全州县| 普格县| 金昌市| 基隆市| 灵武市| 若尔盖县| 贡山| 盐池县| 资阳市| 池州市| 梨树县| 阿勒泰市| 靖远县| 墨竹工卡县| 瓦房店市| 呼图壁县| 金塔县| 青州市| 武安市| 新源县| 永新县| 南郑县| 开平市| 固原市| 古丈县| 永寿县| 潜江市| 大洼县| 兰考县| 油尖旺区| 海门市| 西和县| 望都县| 灵璧县| 海南省| 和平区| 桦甸市| 珲春市|