|
| 1 | +module CompressUDG |
| 2 | +using Graphs |
| 3 | + |
| 4 | +struct UNode |
| 5 | + vertex::Int # vertex index in original graph |
| 6 | + pos::Tuple{Int,Int} |
| 7 | + neighbors::Vector{Int} # neighbors of node |
| 8 | +end |
| 9 | +Base.hash(unode::UNode) = hash(unode.vertex) |
| 10 | +function Base.:(==)(x::UNode, y::UNode) |
| 11 | + x.vertex == y.vertex && x.neighbors == y.neighbors |
| 12 | +end |
| 13 | + |
| 14 | +# get surrounding neighbor points on UDG |
| 15 | +function get_udg_neighbors(pos::Tuple{Int, Int}) |
| 16 | + p_x, p_y = pos |
| 17 | + |
| 18 | + pos_udg_neighbors = Vector{Tuple{Int, Int}}() |
| 19 | + for i=-1:1 |
| 20 | + for j=-1:1 |
| 21 | + if !(i == 0 && j == 0) |
| 22 | + append!(pos_udg_neighbors, (p_x + i, p_y + j)) |
| 23 | + end |
| 24 | + end |
| 25 | + end |
| 26 | + return pos_udg_neighbors |
| 27 | +end |
| 28 | + |
| 29 | +# find the UDNode given a position |
| 30 | +# return nothing if no node at that position |
| 31 | +function get_UNode_from_pos(pos::Tuple{Int, Int}, node_list::Vector{UNode}) |
| 32 | + for u in node_list |
| 33 | + if u.pos == pos |
| 34 | + return u |
| 35 | + end |
| 36 | + end |
| 37 | + return Nothing() |
| 38 | +end |
| 39 | + |
| 40 | +export find_boundaries |
| 41 | +# find the boundaries of a grid graph given list of UNodes |
| 42 | +function find_boundaries(node_list::Vector{UNode}) |
| 43 | + min_x = Inf |
| 44 | + min_y = Inf |
| 45 | + max_x = 0 |
| 46 | + max_y = 0 |
| 47 | + |
| 48 | + for u in node_list |
| 49 | + p_x, p_y = u.pos |
| 50 | + |
| 51 | + if p_x > max_x |
| 52 | + max_x = p_x |
| 53 | + end |
| 54 | + if p_x < min_x |
| 55 | + min_x = p_x |
| 56 | + end |
| 57 | + if p_y > max_y |
| 58 | + max_y = p_y |
| 59 | + end |
| 60 | + if p_y < min_y |
| 61 | + min_y = p_y |
| 62 | + end |
| 63 | + end |
| 64 | + return min_x, min_y, max_x, max_y |
| 65 | +end |
| 66 | + |
| 67 | +# find points on the boundary in which we would like to move |
| 68 | +# divide region into 4 and move nodes greedily closer to center |
| 69 | +function find_boundary_points(node_list::Vector{UNode}, x_min, x_max, y_min, |
| 70 | + y_max) |
| 71 | + half_x = (x_max - x_min)/2 |
| 72 | + half_y = (y_max - y_min)/2 |
| 73 | + |
| 74 | + pts_xmin_upper = Vector{UNode}() |
| 75 | + pts_xmax_upper = Vector{UNode}() |
| 76 | + pts_xmin_lower = Vector{UNode}() |
| 77 | + pts_xmax_lower = Vector{UNode}() |
| 78 | + |
| 79 | + pts_ymin_left = Vector{UNode}() |
| 80 | + pts_ymax_left = Vector{UNode}() |
| 81 | + pts_ymin_right = Vector{UNode}() |
| 82 | + pts_ymax_right = Vector{UNode}() |
| 83 | + |
| 84 | + for u in node_list |
| 85 | + p_x, p_y = u.pos |
| 86 | + |
| 87 | + if p_x == x_min and p_y >= half_y |
| 88 | + append!(pts_xmin_upper, u) |
| 89 | + end |
| 90 | + if p_x == x_min and p_y < half_y |
| 91 | + append!(pts_xmin_lower, u) |
| 92 | + end |
| 93 | + if p_x == x_max and p_y >= half_y |
| 94 | + append!(pts_xmax_upper, u) |
| 95 | + end |
| 96 | + if p_x == x_max and p_y < half_y |
| 97 | + append!(pts_xmax_lower, u) |
| 98 | + end |
| 99 | + if p_x >= half_x and p_y == y_min |
| 100 | + append!(pts_ymin_right, u) |
| 101 | + end |
| 102 | + if p_x < half_x and p_y == y_min |
| 103 | + append!(pts_ymin_left, u) |
| 104 | + end |
| 105 | + if p_x >= half_x and p_y == y_max |
| 106 | + append!(pts_ymax_right, u) |
| 107 | + end |
| 108 | + if p_x < half_x and p_y == y_max |
| 109 | + append!(pts_ymax_left, u) |
| 110 | + end |
| 111 | + end |
| 112 | + |
| 113 | + return pts_xmin_upper, pts_xmin_lower, pts_xmax_upper, pts_xmax_lower, |
| 114 | + pts_ymin_right, pts_ymin_left, pts_ymax_right, pts_ymax_left |
| 115 | +end |
| 116 | + |
| 117 | +export check_UDG_criteria |
| 118 | +# check that the new position of node n satisfies UDG requirements |
| 119 | +function check_UDG_criteria(n::UNode, new_pos::Tuple{Int, Int}, node_list::Vector{UNode}) |
| 120 | + # check if new_pos is already occupied |
| 121 | + if get_UNode_from_pos(new_pos, node_list) != Nothing() |
| 122 | + return false |
| 123 | + end |
| 124 | + |
| 125 | + p_x, p_y = new_pos |
| 126 | + p_neighbors = n.neighbors |
| 127 | + |
| 128 | + new_neighbors = Vector{UNode}() |
| 129 | + UDG_neighbor_pos = get_udg_neighbors(new_pos) |
| 130 | + |
| 131 | + for p in UDG_neighbor_pos |
| 132 | + unode = get_UNode_from_pos(p, node_list) |
| 133 | + |
| 134 | + if unode != Nothing() |
| 135 | + if unode.vertex != n.vertex |
| 136 | + append!(new_neighbors, unode) |
| 137 | + end |
| 138 | + end |
| 139 | + end |
| 140 | + |
| 141 | + if issetequal(new_neighbors, p_neighbors) == true |
| 142 | + return true |
| 143 | + end |
| 144 | + return false |
| 145 | +end |
| 146 | + |
| 147 | +export move_node |
| 148 | +# move node n to a new position new_pos |
| 149 | +function move_node(n::UNode, node_list::Vector{UNode}, candidates::Vector{Tuple{Int, Int}}) |
| 150 | + |
| 151 | + for p in candidates |
| 152 | + if check_UDG_criteria(n, p, node_list) == true |
| 153 | + n.pos = p |
| 154 | + node_list[n.vertex] = n |
| 155 | + return node_list |
| 156 | + end |
| 157 | + end |
| 158 | + return node_list |
| 159 | +end |
| 160 | + |
| 161 | +export candidates_xmin_upper |
| 162 | +# determine candidates |
| 163 | +function candidates_xmin_upper(pos::Tuple{Int, Int}) |
| 164 | + p_x, p_y = pos |
| 165 | + return [(p_x + 1, p_y), (p_x + 1, p_y - 1), (p_x, p_y - 1)] |
| 166 | +end |
| 167 | + |
| 168 | +export candidates_xmin_lower |
| 169 | +function candidates_xmin_lower(pos::Tuple{Int, Int}) |
| 170 | + p_x, p_y = pos |
| 171 | + return [(p_x + 1, p_y), (p_x + 1, p_y + 1), (p_x, p_y + 1)] |
| 172 | +end |
| 173 | + |
| 174 | +export candidates_xmax_upper |
| 175 | +function candidates_xmax_upper(pos::Tuple{Int, Int}) |
| 176 | + p_x, p_y = pos |
| 177 | + return [(p_x - 1, p_y), (p_x - 1, p_y - 1), (p_x, p_y - 1)] |
| 178 | +end |
| 179 | + |
| 180 | +export candidates_xmax_lower |
| 181 | +function candidates_xmax_lower(pos::Tuple{Int, Int}) |
| 182 | + p_x, p_y = pos |
| 183 | + return [(p_x - 1, p_y), (p_x - 1, p_y + 1), (p_x, p_y + 1)] |
| 184 | +end |
| 185 | + |
| 186 | +export candidates_ymin_left |
| 187 | +function candidates_ymin_left(pos::Tuple{Int, Int}) |
| 188 | + p_x, p_y = pos |
| 189 | + return [(p_x, p_y + 1), (p_x + 1, p_y + 1), (p_x + 1, p_y)] |
| 190 | +end |
| 191 | + |
| 192 | +export candidates_ymin_right |
| 193 | +function candidates_ymin_right(pos::Tuple{Int, Int}) |
| 194 | + p_x, p_y = pos |
| 195 | + return [(p_x, p_y + 1), (p_x - 1, p_y + 1), (p_x - 1, p_y)] |
| 196 | +end |
| 197 | + |
| 198 | +export candidates_ymax_left |
| 199 | +function candidates_ymax_left(pos::Tuple{Int, Int}) |
| 200 | + p_x, p_y = pos |
| 201 | + return [(p_x, p_y - 1), (p_x + 1, p_y - 1), (p_x + 1, p_y)] |
| 202 | +end |
| 203 | + |
| 204 | +export candidates_ymax_right |
| 205 | +function candidates_ymax_right(pos::Tuple{Int, Int}) |
| 206 | + p_x, p_y = pos |
| 207 | + return [(p_x, p_y - 1), (p_x - 1, p_y - 1), (p_x - 1, p_y)] |
| 208 | +end |
| 209 | + |
| 210 | +export greedy_step |
| 211 | +function greedy_step(node_list::Vector{UNode}, min_x::Int, max_x::Int, |
| 212 | + min_y::Int, max_y::Int) |
| 213 | + |
| 214 | + xmin_upper, xmin_lower, xmax_upper, xmax_lower, ymin_right, ymin_left, |
| 215 | + ymax_right, ymax_left = find_boundary_points(node_list, min_x, max_x, |
| 216 | + min_y, max_y) |
| 217 | + |
| 218 | + for p in xmin_upper |
| 219 | + candidates = candidates_xmin_upper(p.pos) |
| 220 | + node_list = move_node(p, node_list, candidates) |
| 221 | + end |
| 222 | + for p in xmin_lower |
| 223 | + candidates = candidates_xmin_lower(p.pos) |
| 224 | + node_list = move_node(p, node_list, candidates) |
| 225 | + end |
| 226 | + for p in xmax_upper |
| 227 | + candidates = candidates_xmax_upper(p.pos) |
| 228 | + node_list = move_node(p, node_list, candidates) |
| 229 | + end |
| 230 | + for p in xmax_lower |
| 231 | + candidates = candidates_xmax_lower(p.pos) |
| 232 | + node_list = move_node(p, node_list, candidates) |
| 233 | + end |
| 234 | + |
| 235 | + for p in ymin_left |
| 236 | + candidates = candidates_ymin_left(p.pos) |
| 237 | + node_list = move_node(p, node_list, candidates) |
| 238 | + end |
| 239 | + for p in ymin_right |
| 240 | + candidates = candidates_ymin_right(p.pos) |
| 241 | + node_list = move_node(p, node_list, candidates) |
| 242 | + end |
| 243 | + for p in ymax_left |
| 244 | + candidates = candidates_ymax_left(p.pos) |
| 245 | + node_list = move_node(p, node_list, candidates) |
| 246 | + end |
| 247 | + for p in ymax_right |
| 248 | + candidates = candidates_ymax_right(p.pos) |
| 249 | + node_list = move_node(p, node_list, candidates) |
| 250 | + end |
| 251 | + |
| 252 | + return node_list |
| 253 | +end |
| 254 | + |
| 255 | +# interfaces |
| 256 | +abstract type CompressUDGMethod end |
| 257 | + |
| 258 | +""" |
| 259 | + contract_graph(locs::Vector{Tuple{Int, Int}}) |
| 260 | +
|
| 261 | +Compute a contracted version of input graph node positions and returns a |
| 262 | +corresponding layout of new condensed graph |
| 263 | +""" |
| 264 | +# execute |
| 265 | +export unitdisk_graph |
| 266 | +function contract_graph(node_positions::Vector{Tuple{Int, Int}}) |
| 267 | + # initiate UNodes |
| 268 | + |
| 269 | + n_list = Vector{UNode}(undef, size(node_positions)[1]) |
| 270 | + g = unitdisk_graph(node_positions) |
| 271 | + |
| 272 | + for ind, n_pos in enumerate(node_positions) |
| 273 | + uneighbors = neigbors(g, ind) |
| 274 | + unode = UNode(ind, n_pos, uneighbors) |
| 275 | + n_list[ind] = unode |
| 276 | + end |
| 277 | + |
| 278 | + xmin, xmax, ymin, ymax = find_boundaries(n_list) |
| 279 | + |
| 280 | + while (xmax - xmin > 1) and (ymax - ymin > 1) |
| 281 | + n_list = greedy_step(node_list, xmin, xmax, ymin, ymax) |
| 282 | + |
| 283 | + if xmin < xmax |
| 284 | + xmin += 1 |
| 285 | + xmax -= 1 |
| 286 | + end |
| 287 | + |
| 288 | + if ymin < ymax |
| 289 | + ymin += 1 |
| 290 | + ymax -= 1 |
| 291 | + end |
| 292 | + end |
| 293 | + |
| 294 | + locs_new = Vector{Tuple{Int, Int}}(undef, size(node_positions)[1]) |
| 295 | + for ind, un in enumerate(n_list) |
| 296 | + locs_new[ind] = un.pos |
| 297 | + end |
| 298 | + |
| 299 | + return locs_new |
| 300 | +end |
| 301 | + |
| 302 | +using .CompressUDG |
| 303 | +export contract_graph, CompressUDGMethod |
0 commit comments