Ejemplo n.º 1
0
 public virtual void AddNode(Node n)
 {
     if (nodes.ContainsKey(n.Key))
     {
         throw new ArgumentException("There already exists a node in the graph with key " + n.Key);
     }
     nodes.Add(n.Key, n);
 }
Ejemplo n.º 2
0
 public virtual void AddUndirectedEdge(Node u, Node v, int cost)
 {
     if (!nodes.ContainsKey(u.Key) || !nodes.ContainsKey(v.Key))
     {
         throw new ArgumentException("One or both of the nodes supplied were not members of the graph.");
     }
     u.AddDirected(v, cost);
     v.AddDirected(u, cost);
 }
Ejemplo n.º 3
0
 public virtual Node AddNode(int key, object data)
 {
     if (nodes.ContainsKey(key))
     {
         throw new ArgumentException("There already exists a node in the graph with key " + key);
     }
     var n = new Node(key, data);
     nodes.Add(key, n);
     return n;
 }
Ejemplo n.º 4
0
        /// <summary>
        /// Performs a Depth First search
        /// </summary>
        /// <param name="start"></param>
        /// <param name="end"></param>
        public bool MyDepthFirstSearch(Node start, int s)
        {
            var stack = new Stack<Node>();
            var path = new Path<Node>(start);
            var len = 0;
            start.Path = path;
            stack.Push(start);
            if (len == s)
            {
                Console.WriteLine("Success!!!");
                return true;
            }

            while (stack.Count != 0)
            {
                var u = stack.Pop();
                len = u.Len;

                    // Store n's neighbors in the stack
                    foreach (Track edge in u.Connections)
                    {
                            //Console.WriteLine("Current step: {0} -> {1} ", u.Key, edge.Neighbor.Key);
                        Node from;
                        edge.Neighbor.Path=u.Path.AddStep(edge.Neighbor, edge.Cost);
                        if (u.Path.IsCycle(edge.Neighbor,out from))
                        {
                            int cycleLen = edge.Neighbor.Path.TotalCost - from.Path.TotalCost;
                            if (s % cycleLen == from.Path.TotalCost)
                            {
                                //Console.WriteLine(pathLine + "Success!!!");
                                return true;

                            }

                        }
                            if(u.Path.IsMoveBack(u,edge.Neighbor))
                            {
                                continue;
                            }
                            //edge.Neighbor.Data = "Visited";
                            edge.Neighbor.PathParent = u;

                            edge.Neighbor.Path=u.Path.AddStep(edge.Neighbor, edge.Cost);
                            if (edge.Neighbor.Path.TotalCost>=s)
                            {
                                var pathLine=edge.Neighbor.Path.GetPath();
                                if (edge.Neighbor.Path.TotalCost == s)
                                {
                                    //Console.WriteLine("Success!!!");
                                    Console.WriteLine(pathLine + "Success!!!");
                                    return true;
                                }
                                //Console.WriteLine("Too long!!!");
                                continue;
                            }
                            stack.Push(edge.Neighbor);

                    }
                }
            return false;
        }
Ejemplo n.º 5
0
 public Track(Node neighbor, int cost)
 {
     this.cost = cost;
     this.neighbor = neighbor;
 }
Ejemplo n.º 6
0
 protected internal virtual void AddDirected(Node n, int cost)
 {
     AddDirected(new Track(n, cost));
 }
Ejemplo n.º 7
0
 public virtual bool Contains(Node n)
 {
     return Contains(n.Key);
 }
Ejemplo n.º 8
0
 public virtual void AddUndirectedEdge(Node u, Node v)
 {
     AddUndirectedEdge(u, v, 0);
 }