Exemple #1
0
        public void substituirAresta(Aresta <T> antigaAresta, Aresta <T> novaAresta)
        {
            int index = arestaList.FindIndex(a => a.from == antigaAresta.from && a.to == antigaAresta.to);

            if (index > -1)
            {
                arestaList[index] = novaAresta;
            }
        }
Exemple #2
0
        public void Dijkstra(Vertice <T> from, Vertice <T> to)
        {
            List <Vertice <T> > listaNaoVisitados = new List <Vertice <T> >();
            List <Aresta <T> >  arestas           = Arestas();
            string path      = "";
            int    custo     = 0;
            bool   Eprimeiro = true;

            foreach (Vertice <T> v in verticeSet)
            {
                listaNaoVisitados.Add(v);
            }
            foreach (Aresta <T> a in arestas)
            {
                a.Cost = int.MaxValue;
            }
            arestas.ElementAt(0).Cost = 0;
            listaNaoVisitados.Remove(from);
            while (listaNaoVisitados.Count != 0)
            {
                int         smallest                 = 0;
                Vertice <T> visitado                 = new Vertice <T>();
                Aresta <T>  arestavisitada           = new Aresta <T>(null, null);
                Aresta <T>  arestavisitadaFinalWhile = new Aresta <T>(null, null);

                foreach (Vertice <T> v in from.Neighbors)
                {
                    if (Eprimeiro)
                    {
                        if (arestas.Find(a => a.from == from && a.to == v).Cost < smallest)
                        {
                            smallest       = arestas.Find(a => a.from == from && a.to == v).Cost;
                            visitado       = arestas.Find(a => a.from == from && a.to == v).to;
                            arestavisitada = arestas.Find(a => a.from == from && a.to == v);
                        }
                    }
                    else
                    {
                        if (arestas.Find(a => a.from == from && a.to == v).Cost + arestavisitadaFinalWhile.Cost < arestas.Find(a => a.from == arestavisitadaFinalWhile.to && a.to == v).Cost)
                        {
                            smallest       = arestas.Find(a => a.from == from && a.to == v).Cost;
                            visitado       = arestas.Find(a => a.from == from && a.to == v).to;
                            arestavisitada = arestas.Find(a => a.from == from && a.to == v);
                        }
                    }
                }
                arestavisitadaFinalWhile = arestavisitada;
                path  = path + GetAresta(arestavisitadaFinalWhile).ToString();
                custo = custo + arestavisitada.Cost;
                listaNaoVisitados.Remove(from);
                from      = visitado;
                Eprimeiro = false;
            }
            Console.WriteLine(path);
        }
Exemple #3
0
 public string GetAresta(Aresta <T> aresta)
 {
     try
     {
         return(aresta.from.Value.ToString() + " - " + aresta.to.Value.ToString());
     }
     catch
     {
         return("");
     }
 }
Exemple #4
0
 public bool eDirecionado(Aresta <T> aresta)
 {
     try
     {
         bool duplaDirecao = (aresta.from.Neighbors.Contains(aresta.to)) && (aresta.to.Neighbors.Contains(aresta.from));
         if (duplaDirecao)
         {
             return(false);
         }
         return(true);
     }
     catch { return(true); }
 }
Exemple #5
0
 public Vertice <T> oposto(Vertice <T> vertice, Aresta <T> aresta)
 {
     if (eAdjacente(vertice, aresta.from))
     {
         return(aresta.from);
     }
     else if (eAdjacente(vertice, aresta.to))
     {
         return(aresta.to);
     }
     else
     {
         throw new Exception {
         }
     };
 }
Exemple #6
0
 public Vertice <T>[] finalVertices(Aresta <T> aresta)
 {
     return(new Vertice <T>[2] {
         aresta.from, aresta.to
     });
 }
Exemple #7
0
        static void Main(string[] args)
        {
            Grafo <string> grafo = new Grafo <string>();

            Vertice <string> v1 = new Vertice <string>("Index.htm");
            Vertice <string> v2 = new Vertice <string>("Home.htm");
            Vertice <string> v3 = new Vertice <string>("Contact.htm");
            Vertice <string> v4 = new Vertice <string>("About.htm");

            //InserirVertice()
            grafo.InserirVertice(v1);
            grafo.InserirVertice(v2);
            grafo.InserirVertice(v3);
            grafo.InserirVertice(v4);


            var lista = grafo.Vertices().ToList();

            foreach (Node <string> vertice in lista)
            {
                Console.WriteLine(vertice.Value.ToString());
            }
            Console.WriteLine();

            //InserirVerticeDirecionado()
            Aresta <string> aresta1 = grafo.InserirAresta(v1, v2, 1);
            Aresta <string> aresta2 = grafo.InserirAresta(v2, v3, 2);
            Aresta <string> aresta3 = grafo.InserirAresta(v1, v3, 3);
            Aresta <string> aresta4 = grafo.InserirAresta(v1, v4, 4);

            //Vertice<string> verticeSubst = new Vertice<string>("Gallery.htm");
            //grafo.substituirVertice(v1, verticeSubst);
            //bool verticeRemovido = grafo.RemoverVertice(v2);
            Console.WriteLine("Lista de arestas: ");
            List <Aresta <string> > listaArestas = grafo.Arestas();

            foreach (Aresta <string> arestaItem in listaArestas)
            {
                Console.WriteLine(grafo.GetAresta(arestaItem));
                if (!grafo.eDirecionado(arestaItem))
                {
                    Console.WriteLine(arestaItem.to.Value.ToString() + " - " + arestaItem.from.Value.ToString());
                }
            }

            //eAdjacente()
            if (grafo.eAdjacente(v1, v2))
            {
                Console.WriteLine(v1.Value.ToString() + " é adjacente de " + v2.Value.ToString());
            }
            else
            {
                Console.WriteLine(v1.Value.ToString() + " não é adjacente de " + v2.Value.ToString());
            }

            /*
             * //removerVertice
             * //
             * //oposto
             * Console.WriteLine(grafo.oposto(v1, aresta1).Value.ToString());
             * //finalVertice
             * Vertice<string>[] verticesFinais = grafo.finalVertices(aresta1);
             * Console.WriteLine(verticesFinais[0].Value.ToString()+ " - " +verticesFinais[1].Value.ToString());
             * //Subistuir Vertice
             * Console.WriteLine("\nNova Lista de Vertices: ");
             * Vertice<string> verticeSubst =new Vertice<string>("Galery.htm");
             * grafo.substituirVertice(v1, verticeSubst);
             * var listaSubstituidos = grafo.Vertices().ToList();
             * foreach (Vertice<string> vertice in lista)
             * {
             *  Console.WriteLine(vertice.Value.ToString());
             * }
             * Aresta<string> arestaSubst = new Aresta<string>(v3, v4);
             * grafo.substituirAresta(aresta1, arestaSubst);
             * Console.WriteLine("\nNova Lista de Arestas");
             * List<Aresta<string>> listaSubst = grafo.Arestas();
             * foreach (Aresta<string> arestaItem in listaSubst)
             * {
             *  Console.WriteLine(Aresta<string>.GetAresta(arestaItem));
             *  if (!grafo.eDirecionado(arestaItem))
             *      Console.WriteLine(arestaItem.to.Value.ToString() + " - " + arestaItem.from.Value.ToString());
             * }*/
            //Aresta<string> arestaSubst = new Aresta<string>(v3, v4);
            //grafo.substituirAresta(aresta1, arestaSubst);

            Console.WriteLine("\nMatriz de Custo");
            grafo.MostrarMatrizdeCusto();
            Console.WriteLine("\nMatriz de Adjacencia");
            grafo.MostrarMatrizdeAdjacencia();
            grafo.eEuleriano();
            //grafo.Dijkstra(v1, v2);
            Console.Read();
        }