Пример #1
0
        static public void AddPlanDFS(ref List <List <string> > courses, ref List <List <string> > plan)
        // Plan courses with DFS Algorithm
        {
            CourseNode[] Nodes = new CourseNode[courses.Count];
            // Transform list of courses to DAG (Directed Acyclic Graph)
            Transform(courses, ref Nodes);

            List <string> cNodes = new List <string>();

            //buat nyari node yang pertama (ga punya ortu)
            for (int i = 0; i < Nodes.Length; i++)
            {
                cNodes.Add(Nodes[i].name);
            }
            for (int i = 0; i < Nodes.Length; i++)
            {
                foreach (string nama in Nodes[i].children)
                {
                    int j = 0;
                    int y = cNodes.Count;
                    for (int k = 0; k < y; k++)
                    {
                        if (cNodes[j] == nama)
                        {
                            cNodes.RemoveAt(j);
                        }
                        else
                        {
                            j = j + 1;
                        }
                    }
                }
            }

            //rekursi dfs
            int time = 1;

            foreach (string node in cNodes)
            {
                DFS(ref Nodes, node, ref time);
            }
            //ngurutin
            selectSort(ref Nodes);
            for (int i = 0; i < Nodes.Length; i++)
            {
                Console.Write("{0}\t", Nodes[i].name);
                Console.Write("{0}\t", Nodes[i].startTime);
                Console.WriteLine("{0}", Nodes[i].stopTime);
            }
            for (int i = 0; i < Nodes.Length; i++)
            {
                List <string> TermPlan = new List <string>();
                TermPlan.Add(Nodes[i].name);
                plan.Add(TermPlan);
            }

            NodeForDFS = Nodes;
        }
Пример #2
0
        public static void Execute(string method, string filename)
        {
            try
            {
                Parser.ParseFile(ref CoursesList, WindowsFormsApp1.ExternalFile.Reader(filename));
            }
            catch (IOException)
            {
                Console.WriteLine("File not found!");
                Console.ReadLine();
                return;
            }
            catch (Exception)
            {
                MessageBox.Show("Please Select an item first");
            }


            try
            {
                // Transform list of courses to DAG (Directed Acyclic Graph)
                CourseNode[] Nodes = new CourseNode[CoursesList.Count];
                Planner.Transform(CoursesList, ref Nodes);
                Planner.final_graph = WindowsFormsApp1.ViewerGraph.CreateGraph(Nodes);


                if (method == "DFS")
                {
                    Planner.list_graph = new List <Graph>();
                    Planner.AddPlanDFS(ref CoursesList, ref CoursePlan);
                }
                else   // method == "BFS"
                {
                    Planner.AddPlanBFS(ref CoursesList, ref CoursePlan);
                }
            }
            catch (NotPlanable e)
            {
                Console.WriteLine("Failed to make a plan.");
                Console.WriteLine("Reason: {0}.", e.Message);
                Console.ReadLine();
            }
        }
Пример #3
0
        static void Main(string[] args)
        {
            // KAMUS
            List <List <string> > CoursesList = new List <List <string> >();
            List <List <string> > CoursePlan  = new List <List <string> >();

            // ALGORITMA
            try
            {
                FileReader.ReadFile(ref CoursesList);
            }
            catch (IOException)
            {
                Console.WriteLine("File not found!");
                Console.ReadLine();
                return;
            }

            try
            {
                PrintAllCourses(CoursesList);
                Console.WriteLine("");
                CourseNode[] Nodes = new CourseNode[CoursesList.Count];
                // Transform list of courses to DAG (Directed Acyclic Graph)
                Planner.Transform(CoursesList, ref Nodes);
                ViewerGraph.CreateGraph(ref Nodes);

                /*
                 * Planner.AddPlanDFS(ref CoursesList, ref CoursePlan);
                 * PrintPlan(CoursePlan);
                 * ViewerGraph.CreateGraph(ref CoursePlan);
                 */
                Console.ReadLine();
            }
            catch (NotPlanable e)
            {
                Console.WriteLine("Failed to make a plan.");
                Console.WriteLine("Reason: {0}.", e.Message);
                Console.ReadLine();
            }
        }