private void DepthFirstSearch(Node sourceNode)
        {
            stack.Push(sourceNode);

            while (stack.Count != 0)
            {
                var currentNode = stack.Pop();
                if (currentNode.Value ==0)
                {
                    visited.Add(currentNode);
                }
                if (visited.Contains(currentNode))
                {
                    continue;
                }

                if (!RegionDictionary.ContainsKey(sourceNode))
                {
                    RegionDictionary[sourceNode] = new HashSet<Node>();
                }

                RegionDictionary[sourceNode].Add(currentNode);
                visited.Add(currentNode);
                foreach (var neighborEdge in currentNode.NeighborEdges)
                {
                    stack.Push(neighborEdge.ToNode);
                }
            }
        }
        public Node[,] GetNodeMatrix(int[,] valueMatrix)
        {
            Node[,] nodeMatrix = new Node[_rows, _columns];

            for (int ri = 0; ri < _rows; ri++)
            {
                for (int ci = 0; ci < _columns; ci++)
                {
                    Node node = new Node(valueMatrix[ri, ci]);
                    nodeMatrix[ri, ci] = node;
                }
            }
            return nodeMatrix;
        }
        public IEnumerable<Node> Setup3x3Graph()
        {
            /* Setup Graph like this
            1 1 0
            1 1 1
            1 1 1 */

            List<Node> nodes = new List<Node>
            {
                new Node(1),
                new Node(1),
                new Node(0),
                new Node(1),
                new Node(1),
                new Node(1),
                new Node(1),
                new Node(1),
                new Node(1),
            };

            IEnumerable<Node> nodeZeroNeighbors = new Node[] { nodes[1], nodes[3], nodes[4] };
            IEnumerable<Node> nodeOneNeighbors = new Node[] { nodes[0], nodes[2], nodes[3], nodes[4], nodes[5] };
            IEnumerable<Node> nodeTwoNeighbors = new Node[] { nodes[1], nodes[4], nodes[5] };
            IEnumerable<Node> nodeThreeNeighbors = new Node[] { nodes[0], nodes[1], nodes[4], nodes[6], nodes[7] };
            IEnumerable<Node> nodeFourNeighbors = new Node[] { nodes[0], nodes[1], nodes[2], nodes[3], nodes[5], nodes[6], nodes[7], nodes[8] };
            IEnumerable<Node> nodeFiveNeighbors = new Node[] { nodes[1], nodes[2], nodes[4], nodes[7], nodes[8] };
            IEnumerable<Node> nodeSixNeighbors = new Node[] { nodes[3], nodes[4], nodes[7]};
            IEnumerable<Node> nodeSevenNeighbors = new Node[] { nodes[3], nodes[4], nodes[5], nodes[6], nodes[8] };
            IEnumerable<Node> nodeEightNeighbors = new Node[] { nodes[4], nodes[5], nodes[7] };

            nodes[0].ExtendNeighbors(nodeZeroNeighbors);
            nodes[1].ExtendNeighbors(nodeOneNeighbors);
            nodes[2].ExtendNeighbors(nodeTwoNeighbors);
            nodes[3].ExtendNeighbors(nodeThreeNeighbors);
            nodes[4].ExtendNeighbors(nodeFourNeighbors);
            nodes[5].ExtendNeighbors(nodeFiveNeighbors);
            nodes[6].ExtendNeighbors(nodeSixNeighbors);
            nodes[7].ExtendNeighbors(nodeSevenNeighbors);
            nodes[8].ExtendNeighbors(nodeEightNeighbors);

            return nodes;
        }
        private List<Node> assignNodeNeighbors(Node[,] nodeMatrix)
        {
            List<Node> nodes = new List<Node>();

            for (int ri = 0; ri < _rows; ri++)
            {
                for (int ci = 0; ci < _columns; ci++)
                {
                    var thisNode = nodeMatrix[ri, ci];
                    nodes.Add(thisNode);
                    List<GridIndex> neighborIndices = getNeighborCells(ri, ci);

                    foreach (var index in neighborIndices)
                    {
                        var neigbor = nodeMatrix[index.Row, index.Column];
                        thisNode.AddNeighbor(neigbor);
                    }
                }
            }
            return nodes;
        }
Exemple #5
0
 public Edge(Node fromNode, Node toNode)
 {
     FromNode = fromNode;
     ToNode = toNode;
 }
Exemple #6
0
 public void AddNeighbor(Node neighborNode)
 {
     var edge = new Edge(this, neighborNode);
     _neighborNodes.Add(edge);
 }
 public void RemoveNode(Node node)
 {
     _nodes.Remove(node);
 }
 public void AddNode(Node node)
 {
     _nodes.Add(node);
 }