예제 #1
0
        // *********************************************************************
        // Wczytuje graf z tablicy (wiersze: start, end, weight).
        // Wyrzuca IncorrectGraphException w przypadku, gdy tablica zawiera
        // nieprawidlowe informacje, w innym przypadku zwraca liste sasiedztwa.
        // *********************************************************************

        public static int[][][] Load(int[][] graph)
        {
            if (graph == null || graph.Length < 2)
            {
                throw new IncorrectGraphException();
            }

            int maxVertex = graph.Max(i => Math.Max(i[0], i[1]));

            List <int[]>[] edges = new List <int[]> [maxVertex + 1];

            for (int i = 0; i < edges.Length; i++)
            {
                edges[i] = new List <int[]>();
            }

            foreach (int[] edge in graph)
            {
                if (edge == null || edge.Length != 3 ||
                    edge[0] < 0 || edge[1] < 0 || edge[2] <= 0)
                {
                    throw new IncorrectGraphException();
                }

                ListGraphGenerator.InsertConnection(
                    edges, edge[0], edge[1], edge[2]);
            }

            return(edges.Select(e => e.ToArray()).ToArray());
        }
예제 #2
0
        // *********************************************************************
        // Wczytuje graf z podanej tablicy krawedzi i zwraca liste sasiedztwa.
        // Wyrzuca IncorrectGraphException w przypadku, gdy tablica zawiera
        // nieprawidlowe informacje.
        // *********************************************************************

        public static int[][][] Load(Edge[] edges)
        {
            if (!ListGraphValidator.ValidateEdges(edges))
            {
                throw new IncorrectGraphException();
            }

            // Znalezienie maksymalnego wierzcholka maxVertex:
            int maxVertex = edges.Max(e => Math.Max(e.Start, e.End));

            // Utworzenie listy sasiedztwa i zainicjowanie jej:
            List <int[]>[] graph = new List <int[]> [maxVertex + 1];

            for (int i = 0; i < graph.Length; i++)
            {
                graph[i] = new List <int[]>();
            }

            // Dodanie do listy przekazanych metodzie krawedzi:
            edges.ToList().ForEach(
                e => ListGraphGenerator.InsertConnection(
                    graph, e.Start, e.End, e.Weight));

            if (!ListGraphValidator.ValidateGraph(graph))
            {
                throw new IncorrectGraphException();
            }

            return(graph.Select(i => i.ToArray()).ToArray());
        }