00001
00002
00003 #include "cookInterface.h"
00004
00005
00006 int loadCookGraph (char *file_name,
00007 int *nnodes,
00008 int *nedges,
00009 int **edges,
00010 double **weight)
00011 {
00012 int i,
00013 h,
00014 t;
00015 double w;
00016 FILE *file;
00017 file = fopen (file_name, "r");
00018 TEST (!file, "error opening file");
00019 fscanf (file, "%d %d", nnodes, nedges);
00020 (*edges) = (int *) malloc (sizeof (int) * (*nedges) * 2);
00021 TEST (!(*edges), "error allocating memory");
00022 if (weight)
00023 (*weight) = (double *) malloc (sizeof (double) * (*nedges));
00024 for (i = 0; i < *nedges; i++)
00025
00026 {
00027 fscanf (file, "%d %d %lf", &h, &t, &w);
00028 (*edges)[2 * i] = h;
00029 (*edges)[2 * i + 1] = t;
00030 if (weight)
00031 (*weight)[i] = w;
00032 }
00033 fclose (file);
00034 return 0;
00035 }
00036
00037
00038
00039 int cook2boyerGraph (graphP bGraph,
00040 int nnodes,
00041 int nedges,
00042 int *edges)
00043 {
00044 int i,
00045 rval = 0;
00046 rval = gp_InitGraph (bGraph, nnodes);
00047 for (i = 0; i < nedges && !rval; i++)
00048 if (!gp_IsNeighbor (bGraph, edges[2 * i], edges[2 * i + 1]))
00049
00050 {
00051 rval = gp_AddEdgeWid (bGraph, edges[2 * i], 1, edges[2 * i + 1], 1, i);
00052 TEST (rval, "error converting graph");
00053 }
00054 return rval;
00055 }
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074 int gp_AddEdgeWid (graphP theGraph,
00075 int u,
00076 int ulink,
00077 int v,
00078 int vlink,
00079 int id)
00080 {
00081 int upos,
00082 vpos;
00083 if (theGraph == NULL || u < 0 || v < 0 || u >= 2 * theGraph->N
00084 || v >= 2 * theGraph->N)
00085
00086 {
00087 __EG_PRINTLOC2__;
00088 return NOTOK;
00089 }
00090
00091
00092 if (theGraph->M >= EDGE_LIMIT * theGraph->N)
00093
00094 {
00095 __EG_PRINTLOC2__;
00096 return NONEMBEDDABLE;
00097 }
00098 vpos = 2 * theGraph->N + 2 * theGraph->M;
00099 upos = gp_GetTwinArc (theGraph, vpos);
00100 _AddArcWid (theGraph, u, v, vpos, ulink, id);
00101 _AddArcWid (theGraph, v, u, upos, vlink, id);
00102 theGraph->M++;
00103 return OK;
00104 }
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118 void _AddArcWid (graphP theGraph,
00119 int u,
00120 int v,
00121 int arcPos,
00122 int link,
00123 int id)
00124 {
00125 theGraph->G[arcPos].id = id;
00126 theGraph->G[arcPos].v = v;
00127 if (theGraph->G[u].link[0] == NIL)
00128
00129 {
00130 theGraph->G[u].link[0] = theGraph->G[u].link[1] = arcPos;
00131 theGraph->G[arcPos].link[0] = theGraph->G[arcPos].link[1] = u;
00132 }
00133
00134 else
00135
00136 {
00137 int u0 = theGraph->G[u].link[link];
00138 theGraph->G[arcPos].link[link] = u0;
00139 theGraph->G[arcPos].link[1 ^ link] = u;
00140 theGraph->G[u].link[link] = arcPos;
00141 theGraph->G[u0].link[1 ^ link] = arcPos;
00142 }
00143 }
00144
00145
00146
00147 int extractCookEdgeIds (graphP theGraph,
00148 int *nmnodes,
00149 int *nmedges,
00150 int *medges)
00151 {
00152 int I,
00153 J,
00154 cnt = 0;
00155 if (theGraph == NULL)
00156 return NOTOK;
00157 *nmnodes = theGraph->N;
00158 *nmedges = theGraph->M;
00159 for (I = 0; I < theGraph->N; I++)
00160
00161 {
00162 J = theGraph->G[I].link[1];
00163 while (J >= theGraph->N)
00164
00165 {
00166 if (theGraph->G[J].v > I)
00167 medges[cnt++] = theGraph->G[J].id;
00168 J = theGraph->G[J].link[1];
00169 }
00170 }
00171 return OK;
00172 }
00173
00174 int DPprintEmbed (graphP theGraph,
00175 FILE * file)
00176 {
00177
00178
00179 int I,
00180 J;
00181
00182
00183 if (theGraph == NULL || file == NULL)
00184 return NOTOK;
00185
00186
00187
00188 fprintf (file, "N=%d\n", theGraph->N);
00189 for (I = 0; I < theGraph->N; I++)
00190
00191 {
00192 fprintf (file, "%d:", I);
00193 J = theGraph->G[I].link[1];
00194
00195
00196 while (J >= theGraph->N)
00197
00198 {
00199 fprintf (file, " %d", theGraph->G[J].v);
00200 J = theGraph->G[J].link[1];
00201 }
00202 fprintf (file, " %d\n", NIL);
00203 }
00204 return OK;
00205 }