Пример #1
0
    public Vertex[] GetEulerTour()
    {
        // For all n - 1 edges, we take the edge down to its child and then, eventually, back up
        // to its parent. So each edge contributes 2 vertices to the tour, and we get the root
        // initially without using any edges, so that's 2*(n - 1) + 1 = 2n - 1 vertices.
        var eulerTour       = new Vertex[2 * VertexCount - 1];
        int eulerTourIndex  = -1;
        var verticesToVisit = new Stack <Vertex>();

        verticesToVisit.Push(Root);

        while (verticesToVisit.Count > 0)
        {
            var vertex = verticesToVisit.Peek();
            eulerTour[++eulerTourIndex] = vertex;

            // If the EulerTourInitialIndex is null, it's the first time we're visiting the vertex.
            if (!vertex.EulerTourInitialIndex.HasValue)
            {
                vertex.Depth = 1 + (vertex.Parent?.Depth ?? -1);
                vertex.EulerTourInitialIndex = eulerTourIndex;
            }

            if (vertex.EulerTourChildCounter == vertex.Children.Count)
            {
                verticesToVisit.Pop();
            }
            else
            {
                verticesToVisit.Push(vertex.Children[vertex.EulerTourChildCounter++]);
            }
        }

        return(eulerTour);
    }
Пример #2
0
    private RootedTree(int vertexCount, int rootID)
    {
        var vertices = new Vertex[vertexCount];

        for (int id = 0; id < vertexCount; ++id)
        {
            vertices[id] = new Vertex(this, id);
        }

        Vertices = vertices;
        Root     = vertices[rootID];
    }
Пример #3
0
 public MinimumDepthQueryObject(int index, Vertex value)
 {
     SegmentStartIndex  = SegmentEndIndex = index;
     MinimumDepthVertex = value;
 }