aboutsummaryrefslogtreecommitdiff
path: root/src/backend/optimizer/geqo/minspantree.c
blob: bb5863fec705dbcc47d8020d329335a35d93178c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*------------------------------------------------------------------------
*
* minspantree.c--
*	 routine to sort a join graph which is including cycles
*
* Copyright (c) 1994, Regents of the University of California
*
*
* IDENTIFICATION
*	 $Header: /cvsroot/pgsql/src/backend/optimizer/geqo/Attic/minspantree.c,v 1.4 1997/09/08 21:44:42 momjian Exp $
*
*-------------------------------------------------------------------------
*/

#include <values.h>

#include "postgres.h"

#include "nodes/pg_list.h"
#include "nodes/relation.h"
#include "nodes/primnodes.h"

#include "utils/palloc.h"

#include "optimizer/cost.h"

/*
 include "optimizer/geqo/tsp.h"
 */

#include "optimizer/geqo/geqo_gene.h"
#include "optimizer/geqo/geqo.h"

/*
 * minspantree--
 *		 The function minspantree computes the minimum spanning tree
 *		for a given number of nodes and a given distance function.
 *		For each pair of nodes found to be connected, a given
 *		function is called. Nodes are denoted by the integer numbers
 *		1 .. number_of_joins, where number_of_joins is the number of nodes.
*/

void
minspantree(Query *root, List *join_rels, Rel *garel)
{
	int			number_of_rels = length(root->base_relation_list_);
	int			number_of_joins = length(join_rels);
	int		   *connectto;

	/* connectto[i] = 0, if node i is already connected  */
	/* to the tree, otherwise connectto[i] is the node	 */
	/* nearest to i, which is already connected.	   */

	Cost	   *disttoconnect;	/* disttoconnect[i]: distance between i
								 * and connectto[i] */

	Cost		dist,			/* temporary */
				mindist;		/* minimal distance between connected and
								 * unconnected node */

	Cost		mstlength = 0.0;/* the total length of the minimum
								 * spanning tree */

	int			count;
	int			n,				/* newly attached node */
				nextn,			/* next node to be attached */
				tempn;

	int			i,
				id1,
				id2;
	List	   *r = NIL;
	Rel		   *joinrel = NULL;
	Rel		  **tmprel_array;


	/* allocate memory for matrix tmprel_array[x][y] */
	tmprel_array = (Rel **) palloc((number_of_rels + 1) * sizeof(Rel *));
	for (i = 0; i <= number_of_rels; i++)
		(tmprel_array[i] = (Rel *) palloc((number_of_rels + 1) * sizeof(Rel)));

	/* read relations of join-relations into tmprel_array */

	foreach(r, join_rels)
	{
		joinrel = (Rel *) lfirst(r);
		id1 = (int) lfirst(joinrel->relids);
		id2 = (int) lsecond(joinrel->relids);

		if (id1 > id2)
		{
			tmprel_array[id2][id1] = *(Rel *) joinrel;
		}
		else
		{
			tmprel_array[id1][id2] = *(Rel *) joinrel;	/* ever reached? */
		}
	}

	/* Trivial special cases handled first */
	/* garel is global in "tsp.h" */

	if (number_of_joins <= 2)
	{
		i = 1;
		foreach(r, join_rels)
		{
			garel[i] = *(Rel *) lfirst(r);
			i++;
		}
	}


	else if (number_of_joins == 3)
	{
		Rel		   *rel12 = (Rel *) &tmprel_array[1][2];
		Rel		   *rel13 = (Rel *) &tmprel_array[1][3];
		Rel		   *rel23 = (Rel *) &tmprel_array[2][3];

		if (rel12->cheapestpath->path_cost > rel13->cheapestpath->path_cost)
		{
			garel[1] = tmprel_array[1][3];
			if (rel12->cheapestpath->path_cost > rel23->cheapestpath->path_cost)
			{
				garel[2] = tmprel_array[2][3];
			}
			else
			{
				garel[2] = tmprel_array[1][2];
			}
		}
		else
		{
			garel[1] = tmprel_array[1][2];
			if (rel13->cheapestpath->path_cost > rel23->cheapestpath->path_cost)
			{
				garel[2] = tmprel_array[2][3];
			}
			else
			{
				garel[2] = tmprel_array[1][3];
			}
		}
	}


	/* now the general case */
	else
	{
		connectto = (int *) palloc((number_of_rels + 1) * sizeof(int));
		disttoconnect = (Cost *) palloc((number_of_rels + 1) * sizeof(Cost));

		nextn = 2;
		for (tempn = 2; tempn <= number_of_rels; tempn++)
		{
			connectto[tempn] = 1;
			disttoconnect[tempn] = (Cost) MAXFLOAT;
		}

		joinrel = NULL;
		n = 1;
		i = 1;
		for (count = 2; count <= number_of_rels; count++)
		{
			connectto[n] = 0;
			mindist = (Cost) MAXFLOAT;
			for (tempn = 2; tempn <= number_of_rels; tempn++)
			{
				if (connectto[tempn] != 0)
				{
					if (n > tempn)
					{
						joinrel = (Rel *) &tmprel_array[tempn][n];
					}
					else
					{
						joinrel = (Rel *) &tmprel_array[n][tempn];
					}
					dist = joinrel->cheapestpath->path_cost;

					if (dist < disttoconnect[tempn])
					{
						disttoconnect[tempn] = dist;
						connectto[tempn] = n;
					}
					if (disttoconnect[tempn] < mindist)
					{
						mindist = disttoconnect[tempn];
						nextn = tempn;
					}
				}
			}
			n = nextn;
			if (n > connectto[n])
			{
				garel[i] = tmprel_array[connectto[n]][n];
			}
			else
			{
				garel[i] = tmprel_array[n][connectto[n]];
			}
			i++;
		}

		pfree(connectto);
		pfree(disttoconnect);

	}

	for (i = 0; i <= number_of_rels; i++)
		pfree(tmprel_array[i]);
	pfree(tmprel_array);
}