static void Main(string[] args)
        {
            //ActivitySelectionProblem activitySelectionProblem = new ActivitySelectionProblem();
            //KruskalsMSTalgorithm kruskalsMSTalgorithm = new KruskalsMSTalgorithm();

            #region Disjoint Union Set
            int n = 5;
            DisjointUnionSets disjointUnionSets = new DisjointUnionSets(n);

            // make relations
            disjointUnionSets.Union(0, 2);                              // 0 is a friend of 2
            disjointUnionSets.Union(4, 2);                              // 4 is a friend of 2
            disjointUnionSets.Union(3, 1);                              // 3 is a friend of 1

            if (disjointUnionSets.Find(4) == disjointUnionSets.Find(0)) // Check if 4 is a friend of 0
            {
                Console.WriteLine("Yes");
            }
            else
            {
                Console.WriteLine("No");
            }

            if (disjointUnionSets.Find(1) == disjointUnionSets.Find(0)) // Check if 1 is a friend of 0
            {
                Console.WriteLine("Yes");
            }
            else
            {
                Console.WriteLine("No");
            }
            #endregion

            #region Program to detect cycle in a graph
            GraphDSOptimized.GraphDS graph = new GraphDSOptimized.GraphDS(3, 3);

            // Add edge 0-1
            graph.Edges[0].Src  = 0;
            graph.Edges[0].Dest = 1;

            // Add edge 1-2
            graph.Edges[1].Src  = 1;
            graph.Edges[1].Dest = 2;

            // Add edge 0-2
            graph.Edges[2].Src  = 0;
            graph.Edges[2].Dest = 2;

            Console.WriteLine(graph.HasCycle() ? "Graph contains cycle." : "Graph doesn't contain cycle.");
            #endregion

            #region Kruskal MST Optimized
            KrushkalMSTAlgorithmOptimized krushkalMSTAlgorithmOptimized = new KrushkalMSTAlgorithmOptimized();
            var resultMST = krushkalMSTAlgorithmOptimized.KruskalMST();
            krushkalMSTAlgorithmOptimized.PrintMSTResult(resultMST);
            #endregion
        }
Exemplo n.º 2
0
        private static void TestDisjointSet()
        {
            var n = 5;

            var dus = new DisjointUnionSets(5);

            dus.Union(0, 2);
            dus.Union(4, 2);
            dus.Union(3, 1);

            Console.WriteLine(dus.Find(4) == dus.Find(0));
            Console.WriteLine(dus.Find(1) == dus.Find(0));
        }
Exemplo n.º 3
0
    public IList <IList <string> > AccountsMerge(IList <IList <string> > accounts)
    {
        var unionSet = new DisjointUnionSets(10001);

        var emailToName = new Dictionary <string, string>();
        var emailToId   = new Dictionary <string, int>();

        var id = 0;

        foreach (var account in accounts)
        {
            var name = account[0];

            for (var i = 1; i < account.Count; i++)
            {
                var email = account[i];

                emailToName[email] = name;

                if (!emailToId.ContainsKey(email))
                {
                    emailToId[email] = id++;
                }

                unionSet.Union(emailToId[account[1]], emailToId[email]);
            }
        }

        var result = new Dictionary <int, List <string> >();

        foreach (var email in emailToName.Keys)
        {
            var index = unionSet.Find(emailToId[email]);
            if (!result.ContainsKey(index))
            {
                result[index] = new List <string>();
            }

            result[index].Add(email);
        }

        foreach (var emails in result.Values)
        {
            emails.Sort(string.CompareOrdinal);
            emails.Insert(0, emailToName[emails[0]]);
        }

        return(new List <IList <string> >(result.Values));
    }