/// <summary>
        /// Method restores geometry information for each graph node
        /// </summary>
        /// <param name="xadj">Index array for graph</param>
        /// <param name="size">Size of index array</param>
        /// <param name="adjncy">Adjacency list</param>
        /// <param name="graphNumeration">Restored numeration for the input graph</param>
        /// <returns>
        /// Return code:
        /// 0 - success
        /// -1 - error
        /// </returns>
        public static int Numerate(long[] xadj, int size, int[] adjncy, out int[][] graphNumeration)
        {
            Graph graph = new Graph(xadj, adjncy);

            graphNumeration = null;
            int V     = graph.GetVerticesCount();
            int error = 0;

            switch (GetDimension(graph))
            {
            case 1:
                error = new Numerator1D().Run(graph, out graphNumeration);
                if (error != 0)
                {
                    NumerationHelper.Clear(graphNumeration);
                    error = new Numerator2D().Run(graph, out graphNumeration);
                }
                if (error != 0)
                {
                    NumerationHelper.Clear(graphNumeration);
                    error = new Numerator3D().Run(graph, out graphNumeration);
                }
                break;

            case 2:
                error = new Numerator2D().Run(graph, out graphNumeration);
                if (error != 0)
                {
                    NumerationHelper.Clear(graphNumeration);
                    error = new Numerator3D().Run(graph, out graphNumeration);
                }
                break;

            case 3:
                error = new Numerator3D().Run(graph, out graphNumeration);
                break;

            default: return(-1);
            }
            return(error);
        }
Пример #2
0
        public int Run(Graph graph, out int[][] graphNumeration)
        {
            InitMembers(graph, out graphNumeration);

            int  vertexWithMaxDegree = INVALID_INDEX;
            long maxDegree           = 0;

            var traversal = new Traversal <BFS>(graph);

            //Step 1. Find start vertex with max degree
            traversal.NewVertex = (sender, e) =>
            {
                long currDegree = graph.GetAdjVerticesCount(e);
                if (currDegree > maxDegree)
                {
                    vertexWithMaxDegree = e;
                    maxDegree           = currDegree;
                }
            };
            traversal.Run();

            Error error = Error.OK;

            if (vertexWithMaxDegree != INVALID_INDEX)
            {
                int[] vertices;
                graph.GetAdjVertices(vertexWithMaxDegree, out vertices);
                Array.Sort(vertices);

                bool permutationExists = true;
                while (permutationExists)
                {
                    NumerationHelper.Clear(graphNumeration);
                    foreach (var numerator in numerators)
                    {
                        numerator.Clear();
                    }
                    foreach (var data in verticesData)
                    {
                        data.Clear();
                    }

                    // Step 2. Numerate first vertices
                    NumerateFirstVertices(vertexWithMaxDegree, vertices);

                    // Step 3. Try to numerate adj vertices of the each vertex in the first vertices
                    foreach (var v in vertices)
                    {
                        error = NumerateAdjVertices(v);
                        if (error != Error.OK)
                        {
                            break;
                        }
                    }

                    // Step 4. Try to numerate other ambiguous vertices
                    if (error == Error.OK)
                    {
                        error = TryToNumerateVertices(GetEnumeratedVertices());
                    }
                    permutationExists = error != Error.OK && Helpers.GetNextPermutation(vertices);
                }
                return((int)error);
            }
            return((int)Error.INVALID_DIM);
        }