Beispiel #1
0
        //Perform a DFS traversal starting at the node with id “startID”
        //leaving a list of visited id’s in the visited list.

        public void DepthFirstTraverse(T startID, ref List <T> visited)
        {
            LinkedList <T> adj;
            Stack <T>      toVisit = new Stack <T>();

            GraphNode <T> current = new GraphNode <T>(startID);

            toVisit.Push(startID);

            while (toVisit.Count != 0)
            {
                //to be completed. Hint: get current node to the list of visited nodes
                // and add its adjacent nodes (only those not already visited) to toVist
                current = GetNodeByID(toVisit.Peek());
                adj     = current.GetAdjList();
                visited.Add(current.ID);

                foreach (T type in adj)
                {
                    if (!toVisit.Contains(type) && !visited.Contains((type)))
                    {
                        toVisit.Push(type);
                    }
                }
            }
        }
Beispiel #2
0
        //find from in list of nodes and search its adjList for to
        public bool IsAdjacent(GraphNode <T> from, GraphNode <T> to)
        {
            foreach (GraphNode <T> n in nodes)
            {
                if (n.ID.Equals(from.ID))
                {
                    if (from.GetAdjList().Contains(to.ID))
                    {
                        return(true);
                    }
                }
            }

            return(false);
        }
Beispiel #3
0
 // only returns true if nodes “from “ and “to” are adjacent
 public bool IsAdjacent(GraphNode <T> from, GraphNode <T> to)
 {
     foreach (GraphNode <T> n in nodes)
     {
         if (n.ID.Equals(from.ID))
         {
             if (from.GetAdjList().Contains(to.ID))
             {
                 return(true);
             }
         }
     }
     return(false);
     // to be completed
     //Hint: Find the node “from” in the list of nodes and then search its adjList to see if there is node “to”
 }
Beispiel #4
0
 public bool IsAdjacent(GraphNode <T> from, GraphNode <T> to)
 {
     return(from.GetAdjList().Any(id => id.CompareTo(to.Id) == 0));
 }