bipartite_match.h 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /*
  2. * bipartite_match.h
  3. *
  4. * Copyright (c) 2015-2022, PostgreSQL Global Development Group
  5. *
  6. * src/include/lib/bipartite_match.h
  7. */
  8. #ifndef BIPARTITE_MATCH_H
  9. #define BIPARTITE_MATCH_H
  10. /*
  11. * Given a bipartite graph consisting of nodes U numbered 1..nU, nodes V
  12. * numbered 1..nV, and an adjacency map of undirected edges in the form
  13. * adjacency[u] = [k, v1, v2, v3, ... vk], we wish to find a "maximum
  14. * cardinality matching", which is defined as follows: a matching is a subset
  15. * of the original edges such that no node has more than one edge, and a
  16. * matching has maximum cardinality if there exists no other matching with a
  17. * greater number of edges.
  18. *
  19. * This matching has various applications in graph theory, but the motivating
  20. * example here is Dilworth's theorem: a partially-ordered set can be divided
  21. * into the minimum number of chains (i.e. subsets X where x1 < x2 < x3 ...) by
  22. * a bipartite graph construction. This gives us a polynomial-time solution to
  23. * the problem of planning a collection of grouping sets with the provably
  24. * minimal number of sort operations.
  25. */
  26. typedef struct BipartiteMatchState
  27. {
  28. /* inputs: */
  29. int u_size; /* size of U */
  30. int v_size; /* size of V */
  31. short **adjacency; /* adjacency[u] = [k, v1,v2,v3,...,vk] */
  32. /* outputs: */
  33. int matching; /* number of edges in matching */
  34. short *pair_uv; /* pair_uv[u] -> v */
  35. short *pair_vu; /* pair_vu[v] -> u */
  36. /* private state for matching algorithm: */
  37. short *distance; /* distance[u] */
  38. short *queue; /* queue storage for breadth search */
  39. } BipartiteMatchState;
  40. extern BipartiteMatchState *BipartiteMatch(int u_size, int v_size, short **adjacency);
  41. extern void BipartiteMatchFree(BipartiteMatchState *state);
  42. #endif /* BIPARTITE_MATCH_H */