Ejemplo n.º 1
0
        public static Queue <MyInfo> PerformTopoSort(List <MyInfo> nodes)
        {
            var sorter = new TopologicalSort <MyInfo>();

            Queue <MyInfo> outQueue;

            foreach (var myInfo in nodes.Where(n => n.Checked))
            {
                if (myInfo.Dependencies.Any())
                {
                    foreach (var dependency in myInfo.Dependencies)
                    {
                        if (myInfo.Equals(dependency))
                        {
                            throw new Exception("Cyclic Dependency detected from '" + dependency + "' to '" + myInfo + "'");
                        }

                        sorter.Edge(myInfo, dependency);
                    }
                }
                else
                {
                    sorter.Edge(myInfo);
                }
            }

            sorter.Sort(out outQueue);

            return(outQueue);
        }
        public static void Main()
        {
            var ts = new TopologicalSort<char>();
            Queue<char> outQueue;
            PriorityQueue<char> res= new PriorityQueue<char>();

            bool returnValue;

            int n = int.Parse(Console.ReadLine());

            for (int i = 0; i < n; i++)
            {
                char[] l = Console.ReadLine().ToCharArray();
                foreach (var p in l)
                {
                    res.Enqueue(p);
                }

                for (int j = 0; j < l.Length-1; j++)
                {
                    ts.Edge(l[j+1], l[j]);
                }

            }

            returnValue = ts.Sort(out outQueue);
            StringBuilder result = new StringBuilder();

            if (outQueue.Count == 0)
            {
               StringBuilder m = new StringBuilder();
                while (res.Count != 0)
                {
                    m.Append(res.Dequeue());
                }

                Console.WriteLine(m);
                return;
            }

            while (outQueue.Count != 0)
                result.Append(outQueue.Dequeue());
            Console.WriteLine(result);
        }
Ejemplo n.º 3
0
        private List <Library> GetSortedLibraries(Library rootLibrary)
        {
            var traversed       = new List <Library>();
            var topologicalSort = new TopologicalSort <Library>();

            var toTraverse = new Queue <Library>();

            toTraverse.Enqueue(rootLibrary);
            while (toTraverse.Count > 0)
            {
                Library predecessor = toTraverse.Dequeue();
                if (traversed.Any(l => l.Equals(predecessor)) == false)
                {
                    traversed.Add(predecessor);
                }
                topologicalSort.Node(predecessor);
                foreach (Library successor in predecessor.Dependencies)
                {
                    Library realSuccessor = traversed.Find(t => t.Equals(successor));

                    if (realSuccessor == null)
                    {
                        if (toTraverse.Any(l => l.Equals(successor)) == false)
                        {
                            toTraverse.Enqueue(successor);
                        }

                        realSuccessor = successor;

                        if (traversed.Any(l => l.Equals(realSuccessor)) == false)
                        {
                            traversed.Add(realSuccessor);
                        }
                    }

                    topologicalSort.Edge(predecessor, realSuccessor);
                }
            }

            Queue <Library> sorted       = topologicalSort.Sort();
            List <Library>  allLibraries = sorted.ToList();

            return(allLibraries);
        }