Algoritmo de Floyd Warshall

Iniciado por Luisyoxd, 22 Febrero 2020, 02:30 AM

0 Miembros y 1 Visitante están viendo este tema.

Luisyoxd

tengo este codigo la verdad no estoy seguro de si es correcto en su totalidad, soy algo nuevo en esto.

3 archivos



*******************MatrizQ.cpp******************

Código (cpp) [Seleccionar]
#include "MatrizQ.h"

MatrizQ::MatrizQ(int tamanio)
{
this->tamanio=tamanio;
this->matriz=NULL;
matriz=new int*[tamanio];
for(int i=0;i<tamanio;i++)
{
matriz[i]=new int[tamanio];
}

for(int i=0;i<this->tamanio;i++)
{
cout<<endl;
for(int j=0;j<this->tamanio;j++)
{
matriz[i][j]=0;
}
}

}

MatrizQ::~MatrizQ()
{

}
int MatrizQ::dameTuTamanio()
{
cin>>this->tamanio;
return this->tamanio;
}

void MatrizQ::verificaTusDatos()
{
if(this->tamanio<0)
{
cout<<"el tamanio de la matriz no puede ser negativo, por favor ingresa otro valor: ";

}
}

void MatrizQ::pideleAlUsuariosTusDatos()
{
if(matriz!=NULL)
{
for(int i=0;i<this->tamanio;i++)
delete []matriz[i];
delete []matriz;
}

cout<<endl<<"Dame el orden de la matriz A"<<endl<<"tamanio: ";

do
{
this->dameTuTamanio();
this->verificaTusDatos();

}while(this->tamanio<0);


/**RESERVA DE MEMORIA DINAMICA*/
matriz=new int*[this->tamanio];
for(int i=0;i<=this->tamanio;i++)
{
matriz[i]=new int[this->tamanio];
}

/**LECTURA DE LOS DATOS DE LA MATRIZ*/
cout<<"ingresa los datos de la matriz"<<endl;
for(int i=0;i<this->tamanio;i++)
{
for(int j=0;j<this->tamanio;j++)
{
cout<<"ingresa el dato en la posicion ["<<i+1<<"]["<<j+1<<"]:";
cin>>matriz[i][j];
}
}
}


void MatrizQ::muestraTusDatos()
{

for(int i=0;i<this->tamanio;i++)
{
cout<<endl;
for(int j=0;j<this->tamanio;j++)
{
cout<<"["<<matriz[i][j]<<"] ";
}
}
cout<<endl;
}

int MatrizQ::dameTuDatoRC(int renglon, int columna)
{
renglon=renglon;
columna=columna;

if(renglon<0||columna<0)
{
cout<<"no hay renglones ni columnas negativas"<<endl;

}

return matriz[renglon][columna];
}

void MatrizQ::modificaTuDatoRC(int renglon, int columna, int valor)
{
renglon=renglon;
columna=columna;
this->matriz[renglon][columna]=valor;
}


MatrizQ& MatrizQ::operator=(MatrizQ Q)
{

    if(this!=&Q)
    {
        tamanio=Q.tamanio;
        delete [] this->matriz;
        this->tamanio=Q.tamanio;

        this->matriz = new int *[this->tamanio*this->tamanio];
        for(int i=0; i<this->tamanio; i++)
        {
        this->matriz[i] = new int[tamanio];
        for(int j=0; j<this->tamanio; j++)
        this->matriz[i][j] = Q.matriz[i][j];
        }

    }

    else
        {
            matriz=NULL;
            for(int i=0; i<this->tamanio; i++)
                {
            this->matriz[i] = new int[tamanio];
            for(int j=0; j<this->tamanio; j++)
            this->matriz[i][j] = Q.matriz[i][j];
        }
        }
/**++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
    for(int i=0;i<this->tamanio;i++)
    {
        for(int j=0;j<this->tamanio;j++)

        modificaTuDatoRC(i,j,Q.dameTuDatoRC(i,j));
    }
    return *this;

}




/*****************MatrizQ.h**************



Código (cpp) [Seleccionar]
#ifndef MATRIZQ_H
#define MATRIZQ_H
#include<iostream>
using namespace std;

class MatrizQ
{
public:
MatrizQ(int tamanio = 0);
~MatrizQ();////////////////////////////////////////////////
void pideleAlUsuariosTusDatos(void);
void muestraTusDatos(void);
int dameTuDatoRC(int renglon, int columna);
int dameTuTamanio(void);
void modificaTuDatoRC(int renglon, int columna, int valor);
MatrizQ& operator=(MatrizQ Q);//////////////////////////////////
friend void cam_min(MatrizQ **W, MatrizQ **Q0 );

private:
void verificaTusDatos(void);
void liberaMemoria(void);////////////////////////////
int** matriz;
int tamanio;
};

#endif // MATRIZQ_H


en el main no tengo nada porque intente hacer el algoritmo de warshall pero no se como hacerlo o como empezar.


pregunte y solo me dijeron que es una funcion que no pertenece a la clase pero que si utiliza la clase, la verdad no entendí
y tengo que programar este código pero no se como hacerlo , intente con objetos dinamicos, y otras formas pero no se como hacerlo, si me pudieran orientar a como ejecutarlo y en donde se los agradeceria mucho, URGE  tengo que entregar esto antes de las 12:00 am


*************** algoritmo a ejecutar

Algoritmo del camino mínimo
Un dígrafo con pesos G de M nodos está en memoria mediante una matriz de pesos W. Este
algoritmo encuentra la matriz Q tal que [I, J] es la longitud del camino mínimo del nodo VI al nodo
VJ. Infinito es un número muy grande y MIN es la función del valor mínimo.
1. [Iniciar Q]
Repetir Para I = 1, 2,..., M:
Repetir Para J = 1, 2,..., M:
Si W[I, J] = 0 entonces
Hacer Q[I, J] := Infinito
Sino
Hacer Q[I, J] := W[I, J]
[fin del bucle]
[fin del bucle]
2. [Actualizar Q:]
Repetir pasos 3 y 4 para K = 1,2,..., M:
3. Repetir paso 4 para I = 1,2,..., M:
4. Repetir para J = 1,2,..., M:
Hacer Q[I, J] := MIN( Q[I, J], Q[I, K] + Q[K , J])
[fin del bucle]
[fin del bucle]
[fin del bucle]
5. [Salir]




la matriz de pesos es ********************

[ 7 5 0 0 ]
[ 7 0 0 2 ]
[ 0 3 0 0 ]
[ 4 0 1 0 ]

MOD: Etiquetas GeSHi.

98Fran

#1
Si usas un metodo de la clase y esta clase no hereda de otra no hace falta usar clase::metodo() con metodo() sobra y hace el codigo más legible.
(Solo al llamar a la función, al definirla sí hace falta).

En caso de que herede y se llame igual y quieres especificar cual usar entonces sí.

Vale nada, intenta poner el codigo tabulado, que no se ve bien el código.

Consejo, cuando uses setter haz que retornen un bool tipo:

<code>bool Matriz::SetMatriz(int fil, int col) </code>