/// <summary> /// Prints the results of the HasPathDFS method /// </summary> /// <param name="graph"></param> /// <param name="source"></param> /// <param name="destination"></param> public static void PrintResult_AL_01_DepthFirstSearch(DS01.Graph <T> graph, T source, T destination) { bool result = HasPathDFS(graph, source, destination); Console.Write("\n\n- AL_01 - Depth-First Search: "); Console.Write($"\n\n\t {result}"); Console.WriteLine("\n\n"); }
/// <summary> /// Calls the top sort and then prints out the result /// </summary> /// <param name="graph">Graph that needs to be sorted</param> public static void PrintAllSort(DS01.Graph <T> graph) { List <Node <T> > result = TopSort(graph); Console.Write("\n\n- AQ_02_TopSort_01 - Topological Sort: "); Console.Write("\n\n\t"); foreach (Node <T> node in result) { Console.Write($"->{node.Id.ToString()}"); } Console.WriteLine("\n\n"); }
/// <summary> /// Parent method which calls the recursive HasPathDFSUtil method. /// Finds out if there is a path to a node in a graph from a starting node using a Depth-First Search. /// </summary> /// <param name="graph">Graph for the DFS search</param> /// <param name="source">The starting point of the algorithm</param> /// <param name="destination">The node to be found</param> /// <returns>Whether or not a path exists to a destination node from a source node.</returns> public static bool HasPathDFS(DS01.Graph <T> graph, T source, T destination) { //base case if there is no destination node if (graph.GetNode(destination) == null) { return(false); } HashSet <Node <T> > visited = new HashSet <Node <T> >(); return(HasPathDFSUtil(graph.GetNode(source), graph.GetNode(destination), visited)); }
/// <summary> /// Topologicaly sorts the graph that is passed in. /// Uses a stack of resulting nodes and a hash set of visited nodes. /// A Depth-First Search is used to get the last node in the list to add to the stack. Which is the TopSortUtil method. /// </summary> /// <param name="graph">The graph that needs to be sorted</param> /// <returns>A topologically sorted list of nodes in the graph.</returns> public static List <Node <T> > TopSort(DS01.Graph <T> graph) { List <Node <T> > stack = new List <Node <T> >(); // Is a list but is used as a stack HashSet <Node <T> > visited = new HashSet <Node <T> >(); // Nodes that have already been visited // Iterates through all the nodes in the graph and calls TopSortUtil // TopSortUtil is a DFS algorithm that will add the last node as the first item in the stack foreach (Node <T> node in graph.GetAllNodes()) { if (visited.Contains(node)) { continue; } TopSortUtil(node, stack, visited); } return(stack); }
static void Main(string[] args) { #region Algorithms Console.WriteLine("\n============================================================== AL_01_DepthFirstSeach"); DS01.Graph <int> gAL01_01 = new DS01.Graph <int>(); DS01.AutoCreateGraph_02_Directed_int(gAL01_01); AL01.PrintResult_AL_01_DepthFirstSearch(gAL01_01, 1, 5); Console.WriteLine("\n============================================================== AL_02_BreadthFirstSeach"); DS01.Graph <int> gAL02_01 = new DS01.Graph <int>(); DS01.AutoCreateGraph_02_Directed_int(gAL02_01); AL01.PrintResult_AL_01_DepthFirstSearch(gAL02_01, 1, 5); #endregion //======================================================================================================================= #region Questions Console.WriteLine("\n============================================================== Q_02 - 01 - Topological Sort"); DS01.Graph <string> graphAQ02_01__1 = new DS01.Graph <string>(); DS01.AutoCreateGraph_03_TopSort_Directed_for_AQ_02_string(graphAQ02_01__1); graphAQ02_01__1.PrintGraph(); Q02.PrintAllSort(graphAQ02_01__1); Console.WriteLine("\n==============================================================AQ_08 - 01 Convert from matrix to adjacency list\n"); Q08 Q08 = new Q08(); int[,] matrix = DS02.AutoCreateMatrix_01_Undirected(); DS02.PrintMatrix(matrix); Q08.PrintAdjList(Q08.ConvertFrom_01_AdjMatrix_To_AdjList(matrix)); Console.WriteLine("\n==============================================================AQ_10 - DAG Evaluation Algo\n"); Q10 AQ10 = new Q10(); AQ10.GetAnswer(); #endregion }