Ejemplo n.º 1
0
        public override IEnumerable<string> Executar(Grafo grafo)
        {
            grafo.Direcionado = true;

            var retorno = new List<string>();
            Dictionary<string, Vertice> Q = new Dictionary<string, Vertice>();
            var primeiro = grafo.Vertices.First();
            Q.Add(primeiro.Key, primeiro.Value);
            while (Q.Any())
            {
                var U = Q.First();
                Q.Remove(Q.Keys.First());
                foreach (var vertice in grafo.GetAdj(U.Key))
                {
                    if (vertice.Value.Cor == CoresEnum.Branco)
                    {
                        vertice.Value.Descoberta = U.Value.Descoberta + 1;
                        vertice.Value.Pai = U.Value;
                        vertice.Value.Cor = CoresEnum.Cinza;
                        Q.Add(vertice.Key, vertice.Value);
                    }
                }
                retorno.Add(String.Format("{0} {1} {2}", grafo.Vertices.First().Key, U.Value.Id, U.Value.Descoberta));
                U.Value.Cor = CoresEnum.Preto;
            }

            return retorno;
        }
Ejemplo n.º 2
0
        public override IEnumerable<string> Executar(Grafo grafo)
        {
            var retorno = new List<string>();
            grafo.Direcionado = true;
            initialize(grafo);
            var s = grafo.Vertices.First();
            s.Value.Descoberta = 0;
            Dictionary<string, Vertice> q = new Dictionary<string, Vertice>();
            List<Vertice> w = new List<Vertice>();
            q = grafo.Vertices;
            while (q.Any())
            {
                q.OrderBy(e => e.Value.Descoberta).ToList();
                var u = q.First();
                q.Remove(q.Keys.First());

                foreach (var vertice in grafo.GetAdj(u.Value.Id))
                {
                    var aresta = grafo.Arestas
                        .Where(e => e.Origem.Equals(u.Key)
                                 && e.Destino.Equals(vertice.Key))
                        .FirstOrDefault();
                    relax(u.Value, vertice.Value, aresta.Peso);

                }

                if (u.Value.Descoberta != Int32.MaxValue)
                    retorno.Add(String.Format("{0} {1} {2}", s.Value.Id, u.Value.Id, u.Value.Descoberta));

            }
            return retorno;
        }