fork(7) download
  1. /*
  2.   Oleg Orlov, 2012(c), generating randomly adjacency matrix and graph connections
  3. */
  4.  
  5. using System;
  6. using System.Collections.Generic;
  7.  
  8. class Graph
  9. {
  10. internal int id;
  11. private int value;
  12. internal Graph[] links;
  13.  
  14. public Graph(int inc_id, int inc_value)
  15. {
  16. this.id = inc_id;
  17. this.value = inc_value;
  18. links = new Graph[Program.random_generator.Next(0, 4)];
  19. }
  20. }
  21.  
  22. class Program
  23. {
  24. private const int graphs_count = 10;
  25. private static List<Graph> list;
  26. public static Random random_generator;
  27.  
  28. private static void Init()
  29. {
  30. random_generator = new Random();
  31. list = new List<Graph>(graphs_count);
  32.  
  33. for (int i = 0; i < list.Capacity; i++)
  34. {
  35. list.Add(new Graph(i, random_generator.Next(100, 255) * i + random_generator.Next(0, 32)));
  36. }
  37. }
  38.  
  39. private static void InitGraphs()
  40. {
  41. for (int i = 0; i < list.Count; i++)
  42. {
  43. Graph graph = list[i] as Graph;
  44. graph.links = new Graph[random_generator.Next(1, 4)];
  45.  
  46. for (int j = 0; j < graph.links.Length; j++)
  47. {
  48. graph.links[j] = list[random_generator.Next(0, 10)];
  49. }
  50.  
  51. list[i] = graph;
  52. }
  53. }
  54.  
  55. private static bool[,] ParseAdjectiveMatrix()
  56. {
  57. bool[,] matrix = new bool[list.Count, list.Count];
  58.  
  59. foreach (Graph graph in list)
  60. {
  61. int[] links = new int[graph.links.Length];
  62.  
  63. for (int i = 0; i < links.Length; i++)
  64. {
  65. links[i] = graph.links[i].id;
  66. matrix[graph.id, links[i]] = matrix[links[i], graph.id] = true;
  67. }
  68. }
  69.  
  70. return matrix;
  71. }
  72.  
  73. private static void PrintMatrix(ref bool[,] matrix)
  74. {
  75. for (int i = 0; i < list.Count; i++)
  76. {
  77. Console.Write("{0} | [ ", i);
  78.  
  79. for (int j = 0; j < list.Count; j++)
  80. {
  81. Console.Write(" {0},", Convert.ToInt32(matrix[i, j]));
  82. }
  83.  
  84. Console.Write(" ]\r\n");
  85. }
  86.  
  87. Console.Write("{0}", new string(' ', 7));
  88.  
  89. for (int i = 0; i < list.Count; i++)
  90. {
  91. Console.Write("---");
  92. }
  93.  
  94. Console.Write("\r\n{0}", new string(' ', 7));
  95.  
  96. for (int i = 0; i < list.Count; i++)
  97. {
  98. Console.Write("{0} ", i);
  99. }
  100.  
  101. Console.Write("\r\n");
  102. }
  103.  
  104. private static void PrintGraphs()
  105. {
  106. foreach (Graph graph in list)
  107. {
  108. Console.Write("\r\nGraph id: {0}. It references to the graphs: ", graph.id);
  109.  
  110. for (int i = 0; i < graph.links.Length; i++)
  111. {
  112. Console.Write(" {0}", graph.links[i].id);
  113. }
  114. }
  115. }
  116.  
  117. [STAThread]
  118. static void Main()
  119. {
  120. try
  121. {
  122. Init();
  123. InitGraphs();
  124. bool[,] matrix = ParseAdjectiveMatrix();
  125. PrintMatrix(ref matrix);
  126. PrintGraphs();
  127. }
  128. catch (Exception exc)
  129. {
  130. Console.WriteLine(exc.Message);
  131. }
  132.  
  133. Console.Write("\r\n\r\nPress enter to exit this program...");
  134. Console.ReadLine();
  135. }
  136. }
Success #stdin #stdout 0.04s 36968KB
stdin
Standard input is empty
stdout
0 | [  0, 0, 0, 1, 0, 0, 0, 0, 1, 1, ]
1 | [  0, 0, 0, 1, 1, 0, 0, 0, 1, 0, ]
2 | [  0, 0, 1, 0, 1, 0, 0, 0, 1, 1, ]
3 | [  1, 1, 0, 0, 0, 0, 0, 1, 0, 0, ]
4 | [  0, 1, 1, 0, 0, 1, 0, 1, 0, 0, ]
5 | [  0, 0, 0, 0, 1, 0, 0, 1, 1, 1, ]
6 | [  0, 0, 0, 0, 0, 0, 1, 1, 0, 1, ]
7 | [  0, 0, 0, 1, 1, 1, 1, 0, 0, 0, ]
8 | [  1, 1, 1, 0, 0, 1, 0, 0, 0, 0, ]
9 | [  1, 0, 1, 0, 0, 1, 1, 0, 0, 1, ]
       ------------------------------
       0  1  2  3  4  5  6  7  8  9  

Graph id: 0. It references to the graphs:  8
Graph id: 1. It references to the graphs:  4 8
Graph id: 2. It references to the graphs:  4 2 4
Graph id: 3. It references to the graphs:  1 0 1
Graph id: 4. It references to the graphs:  7 5
Graph id: 5. It references to the graphs:  9 7
Graph id: 6. It references to the graphs:  6 9
Graph id: 7. It references to the graphs:  3 6
Graph id: 8. It references to the graphs:  5 2 5
Graph id: 9. It references to the graphs:  2 0 9

Press enter to exit this program...