00001 #include "graphtypes.h"
00002 #include <stdio.h>
00003 #include <math.h>
00004
00005
00006 #ifdef MATLAB_MEX_FILE
00007 #include <mex.h>
00008 #endif
00009
00010
00011 AddEdge (Graph g, int n, int m, int label)
00012 {
00013 Edge edge1 = (Edge) NULL;
00014 Edge edge2 = (Edge) NULL;
00015 long bytes = 2 * sizeof(struct edge_ent);
00016
00017
00018 #ifdef MEX_DEBUG
00019 mexPrintf( "Debug: Entering AddEdge().\n" );
00020 mexPrintf( "Debug: n = %d.\n", n );
00021 mexPrintf( "Debug: m = %d.\n", m );
00022 mexPrintf( "Debug: label = %d.\n", label );
00023 #endif
00024
00025 #ifdef MATLAB_MEX_FILE
00026 edge1 = (Edge) mxMalloc( bytes );
00027 #else
00028 edge1 = (Edge) malloc ( bytes );
00029 #endif
00030
00031 edge2 = edge1 + 1;
00032
00033 edge1->label = label;
00034 edge1->endpoint = m;
00035 edge1->otheredge = edge2;
00036 edge1->prevedge = NULL;
00037 edge1->nextedge = g[n].adj_list;
00038
00039 if (edge1->nextedge != NULL)
00040 {
00041 edge1->nextedge->prevedge = edge1;
00042 }
00043
00044 g[n].adj_list = edge1;
00045 g[n].degree++;
00046
00047 edge2->label = label;
00048 edge2->endpoint = n;
00049 edge2->otheredge = edge1;
00050 edge2->prevedge = NULL;
00051 edge2->nextedge = g[m].adj_list;
00052
00053 if (edge2->nextedge != NULL)
00054 {
00055 edge2->nextedge->prevedge = edge2;
00056 }
00057
00058 g[m].adj_list = edge2;
00059 g[m].degree++;
00060
00061 #ifdef MEX_DEBUG
00062 mexPrintf("Debug: Leaving AddEdge().\n");
00063 #endif
00064 }
00065
00066
00067 Edge FindEdge(graph,i,j)
00068 Graph graph;
00069 int i,j;
00070
00071 { Edge edge;
00072
00073 edge = graph[i].adj_list;
00074 while (edge!=NULL && edge->endpoint!=j)
00075 edge = edge->nextedge;
00076 if (edge==NULL) return(NULL);
00077 else return(edge);
00078 }
00079
00080 int RemoveEdge(graph,edge)
00081 Graph graph;
00082 Edge edge;
00083
00084 { Edge other;
00085 int i,j;
00086
00087 if (edge==NULL) return(0);
00088 other = edge->otheredge;
00089 i = other->endpoint;
00090 j = edge->endpoint;
00091 graph[i].degree--; graph[j].degree--;
00092 if (edge->prevedge == NULL) {
00093 graph[i].adj_list = edge->nextedge;
00094 if (edge->nextedge != NULL)
00095 edge->nextedge->prevedge = NULL;
00096 }
00097 else if (edge->nextedge == NULL)
00098 (edge->prevedge)->nextedge = NULL;
00099 else {
00100 (edge->nextedge)->prevedge = edge->prevedge;
00101 (edge->prevedge)->nextedge = edge->nextedge;
00102 }
00103 if (other->prevedge == NULL) {
00104 graph[j].adj_list = other->nextedge;
00105 if (other->nextedge != NULL)
00106 other->nextedge->prevedge = NULL;
00107 }
00108 else if (other->nextedge == NULL)
00109 (other->prevedge)->nextedge = NULL;
00110 else {
00111 (other->nextedge)->prevedge = other->prevedge;
00112 (other->prevedge)->nextedge = other->nextedge;
00113 }
00114
00115 #ifdef MATLAB_MEX_FILE
00116 if (edge < other)
00117 mxFree(edge);
00118 else
00119 mxFree(other);
00120 #else
00121 free((edge < other) ? edge : other);
00122 #endif
00123 return(1);
00124 }
00125
00126 int NumEdges(g)
00127 Graph g;
00128 { int i,size,edges;
00129
00130 edges = 0;
00131 size = Degree(g,0);
00132 for (i=1; i<=size; i++)
00133 edges += Degree(g,i);
00134 edges /= 2;
00135 return(edges);
00136 }
00137
00138
00139 Graph NewGraph (int size)
00140 {
00141 Graph tmp = (Graph) NULL;
00142 int i = 0;
00143 long bytes = (size + 1) * sizeof(struct node_entry);
00144
00145
00146 #ifdef MEX_DEBUG
00147 mexPrintf("Debug: Entering NewGraph().\n");
00148 #endif
00149
00150
00151 #ifdef MATLAB_MEX_FILE
00152 tmp = (Graph) mxMalloc( bytes );
00153 #else
00154 tmp = (Graph) malloc ( bytes );
00155 #endif
00156
00157 for (i = 1; i <= size; i++)
00158 {
00159 Degree (tmp, i) = 0;
00160 FirstEdge(tmp, i) = NULL;
00161 NLabel (tmp, i) = i;
00162 }
00163
00164 Degree(tmp, 0) = size;
00165
00166
00167 #ifdef MEX_DEBUG
00168 mexPrintf("Debug: Leaving NewGraph().\n");
00169 #endif
00170
00171 return tmp;
00172 }
00173
00174
00175 EuclidGraph NewEuclid(size)
00176 int size;
00177 {
00178 EuclidGraph xy;
00179
00180 xy = (EuclidGraph) malloc((size+1)*2*sizeof(int));
00181 xy[0][0] = size;
00182 return(xy);
00183 }
00184
00185 MatrixGraph NewMatrix(size)
00186 int size;
00187 {
00188 MatrixGraph graph;
00189 int i;
00190
00191 graph = (MatrixGraph) malloc((size*(size+1)+1)*sizeof(int));
00192 graph[0] = size;
00193
00194 for (i=1; i<=size; i++)
00195 graph[i*(size+1)] = 0;
00196
00197 return(graph);
00198 }
00199
00200 Graph CopyGraph(g)
00201 Graph g;
00202 { int i,j,size;
00203 Edge edge;
00204 Graph cp;
00205
00206 size = Degree(g,0);
00207 cp = NewGraph(size);
00208 for (i=1; i<=size; i++) {
00209 Xcoord(cp,i) = Xcoord(g,i);
00210 Ycoord(cp,i) = Ycoord(g,i);
00211 edge = FirstEdge(g,i);
00212 for (j=1; j<=Degree(g,i); j++) {
00213 if (i < EndPoint(edge))
00214 AddEdge(cp,i,EndPoint(edge),ELabel(edge));
00215 edge = NextEdge(edge);
00216 }
00217 }
00218 return (cp);
00219 }
00220
00221
00222
00223 int eucdist (graph,i,j)
00224
00225 EuclidGraph graph;
00226 int i,j;
00227 { int dv,dh;
00228 register int k, l;
00229
00230 dv = graph[i][0]-graph[j][0];
00231 dh = graph[i][1]-graph[j][1];
00232 k = dv*dv + dh*dh;
00233 if (k==0) return(0);
00234 if (dv<0) dv = -dv;
00235 if (dh<0) dh = -dh;
00236 l = dv + dh;
00237 l = (l + k/l)>>1;
00238 l = (l + k/l)>>1;
00239 l = (l + k/l)>>1;
00240 l = (l + k/l)>>1;
00241 return ((l*l<k) ? ++l : l);
00242 }
00243
00244
00245 int eucdist2 (graph,i,j)
00246
00247 EuclidGraph graph;
00248 int i,j;
00249 { double dv,dh,d;
00250 int l;
00251
00252 dv = (double) graph[i][0]-graph[j][0];
00253 dh = (double) graph[i][1]-graph[j][1];
00254 d = sqrt(dv*dv + dh*dh);
00255 l = (int) d;
00256 return((d-l > .000000001) ? l+1 : l);
00257 }
00258
00259
00260 int eucdistsq(graph,i,j)
00261 EuclidGraph graph;
00262 int i,j;
00263 {
00264 int dv,dh;
00265
00266 dv = graph[i][0]-graph[j][0];
00267 dh = graph[i][1]-graph[j][1];
00268 return(dv*dv+dh*dh);
00269 }
00270