Ejemplo n.º 1
0
        private void VisitVertex(Vertex vertex, VisitContext context)
        {
            Stack <Vertex> walkOrder = new Stack <Vertex>(new [] { vertex });

            while (walkOrder.Count > 0)
            {
                Vertex          walkVertex          = walkOrder.Pop();
                VertexVisitData walkVertexVisitData = context.VisitData[walkVertex];

                if (walkVertexVisitData.Color == Color.White)
                {
                    walkVertexVisitData.Color        = Color.Gray;
                    walkVertexVisitData.DiscoverTime = context.Time++;
                    walkOrder.Push(walkVertex);

                    Vertex[] neighbors = _graph.GetVertexNeighbors(walkVertex);
                    foreach (Vertex neighbor in neighbors)
                    {
                        VertexVisitData neighborVertexVisitData = context.VisitData[neighbor];
                        if (neighborVertexVisitData.Color == Color.White)
                        {
                            walkOrder.Push(neighbor);
                        }
                    }
                }
                else if (walkVertexVisitData.Color == Color.Gray)
                {
                    walkVertexVisitData.Color      = Color.Black;
                    walkVertexVisitData.FinishTime = context.Time++;
                }
            }
        }
Ejemplo n.º 2
0
        private List <Vertex> VisitVertex(Graph graph, Vertex vertex, VisitContext context)
        {
            List <Vertex>  componentContent = new List <Vertex>();
            Stack <Vertex> walkOrder        = new Stack <Vertex>(new [] { vertex });

            while (walkOrder.Count > 0)
            {
                Vertex          walkVertex            = walkOrder.Pop();
                VertexVisitData walkVisitedVertexData = context.VisitData[walkVertex];

                if (walkVisitedVertexData.Color == Color.White)
                {
                    componentContent.Add(walkVertex);
                    walkVisitedVertexData.Color = Color.Gray;

                    Vertex[] neighbors = graph.GetVertexNeighbors(walkVertex);
                    foreach (Vertex neighbor in neighbors)
                    {
                        walkOrder.Push(neighbor);
                    }
                }
            }

            return(componentContent);
        }
Ejemplo n.º 3
0
        private List <List <Vertex> > FindComponentsInTransponesGraph(
            Graph transponesGraph,
            Dictionary <Vertex, int> vertexFinishTime
            )
        {
            List <List <Vertex> > components = new List <List <Vertex> >();
            VisitContext          context    = new VisitContext
            {
                Time      = 1,
                VisitData = _graph
                            .Vertexes
                            .ToDictionary(v => v, v => new VertexVisitData(v))
            };

            Vertex[] orderedVertexes = _graph.Vertexes
                                       .OrderByDescending(v => vertexFinishTime[v])
                                       .ToArray();

            foreach (Vertex vertex in orderedVertexes)
            {
                VertexVisitData visitData = context.VisitData[vertex];
                if (visitData.Color == Color.White)
                {
                    List <Vertex> component = VisitVertex(transponesGraph, visitData.Vertex, context);
                    components.Add(component);
                }
            }

            return(components);
        }
Ejemplo n.º 4
0
        public Dictionary <Vertex, VertexVisitData> VisitGraph()
        {
            VisitContext context = new VisitContext
            {
                Time      = 1,
                VisitData = CreateInitalVertexVisitData()
            };

            foreach (Vertex vertex in _graph.Vertexes)
            {
                VertexVisitData visitData = context.VisitData[vertex];
                if (visitData.Color == Color.White)
                {
                    VisitVertex(visitData.Vertex, context);
                }
            }

            return(context.VisitData);
        }