Sample C# code to find the possible number of ways to navigate from source to destination

 

Nodes A, B, C, D, E, F and G are connected as shown below. Find the possible number of ways to traverse from D to F.

          A———–F
        / |              |
      /   |    G——|
     B   |   / 
          | /
         C———-E
          |
          |
          |
          D
 

using System;

using System.Collections.Generic;

using System.Text;

//program to find the possible number of ways to navigate from source node to

//destination node wherein the node links are defined by a connection matrix

//A connection is represented by 1 and a disconnection by 0

namespace permutation1

{

    class Program

    {

        static void Main(string[] args)

        {

            int i, j, k, m, p = 0, q = 0, flag = 0, count = 0;

            string s = "d";//source node

            string d = "f";//destination node

            string[] digits = { "a", "b", "c", "d", "e", "f", "g" };//nodes

            string[,] matrix = new string[digits.Length, digits.Length];

            int[] counter = new int[digits.Length];

            int[,] check = new int[2, digits.Length];

            string[,] node = new string[2, digits.Length];

            int[,] connect = {

                                { 1, 1, 1, 0, 0, 1, 0 },

                                { 1, 1, 0, 0, 0, 0, 0 },

                                { 1, 0, 1, 1, 1, 0, 1 },

                                { 0, 0, 1, 1, 0, 0, 0 },

                                { 0, 0, 1, 0, 1, 0, 0 },

                                { 1, 0, 0, 0, 0, 1, 1 },

                                { 0, 0, 1, 0, 0, 1, 1 }

                             };//connection matrix

            for (i = 0; i < digits.Length; i++)

            {

                check[0, i] = i;

                check[1, i] = 0;

                node[0, i] = digits[i];

                node[1, i] = i.ToString();

                for (j = 0; j < digits.Length; j++)

                {

                    matrix[i, j] = digits[j];

                }

            }

            for (k = (digits.Length – 2); k >= 0; k–)

            {

                for (i = (digits.Length – 1); i >= k; i–)

                {

                    counter[i] = 0;

                }

                i++;

                do

                {

                    for (j = i; j < digits.Length; j++)

                    {

                        for (m = 0; m < digits.Length; m++)

                        {

                            if (check[0, m] == counter[j])

                                check[1, m]++;

                        }

                    }

                    flag = 0;

                    for (j = 0; j < digits.Length; j++)

                    {

                        if (check[1, j] >= 2)

                            flag = 1;

                    }

                    if (flag == 0)

                    {

                        if (s == matrix[i, counter[i]] && d == matrix[digits.Length – 1, counter[digits.Length – 1]])

                        {

                            flag = 0;

                            for (m = i; m < digits.Length – 1; m++)

                            {

                                for (j = 0; j < digits.Length; j++)

                                {

                                    if (node[0, j] == matrix[m, counter[m]])

                                    {

                                        p = int.Parse(node[1, j]);

                                    }

                                }

                                for (j = 0; j < digits.Length; j++)

                                {

                                    if (node[0, j] == matrix[m + 1, counter[m + 1]])

                                    {

                                        q = int.Parse(node[1, j]);

                                    }

                                }

                                if (connect[p, q] != 1)

                                    flag = 1;

                            }

                            if (flag == 0)

                            {

                                for (j = i; j < digits.Length; j++)

                                {

                                    if (j == digits.Length – 1)

                                    {

                                        Console.Write(matrix[j, counter[j]]);

                                    }

                                    else

                                    {

                                        Console.Write(matrix[j, counter[j]] + " –> ");

                                    }

                                }

                                count++;

                                Console.WriteLine();

                            }

                        }

                    }

                    for (m = 0; m < digits.Length; m++)

                    {

                        check[1, m] = 0;

                    }

                    for (j = (digits.Length – 1); ; j–)

                    {

                        counter[j]++;

                        if (counter[j] == digits.Length && j != k)

                        {

                            counter[j] = 0;

                            continue;

                        }

                        else

                        {

                            break;

                        }

                    }

                } while (counter[k] < digits.Length);

            }

            Console.WriteLine("Total number of ways to go from " + s + " to " + d + " is " + count);

        }

    }

}

 
Here is the output of the above code:
d –> c –> a –> f
d –> c –> g –> f
Total number of ways to go from d to f is 2
Press any key to continue . . .
Advertisements
This entry was posted in Information Technology. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s