Exemplo n.º 1
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public GrafoM<N> KruskalGrafoMatriz(IGrafo<N> grafo) throws Excepciones.ExcepcionAristaImposible, Excepciones.ExcepcionNodoNoEncontrado
		public virtual GrafoM<N> KruskalGrafoMatriz(IGrafo<N> grafo)
		{
			GrafoM<N> graph = null;

			if (grafo.esDirigido())
			{
				graph = new GrafoM<N>(true, true);
			}
			else
			{
				graph = new GrafoM<N>(false, true);
			}

			List<Arista<N>> aristas = grafo.generarAristas();

			aristas.sort(null);

			List<N> nodos = grafo.darValorNodos();

			for (int i = 0; i < nodos.Count; i++)
			{
				graph.agregarNodo(nodos[i]);
			}

			List<Conjunto<N>> conjuntos = new List<Conjunto<N>>();

			for (int i = 0; i < nodos.Count; i++)
			{
				conjuntos.Add(new Conjunto<N>(nodos[i]));
			}

			for (int i = 0; i < aristas.Count && conjuntos.Count > 1; i++)
			{
				int n = conjuntos.Count;
				Arista<N> arista = aristas[i];
				N nodo1 = arista.NodoInicial.Valor;
				N nodo2 = arista.NodoFinal.Valor;
				for (int j = 0; j < conjuntos.Count; j++)
				{
					if (conjuntos[j].existeNodoEnElConjunto(nodo1) && !conjuntos[j].existeNodoEnElConjunto(nodo2))
					{
Exemplo n.º 2
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public GrafoL<N> KruskalGrafoLista(IGrafo<N> grafo) throws Excepciones.ExcepcionAristaImposible
		public virtual GrafoL<N> KruskalGrafoLista(IGrafo<N> grafo)
		{

			GrafoL<N> graph = null;

			if (grafo.esDirigido())
			{
				graph = new GrafoL<N>(true, true);
			}
			else
			{
				graph = new GrafoL<N>(false, true);
			}

			List<Arista<N>> aristas = grafo.generarAristas();

			aristas.sort(null);

			List<N> nodos = grafo.darValorNodos();

			for (int i = 0; i < nodos.Count; i++)
			{
				graph.agregarNodo(nodos[i]);
			}

			List<Conjunto<N>> conjuntos = new List<Conjunto<N>>();

			for (int i = 0; i < nodos.Count; i++)
			{
				conjuntos.Add(new Conjunto<N>(nodos[i]));
			}

			for (int i = 0; i < aristas.Count && conjuntos.Count > 1; i++)
			{
				int n = conjuntos.Count;
				Arista<N> arista = aristas[i];
				N nodo1 = arista.NodoInicial.Valor;
				N nodo2 = arista.NodoFinal.Valor;
				for (int j = 0; j < conjuntos.Count; j++)
				{
					if (conjuntos[j].existeNodoEnElConjunto(nodo1) && !conjuntos[j].existeNodoEnElConjunto(nodo2))
					{
						Conjunto<N> c1 = conjuntos[j];
						Conjunto<N> c2 = null;
						int l = -1;
						for (int h = 0; c2 == null && h < conjuntos.Count; h++)
						{
							if (conjuntos[h].existeNodoEnElConjunto(nodo2))
							{
								c2 = conjuntos[h];
								l = h;
							}
						}
						if (c2 != null)
						{
							c1.unirConjuntos(c2);
							conjuntos.RemoveAt(l);
						}
					}
				}
				if (n > conjuntos.Count)
				{
					graph.agregarArista(nodo1, nodo2, arista.Peso);
				}
			}

			return graph;

		}