Ejemplo n.º 1
0
Archivo: SET.cs Proyecto: zzhi/Algs4Net
        public static void MainTest(string[] args)
        {
            SET <string> set = new SET <string>();

            // insert some keys
            set.Add("www.cs.princeton.edu");
            set.Add("www.cs.princeton.edu"); // overwrite old value
            set.Add("www.princeton.edu");
            set.Add("www.math.princeton.edu");
            set.Add("www.yale.edu");
            set.Add("www.amazon.com");
            set.Add("www.simpsons.com");
            set.Add("www.stanford.edu");
            set.Add("www.google.com");
            set.Add("www.ibm.com");
            set.Add("www.apple.com");
            set.Add("www.slashdot.com");
            set.Add("www.whitehouse.gov");
            set.Add("www.espn.com");
            set.Add("www.snopes.com");
            set.Add("www.movies.com");
            set.Add("www.cnn.com");
            set.Add("www.iitb.ac.in");


            Console.WriteLine(set.Contains("www.cs.princeton.edu"));
            Console.WriteLine(!set.Contains("www.harvardsucks.com"));
            Console.WriteLine(set.Contains("www.simpsons.com"));
            Console.WriteLine();

            Console.WriteLine("ceiling(www.simpsonr.com) = " + set.Ceiling("www.simpsonr.com"));
            Console.WriteLine("ceiling(www.simpsons.com) = " + set.Ceiling("www.simpsons.com"));
            Console.WriteLine("ceiling(www.simpsont.com) = " + set.Ceiling("www.simpsont.com"));
            Console.WriteLine("floor(www.simpsonr.com)   = " + set.Floor("www.simpsonr.com"));
            Console.WriteLine("floor(www.simpsons.com)   = " + set.Floor("www.simpsons.com"));
            Console.WriteLine("floor(www.simpsont.com)   = " + set.Floor("www.simpsont.com"));
            Console.WriteLine();


            // print out all keys in this set in lexicographic order
            foreach (string s in set)
            {
                Console.WriteLine(s);
            }

            Console.WriteLine();
            SET <string> set2 = new SET <string>(set);

            Console.WriteLine("Set equality: {0}", set.Equals(set2));
            Console.WriteLine("ToString() equality: {0}", set2.ToString().Equals(set.ToString()));
        }
Ejemplo n.º 2
0
        /// <summary>Returns a random simple digraph containing <c>V</c> vertices and <c>E</c> edges.</summary>
        /// <param name="V">the number of vertices</param>
        /// <param name="E">the number of vertices</param>
        /// <returns>a random simple digraph on <c>V</c> vertices, containing a total
        ///    of <c>E</c> edges</returns>
        /// <exception cref="ArgumentException">if no such simple digraph exists</exception>
        ///
        public static Digraph Simple(int V, int E)
        {
            if (E > (long)V * (V - 1))
            {
                throw new ArgumentException("Too many edges");
            }
            if (E < 0)
            {
                throw new ArgumentException("Too few edges");
            }
            Digraph    G   = new Digraph(V);
            SET <Edge> set = new SET <Edge>();

            while (G.E < E)
            {
                int  v = StdRandom.Uniform(V);
                int  w = StdRandom.Uniform(V);
                Edge e = new Edge(v, w);
                if ((v != w) && !set.Contains(e))
                {
                    set.Add(e);
                    G.AddEdge(v, w);
                }
            }
            return(G);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Returns a random simple DAG containing <c>V</c> vertices and <c>E</c> edges.
        /// Note: it is not uniformly selected at random among all such DAGs.</summary>
        /// <param name="V">the number of vertices</param>
        /// <param name="E">the number of vertices</param>
        /// <returns>a random simple DAG on <c>V</c> vertices, containing a total
        ///    of <c>E</c> edges</returns>
        /// <exception cref="ArgumentException">if no such simple DAG exists</exception>
        ///
        public static Digraph Dag(int V, int E)
        {
            if (E > (long)V * (V - 1) / 2)
            {
                throw new ArgumentException("Too many edges");
            }
            if (E < 0)
            {
                throw new ArgumentException("Too few edges");
            }
            Digraph    G   = new Digraph(V);
            SET <Edge> set = new SET <Edge>();

            int[] vertices = new int[V];
            for (int i = 0; i < V; i++)
            {
                vertices[i] = i;
            }
            StdRandom.Shuffle(vertices);
            while (G.E < E)
            {
                int  v = StdRandom.Uniform(V);
                int  w = StdRandom.Uniform(V);
                Edge e = new Edge(v, w);
                if ((v < w) && !set.Contains(e))
                {
                    set.Add(e);
                    G.AddEdge(vertices[v], vertices[w]);
                }
            }
            return(G);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Returns a random simple bipartite graph on <c>V1</c> and <c>V2</c> vertices
        /// with <c>E</c> edges.</summary>
        /// <param name="V1">the number of vertices in one partition</param>
        /// <param name="V2">the number of vertices in the other partition</param>
        /// <param name="E">the number of edges</param>
        /// <returns>a random simple bipartite graph on <c>V1</c> and <c>V2</c> vertices,
        ///   containing a total of <c>E</c> edges</returns>
        /// <exception cref="ArgumentException">if no such simple bipartite graph exists</exception>
        ///
        public static Graph Bipartite(int V1, int V2, int E)
        {
            if (E > (long)V1 * V2)
            {
                throw new ArgumentException("Too many edges for bipartite graphs");
            }
            if (E < 0)
            {
                throw new ArgumentException("Too few edges for bipartite graphs");
            }
            Graph G = new Graph(V1 + V2);

            int[] vertices = new int[V1 + V2];
            for (int i = 0; i < V1 + V2; i++)
            {
                vertices[i] = i;
            }
            StdRandom.Shuffle(vertices);

            SET <Edge> set = new SET <Edge>();

            while (G.E < E)
            {
                int  i = StdRandom.Uniform(V1);
                int  j = V1 + StdRandom.Uniform(V2);
                Edge e = new Edge(vertices[i], vertices[j]);
                if (!set.Contains(e))
                {
                    set.Add(e);
                    G.AddEdge(vertices[i], vertices[j]);
                }
            }
            return(G);
        }
Ejemplo n.º 5
0
Archivo: SET.cs Proyecto: zzhi/Algs4Net
        /// <summary>
        /// Returns the intersection of this set and that set.</summary>
        /// <param name="that"> that the other set</param>
        /// <returns>the intersection of this set and that set</returns>
        /// <exception cref="ArgumentNullException">if <c>that</c> is <c>null</c></exception>
        ///
        public SET <Key> Intersects(SET <Key> that)
        {
            if (that == null)
            {
                throw new ArgumentNullException("called intersects() with a null argument");
            }
            SET <Key> c = new SET <Key>();

            if (this.Count < that.Count)
            {
                foreach (Key x in this)
                {
                    if (that.Contains(x))
                    {
                        c.Add(x);
                    }
                }
            }
            else
            {
                foreach (Key x in that)
                {
                    if (this.Contains(x))
                    {
                        c.Add(x);
                    }
                }
            }
            return(c);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Returns a random rooted-out DAG on <c>V</c> vertices and <c>E</c> edges.
        /// A rooted out-tree is a DAG in which every vertex is reachable from a
        /// single vertex.
        /// The DAG returned is not chosen uniformly at random among all such DAGs.</summary>
        /// <param name="V">the number of vertices</param>
        /// <param name="E">the number of edges</param>
        /// <returns>a random rooted-out DAG on <c>V</c> vertices and <c>E</c> edges</returns>
        ///
        public static Digraph RootedOutDAG(int V, int E)
        {
            if (E > (long)V * (V - 1) / 2)
            {
                throw new ArgumentException("Too many edges");
            }
            if (E < V - 1)
            {
                throw new ArgumentException("Too few edges");
            }
            Digraph    G   = new Digraph(V);
            SET <Edge> set = new SET <Edge>();

            // fix a topological order
            int[] vertices = new int[V];
            for (int i = 0; i < V; i++)
            {
                vertices[i] = i;
            }
            StdRandom.Shuffle(vertices);

            // one edge pointing from each vertex, other than the root = vertices[V-1]
            for (int v = 0; v < V - 1; v++)
            {
                int  w = StdRandom.Uniform(v + 1, V);
                Edge e = new Edge(w, v);
                set.Add(e);
                G.AddEdge(vertices[w], vertices[v]);
            }

            while (G.E < E)
            {
                int  v = StdRandom.Uniform(V);
                int  w = StdRandom.Uniform(V);
                Edge e = new Edge(w, v);
                if ((v < w) && !set.Contains(e))
                {
                    set.Add(e);
                    G.AddEdge(vertices[w], vertices[v]);
                }
            }
            return(G);
        }
Ejemplo n.º 7
0
        public static void MainTest(string[] args)
        {
            TextInput StdIn = new TextInput();

            SET <string> set = new SET <string>();

            // read in strings and add to set
            while (!StdIn.IsEmpty)
            {
                string key = StdIn.ReadString();
                if (key.Equals(""))
                {
                    continue;
                }
                if (!set.Contains(key))
                {
                    set.Add(key);
                    Console.WriteLine(key);
                }
            }
        }
Ejemplo n.º 8
0
        public static void MainTest(string[] args)
        {
            TextInput    StdIn = new TextInput();
            SET <string> set   = new SET <string>();

            // read in strings and add to set
            TextInput input = new TextInput(args[0]);

            while (!input.IsEmpty)
            {
                string word = input.ReadString();
                set.Add(word);
            }

            // read in string from standard input, printing out all exceptions
            while (!StdIn.IsEmpty)
            {
                string word = StdIn.ReadString();
                if (!set.Contains(word))
                {
                    Console.WriteLine(word);
                }
            }
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Returns a random simple digraph on <c>V</c> vertices, <c>E</c>
        /// edges and (at least) <c>c</c> strong components. The vertices are randomly
        /// assigned integer labels between <c>0</c> and <c>c-1</c> (corresponding to
        /// strong components). Then, a strong component is creates among the vertices
        /// with the same label. Next, random edges (either between two vertices with
        /// the same labels or from a vetex with a smaller label to a vertex with a
        /// larger label). The number of components will be equal to the number of
        /// distinct labels that are assigned to vertices.</summary>
        /// <param name="V">the number of vertices</param>
        /// <param name="E">the number of edges</param>
        /// <param name="c">the (maximum) number of strong components</param>
        /// <returns>a random simple digraph on <c>V</c> vertices and
        /// <c>E</c> edges, with (at most) <c>c</c> strong components</returns>
        /// <exception cref="ArgumentException">if <c>c</c> is larger than <c>V</c></exception>
        ///
        public static Digraph Strong(int V, int E, int c)
        {
            if (c >= V || c <= 0)
            {
                throw new ArgumentException("Number of components must be between 1 and V");
            }
            if (E <= 2 * (V - c))
            {
                throw new ArgumentException("Number of edges must be at least 2(V-c)");
            }
            if (E > (long)V * (V - 1) / 2)
            {
                throw new ArgumentException("Too many edges");
            }

            // the digraph
            Digraph G = new Digraph(V);

            // edges added to G (to avoid duplicate edges)
            SET <Edge> set = new SET <Edge>();

            int[] label = new int[V];
            for (int v = 0; v < V; v++)
            {
                label[v] = StdRandom.Uniform(c);
            }

            // make all vertices with label c a strong component by
            // combining a rooted in-tree and a rooted out-tree
            for (int i = 0; i < c; i++)
            {
                // how many vertices in component c
                int count = 0;
                for (int v = 0; v < G.V; v++)
                {
                    if (label[v] == i)
                    {
                        count++;
                    }
                }

                // if (count == 0) System.err.println("less than desired number of strong components");

                int[] vertices = new int[count];
                int   j        = 0;
                for (int v = 0; v < V; v++)
                {
                    if (label[v] == i)
                    {
                        vertices[j++] = v;
                    }
                }
                StdRandom.Shuffle(vertices);

                // rooted-in tree with root = vertices[count-1]
                for (int v = 0; v < count - 1; v++)
                {
                    int  w = StdRandom.Uniform(v + 1, count);
                    Edge e = new Edge(w, v);
                    set.Add(e);
                    G.AddEdge(vertices[w], vertices[v]);
                }

                // rooted-out tree with root = vertices[count-1]
                for (int v = 0; v < count - 1; v++)
                {
                    int  w = StdRandom.Uniform(v + 1, count);
                    Edge e = new Edge(v, w);
                    set.Add(e);
                    G.AddEdge(vertices[v], vertices[w]);
                }
            }

            while (G.E < E)
            {
                int  v = StdRandom.Uniform(V);
                int  w = StdRandom.Uniform(V);
                Edge e = new Edge(v, w);
                if (!set.Contains(e) && v != w && label[v] <= label[w])
                {
                    set.Add(e);
                    G.AddEdge(v, w);
                }
            }

            return(G);
        }