int minimum_disjoint_path_covering(graph_matrix g) {//简单有向图G有g_cnt个节点,下标从0到g_cnt-1 //若从节点i指向节点j存在有向边则g_m[i][j]为1 //同一节点的g_m值为0,不相连节点的g_m值为INF //返回最小不相交路径覆盖数 bipartite b; int xmatch[MAX], ymatch[MAX]; memset(xmatch, -1, MAX * sizeof(int)); memset(ymatch, -1, MAX * sizeof(int)); construct_b(g, b); return(g.g_cnt - hopcroft_karp(b, xmatch, ymatch)); }
int compute_y_coords(vtx_data * graph, int n, double *y_coords, int max_iterations) { /* Find y coords of a directed graph by solving L*x = b */ int i, j, rv = 0; double *b = N_NEW(n, double); double tol = hierarchy_cg_tol; int nedges = 0; float *uniform_weights; float *old_ewgts = graph[0].ewgts; construct_b(graph, n, b); init_vec_orth1(n, y_coords); for (i = 0; i < n; i++) { nedges += graph[i].nedges; } /* replace original edge weights (which are lengths) with uniform weights */ /* for computing the optimal arrangement */ uniform_weights = N_GNEW(nedges, float); for (i = 0; i < n; i++) { graph[i].ewgts = uniform_weights; uniform_weights[0] = (float) -(graph[i].nedges - 1); for (j = 1; j < graph[i].nedges; j++) { uniform_weights[j] = 1; } uniform_weights += graph[i].nedges; } if (conjugate_gradient(graph, y_coords, b, n, tol, max_iterations) < 0) { rv = 1; } /* restore original edge weights */ free(graph[0].ewgts); for (i = 0; i < n; i++) { graph[i].ewgts = old_ewgts; old_ewgts += graph[i].nedges; } free(b); return rv; }