// C# program to count total number of
// ways to reach destination in a graph
using System;
class GFG{
// Utility Function to count total ways
static int countWays(int[,] mtrx, int vrtx,
int i, int dest, bool[] visited)
{
// Base condition
// When reach to the destination
if (i == dest) {
return 1;
}
int total = 0;
for (int j = 0; j < vrtx; j++) {
if (mtrx[i,j] == 1 && !visited[j]) {
// Make vertex visited
visited[j] = true;
// Recursive function, for count ways
total += countWays(mtrx, vrtx,
j, dest, visited);
// Backtracking
// Make vertex unvisited
visited[j] = false;
}
}
// Return total ways
return total;
}
// Function to count total ways
// to reach destination
static int totalWays(int[,] mtrx, int vrtx,
int src, int dest)
{
bool[]visited = new bool[vrtx];
// Loop to make all vertex unvisited,
// Initially
for (int i = 0; i < vrtx; i++) {
visited[i] = false;
}
// Make source visited
visited[src] = true;
return countWays(mtrx, vrtx, src, dest,
visited);
}
public static void Main()
{
int vrtx = 11;
int[,] mtrx = {
{ 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0 },
{ 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0 },
{ 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0 },
{ 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0 },
{ 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0 }
};
int src = 3;
int dest = 9;
// Print total ways
Console.Write(totalWays(mtrx, vrtx, src - 1,
dest - 1));
}
}