Example #1
0
        private void dfs_btn(object sender, RoutedEventArgs e)
        {
            Parser p = new Parser();
            List <List <string> > graphdetails = p.Parse(this.FileField.Text);

            if (graphdetails != null)
            {
                graphdetails = graphdetails.OrderBy(x => x[0]).ToList();
                DFSSorter DFSResult = new DFSSorter();
                DFSSorter.init_topoSort(graphdetails);
                DFSSorter.sort_semester();
                string output = "";
                int    maxSem = DFSSorter.Semester.Max <int>();
                Debug.WriteLine($"maxSem = {maxSem}");
                for (int k = 0; k < DFSSorter.Semester.Count(); k++)
                {
                    Debug.WriteLine($"{graphdetails[k][0]} start {DFSSorter.Timestamp_Start[k]} stop {DFSSorter.Timestamp[k]} semester {DFSSorter.Semester[k]}");
                }
                foreach (List <string> L in graphdetails)
                {
                    output += $"Mata Kuliah: {L[0]}\n";
                    for (int i = 1; i < L.Count; i++)
                    {
                        output += $"\tPrequisite: {L[i]}\n";
                    }
                }
                GraphInfo.Text = output;
                string outputRes = "";
                for (int j = 1; j <= maxSem; j++)
                {
                    outputRes += $"Semester {j}: ";
                    while (DFSSorter.Semester.Contains(j))
                    {
                        int    idxCurrentMK = DFSSorter.Semester.IndexOf(j);
                        string currentMK    = graphdetails[idxCurrentMK][0];
                        outputRes += $"{currentMK}";
                        DFSSorter.Semester[idxCurrentMK] = 0;
                        if (DFSSorter.Semester.Contains(j))
                        {
                            outputRes += " ,";
                        }
                    }
                    outputRes += "\n";
                }
                GraphResult.Text = outputRes;
                GraphAnimation.viewDFS(DFSResult, graphdetails);
            }
        }
Example #2
0
        public static void viewDFS(DFSSorter DFSRes, List <List <String> > graphdetail)
        {
            // create a form
            System.Windows.Forms.Form form = new System.Windows.Forms.Form();
            // create viewer object
            Microsoft.Msagl.GraphViewerGdi.GViewer viewer = new Microsoft.Msagl.GraphViewerGdi.GViewer();
            // create a graph object
            Microsoft.Msagl.Drawing.Graph graph = new Microsoft.Msagl.Drawing.Graph("graph");
            // bind graph to viewer
            viewer.Graph = graph;
            // set form properties
            form.Text   = "DFS";
            form.Height = 600;
            form.Width  = 600;
            form.SuspendLayout();
            viewer.Dock = System.Windows.Forms.DockStyle.Fill;
            form.Controls.Add(viewer);
            form.ResumeLayout();
            //show the form
            form.Show();
            int         currentTimestamp = 1;
            int         maxTimestamp     = graphdetail.Count() * 2;
            int         size             = graphdetail.Count;
            List <bool> visited          = new List <bool>(size);

            for (int i = 0; i < size; i++)
            {
                visited.Add(false);
            }
            AnimateGraphDFS(2000, currentTimestamp, maxTimestamp, (s, e) =>
            {
                while (!DFSSorter.Timestamp_Start.Contains(currentTimestamp))
                {
                    currentTimestamp++;
                }
                if (DFSSorter.Timestamp_Start.Contains(currentTimestamp))
                {
                    viewer.Graph     = null;
                    int nodeIdx      = DFSSorter.Timestamp_Start.IndexOf(currentTimestamp);
                    visited[nodeIdx] = true;
                    graph.AddNode(graphdetail[nodeIdx][0]);
                    for (int i = 0; i < graphdetail.Count; i++)
                    {
                        if (graphdetail[i].Contains(graphdetail[nodeIdx][0]))
                        {
                            if (i == nodeIdx)
                            {
                                for (int j = 1; j < graphdetail[i].Count; j++)
                                {
                                    int indexMK = graphdetail.FindIndex(L => L[0] == graphdetail[i][j]);
                                    if (visited[indexMK])
                                    {
                                        graph.AddEdge(graphdetail[i][j], graphdetail[i][0]);
                                    }
                                }
                            }
                            else
                            {
                                if (visited[i])
                                {
                                    graph.AddEdge(graphdetail[nodeIdx][0], graphdetail[i][0]);
                                }
                            }
                        }
                    }
                    currentTimestamp++;
                    viewer.Graph = graph;
                }
            });
        }