File: Set.cpp

package info (click to toggle)
bandage 0.9.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 15,684 kB
  • sloc: cpp: 45,359; sh: 491; makefile: 12
file content (241 lines) | stat: -rw-r--r-- 6,640 bytes parent folder | download | duplicates (3)
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*
 * $Revision: 2552 $
 *
 * last checkin:
 *   $Author: gutwenger $
 *   $Date: 2012-07-05 16:45:20 +0200 (Do, 05. Jul 2012) $
 ***************************************************************/

/** \file
 * \brief Implementation of class Set.
 *
 * \author Stefan Hachul
 *
 * \par License:
 * This file is part of the Open Graph Drawing Framework (OGDF).
 *
 * \par
 * Copyright (C)<br>
 * See README.txt in the root directory of the OGDF installation for details.
 *
 * \par
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * Version 2 or 3 as published by the Free Software Foundation;
 * see the file LICENSE.txt included in the packaging of this file
 * for details.
 *
 * \par
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * \par
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 *
 * \see  http://www.gnu.org/copyleft/gpl.html
 ***************************************************************/


#include "Set.h"

namespace ogdf {

Set::Set()
{
	last_selectable_index_of_S_node = -1;
	S_node = NULL;
	using_S_node = false;
}


Set::~Set()
{
	if (using_S_node) delete [] S_node;
}


void Set::set_seed(int rand_seed)
{
	srand(rand_seed);
}


void Set::init_node_set(Graph& G)
{
	using_S_node = true;
	node v;

	S_node = new node[G.numberOfNodes()];
	position_in_node_set.init(G);

	forall_nodes(v,G)
	{
		S_node[v->index()] = v;
		position_in_node_set[v] = v->index();
	}
	last_selectable_index_of_S_node = G.numberOfNodes()-1;
}


bool Set::empty_node_set()
{
	if(last_selectable_index_of_S_node < 0)
		return true;
	else
		return false;
}


bool Set::is_deleted(node v)
{
	if (position_in_node_set[v] > last_selectable_index_of_S_node )
		return true;
	else
		return false;
}


void Set::delete_node(node del_node)
{
	int del_node_index = position_in_node_set[del_node];
	node last_selectable_node = S_node[last_selectable_index_of_S_node];

	S_node[last_selectable_index_of_S_node] = del_node;
	S_node[del_node_index] = last_selectable_node;
	position_in_node_set[del_node] = last_selectable_index_of_S_node;
	position_in_node_set[last_selectable_node] = del_node_index;
	last_selectable_index_of_S_node -=1;
}


//---------------- for set of nodes with uniform probability -------------------

node Set::get_random_node()
{
	int rand_index = randomNumber(0,last_selectable_index_of_S_node);
	node random_node =  S_node[rand_index];
	node last_selectable_node = S_node[last_selectable_index_of_S_node];

	S_node[last_selectable_index_of_S_node] = random_node;
	S_node[rand_index] = last_selectable_node;
	position_in_node_set[random_node] = last_selectable_index_of_S_node;
	position_in_node_set[last_selectable_node] = rand_index;
	last_selectable_index_of_S_node -=1;
	return random_node;
}


//---------------- for set of nodes with weighted  probability ------------------

void Set::init_node_set(Graph& G,NodeArray<NodeAttributes>& A)
{
	node v,v_adj;
	edge e_adj;

	init_node_set(G);
	mass_of_star.init(G);
	forall_nodes(v,G)
	{
		mass_of_star[v] = A[v].get_mass();
		forall_adj_edges(e_adj, v)
		{
			if(e_adj->source() != v)
				v_adj = e_adj->source();
			else
				v_adj = e_adj->target();
			mass_of_star[v] += A[v_adj].get_mass();
		}
	}
}

//---------------- for set of nodes with ``lower mass'' probability --------------

node Set::get_random_node_with_lowest_star_mass(int rand_tries)
{
    int rand_index = 0, new_rand_index, min_mass = 0;
	int i = 1;
    node random_node = node(), new_rand_node,last_trie_node,last_selectable_node;

	//randomly select rand_tries distinct!!! nodes from S_node and select the one
	//with the lowest mass

	int last_trie_index = last_selectable_index_of_S_node;
	while( (i<= rand_tries) && (last_trie_index >= 0) )
	{//while
		last_trie_node = S_node[last_trie_index];
		new_rand_index = randomNumber(0,last_trie_index);
		new_rand_node = S_node[new_rand_index];
		S_node[last_trie_index] = new_rand_node;
		S_node[new_rand_index] = last_trie_node;
		position_in_node_set[new_rand_node] = last_trie_index;
		position_in_node_set[last_trie_node] = new_rand_index;

		if( (i == 1) || (min_mass > mass_of_star[S_node[last_trie_index]]) )
		{
			rand_index = last_trie_index;
			random_node = S_node[last_trie_index];
			min_mass = mass_of_star[random_node];
		}
		i++;
		last_trie_index -=1;
	}//while

	//now rand_index and random_node have been fixed
	last_selectable_node = S_node[last_selectable_index_of_S_node];
	S_node[last_selectable_index_of_S_node] = random_node;
	S_node[rand_index] = last_selectable_node;
	position_in_node_set[random_node] = last_selectable_index_of_S_node;
	position_in_node_set[last_selectable_node] = rand_index;
	last_selectable_index_of_S_node -=1;
	return random_node;
}


//---------------- for set of nodes with ``higher mass'' probability --------------

node Set::get_random_node_with_highest_star_mass(int rand_tries)
{
    int rand_index = 0, new_rand_index, min_mass = 0;
	int i = 1;
    node random_node = node(), new_rand_node,last_trie_node,last_selectable_node;

	//randomly select rand_tries distinct!!! nodes from S_node and select the one
	//with the lowest mass

	int last_trie_index = last_selectable_index_of_S_node;
	while( (i<= rand_tries) && (last_trie_index >= 0) )
	{//while
		last_trie_node = S_node[last_trie_index];
		new_rand_index = randomNumber(0,last_trie_index);
		new_rand_node = S_node[new_rand_index];
		S_node[last_trie_index] = new_rand_node;
		S_node[new_rand_index] = last_trie_node;
		position_in_node_set[new_rand_node] = last_trie_index;
		position_in_node_set[last_trie_node] = new_rand_index;

		if( (i == 1) || (min_mass < mass_of_star[S_node[last_trie_index]]) )
		{
			rand_index = last_trie_index;
			random_node = S_node[last_trie_index];
			min_mass = mass_of_star[random_node];
		}
		i++;
		last_trie_index -=1;
	}//while

	//now rand_index and random_node have been fixed
	last_selectable_node = S_node[last_selectable_index_of_S_node];
	S_node[last_selectable_index_of_S_node] = random_node;
	S_node[rand_index] = last_selectable_node;
	position_in_node_set[random_node] = last_selectable_index_of_S_node;
	position_in_node_set[last_selectable_node] = rand_index;
	last_selectable_index_of_S_node -=1;
	return random_node;
}

}//namespace ogdf