示例#1
0
 /// <summary>
 /// secondsary constructor for edge built with provided nodes
 /// </summary>
 /// <param name="start">start node for edge</param>
 /// <param name="end">end node for edge</param>
 public CSC382Graph_Edge(CSC382Graph_Vertex <T> start, CSC382Graph_Vertex <T> end)
 {
     starting_vertex = start;
     ending_vertex   = end;
 }
示例#2
0
        static public void Testing_NodeBased()
        {
            // Test CSC382Graph_NodeBased Constructor(s)
            CSC382Graph_Vertex <string>    baseNode    = new CSC382Graph_Vertex <string>("The answer is: ");
            CSC382Graph_NodeBased <string> stringGraph = new CSC382Graph_NodeBased <string> (baseNode);

            // Test creating lots of nodes
            string[] lines = File.ReadAllLines("num100.dat");


            Console.WriteLine("Adding vertex nodes to the graph");
            foreach (var line in lines)
            {
                bool exists = false;
                CSC382Graph_Vertex <string> temp = new CSC382Graph_Vertex <string>(line);
                foreach (var item in stringGraph.Graph)
                {
                    //make sure there aren't duplicates in the graph
                    if (item.GetData().Equals(temp.GetData()))
                    {
                        exists = true;
                        break;
                    }
                }

                if (!exists)
                {
                    stringGraph.Insert(temp);
                }
            }

            // Test functions of the CSC382Graph_NodeBased class and affiliated classes

            //test size: should be 101 -- 1 for the baseNode, plust one for the 100 lines that are in the file we read in
            Console.WriteLine("The length of the string Graph is: " + stringGraph.Size());
            Console.WriteLine();



            CSC382Graph_Vertex <string> fortyTwo = new CSC382Graph_Vertex <string>("42");
            bool found = stringGraph.FindVertex(fortyTwo) != default;

            Console.WriteLine("Let's try and find the node containing \"42\". ");
            Console.WriteLine("Did we find it? " + stringGraph.Graph.First().GetData() + found);
            if (found)
            {
                Console.WriteLine("Weird, \"42\" is not supposed to be in the Graph");
                Console.WriteLine();
            }
            else
            {
                Console.WriteLine("That's ok. I wasn't expecting it to be in there.");
                Console.WriteLine();
            }


            //Now we find one that is in the list
            Console.WriteLine("How about the node containing \"47\". ");
            CSC382Graph_Vertex <string> fortySeven = stringGraph.FindVertex("47");

            found = fortySeven != default;
            Console.WriteLine("Did we find it? " + stringGraph.Graph.First().GetData() + found);

            if (!found)
            {
                Console.WriteLine("Weird, \"47\" is supposed to be in the Graph");
                Console.WriteLine();
            }
            else
            {
                Console.WriteLine("Great!");
                Console.WriteLine();
            }

            Console.WriteLine("Let's add \"42\" to the Graph with an edge connecting the two");
            stringGraph.Insert(fortyTwo);
            stringGraph.AddEdge(fortySeven, fortyTwo);
            Console.WriteLine("Is \"42\" connected to \"47\"?" + stringGraph.Graph.First().GetData() + stringGraph.IsEdge(fortySeven, fortyTwo));
            Console.WriteLine();
            Console.WriteLine("I know \"100\" is in the Graph.  Let's see if it is connected to \"47\".");
            found = fortySeven.VertexConnected(stringGraph.FindVertex("100")) != default;
            Console.WriteLine(stringGraph.Graph.First().GetData() + found);
            if (!found)
            {
                Console.WriteLine("I wander why?");
                Console.WriteLine();
            }
            else
            {
                Console.WriteLine("Great!");
                Console.WriteLine();
            }

            found = fortySeven.VertexConnected(fortyTwo) != default;
            Console.WriteLine("Let's verify that \"42\" got added with an edge to \"47\". " + stringGraph.Graph.First().GetData() + found);
            Console.WriteLine();

            int edgeNum = 0;

            var current = stringGraph.Graph.First;

            while (current != null)
            {
                var compare = current.Next;

                while (compare != null)
                {
                    if (stringGraph.IsEdge(current.Value, compare.Value))
                    {
                        edgeNum++;
                    }
                    compare = compare.Next;
                }

                current = current.Next;
            }

            //at this point there should not be any edges except for the one between 42 and 47
            Console.WriteLine("How many edges do we have in the Graph?" + stringGraph.Graph.First().GetData() + edgeNum);

            Console.WriteLine();
            Console.WriteLine("Let's add some more edges so that all the nodes are connected");
            current = stringGraph.Graph.First;
            while (current != null)
            {
                if (current.Next != null)
                {
                    current.Value.AddEdge(current.Next.Value);
                    current.Next.Value.AddEdge(current.Value);

                    if (current.Next.Next != null)
                    {
                        current.Value.AddEdge(current.Next.Next.Value);
                        current.Next.Next.Value.AddEdge(current.Value);
                    }
                }


                current = current.Next;
            }
            Console.WriteLine();
            edgeNum = 0;
            current = stringGraph.Graph.First;

            while (current != null)
            {
                var compare = current.Next;

                while (compare != null)
                {
                    if (stringGraph.IsEdge(current.Value, compare.Value))
                    {
                        edgeNum++;
                    }
                    compare = compare.Next;
                }

                current = current.Next;
            }

            //at this point there should not be any edges except for the one between 42 and 47
            Console.WriteLine("How many edges do we have Now?" + stringGraph.Graph.First().GetData() + edgeNum);
            Console.WriteLine();

            Console.WriteLine("the current graph edges are: ");
            current = stringGraph.Graph.First;
            while (current != null)
            {
                foreach (var item in current.Value.Connected_vertices)
                {
                    Console.WriteLine(current.Value.GetData() + " -- " + item.GetData());
                }


                current = current.Next;
            }
            Console.WriteLine();
        }
示例#3
0
 /// <summary>
 /// default constructor for edge
 /// </summary>
 public CSC382Graph_Edge()
 {
     starting_vertex = default;
     ending_vertex   = default;
 }