Beispiel #1
0
        public bool CanFinish(int numCourses, int[][] prerequisites)
        {
            List <Vertacy> vertacies = new List <Vertacy>();

            for (int i = 0; i < numCourses; i++)
            {
                Vertacy vertacy = new Vertacy()
                {
                    ID = i
                };

                vertacies.Add(vertacy);
            }

            for (int n = 0; n < prerequisites.Length; n++)
            {
                vertacies[prerequisites[n][0]].Edges.Add(prerequisites[n][1]);
            }

            for (int i = 0; i < numCourses; i++)
            {
                if (DepthFirstSearchCanFinish(vertacies, i) == false)
                {
                    return(false);
                }
            }

            return(true);
        }
Beispiel #2
0
        public int[] FindOrder(int numCourses, int[][] prerequisites)
        {
            HashSet <int>  res       = new HashSet <int>();
            List <Vertacy> vertacies = new List <Vertacy>();

            for (int i = 0; i < numCourses; i++)
            {
                Vertacy vertacy = new Vertacy()
                {
                    ID = i
                };

                vertacies.Add(vertacy);
            }

            for (int n = 0; n < prerequisites.Length; n++)
            {
                vertacies[prerequisites[n][0]].Edges.Add(prerequisites[n][1]);
            }

            int index = 0;

            while (index < vertacies.Count)
            {
                Vertacy vertacy = vertacies[index];
                if (vertacy.Edges.Count == 0)
                {
                    int id = vertacy.ID;
                    res.Add(id);
                    vertacies.Remove(vertacy);
                    index = 0;
                    foreach (Vertacy v in vertacies)
                    {
                        v.Edges.Remove(id);
                    }
                }
                else
                {
                    index++;
                }
            }

            if (vertacies.Count > 1)
            {
                res.Clear();
            }

            return(res.ToArray());
        }
Beispiel #3
0
 public void TopologicalOrder(List <Vertacy> Vertacies, List <int> Result)
 {
     while (Vertacies.Count > 0)
     {
         Vertacy vertacy = Vertacies[0];
         if (vertacy.Edges.Count == 0)
         {
             int id = vertacy.ID;
             Result.Add(id);
             Vertacies.Remove(vertacy);
             foreach (Vertacy v in Vertacies)
             {
                 v.Edges.Remove(id);
             }
         }
     }
 }