Floyd-Warshall algorithm: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
m (→{{header|C}}) |
||
Line 1,370: | Line 1,370: | ||
</pre> |
</pre> |
||
==={{libheader|Gadget}}=== |
|||
VERSION 2. Usando una librería experimental, que pronto será liberada en GitHub, desarrollé la versión anterior del programa. La librería trabaja con memoria dinámica, tanto para strings, como para arrays de un tipo y multitipo. En el ejemplo, se usan funciones para arrays de un tipo. Lo que busco con esta librería experimental, es simplificar la programación, elevar el nivel de abstracción del lenguaje C (un poco), y lograr código elegante. |
|||
VERSION 2. Using Gadget, an a "C" library. |
|||
<syntaxhighlight lang="c"> |
<syntaxhighlight lang="c"> |
||
⚫ | |||
#include |
#include <limits.h> |
||
⚫ | |||
LIB_GADGET_START |
|||
/* algunos datos globales */ |
/* algunos datos globales */ |
||
Line 1,379: | Line 1,384: | ||
/* algunos prototipos */ |
/* algunos prototipos */ |
||
F_STAT DatosdeArchivo( char *cFile); |
F_STAT DatosdeArchivo( const char *cFile); |
||
⚫ | |||
void SeteaRangosLectura(F_STAT dataFile); |
|||
int * |
int * CargaGrafo(int * graph, DS_ARRAY * graph_data, const char *cFile); |
||
⚫ | |||
void Floyd_Warshall(int * graph, DS_ARRAY graph_data); |
void Floyd_Warshall(int * graph, DS_ARRAY graph_data); |
||
/* bloque principal */ |
/* bloque principal */ |
||
Main |
Main |
||
if ( Arg_count != 2 ){ |
|||
⚫ | |||
Msg_yellow("Modo de uso:\n ./floyd <archivo_de_vertices>\n"); |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
Cls; |
Cls; |
||
if( |
if(Exist_file(cFile)){ |
||
New |
New array graph as int; |
||
graph = CargaGrafo( |
graph = CargaGrafo( pSDS(graph), cFile); |
||
if(graph){ |
if(graph){ |
||
/* calcula Floyd-Warshall */ |
/* calcula Floyd-Warshall */ |
||
Print "Vertices=%d, edges=%d\n",vertices,edges; |
|||
Floyd_Warshall(graph |
Floyd_Warshall( SDS(graph) ); Prnl; |
||
Free |
Free array graph; |
||
} |
} |
||
}else{ |
}else{ |
||
Msg_redf("No existe el archivo %s",cFile); |
|||
} |
} |
||
Free |
Free secure cFile; |
||
End |
End |
||
void Floyd_Warshall(int |
void Floyd_Warshall( RDS(int,graph) ){ |
||
Array |
Array processedVertices as int(vertices,vertices); |
||
Fill array processWeights as int(vertices,vertices) with SHRT_MAX; |
|||
⚫ | |||
ROWS 0:1:vertices-1; COLS 0:1:vertices-1; |
|||
int i,j,k; |
|||
Range for processWeights [0:1:vertices, 0:1:vertices ]; |
|||
⚫ | |||
Compute_for( processWeights, i,j, |
|||
⚫ | |||
⚫ | |||
#define VERT_ORIG 0 |
#define VERT_ORIG 0 |
||
Line 1,421: | Line 1,432: | ||
#define WEIGHT 2 |
#define WEIGHT 2 |
||
Iterator up i [0:1:edges] { |
|||
$2processWeights[ $graph[i,VERT_ORIG]-1, $graph[i,VERT_DEST]-1 ] = $graph[i,WEIGHT]; |
|||
IterVec( i, |
|||
⚫ | |||
int vert_origen = Cell(graph,i,VERT_ORIG)-1; |
|||
int vert_destino = Cell(graph,i,VERT_DEST)-1; |
|||
Cell( processWeights, vert_origen, vert_destino ) = Cell( graph,i,WEIGHT) ); |
|||
Compute_for (processWeights,i,j, |
|||
Iterator up k [0:1:vertices] { |
|||
⚫ | |||
if( $processWeights[j,i] + $processWeights[i,k] < $processWeights[j,k] ) |
|||
{ |
|||
$processWeights[j,k] = $processWeights[j,i] + $processWeights[i,k]; |
|||
$processedVertices[j,k] = $processedVertices[j,i]; |
|||
} |
|||
} ); |
|||
Print "pair dist path"; |
|||
// ya existen rangos definios para "processWeights": |
|||
COMPUTE_MAT ( if(i!=j) |
|||
Compute_for(processWeights, i, j, |
|||
⚫ | |||
if(i!=j) |
|||
{ |
|||
Print "\n%d -> %d %3d %5d", i+1, j+1, $processWeights[i,j], i+1; |
|||
int k = i+1; |
|||
do{ |
|||
k = $processedVertices[k-1,j]; |
|||
Print " -> %d", k; |
|||
); |
}while(k!=j+1); |
||
} |
|||
); |
|||
Free |
Free array processWeights, processedVertices; |
||
} |
} |
||
F_STAT DatosdeArchivo( char *cFile){ |
F_STAT DatosdeArchivo( const char *cFile){ |
||
return |
return Stat_file(cFile); |
||
} |
} |
||
int * CargaMatriz( pRDS(int, mat), const char * cFile, F_STAT stat ){ |
|||
void SeteaRangosLectura(F_STAT df){ |
|||
⚫ | |||
ROWS 0:1:df.total_lines-1 ; |
|||
COLS 0:1:df.max_tokens_per_line-1; |
|||
PAGS NONE; |
|||
} |
} |
||
int * |
int * CargaGrafo( pRDS(int, graph), const char *cFile){ |
||
⚫ | |||
} |
|||
int * CargaGrafo(int * graph, DS_ARRAY * graph_data, char *cFile){ |
|||
F_STAT dataFile = DatosdeArchivo(cFile); |
F_STAT dataFile = DatosdeArchivo(cFile); |
||
if(dataFile.is_matrix){ |
if(dataFile.is_matrix){ |
||
⚫ | |||
SeteaRangosLectura(dataFile); |
|||
Range ptr graph [0:1:dataFile.total_lines-1, 0:1:dataFile.max_tokens_per_line-1]; |
|||
graph = CargaMatriz( SDS(graph), cFile, dataFile); |
|||
if( graph ){ |
if( graph ){ |
||
/* obtengo vertices y edges */ |
/* obtengo vertices = 4 y edges = 5 */ |
||
edges = dataFile.total_lines; |
edges = dataFile.total_lines; |
||
Block( vertices, Range ptr graph [ 0:1:pRows(graph), 0:1:1 ]; |
|||
DS_MAXMIN maxNode = Max_array( SDS(graph) ); |
|||
Out_int( $graph[maxNode.local] ) ); |
|||
ROWS 0:1:graph_data->rows-1; // busque en todas las filas |
|||
PAGS NONE; // no hay páginas |
|||
DS_MAXMIN maxNode = MaxArray_int( graph,graph_data ); |
|||
OutInt( Cell(graph, maxNode.local) ) ); |
|||
}else{ |
}else{ |
||
Msg_redf("Archivo \"%s\" no ha podido ser cargado",cFile); |
|||
} |
} |
||
}else{ |
}else{ |
||
Msg_redf("Archivo \"%s\" no es cuadrado",cFile); |
|||
} |
} |
||
return graph; |
return graph; |