【发布时间】:2019-04-18 20:07:20
【问题描述】:
例子:
degree = [2,3,3,2,2,2]
vertex = [1,2,3,4,5,6]
顶点 1 的度数为 2,顶点 2 的度数为 3,等等...
我需要这个解决方案vertex = [2,3,4,5,6,1]。我想使用度数来更改顶点。 (最后4个数字的顺序无关紧要(具有相同的度数。
我有更多的代码,但我认为这样就可以了。
谢谢!
typedef struct {
u32 Orden;
u32 Grado;
} ParGradoOrden;
int comGrado (const void * a, const void * b) {
const u32 x = ((ParGradoOrden *) a)->Grado;
const u32 y = ((ParGradoOrden *) b)->Grado;
const u32 xx = ((ParGradoOrden *) a)->Orden;
const u32 yy = ((ParGradoOrden *) b)->Orden;
if (x < y)
return 1;
else if (x > y)
return -1;
if (xx < yy)
return -1;
else if (xx > yy)
return 1;
return 0;
}
qsort(G->vertices, n, sizeof(ParGradoOrden), comGrado);
Grafostv* ConstruccionDelGrafo()
{
Grafostv* G = (Grafostv*)malloc(sizeof(Grafostv));
u32 n = 0; //number of vertexs
u32 m = 0; //number of edges
u32 v = 0; //vertex name
u32 w = 0; // vertex name
int c = 0; //auxiliar char needed for fgetc function
char prev = 'a';
char edge[50] = {0};
G->m = m;
G->n = n;
G->orden = (u32*)malloc(sizeof(u32) * n);
G->grados = (u32*)malloc(sizeof(u32) * n);
G->vertices = (u32*)malloc(sizeof(u32*) * n);
for (u32 i = 0; i < 2*m; ++i)
G->vecinos[i] = (u32*)malloc(sizeof(u32)*2);
for (u32 i = 0; i < 2*m; i += 2)
{
if(scanf(" %c %u %u",&prev, &v, &w) != 3 || prev != 'e')
{
free(G->color);
free(G->orden);
free(G->grados);
return NULL;
}
G->vecinos[i][0] = v;
G->vecinos[i][1] = w;
G->vecinos[i+1][0] = w;
G->vecinos[i+1][1] = v;
}
qsort(G->vecinos, 2*m, 2*sizeof(u32), compare);
G->vertices[0] = G->vecinos[0][0];
copiarAVertice(G->vertices,G->vecinos,G->grados, G->indEnVecinos, 2*m);
memset(G->visitados,0,n*sizeof(u32));
memcpy(G->orden,G->vertices,n*sizeof(u32));
G->max = Greedy(G);
printf("terminé Greedy\n");
return G;
}
【问题讨论】:
-
您是否特别需要结果为
[2,3,4,5,6,1],或者其他按程度降序排列的替代方案也可以,例如[2,3,1,4,5,6]? -
您可以使用
struct的一个数组而不是两个数组。或者您可以使用顶点数组(传递给qsort)数据来索引度数数组,该数组仍然未排序。例如degree[ vertex[n] - 1] -
是的@JohnBollinger 就像你说的那样。它是相同的 [2,3,1,4,5,6] 或 [2,3,4,5,6,1] 等...