Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

327 lines
9.2 KiB

  1. /*******************************************************************************
  2. *
  3. * LHMatch: A library to compute 'LH Matchings', a generalization of ordinary
  4. * bipartite matchings.
  5. *
  6. * Authors: Nicholas Harvey and Laszlo Lovasz
  7. *
  8. * Developed: Nov 2000 - July 2001
  9. *
  10. * Problem Description:
  11. *
  12. * Let G=(U \union V, E) be a bipartite graph. We define an assignment
  13. * to be a set of edges, M \subset E, such that every u \member U has
  14. * deg_M(u)=1. For v \member V, deg_M(v) is unrestricted. An LH Matching is
  15. * an assignment for which the variance of {deg_M(v):v \member V} is
  16. * minimized.
  17. *
  18. * Note that it is easy to compute an ordinary maximum cardinality bipartite
  19. * matching from a optimum relaxed matching: at each RHS-vertex, throw away
  20. * all of its matching edges but one.
  21. *
  22. *******************************************************************************/
  23. /***** Type Definitions *****/
  24. typedef void* LHGRAPH;
  25. /***** Graph Statistics *****/
  26. typedef struct {
  27. /* numEdges: The total number of edges in the graph */
  28. int numEdges;
  29. /* numMatchingEdges: The total number of matching edges */
  30. int numMatchingEdges;
  31. /* matchingCost: The total cost of the current matching. The total cost
  32. * is defined to be the sum of the load of the matching on each of the
  33. * right-hand vertices. If a right-hand vertex has c matching partners,
  34. * the load at this vertex is c*(c+1)/2. */
  35. int matchingCost;
  36. /* bipMatchingSize: If the current LH Matching were converted into a
  37. * bipartite matching, this would be the size of the bipartite matching. */
  38. int bipMatchingSize;
  39. } LHSTATS;
  40. /***** Error Codes *****/
  41. #define LH_SUCCESS 0
  42. #define LH_INTERNAL_ERR -1
  43. #define LH_PARAM_ERR -2
  44. #define LH_MEM_ERR -3
  45. #define LH_MATCHING_ERR -4
  46. #define LH_EDGE_EXISTS -5
  47. /***** Algorithm Types *****/
  48. /* The LHMatch library actually includes two algorithms to compute
  49. * LH Matchings: LHOnline, LHBFS.
  50. *
  51. * LHOnline is the simpler algorithm and also the slower one. It cannot
  52. * incrementally improve an initial matching. Instead, it computes an optimal
  53. * matching from scratch each time it runs.
  54. *
  55. * LHBFS is faster than LHOnline but the implementation is more
  56. * complicated. It can take an existing matching as input and incrementally
  57. * improve it until it becomes optimal.
  58. */
  59. typedef enum {
  60. LH_ALG_ONLINE,
  61. LH_ALG_BFS
  62. } LHALGTYPE;
  63. #define LH_ALG_DEFAULT LH_ALG_BFS
  64. /***** LHCreateGraph *****/
  65. /*
  66. * Description:
  67. *
  68. * Create a graph structure on which which to compute the matching.
  69. *
  70. * Parameters:
  71. *
  72. * IN numLHSVtx The number of vertices on the left-hand side of
  73. * the bipartite graph.
  74. *
  75. * IN numRHSVtx The number of vertices on the right-hand side of
  76. * the bipartite graph.
  77. *
  78. * OUT pGraph On successful completion, pGraph will contain a
  79. * valid graph structure.
  80. *
  81. * Return Value:
  82. *
  83. * Error Code
  84. */
  85. int LHCreateGraph( int numLHSVtx, int numRHSVtx, LHGRAPH* pGraph );
  86. /***** LHAddEdge *****/
  87. /*
  88. * Description:
  89. *
  90. * Add an edge to the graph connecting lhsVtx and rhsVtx
  91. *
  92. * Parameters:
  93. *
  94. * IN graph A graph successfully created by LHGraphCreate.
  95. *
  96. * IN lhsVtx The ID of the left-hand vertex. Legal values are
  97. * 0 <= lhsVtx < numLHSVtx
  98. *
  99. * IN rhsVtx The ID of the right-hand vertex. Legal values are
  100. * 0 <= rhsVtx < numRHSVtx
  101. *
  102. * Return Value:
  103. *
  104. * Error Code
  105. */
  106. int LHAddEdge( LHGRAPH graph, int lhsVtx, int rhsVtx );
  107. /***** LHSetMatchingEdge *****/
  108. /*
  109. * Description:
  110. *
  111. * Set the edge connecting lhsVtx and rhsVtx in the graph to be
  112. * a matching edge. The edge (lhsVtx,rhsVtx) must have already been
  113. * created with a call to LHVtxAddEdge.
  114. *
  115. * Left-hand vertices may only have one matching edge. If lhsVtx
  116. * already has a matching edge, that edge will be demoted from a
  117. * matching edge to a normal edge. Right-hand vertices may have
  118. * multiple matching edges.
  119. *
  120. * Parameters:
  121. *
  122. * IN graph A graph successfully created by LHGraphCreate.
  123. *
  124. * IN lhsVtx The ID of the left-hand vertex. Legal values are
  125. * 0 <= lhsVtx < numLHSVtx
  126. *
  127. * IN rhsVtx The ID of the right-hand vertex. Legal values are
  128. * 0 <= rhsVtx < numRHSVtx
  129. *
  130. * Return Value:
  131. *
  132. * Error Code
  133. */
  134. int LHSetMatchingEdge( LHGRAPH graph, int lhsVtx, int rhsVtx );
  135. /***** LHGetDegree *****/
  136. /*
  137. * Description:
  138. *
  139. * Get the degree (number of neighbours) of a vertex.
  140. *
  141. * Parameters:
  142. *
  143. * IN graph A graph successfully created by LHGraphCreate.
  144. *
  145. * IN vtxID The ID of the vertex to examine.
  146. *
  147. * IN left If the vertex to examine is a left-vertex, this
  148. * parameter should be TRUE. If the vertex is a right-
  149. * vertex, this parameter should be FALSE.
  150. *
  151. * Return Value:
  152. *
  153. * >=0 The number of neighbours
  154. *
  155. * <0 Error Code
  156. */
  157. int LHGetDegree( LHGRAPH graph, int vtxID, char left );
  158. /***** LHGetMatchedDegree *****/
  159. /*
  160. * Description:
  161. *
  162. * Get the matched degree (number of matched neighbours) of a
  163. * right-hand vertex.
  164. *
  165. * Parameters:
  166. *
  167. * IN graph A graph successfully created by LHGraphCreate.
  168. *
  169. * IN vtxID The ID of the right hand vertex to examine.
  170. *
  171. * Return Value:
  172. *
  173. * >=0 The number of neighbours
  174. *
  175. * <0 Error Code
  176. */
  177. int LHGetMatchedDegree( LHGRAPH graph, int vtxID );
  178. /***** LHGetNeighbour *****/
  179. /*
  180. * Description:
  181. *
  182. * Get the n'th neighbour of the vertex specified by vtxID.
  183. *
  184. * Parameters:
  185. *
  186. * IN graph A graph successfully created by LHGraphCreate.
  187. *
  188. * IN vtxID The ID of the vertex to examine.
  189. *
  190. * IN left If the vertex to examine is a left-vertex, this
  191. * parameter should be TRUE. If the vertex is a right-
  192. * vertex, this parameter should be FALSE.
  193. *
  194. * IN n The index of the neighbour to retrieve. Legal values
  195. * are 0 <= n < Degree(vtxID).
  196. *
  197. * Return Value:
  198. *
  199. * >=0 The vertex ID of the n'th neighbour. If 'left' is TRUE,
  200. * this ID refers to a right-hand vertex. Conversely, if
  201. * 'left' is FALSE, this ID refers to a left-hand vertex.
  202. *
  203. * <0 Error Code
  204. */
  205. int LHGetNeighbour( LHGRAPH graph, int vtxID, char left, int n );
  206. /***** LHFindLHMatching *****/
  207. /*
  208. * Description:
  209. *
  210. * Find an optimal matching in the graph.
  211. *
  212. * Parameters:
  213. *
  214. * IN graph A graph successfully created by LHGraphCreate,
  215. * to which edges have been added using LHAddEdge.
  216. *
  217. * IN alg Specifies which algorithm to use. For most purposes,
  218. * LH_ALG_DEFAULT is fine.
  219. *
  220. * Return Value:
  221. *
  222. * Error Code
  223. */
  224. int LHFindLHMatching( LHGRAPH graph, LHALGTYPE alg );
  225. /***** LHGetMatchedVtx *****/
  226. /*
  227. * Description:
  228. *
  229. * Determine which vertex on the right-hand side is matched to a
  230. * given vertex on the left-hand side.
  231. *
  232. * Parameters:
  233. *
  234. * IN graph A graph successfully created by LHGraphCreate,
  235. * to which edges have been added using LHAddEdge.
  236. *
  237. * IN lhsVtx The left-hand vertex to query.
  238. *
  239. * Return Value:
  240. *
  241. * >=0 The index of the right-hand vertex that is matched
  242. * with lhsVtx.
  243. *
  244. * <0 Error Code. If LH_MATCHING_ERR is returned, then
  245. * lhsVtx is not matched with any right-hand vertex.
  246. */
  247. int LHGetMatchedVtx( LHGRAPH graph, int lhsVtx );
  248. /***** LHGetStatistics *****/
  249. /*
  250. * Description:
  251. *
  252. * Obtain statistics about the current matching.
  253. *
  254. * Parameters:
  255. *
  256. * IN graph A graph for which an LH Matching has been
  257. * computed using LHFindLHMatching().
  258. *
  259. * Return Value:
  260. *
  261. * Error Code
  262. */
  263. int LHGetStatistics( LHGRAPH graph, LHSTATS *stats );
  264. /***** LHClearMatching *****/
  265. /*
  266. * Description:
  267. *
  268. * Clear the current matching. All edges are demoted to normal edges.
  269. *
  270. * Parameters:
  271. *
  272. * IN graph A graph successfully created by LHGraphCreate,
  273. * to which edges have been added using LHAddEdge.
  274. *
  275. * Return Value:
  276. *
  277. * Error Code
  278. */
  279. int LHClearMatching( LHGRAPH graph );
  280. /***** LHDestroyGraph *****/
  281. /*
  282. * Description:
  283. *
  284. * Destroy the current graph.
  285. *
  286. * Parameters:
  287. *
  288. * IN graph A graph successfully created by LHGraphCreate.
  289. *
  290. * Return Value:
  291. *
  292. * Error Code
  293. */
  294. int LHDestroyGraph( LHGRAPH graph );