C++ 课程安排 算法设计

已知有n个课程,标记从0至n-1,课程之间是有依赖关系的,例如希望完成A课程,可能需要先完成B课程。已知n个课程的依赖关系,求是否可以将n个课程全部完成。

方法一,判断有没有环,有环则不能全部完成。

#include <vector>
struct GraphNode
{
 int label;
 std::vector<GraphNode*> neighbors;
 GraphNode(int x) : label(x) {};
};
class Solution
{
 public:
  Solution() {}
  ~Solution() {}
  bool canFinish(int numCourses, std::vector<std::pair<int,int>> &prerequisites)
  {
   std::vector<GraphNode*> graph;
   std::vector<int> visit;
   for (int i = 0; i < numCourses; i++)
   {
    graph.push_back(new GraphNode(i));
    visit.push_back(-1);
   }
   for (int i = 0; i < prerequisites.size(); i++)
   {
    GraphNode* begin = graph[prerequisites[i].second];
    GraphNode* end = graph[prerequisites[i].first];
    begin->neighbors.push_back(end);
   }
   for (int i = 0; i < graph.size(); i++)
   {
    if (visit[i]==-1 && !DFS_graph(graph[i],visit))
    {
     return false;
    }
   }
   for (int i = 0; i < numCourses; i++)
   {
    delete(graph[i]);
   }
   return true;
  }
  bool DFS_graph(GraphNode* node, std::vector<int>& visit)
  {
   visit[node->label] = 0;
   for (int i = 0; i < node->neighbors.size(); i++)
   {
    if (visit[node->neighbors[i]->label] == -1)
    {
     if (DFS_graph(node->neighbors[i], visit) == 0)
     {
      return false;
     }
    }
    else if (visit[node->neighbors[i]->label] == 0)
    {
     return false;
    }
   }
    visit[node->label] = 1;
    return true;
  }
};
int main()
{
 std::vector<std::pair<int, int>> prerequisites;
 prerequisites.push_back(std::make_pair(1,0));
 prerequisites.push_back(std::make_pair(2,0));
 prerequisites.push_back(std::make_pair(3, 1));
 prerequisites.push_back(std::make_pair(3, 2));
 Solution solve;
 printf("%d\n",solve.canFinish(4,prerequisites));
 return 0;
}

运行结果:
1

方法二,用入度的方法,入度都能变为0,则表示能全部完成课程。

#include <vector>
#include <queue>
struct GraphNode
{
 int label;
 std::vector<GraphNode*> neighbors;
 GraphNode(int x) : label(x) {};
};
class Solution
{
public:
 Solution() {}
 ~Solution() {}
 bool canFinish(int numCourses, std::vector<std::pair<int,int>> &prerequisites)
 {
  std::vector<int> degree;
  std::queue<GraphNode*> Q;
  std::vector<GraphNode*> graph;
  for (int i = 0; i < numCourses; i++)
  {
   graph.push_back(new GraphNode(i));
   degree.push_back(0);
  }
  for (int i = 0; i < prerequisites.size(); i++)
  {
   GraphNode* begin = graph[prerequisites[i].second];
   GraphNode* end = graph[prerequisites[i].first];
   begin->neighbors.push_back(end);
   degree[prerequisites[i].first]++;
  }
  for (int i = 0; i < graph.size(); i++)
  {
   if (degree[i]==0)
   {
    Q.push(graph[i]);
   }
  }
  while (!Q.empty())
  {
   GraphNode* node = Q.front();
   Q.pop();
   for (int i = 0; i < node->neighbors.size(); i++)
   {
    degree[node->neighbors[i]->label]--;
    if (degree[node->neighbors[i]->label]==0)
    {
     Q.push(node->neighbors[i]);
    }
   }
  }
  for (int i = 0; i < graph.size(); i++)
  {
   delete(graph[i]);
  }
  for (int i = 0; i < degree.size(); i++)
  {
   if (degree[i])
   {
    return false;
   }
  }
  return true;
 }
};
int main()
{
 std::vector<std::pair<int, int>> prerequisites;
 prerequisites.push_back(std::make_pair(1, 0));
 prerequisites.push_back(std::make_pair(2, 0));
 prerequisites.push_back(std::make_pair(3, 1));
 prerequisites.push_back(std::make_pair(3, 2));
 Solution solve;
 printf("%d\n", solve.canFinish(4, prerequisites));
 return 0;
}

运行结果:
1

发布了79 篇原创文章 · 获赞 62 · 访问量 2212

猜你喜欢

转载自blog.csdn.net/weixin_44208324/article/details/104934210
今日推荐