Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
75
Task/Floyd-Warshall-algorithm/C/floyd-warshall-algorithm-1.c
Normal file
75
Task/Floyd-Warshall-algorithm/C/floyd-warshall-algorithm-1.c
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
#include<limits.h>
|
||||
#include<stdlib.h>
|
||||
#include<stdio.h>
|
||||
|
||||
typedef struct{
|
||||
int sourceVertex, destVertex;
|
||||
int edgeWeight;
|
||||
}edge;
|
||||
|
||||
typedef struct{
|
||||
int vertices, edges;
|
||||
edge* edgeMatrix;
|
||||
}graph;
|
||||
|
||||
graph loadGraph(char* fileName){
|
||||
FILE* fp = fopen(fileName,"r");
|
||||
|
||||
graph G;
|
||||
int i;
|
||||
|
||||
fscanf(fp,"%d%d",&G.vertices,&G.edges);
|
||||
|
||||
G.edgeMatrix = (edge*)malloc(G.edges*sizeof(edge));
|
||||
|
||||
for(i=0;i<G.edges;i++)
|
||||
fscanf(fp,"%d%d%d",&G.edgeMatrix[i].sourceVertex,&G.edgeMatrix[i].destVertex,&G.edgeMatrix[i].edgeWeight);
|
||||
|
||||
fclose(fp);
|
||||
|
||||
return G;
|
||||
}
|
||||
|
||||
void floydWarshall(graph g){
|
||||
int processWeights[g.vertices][g.vertices], processedVertices[g.vertices][g.vertices];
|
||||
int i,j,k;
|
||||
|
||||
for(i=0;i<g.vertices;i++)
|
||||
for(j=0;j<g.vertices;j++){
|
||||
processWeights[i][j] = SHRT_MAX;
|
||||
processedVertices[i][j] = (i!=j)?j+1:0;
|
||||
}
|
||||
|
||||
for(i=0;i<g.edges;i++)
|
||||
processWeights[g.edgeMatrix[i].sourceVertex-1][g.edgeMatrix[i].destVertex-1] = g.edgeMatrix[i].edgeWeight;
|
||||
|
||||
for(i=0;i<g.vertices;i++)
|
||||
for(j=0;j<g.vertices;j++)
|
||||
for(k=0;k<g.vertices;k++){
|
||||
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];
|
||||
}
|
||||
}
|
||||
|
||||
printf("pair dist path");
|
||||
for(i=0;i<g.vertices;i++)
|
||||
for(j=0;j<g.vertices;j++){
|
||||
if(i!=j){
|
||||
printf("\n%d -> %d %3d %5d",i+1,j+1,processWeights[i][j],i+1);
|
||||
k = i+1;
|
||||
do{
|
||||
k = processedVertices[k-1][j];
|
||||
printf("->%d",k);
|
||||
}while(k!=j+1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argC,char* argV[]){
|
||||
if(argC!=2)
|
||||
printf("Usage : %s <file containing graph data>");
|
||||
else
|
||||
floydWarshall(loadGraph(argV[1]));
|
||||
return 0;
|
||||
}
|
||||
121
Task/Floyd-Warshall-algorithm/C/floyd-warshall-algorithm-2.c
Normal file
121
Task/Floyd-Warshall-algorithm/C/floyd-warshall-algorithm-2.c
Normal file
|
|
@ -0,0 +1,121 @@
|
|||
#include <limits.h>
|
||||
#include <gadget/gadget.h>
|
||||
|
||||
LIB_GADGET_START
|
||||
|
||||
/* algunos datos globales */
|
||||
int vertices,edges;
|
||||
|
||||
/* algunos prototipos */
|
||||
F_STAT DatosdeArchivo( const char *cFile);
|
||||
int * CargaMatriz(int * mat, DS_ARRAY * mat_data, const char * cFile, F_STAT stat );
|
||||
int * CargaGrafo(int * graph, DS_ARRAY * graph_data, const char *cFile);
|
||||
void Floyd_Warshall(int * graph, DS_ARRAY graph_data);
|
||||
|
||||
/* bloque principal */
|
||||
Main
|
||||
if ( Arg_count != 2 ){
|
||||
Msg_yellow("Modo de uso:\n ./floyd <archivo_de_vertices>\n");
|
||||
Stop(1);
|
||||
}
|
||||
Get_arg_str (cFile,1);
|
||||
Set_token_sep(' ');
|
||||
Cls;
|
||||
if(Exist_file(cFile)){
|
||||
New array graph as int;
|
||||
graph = CargaGrafo( pSDS(graph), cFile);
|
||||
if(graph){
|
||||
/* calcula Floyd-Warshall */
|
||||
Print "Vertices=%d, edges=%d\n",vertices,edges;
|
||||
|
||||
Floyd_Warshall( SDS(graph) ); Prnl;
|
||||
|
||||
Free array graph;
|
||||
}
|
||||
|
||||
}else{
|
||||
Msg_redf("No existe el archivo %s",cFile);
|
||||
}
|
||||
Free secure cFile;
|
||||
End
|
||||
|
||||
void Floyd_Warshall( RDS(int,graph) ){
|
||||
|
||||
Array processedVertices as int(vertices,vertices);
|
||||
Fill array processWeights as int(vertices,vertices) with SHRT_MAX;
|
||||
|
||||
int i,j,k;
|
||||
Range for processWeights [0:1:vertices, 0:1:vertices ];
|
||||
|
||||
Compute_for( processWeights, i,j,
|
||||
$processedVertices[i,j] = (i!=j)?j+1:0;
|
||||
)
|
||||
|
||||
#define VERT_ORIG 0
|
||||
#define VERT_DEST 1
|
||||
#define WEIGHT 2
|
||||
|
||||
Iterator up i [0:1:edges] {
|
||||
$2processWeights[ $graph[i,VERT_ORIG]-1, $graph[i,VERT_DEST]-1 ] = $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_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 array processWeights, processedVertices;
|
||||
}
|
||||
|
||||
F_STAT DatosdeArchivo( const char *cFile){
|
||||
return Stat_file(cFile);
|
||||
}
|
||||
|
||||
int * CargaMatriz( pRDS(int, mat), const char * cFile, F_STAT stat ){
|
||||
return Load_matrix( SDS(mat), cFile, stat);
|
||||
}
|
||||
|
||||
int * CargaGrafo( pRDS(int, graph), const char *cFile){
|
||||
|
||||
F_STAT dataFile = DatosdeArchivo(cFile);
|
||||
if(dataFile.is_matrix){
|
||||
|
||||
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 ){
|
||||
/* obtengo vertices = 4 y edges = 5 */
|
||||
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] ) );
|
||||
}else{
|
||||
Msg_redf("Archivo \"%s\" no ha podido ser cargado",cFile);
|
||||
}
|
||||
|
||||
}else{
|
||||
Msg_redf("Archivo \"%s\" no es cuadrado",cFile);
|
||||
}
|
||||
return graph;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue