Exemplo n.º 1
0
        //note:
        //weight = 0 = unvisited
        //weight = 1 = temporarily visited
        //weight = 2 = permanently visited
        private static bool Visit(DirectedGraphNode n)
        {
            if (n.Weight == 1) //stop all (not a DAG)
            {
                return(false);
            }

            if (n.Weight == 2)
            {
                return(true);
            }

            n.Weight = 1; //mark node temporarily
            foreach (var m in n.Neighbors)
            {
                if (!Visit(m))
                {
                    return(false); //not a DAG
                }
            }
            //mark node permanently and push it to the stack
            n.Weight = 2;
            L.Push(n);

            return(true);
        }
Exemplo n.º 2
0
        static DirectedGraphNode[] GraphFactory(int[,] matrix)
        {
            byte asciiSymbol = 65;

            DirectedGraphNode[] arr = new DirectedGraphNode[matrix.GetLength(0)];
            for (int i = 0; i < arr.Length; i++)
            {
                DirectedGraphNode node = new DirectedGraphNode(asciiSymbol++);
                arr[i] = node;
            }

            for (int i = 0; i < matrix.GetLength(0); i++)
            {
                for (int j = 0; j < matrix.GetLength(1); j++)
                {
                    if (matrix[i, j] == 1)
                    {
                        arr[i].Neighbors.Add(arr[j]);
                    }
                }
            }
            return(arr);
        }