Example #1
0
        public bool RemoveNode(Node <CONTENT> node)
        {
            Debug.Assert(null != node);
            Debug.Assert(NodeSet.Contains(node));

            if (null == node || !NodeSet.Contains(node))
            {
                return(false);
            }

            // otherwise, the node was found
            NodeSet.Remove(node);

            // This is optimized as we assume that edges are always bidirectional
            node.ForEachNeighbor(neighbor => neighbor.RemoveDirectedEdge(node));

            // For directed edges, must enumerate through each node in the nodeSet,
            // removing edges to this node
            //foreach (WeightedNode<CONTENT> otherNode in nodeSet)
            //{
            //    if (otherNode.Neighbors.ContainsKey(node))
            //    {
            //        otherNode.Neighbors.Remove(node);
            //    }
            //}

            return(true);
        }
Example #2
0
        /// <summary>
        /// Breadth First Search
        /// </summary>
        /// <param name="action">an Action accepting 1 parameter CONTENT</param>
        public void BFS(Action <Node <CONTENT> > action)
        {
            var visited    = new HashSet <Node <CONTENT> >();
            var notVisited = new Node <CONTENT> [Count];

            NodeSet.CopyTo(notVisited);

            // This is a disjoint set, so visit each vertice
            foreach (var start in NodeSet)
            {
                if (!visited.Contains(start))
                {
                    var queue = new Queue <Node <CONTENT> >();
                    visited.Add(start);
                    action(start);
                    queue.Enqueue(start);

                    while (queue.Count != 0)
                    {
                        Node <CONTENT> node = queue.Dequeue();
                        node.ForEachNeighbor(neighbor =>
                        {
                            if (!visited.Contains(neighbor))
                            {
                                visited.Add(neighbor);
                                action(neighbor);
                                queue.Enqueue(neighbor);
                            }
                        });
                    }
                }
            }
        }
Example #3
0
 private void RunDFS(Node <CONTENT> node, Dictionary <Node <CONTENT>, VertexState> state, Action <Node <CONTENT> > action)
 {
     state[node] = VertexState.Gray;
     action(node);
     node.ForEachNeighbor((neighbor) =>
     {
         if (!state.ContainsKey(neighbor)) //  VertexState.White
         {
             RunDFS(neighbor, state, action);
         }
     });
     state[node] = VertexState.Black;
 }