Advertisement
ikar

C# UNIPI Exams Feb 2011 Ex 2

Jan 15th, 2013
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.21 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3.  
  4. namespace csex2
  5. {
  6.     public class Program
  7.     {
  8.         static bool[,] adjMatrix;
  9.  
  10.         static int[] path;
  11.  
  12.         static void ComputeSubGraphs(int n, int m, int cnode, int count)
  13.         {
  14.             if (count == m)
  15.             {
  16.                 for (int i = 0; i < m; i++)
  17.                 {
  18.                     Console.Write("Node[{0}]", path[i]);
  19.                     if (i < m - 1)
  20.                         Console.Write(" -> ");
  21.                     else
  22.                         Console.Write("\n");
  23.                 }
  24.                 path = new int[m];
  25.             }
  26.             else if(cnode+1<n && m-count<n-cnode)
  27.             {
  28.                 for (int i = cnode + 1; i < n; i++)
  29.                 {
  30.                     if (adjMatrix[cnode, i])
  31.                     {
  32.                         path[count] = i;
  33.                         ComputeSubGraphs(n, m, i, count + 1);
  34.                     }
  35.  
  36.                 }
  37.             }
  38.         }
  39.         public static void Main(string[] args)
  40.         {
  41.             try
  42.             {
  43.                 int n, m;
  44.                 Console.Write("Please enter the number of nodes: ");
  45.                 var inp = Console.ReadLine();
  46.                 n = Int32.Parse(inp);
  47.                 Console.Write("Please enter the depth (m) : ");
  48.                 inp = Console.ReadLine();
  49.                 m = int.Parse(inp);
  50.  
  51.                 adjMatrix = new bool[n, n];
  52.                 path = new int[m];
  53.  
  54.                 Console.WriteLine("Enter the adjacent matrix with spaces {0,1}");
  55.                 for (var i = 0; i < n; i++)
  56.                 {
  57.                     inp = Console.ReadLine();
  58.                     var inparr = inp.Split(' ');
  59.                     if (inparr.Length != n) throw new Exception("Input array length is not correct.");
  60.                     for (var j = 0; j < n; j++)
  61.                     {
  62.                         adjMatrix[i, j] = int.Parse(inparr[j]) == 0 ? false : true;
  63.                     }
  64.                 }
  65.                 ComputeSubGraphs(n, m, -1, 0);
  66.  
  67.             }
  68.             catch (Exception ex)
  69.             {
  70.                 Console.WriteLine(ex.Message);
  71.             }
  72.         }
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement