using System; using System.Collections.Generic; class GfG { static void dfs(int src, int dest, int[,] graph, bool[] visited, List<int> path, List<List<int>> allPaths) { // Add the current vertex to the path path.Add(src); // If destination is reached, store the path if (src == dest) { allPaths.Add(new List<int>(path)); } else { // Recurse for all adjacent vertices for (int adjNode = 0; adjNode < graph.GetLength(0); adjNode++) { if (graph[src, adjNode] == 1 && !visited[adjNode]) { visited[adjNode] = true; dfs(adjNode, dest, graph, visited, path, allPaths); visited[adjNode] = false; } } } // Remove the current vertex from path path.RemoveAt(path.Count - 1); } public static List<List<int>> FindPaths(int v, int[,] edges, int src, int dest) { int[,] graph = new int[v, v]; // Build the graph from edges for (int i = 0; i < edges.GetLength(0); i++) { int u = edges[i, 0]; int vtx = edges[i, 1]; // mark the edge between u and vtx graph[u, vtx] = 1; } bool[] visited = new bool[v]; List<int> path = new List<int>(); List<List<int>> allPaths = new List<List<int>>(); visited[src] = true; dfs(src, dest, graph, visited, path, allPaths); return allPaths; } public static void Main(string[] args) { int[,] edges = new int[,] { { 0, 3 }, { 0, 1 }, { 1, 3 }, { 2, 0 }, { 2, 1 } }; int src = 2, dest = 3; int v = 4; List<List<int>> paths = FindPaths(v, edges, src, dest); foreach (List<int> path in paths) { foreach (int vtx in path) { Console.Write(vtx + " "); } Console.WriteLine(); } } }