Пример #1
0
 public SimpleVertex <T> DepthFirstTraversal(SimpleVertex <T> root, T searchVal)
 {
     item    = null;
     visited = new List <SimpleVertex <T> >();
     DepthFirstTraversalHelper(root, searchVal);
     return(item);
 }
Пример #2
0
        public SimpleVertex <T> BreadthFirstTraversal(SimpleVertex <T> root, T searchVal)
        {
            visited = new List <SimpleVertex <T> >();
            q       = new Queue <SimpleVertex <T> >();

            q.Enqueue(root);
            while (q.Count != 0)
            {
                var n = q.Peek();
                if (n.val.CompareTo(searchVal) == 0)
                {
                    return(n);
                }
                q.Dequeue();
                foreach (var i in n.neighbors)
                {
                    if (!visited.Contains(i))
                    {
                        q.Enqueue(i);
                    }
                }
                visited.Add(n);
            }

            Console.WriteLine("hi!");
            return(null);
        }
Пример #3
0
        public void AddEdge(SimpleVertex <T> a, SimpleVertex <T> b)
        {
            SimpleVertex <T> aVertex = vertices.Find(i => i == a);
            SimpleVertex <T> bVertex = vertices.Find(i => i == b);

            aVertex.neighbors.Add(bVertex);
            bVertex.neighbors.Add(aVertex);
        }
Пример #4
0
 public void RemoveVertex(SimpleVertex <T> vertex)
 {
     foreach (SimpleVertex <T> possibleNeighbor in vertices) //any better way to do this? Is there a List.Map? Can I use List.ConvertAll?
     {
         if (possibleNeighbor.neighbors.Contains(vertex))
         {
             possibleNeighbor.neighbors.Remove(vertex);
         }
     }
     vertices.Remove(vertex);
 }
Пример #5
0
 public SimpleVertex <T> AddVertex(SimpleVertex <T> newVertex)
 {
     vertices.Add(newVertex);
     foreach (SimpleVertex <T> i in vertices)
     {
         if (i == newVertex)
         {
             return(i);
         }
     }
     throw new Exception("Can't find the newly added Vertex!");
 }
Пример #6
0
        void DepthFirstTraversalHelper(SimpleVertex <T> thisNode, T searchVal)
        {
            if (item != null)
            {
                return;
            }
            foreach (SimpleVertex <T> n in thisNode.neighbors)
            {
                if (visited.Contains(n))
                {
                    continue;
                }
                if (n.val.CompareTo(searchVal) == 0)
                {
                    item = n;
                    return;
                }

                visited.Add(n);
                DepthFirstTraversalHelper(n, searchVal);
            }
        }
Пример #7
0
 public bool HasVertex(SimpleVertex <T> vertex) => vertices.Contains(vertex);