/* Oleg Orlov, 2012(c), generating randomly adjacency matrix and graph connections */ using System; using System.Collections.Generic; class Graph { internal int id; private int value; internal Graph[] links; public Graph(int inc_id, int inc_value) { this.id = inc_id; this.value = inc_value; links = new Graph[Program.random_generator.Next(0, 4)]; } } class Program { private const int graphs_count = 10; private static List<Graph> list; public static Random random_generator; private static void Init() { random_generator = new Random(); list = new List<Graph>(graphs_count); for (int i = 0; i < list.Capacity; i++) { list.Add(new Graph(i, random_generator.Next(100, 255) * i + random_generator.Next(0, 32))); } } private static void InitGraphs() { for (int i = 0; i < list.Count; i++) { Graph graph = list[i] as Graph; graph.links = new Graph[random_generator.Next(1, 4)]; for (int j = 0; j < graph.links.Length; j++) { graph.links[j] = list[random_generator.Next(0, 10)]; } list[i] = graph; } } private static bool[,] ParseAdjectiveMatrix() { bool[,] matrix = new bool[list.Count, list.Count]; foreach (Graph graph in list) { int[] links = new int[graph.links.Length]; for (int i = 0; i < links.Length; i++) { links[i] = graph.links[i].id; matrix[graph.id, links[i]] = matrix[links[i], graph.id] = true; } } return matrix; } private static void PrintMatrix(ref bool[,] matrix) { for (int i = 0; i < list.Count; i++) { Console.Write("{0} | [ ", i); for (int j = 0; j < list.Count; j++) { Console.Write(" {0},", Convert.ToInt32(matrix[i, j])); } Console.Write(" ]\r\n"); } Console.Write("{0}", new string(' ', 7)); for (int i = 0; i < list.Count; i++) { Console.Write("---"); } Console.Write("\r\n{0}", new string(' ', 7)); for (int i = 0; i < list.Count; i++) { Console.Write("{0} ", i); } Console.Write("\r\n"); } private static void PrintGraphs() { foreach (Graph graph in list) { Console.Write("\r\nGraph id: {0}. It references to the graphs: ", graph.id); for (int i = 0; i < graph.links.Length; i++) { Console.Write(" {0}", graph.links[i].id); } } } [STAThread] static void Main() { try { Init(); InitGraphs(); bool[,] matrix = ParseAdjectiveMatrix(); PrintMatrix(ref matrix); PrintGraphs(); } catch (Exception exc) { Console.WriteLine(exc.Message); } Console.Write("\r\n\r\nPress enter to exit this program..."); Console.ReadLine(); } }
Standard input is empty
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...