static void Main(string[] args)
        {
            Console.WriteLine("Hello Graphs");
            Console.WriteLine("");

            List <object> listTest = new List <object>();

            listTest.Add("soloNode");
            Graph.Classes.Graph graph = new Graph.Classes.Graph(listTest);
            graph.AddEdge("Apple", "Banana", 10);
            graph.AddEdge("Dog", "Cat", 22);
            graph.AddEdge("xBox", "PlayStation", 7);
            Console.WriteLine("Creating a new graph with 7 nodes and 3 edges");
            Console.WriteLine($"Return size of graph:  {graph.Size()}");

            Dictionary <object, int> toNeighbor   = graph.GetNeighbors("Dog");
            Dictionary <object, int> fromNeighbor = graph.GetNeighbors("xBox");

            //Console.WriteLine($"Return neighbor of graph from Dog node:");
            foreach (var item in toNeighbor.Values)
            {
                //Console.WriteLine(item);
            }

            Console.WriteLine("Return all vertices in the graph:  ");
            foreach (var item in graph.Vertices)
            {
                Console.WriteLine(item.Value);
            }

            Console.ReadLine();
        }
        public void GetNeighbots(object to, object from, int weight)
        {
            List <object> listTest = new List <object>();

            Graph.Classes.Graph graph = new Graph.Classes.Graph(listTest);
            graph.AddEdge(to, from, weight);

            Dictionary <object, int> toNeighbor   = graph.GetNeighbors(to);
            Dictionary <object, int> fromNeighbor = graph.GetNeighbors(from);

            Assert.True(toNeighbor.ContainsValue(weight) && fromNeighbor.ContainsValue(weight));
        }
        public void GetNeigborsCollectionTest(string expected, int index)
        {
            Graph.Classes.Graph graph = new Graph.Classes.Graph();
            Node parent = new Node();

            parent.Value = "A";

            LinkedList <Tuple <Node, int> > originList = new LinkedList <Tuple <Node, int> >();
            Tuple <Node, int> origin = new Tuple <Node, int>(parent, 0);

            originList.AddFirst(origin);

            graph.AdjacencyList.AddFirst(originList);

            Node child = new Node();

            child.Value = "B";

            graph.AddEdge(parent, new Tuple <Node, int>(child, 5));

            Node c = new Node();

            c.Value = "C";

            graph.AddEdge(parent, new Tuple <Node, int>(c, 4));

            var nodeList = graph.GetNeighbors(parent);

            Assert.Equal(expected, nodeList[index].Item1.Value);
        }
        public void NeighborsCountTest()
        {
            Graph.Classes.Graph graph = new Graph.Classes.Graph();
            Node parent = new Node();

            parent.Value = "A";

            LinkedList <Tuple <Node, int> > originList = new LinkedList <Tuple <Node, int> >();
            Tuple <Node, int> origin = new Tuple <Node, int>(parent, 0);

            originList.AddFirst(origin);

            graph.AdjacencyList.AddFirst(originList);

            Node child = new Node();

            child.Value = "B";

            graph.AddEdge(parent, new Tuple <Node, int>(child, 5));

            Node c = new Node();

            c.Value = "C";

            graph.AddEdge(parent, new Tuple <Node, int>(c, 4));

            var nodeList = graph.GetNeighbors(parent);

            Assert.Equal(2, nodeList.Count);
        }
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            Graph.Classes.Graph graph = new Graph.Classes.Graph();
            Node parent = new Node();

            parent.Value = "A";

            LinkedList <Tuple <Node, int> > originList = new LinkedList <Tuple <Node, int> >();
            Tuple <Node, int> origin = new Tuple <Node, int>(parent, 0);

            originList.AddFirst(origin);

            graph.AdjacencyList.AddFirst(originList);

            Node child = new Node();

            child.Value = "B";

            graph.AddEdge(parent, new Tuple <Node, int>(child, 5));

            Node c = new Node();

            c.Value = "C";

            graph.AddEdge(parent, new Tuple <Node, int>(c, 4));


            foreach (var item in graph.AdjacencyList)
            {
                foreach (var node in item)
                {
                    Console.Write(node.Item2);
                    Console.WriteLine(node.Item1.Value);
                }
            }

            var nodeList = graph.GetNodes();

            foreach (var item in nodeList)
            {
                Console.Write(item.Value);
            }

            var neighbor = graph.GetNeighbors(parent);

            foreach (var item in neighbor)
            {
                Console.Write(item.Item1.Value);
                Console.WriteLine(item.Item2);
            }

            var count = graph.Size();

            Console.WriteLine(count);
            Console.ReadKey();
        }
        public void NeighborsNullTest()
        {
            Graph.Classes.Graph graph = new Graph.Classes.Graph();
            Node parent = new Node();

            parent.Value = "A";

            var nodeList = graph.GetNeighbors(parent);

            Assert.Empty(nodeList);
        }
Пример #7
0
        /// <summary>
        /// checks if direct flight is possible
        /// </summary>
        /// <param name="graph">graph of cities</param>
        /// <param name="cities">city iternary</param>
        /// <returns>tuple contain bool and string in money format</returns>
        public static Tuple <bool, string> GetEdge(Graph.Classes.Graph graph, Node[] cities)
        {
            if (cities.Length <= 1 || graph.AdjacencyList.Count == 0)
            {
                return(new Tuple <bool, string>(false, 0.ToString("C2")));
            }

            int  money  = 0;
            bool exists = true;
            Tuple <bool, string> costs = new Tuple <bool, string> (exists, money.ToString("C2"));

            for (int i = 0; i < cities.Length - 1; i++)
            {
                List <Tuple <Node, int> > neighborNodes = graph.GetNeighbors(cities[i]);
                foreach (var item in neighborNodes)
                {
                    exists = false;
                    if (item.Item1 == cities[i + 1])
                    {
                        money += item.Item2;
                        exists = true;
                    }
                    if (exists)
                    {
                        break;
                    }
                }
                if (!exists)
                {
                    break;
                }
            }

            if (!exists)
            {
                return(new Tuple <bool, string>(exists, 0.ToString("C2")));
            }
            else
            {
                return(new Tuple <bool, string>(exists, money.ToString("C2")));
            }
        }
 /// <summary>
 /// Recursive private method for traversing a graph starting from a given vertex, and looks for the parameter route.
 /// </summary>
 /// <param name="graph">
 /// Graph<string, int>: the graph we are traversing
 /// </param>
 /// <param name="traversal">
 /// List<Vertex<string>>: a List of the vertices seen so far in a given traversal. Allows checking for whether we are in an infinite loop in the graph.
 /// </param>
 /// <param name="currVertex">
 /// Vertex<string>: the current vertex we are looking at in the traversal
 /// </param>
 /// <param name="route">
 /// string[]: the route we are looking for as an array of destinations
 /// </param>
 /// <param name="index">
 /// int: the index of the next destination we are looking for in the route string[]
 /// </param>
 /// <param name="cost">
 /// int: once a possible route is found, keeps track of the total cost of the route
 /// </param>
 /// <returns>
 /// Tuple<bool, int>: a tuple containing whether the route is possible (bool), and if it is, its cost (int). If the route is not possible, returns -1 for the cost.
 /// </returns>
 private static Tuple <bool, int> GetEdges(this Graph <string, int> graph, List <Vertex <string> > traversal, Vertex <string> currVertex, string[] route, int index, int cost)
 {
     if (!traversal.Contains(currVertex))
     {
         traversal.Add(currVertex);
         if (currVertex.Value.ToLower() == route[index].ToLower())
         {
             index++;
             if (index >= route.Length)
             {
                 return(Tuple.Create(true, cost));
             }
         }
         var vertexEdges = graph.GetNeighbors(currVertex);
         foreach (var oneEdge in vertexEdges)
         {
             cost += oneEdge.Weight;
             return(GetEdges(graph, traversal, oneEdge.Vertex, route, index, cost));
         }
     }
     return(Tuple.Create(false, -1));
 }