private void visitaBfs(int u, int[] cor) { cor[u] = cinza; this.d[u] = 0; Fila fila = new Fila(); fila.enfileira(u); while (!fila.vazia()) { int aux = (int)fila.desenfileira(); u = aux; if (!this.grafo.listaAdjVazia(u)) { Aresta a = this.grafo.primeiroListaAdj(u); while (a != null) { int v = a.v2; if (cor[v] == branco) { cor[v] = cinza; this.d[v] = this.d[u] + 1; this.antecessor[v] = u; fila.enfileira(v); } a = this.grafo.proxAdj(u); } } cor[u] = preto; } }
public void AdicionaAresta(Vertice v2, int peso) { Aresta a = new Aresta(this, v2, peso); Arestas.Add(a); //Adjacentes.Add(v2); }
public Aresta proxAdj(int v) { // Retorna a próxima aresta que o vértice v participa ou // null se a lista de adjacência de v estiver no fim Aresta item = (Aresta)this.adj[v].proximo(); return(item != null ? new Aresta(v, item.v2, item.peso) : null); }
public Aresta primeiroListaAdj(int v) { // Retorna a primeira aresta que o vértice v participa ou // null se a lista de adjacência de v for vazia Aresta item = (Aresta)this.adj[v].Primeiro(); return(item != null ? new Aresta(v, item.v2, item.peso) : null); }
public bool retiraAresta(int v1, int v2, int peso) { Aresta a = new Aresta(v1, v2, peso); Aresta item = (Aresta)this.adj[v1].retira(a); return(item != null ? true : false); }
public bool retiraAresta(int v1, int v2, int peso) { if (this.mat[v1, v2] == 0) { return(false); } else { Aresta aresta = new Aresta(v1, v2, this.mat[v1, v2]); this.mat[v1, v2] = 0; return(true); } }
public void imprime() { for (int i = 0; i < this.numVertices; i++) { Console.Write("Vertice " + i); Aresta item = (Aresta)this.adj[i].Primeiro(); while (item != null) { Console.Write(" -> " + item.v2 + " ( " + item.peso + " )"); item = (Aresta)this.adj[i].proximo(); } Console.WriteLine(); } }
public GrafoMatriz grafoTransposto() { GrafoMatriz grafoT = new GrafoMatriz(this.numVertices); for (int v = 0; v < this.numVertices; v++) { if (!this.listaAdjVazia(v)) { Aresta adj = this.primeiroListaAdj(v); while (adj != null) { grafoT.insereAresta(adj.v2, adj.v1, adj.peso); adj = this.proxAdj(v); } } } return(grafoT); }
private int visitaDfs(int u, int tempo, int[] cor) { cor[u] = cinza; this.d[u] = ++tempo; if (!this.grafo.listaAdjVazia(u)) { Aresta a = this.grafo.primeiroListaAdj(u); while (a != null) { int v = a.v2; if (cor[v] == branco) { this.antecessor[v] = u; tempo = this.visitaDfs(v, tempo, cor); } a = this.grafo.proxAdj(u); } } cor[u] = preto; this.t[u] = ++tempo; return(tempo); }
public void ConectarArestas(Vertice n_vertice, string NomeAresta, int Peso) { Aresta.Criar(n_vertice, this, NomeAresta, Peso); }
public void Atribuir(Aresta no) { _Arestas.Add(no); }
public void insereAresta(int v1, int v2, int peso) { Aresta a = new Aresta(v1, v2, peso); this.adj[v1].insere(a); }
static void Main(string[] args) { int[,] MatrizAdjacencia; int count = 0; Console.ForegroundColor = ConsoleColor.Red; List <Vertice> _verticesGrafo = new List <Vertice>(); List <string> _TodasArestas = new List <string>(); string[] lines = System.IO.File.ReadAllLines("Arquivo.txt"); foreach (string line in lines) { _TodasArestas.Add(line.ToArray()[3].ToString()); if (_verticesGrafo.Where(x => x.Nome == line.ToArray()[0].ToString()).FirstOrDefault() == null) { Vertice novo = new Vertice(line.ToArray()[0].ToString()); _verticesGrafo.Add(novo); } if (_verticesGrafo.Where(x => x.Nome == line.ToArray()[5].ToString()).FirstOrDefault() == null) { _verticesGrafo.Add(new Vertice(line.ToArray()[5].ToString())); } count++; } MatrizAdjacencia = new int[count, count]; Console.WriteLine("TODOS OS VÉRTICES DO GRAFO!"); foreach (Vertice obj in _verticesGrafo) { Console.WriteLine(obj.Nome); } foreach (string line in lines) { Vertice no = _verticesGrafo.Where(x => x.Nome == line.ToArray()[0].ToString()).FirstOrDefault(); Vertice no2 = _verticesGrafo.Where(x => x.Nome == line.ToArray()[5].ToString()).FirstOrDefault(); if (no.Arestas.Where(y => y.No1.Nome == no.Nome && y.No2.Nome == no2.Nome).FirstOrDefault() == null) { no.ConectarArestas(no2, line.ToArray()[3].ToString(), Convert.ToInt32(line.ToArray()[3].ToString())); } else if (no2.Arestas.Where(y => y.No1.Nome == no.Nome && y.No2.Nome == no2.Nome).FirstOrDefault() == null) { no2.ConectarArestas(no, line.ToArray()[3].ToString(), Convert.ToInt32(line.ToArray()[3].ToString())); } } //------------------------------------------------------------------------------------ // MATRIZ DE ADJACENCIA //------------------------------------------------------------------------------------ Console.WriteLine("\n\tMATRIZ DE ADJACÊNCIA"); foreach (Vertice obj in _verticesGrafo) { Console.Write("\t" + obj.Nome); } Console.WriteLine("\n"); int i = 0, j = 0; foreach (Vertice obj in _verticesGrafo) { Console.Write(obj.Nome); foreach (Vertice second in _verticesGrafo) { Aresta aux = obj.Arestas.Where(x => x.No1.Nome == second.Nome || x.No2.Nome == second.Nome).FirstOrDefault(); if (aux != null && obj.Nome != second.Nome) { MatrizAdjacencia[i, j] = aux.Peso; Console.Write("\t1"); } else { MatrizAdjacencia[i, j] = 0; Console.Write("\t0"); } aux = null; j++; } i++; j = 0; Console.WriteLine("\n"); } //------------------------------------------------------------------------------------ // MATRIZ DE INCIDÊNCIA //------------------------------------------------------------------------------------ Console.WriteLine("\n\tMATRIZ DE INCIDÊNCIA"); Console.Write(" "); foreach (string obj in _TodasArestas) { Console.Write(" " + obj); } Console.WriteLine("\n"); foreach (Vertice obj in _verticesGrafo) { Console.Write(obj.Nome); foreach (string second in _TodasArestas) { if (obj.Arestas.Where(x => x.NomeAresta == second).FirstOrDefault() != null) { Console.Write(" S"); } else { Console.Write(" N"); } } Console.WriteLine("\n"); } Vertice _GrauMaior = _verticesGrafo.OrderByDescending(y => y.Arestas.Count()).FirstOrDefault(); Console.WriteLine("VÉRTICE DE MAIOR GRAU: " + _GrauMaior.Nome + "\n"); //Console.WindowHeight = 800; foreach (Vertice obj in _verticesGrafo) { Console.Write(obj.Nome + " - GRAU: " + obj.Arestas.Count() + " - VIZINHO: "); foreach (Aresta _aresta in obj.Arestas) { if (_aresta.No2.Nome != obj.Nome) { Console.Write("\t" + _aresta.No2.Nome); } else { Console.Write("\t" + _aresta.No1.Nome); } } Console.WriteLine("\n"); } for (int a = 0; a < count; a++) { for (int b = 0; b < count; b++) { Console.Write("\t" + MatrizAdjacencia[a, b]); } Console.Write("\n"); } Dijkstra minimo = new Dijkstra(); minimo.TotalVertices = count - 1; minimo.Calcular(MatrizAdjacencia, 0, count - 1); Console.ReadLine(); //Console.Write(obj.Nome + " - " + obj.Arestas.f); //Console.WriteLine("VIZINHOS: ") /** * 1 3 * A ------ e ---C * | | / * | 6 4 | /2 * | | / * B------- D/ * 8 * * A = 0 * E = 1 * B = 2 * D = 3 * C = 4 * **/ }