Unpacking a Schedule

Source: Internet
Author: User
Tags data structures in degrees sort

Consider the question: given a number of computer courses, each course has a pre-existing course, only the pre-curriculum can be completed to begin the current course of study; Our goal is to choose a group of courses that must be completed in sequence. The pre-set courses for each course are as follows:

Gopl.io/ch5/toposort
//Prereqs recorded the pre-curriculum for each course
var prereqs = map[string][]string{
    "algorithms": {"Data Structures "},
    " calculus ": {" linear algebra "},
    " compilers ": {
        " Data structures ",
        " formal languages ", "
        Computer organization",
    },
    "Data Structures":       {"Discrete math"},
    "Databases":             {"Data Structures "},
    " Discrete math ":         {" Intro to Programming "},
    " Formal languages ":      {" Discrete math "},
    "Networks":              {"Operating Systems"},
    "Operating systems":     {"Data Structures", "Computer Organization"},
    " Programming languages ": {" Data Structures "," Computer Organization "},
}

A workable solution:
1:intro to programming
2:discrete math
3:data Structures 4:algorithms 5:linear algebra
6:calcul US
7:formal Languages
8:computer organization
9:compilers
10:databases
11:operating systems< C10/>12:networks
13:programming languages

Solution to the idea of reference: http://www.cnblogs.com/shanyou/archive/2006/11/16/562861.html

I wrote a demo program as follows:

#include <iostream> #include <map> #include <vector> #include <string> #include <algorithm

> #include <iterator> using namespace std;
typedef map<string, std::vector<string> > Graphtype;


Graphtype g_coursegraph;  Title Source: http://docs.ruanjiadeng.com/gopl-zh/ch5/ch5-06.html/*var prereqs = map[string][]string{"Algorithms": {"Data Structures "}," calculus ": {" linear algebra "}," compilers ": {" Data Structures "," formal languages "," Computer organization ",}," Data structures ": {" Discrete math "}," Databases ": {"
    Data Structures "}," Discrete math ": {" Intro to Programming "}," formal languages ": {" Discrete math "}, "Networks": {"Operating Systems"}, "operating systems": {"Data Structures", "computer organizatio N "}," programming languages ": {" Data Structures "," Computer Organization "},} */void Initgraph () {g_coursegraph ["Algorithms "].push_back (" Data Structures ");

    g_coursegraph["calculus"].push_back ("linear algebra");
    g_coursegraph["compilers"].push_back ("Data Structures");
    g_coursegraph["compilers"].push_back ("formal languages");


    g_coursegraph["compilers"].push_back ("computer organization");
    g_coursegraph["Data Structures"].push_back ("Discrete math");
    g_coursegraph["Databases"].push_back ("Data Structures");
    g_coursegraph["Discrete Math"].push_back ("Intro to Programming");
    g_coursegraph["Formal Languages"].push_back ("Discrete math");
         
    g_coursegraph["Networks"].push_back ("Operating Systems");
    g_coursegraph["Operating Systems"].push_back ("Data Structures");
    g_coursegraph["Operating Systems"].push_back ("computer organization");
    g_coursegraph["Programming Languages"].push_back ("Data Structures");

g_coursegraph["Programming Languages"].push_back ("computer organization"); } vector<string> Getallcourcefrom (graphtype& input) {std::Vector<std::string> Retvalvec;  for (Graphtype::iterator itor = Input.begin (); Itor! = Input.end ();
    itor++) {retvalvec.push_back (Itor->first);  } for (Graphtype::iterator itor = Input.begin (); Itor! = Input.end ();
        itor++) {std::vector<string> v = itor->second; for (Vector<string>::iterator it = V.begin (); It!=v.end (); it++) {if (Find (Retvalvec.begin (), Retvalvec.en
            D (), *it) = = Retvalvec.end ()) {retvalvec.push_back (*it);
}}} return Retvalvec; }//Test getallcourcefrom void Testgetallcourcefrom () {std::vector<std::string> rst = Getallcourcefrom (g_cour
    Segraph);  
Copy (Rst.begin (), Rst.end (), ostream_iterator<string> (cout, "\ n"));
    }//Get all course values map<string, int> getalldegree (graphtype& input) {map<string, int> degree;
    std::vector<std::string> Coursevec = getallcourcefrom (input); for (vector<string>:: Iterator it = Coursevec.begin (); It!=coursevec.end ();
    it++) {Degree[*it] = input[*it].size ();
} return degree;
   } void Testgetalldegree () {map<string, int> rst = Getalldegree (g_coursegraph); For (map<string, int>::iterator it = Rst.begin (); It! = Rst.end (); it++) {cout << key= << it-
   >first << ", value=" << it->second << Endl; }} string Findzero (std::vector<std::string>& Coursevec, map<string, int>& InputDegree) {for (VEC Tor<string>::iterator it = Coursevec.begin (); It!=coursevec.end ();
       it++) {if (inputdegree[*it] = = 0) {//finds the first entry to 0 and exits the return *it;

}} return "";}
    void Testfindzero () {std::vector<std::string> Coursevec = Getallcourcefrom (g_coursegraph);
    
    map<string, int> degree = Getalldegree (g_coursegraph);
cout << Findzero (Coursevec, degree) <<endl; } void Update(string course, std::vector<std::string>& Coursevec, map<string, int>& Inputdegree, GraphType
    & Inputgraph) {if (Course.empty ()) {return;}
    Coursevec.erase (Remove (Coursevec.begin (), Coursevec.end (), course), coursevec.end ()); Inputdegree[course] =-1;
      Completely exclude this course for (map<string, int>::iterator it = Inputdegree.begin (); It! = Inputdegree.end (); it++) { if (Inputdegree[it->first]! =-1 && Find (Inputgraph[it->first].begin (), Inputgraph[it->first].end ()  , course)! = Inputgraph[it->first].end ()) {inputdegree[it->first]--; In Degrees -1}}} void Testupdate () {std::vector<std::string> Coursevec = Getallcourcefrom (G_coursegr
    APH);
    map<string, int> degree = Getalldegree (g_coursegraph);

   Update ("Computer organization", Coursevec, Degree, g_coursegraph); For (map<string, int>::iterator it = Degree.begin (); It! = Degree.end (); it++) {cout << "before update:";
   cout << "key=" << it->first << ", value=" << it->second << Endl;
   } cout << "after update:";  
   Copy (Coursevec.begin (), Coursevec.end (), ostream_iterator<string> (cout, "\ n")); For (map<string, int>::iterator it = Degree.begin (); It! = Degree.end (); it++) {cout << "key=" &LT;&L T
   It->first << ", value=" << it->second << Endl; }}//Implement a sort of dependent course diagram bool Topo (graphtype& inputgraph, vector<string>& Outputvec) {vector<string> C  Oursevec = Getallcourcefrom (inputgraph); Get all course vector map<string, int> inputdegree = Getalldegree (inputgraph);
   Save all course in the//sort (inputdegree)//Sort, sort by string, the purpose of sorting is likely to have multiple degrees of the same, so always follow the order to take string course;  do{course = Findzero (Coursevec, Inputdegree);
            Find a course in degrees 0 if (!course.empty ()) {outputvec.push_back (course); UPdate (course, Coursevec, Inputdegree, inputgraph); }}while (Course! = "");
   That is, if you still find a point with a degree of 0 to continue, otherwise you exit if (Outputvec.size () < Coursevec.size ()) {return false;//the graph has a loop}
   else {return true;

   }} int main () {initgraph ();
   Vector<string> Outputvec;
       if (Topo (G_coursegraph, Outputvec)) {cout << "after topo sort:" <<endl; 
   Copy (Outputvec.begin (), Outputvec.end (), ostream_iterator<string> (cout, "\ n")); 
} return 0;
 }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.