From 37da3962d275a2a566799f0fe430239b0cc53a55 Mon Sep 17 00:00:00 2001 From: yiakwy Date: Tue, 29 Nov 2016 03:31:56 +0800 Subject: [PATCH] New method (non recursive) for CourseSchedule --- .../non-recursive/course_schedule.cpp | 149 ++++++++++++++++++ .../cpp/courseSchedule/non-recursive/main.cpp | 30 ++++ 2 files changed, 179 insertions(+) create mode 100644 algorithms/cpp/courseSchedule/non-recursive/course_schedule.cpp create mode 100644 algorithms/cpp/courseSchedule/non-recursive/main.cpp diff --git a/algorithms/cpp/courseSchedule/non-recursive/course_schedule.cpp b/algorithms/cpp/courseSchedule/non-recursive/course_schedule.cpp new file mode 100644 index 0000000..b7decde --- /dev/null +++ b/algorithms/cpp/courseSchedule/non-recursive/course_schedule.cpp @@ -0,0 +1,149 @@ +// +// course_schedule.cpp +// LeeteCodeOJ#207 +// +// Created by Wang Yi on 28/11/16. +// Copyright (c) 2016 Wang Yi. All rights reserved. +// + +#include + +#include +#include +#include +#include + +using std::stack; +using std::queue; +using std::vector; +using std::unordered_set; +using std::pair; + +typedef pair Val; +typedef vector> Graph; + +#define FOR(s, e) \ +for (s = 0; s < e; s++) { +#define END ;} + + typedef struct _node { + int val; + // int depth; + struct _node * parent; + } node; + + class Solution { + public: + bool canFinish(int numCourses, vector>& prerequisites) { + vector outdepth(numCourses, 0); + unordered_set vex; + if ((int) prerequisites.size() == 0) + return true; + vector> graph = to_neighbor_repr(numCourses, prerequisites, outdepth); + + return dsf(numCourses, graph, outdepth); + } + + vector> to_neighbor_repr(int numOfvex, vector& edges, + vector& outdepth) { + // std::cout << "building ... " << std::endl; + vector> graph(numOfvex); + for (auto edge : edges) { + graph[edge.second].insert(edge.first); // second -> first + outdepth[edge.second]++; + // std::cout << edge.first << " <- " << edge.second << std::endl; + } + + return graph; + + } + + + bool dsf(int numOfvex, vector>& graph, vector& outdepth) + { + // preparation + stack s; + vector visited(numOfvex, false); + vector onpath(numOfvex, false); + vector starts; + + int i; + + unordered_set children; + + + FOR(i, numOfvex) + if (outdepth[i] !=0 ) { + starts.push_back(i); + } + END + + if ((int)starts.size() == 0) + return false; // no vertex with indepth of 0 found, a circle found + + for (auto j: starts) { + // std::cout << "start from " << j << std::endl; + // do dsf search, if not visited + // when a circle in a path found, return false. + // else return true + if (visited[j]) + continue; + + node head; + head.val = j; + head.parent = NULL; + // head.depth = 1; + s.push(head); + + // non-recursive search + while (!s.empty()) { + node* curr = &(s.top()); + + if (visited[curr->val]) { + // all children have been read + s.pop(); + onpath[curr->val] = false; + continue; // ignore ... + } + + onpath[curr->val] = visited[curr->val]= true; + children = graph[curr->val]; + + if ((int)children.size() == 0) { + s.pop(); + onpath[curr->val] = false; + // std::cout << "==== reach the end ===="<val << "'s children is: "; + for (auto child: children) { + // std::cout << child << " "; + //if (onpath.find(child) == onpath.end()) + if (onpath[child]) { + + // std::cout << std::endl << "a circle found:" << std::endl; + // std::cout << child << " <- "; + // while (curr->parent != NULL) { + // std::cout << curr->val << " <- "; + // curr = curr->parent; + // } + // std::cout << curr->val << std::endl; + + return false; // a circle found, might print debug here + } + + node nd; + nd.val = child; + // nd.depth = curr->depth+1; + nd.parent = curr; // for debug purpose ... + s.push(nd); + } + // std::cout << std::endl; + } + + } // end of for + return true; + } + + }; \ No newline at end of file diff --git a/algorithms/cpp/courseSchedule/non-recursive/main.cpp b/algorithms/cpp/courseSchedule/non-recursive/main.cpp new file mode 100644 index 0000000..f667c87 --- /dev/null +++ b/algorithms/cpp/courseSchedule/non-recursive/main.cpp @@ -0,0 +1,30 @@ +// +// main.cpp +// LeeteCodeOJ#207 +// +// Created by Wang Yi on 28/11/16. +// Copyright (c) 2016 Wang Yi. All rights reserved. +// + +#include +#include +#include + +#include "course_schedule.cpp" + +using std::make_pair; + +int main(int argc, const char * argv[]) { + // insert code here... + Solution sl; + int numCourses = 4; + vector prerequisites; + prerequisites.push_back(make_pair(0,1)); + prerequisites.push_back(make_pair(3,1)); + prerequisites.push_back(make_pair(1,3)); + prerequisites.push_back(make_pair(3,2)); + + + std::cout << sl.canFinish(numCourses, prerequisites); + return 0; +}