資源簡介
用gtk+2.0開發(fā)的一個小程序,用來顯示最短路徑,前臺界面用gtk+2.0開發(fā),后臺用flody算法,支持手工畫圖,動態(tài)修改圖的結(jié)構(gòu),包括修改頂點、邊長等等,可以在界面上顯示任意兩點之間的最短路徑,可以在圖上畫出來,用不同的顏色顯示。程序用eclipse開發(fā),可以在linux(fedora13)下運行,也可以到window下編譯運行。
代碼片段和文件信息
/*
?*?floyd.c
?*
?*??Created?on:?2011-3-4
?*??????Author:?root
?*/
#include?“graph.h“
#include?
#include?
#include?
#include?
#include?
extern?struct?graph_struct?*my_graph;
unsigned?int?malloc_error()
{
printf(“malloc?error:not?enough_memory\n“);
//后期改進:彈出對話框,在界面提示。
return?1;
}
int?init_array_double(double?***array?unsigned?int?base)
{
unsigned?int?i?=?0;
unsigned?int?j?=?0;
//申請空間,構(gòu)造動態(tài)二維數(shù)組
*array?=?(double?**)?malloc(sizeof(double*)?*?base);
if?(*array)
{
memset(*array?0?sizeof(double?*)?*?base);
for?(i?=?0;?i?se;?++i)
{
(*array)[i]?=?(double?*)?malloc(sizeof(double)?*?base);
if?((*array)[i])
{
memset((*array)[i]?0?sizeof(double)?*?base);
}?else
{
printf(“malloc?error:not?enough?memory\n“);
return?1;
}
}
}?else
{
printf(“malloc?error:not?enough?memory\n“);
return?1;
}
for?(i?=?0;?i?se;?++i)
for?(j?=?0;?j?se;?++j)
if?(i?==?j)
(*array)[i][j]?=?0;
else
(*array)[i][j]?=?DBL_MAX;
return?0;
}
int?init_array_int(unsigned?int?***array?unsigned?int?base)
{
unsigned?int?i?=?0;
unsigned?int?j?=?0;
//申請空間,構(gòu)造動態(tài)二維數(shù)組
*array?=?(unsigned?int?**)?malloc(sizeof(unsigned?int*)?*?base);
if?(*array)
{
memset(*array?0?sizeof(unsigned?int?*)?*?base);
for?(i?=?0;?i?se;?++i)
{
(*array)[i]?=?(unsigned?int?*)?malloc(sizeof(unsigned?int)?*?base);
if?((*array)[i])
{
memset((*array)[i]?0?sizeof(unsigned?int)?*?base);
}?else
{
printf(“malloc?error:not?enough?memory\n“);
return?1;
}
}
}?else
{
printf(“malloc?error:not?enough?memory\n“);
return?1;
}
for?(i?=?0;?i?se;?++i)
for?(j?=?0;?j?se;?++j)
{
(*array)[i][j]?=?INT_MAX;
}
return?0;
}
unsigned?int?init_graph()
{
my_graph?=?(struct?graph_struct?*)?malloc(sizeof(struct?graph_struct));
if?(!my_graph)
return?malloc_error();
memset(my_graph?0?sizeof(struct?graph_struct));
my_graph->point_cur_num?=?0;
my_graph->side_cur_num?=?0;
my_graph->point_max_num?=?10;/*初始10個頂點*/
my_graph->side_max_num?=?my_graph->point_max_num?*?(my_graph->point_max_num?+?1)?/?2;
my_graph->graph_point?=?(struct?graph_point?*)?malloc(sizeof(struct?graph_point)?*?my_graph->point_max_num);
if?(!my_graph->graph_point)
return?malloc_error();
memset(my_graph->graph_point?0?sizeof(struct?graph_point)?*?my_graph->point_max_num);
my_graph->graph_side?=?(struct?graph_side?*)?malloc(sizeof(struct?graph_side)?*?my_graph->side_max_num);
if?(!my_graph->graph_side)
return?malloc_error();
memset(my_graph->graph_side?0?sizeof(struct?graph_side)?*?my_graph->side_max_num);
return?0;
}
/**
?*?函數(shù)名稱:?flody
?*?函數(shù)參數(shù):?graph圖的鄰接矩陣,min_path?最短路徑長度,n頂點個數(shù),path路徑矩陣
?*?函數(shù)功能:?求解任意頂點間的最短路徑長度和路徑經(jīng)過的頂點
?*?返回值:?無
?*?*/
void?flody(double?**graph?double?**min_path?unsigned?n?unsigned?int?**path)
{
unsigned?int?i?=?0?j?=?0?k?=?0;
for?(i?=?0;?i? for?(j?=?0;?j? {
min_path[i][j]?=?graph[i][j];
path[i][j]?=?0;
}
for?(k?=?0;
評論
共有 條評論