예제 #1
0
        public Graph(string fileName)
        {
            StreamReader reader = File.OpenText(fileName);
            string       line   = reader.ReadLine();

            if (line == null)
            {
                throw new ArgumentException("Graph file is empty");
            }
            if (!int.TryParse(line, out int n))
            {
                throw new ArgumentException("The file does not start with integral graph size");
            }

            int[] weights = new int[n * (n - 1) / 2];
            for (int i = 0; i < n - 1; i++)
            {
                line = reader.ReadLine();
                if (line == null)
                {
                    throw new ArgumentException("Too few lines in the graph file");
                }

                string[] items = line.Split(',');
                if (items.Length != n - i - 1)
                {
                    throw new ArgumentException(string.Format("Too few elements in line {0}", i + 1));
                }
                for (int j = i + 1; j < n; j++)
                {
                    int ind = j - i - 1;
                    if (!int.TryParse(items[ind], out int w))
                    {
                        throw new ArgumentException(string.Format("Incorrect entry {0} on line {1}", i + 1, ind));
                    }
                    weights[(i * (2 * n - 1 - i)) / 2 + ind] = w;
                }
            }
            VertexCount = n;
            Weights     = new AdjTriangle(weights, VertexCount);
            Name        = Path.GetFileName(fileName);
        }
예제 #2
0
 public Graph(int[] adjTriangle, int vertexCount, string name)
 {
     Weights     = new AdjTriangle(adjTriangle, vertexCount);
     VertexCount = vertexCount;
     Name        = name;
 }