Ejemplo n.º 1
0
        public static void EvalMatrix(int[,] matrix, string end)
        {
            Console.WriteLine("\r\n");
            for (int i = 0; i < matrix.GetLength(0); i++)
            {
                for (int j = 0; j < matrix.GetLength(1); j++)
                {
                    Console.Write(" {0}", matrix[i, j]);
                }
                Console.WriteLine();
            }
            Console.WriteLine("\r\n");

            DirectedGraphNode start = GraphFactory(matrix);
            List <string>     path  = AnyPathAlgorithm.FindFirstPath(start, end, new List <string>());

            if (path == null)
            {
                Console.WriteLine("No path from {0} to {1}", start.Symbol, end);
            }
            else
            {
                Console.WriteLine("First path from {0} to {1}: ", start.Symbol, end);
                foreach (string s in path)
                {
                    Console.Write("{0}, ", s);
                }
            }
            Console.WriteLine();
        }
Ejemplo n.º 2
0
 public static List <string> FindFirstPath(DirectedGraphNode node, string end, List <string> path)
 {
     path.Add(node.Symbol);
     if (node.Symbol == end)
     {
         //we are done
         return(path);
     }
     foreach (var neig in node.Neighbors)
     {
         //avoid looping
         if (!path.Contains(neig.Symbol))
         {
             //recursion
             var aux = FindFirstPath(neig, end, path.ToList());
             //found anything? return it, else try with next neighbor
             if (aux != null && aux.Contains(end))
             {
                 return(aux);
             }
         }
     }
     //if we got here, we haven't find any path
     return(null);
 }
Ejemplo n.º 3
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(((char)asciiSymbol++).ToString());
                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[0]);
        }