Tilted Forum Project Discussion Community  

Go Back   Tilted Forum Project Discussion Community > Interests > Tilted Technology


 
 
LinkBack Thread Tools
Old 01-22-2004, 07:04 PM   #1 (permalink)
Banned
 
Location: 'bout 2 feet from my iMac
deleting elements in an array...

Ok, I'm working on my algorithms homework, and I come across this problem:

Quote:
Describe how one can implement each of the following operations on an array so that the time it takes does not depend on the array's size n.
a. Delete the ith element of an array(1<=i<=n).
b. Delete the ith element of a sorted array (the remaining array has to stay sorted, of course.)
now, I'm honestly baffled and the chapter has nothing to say about array element deletion. Because in Data Structures they tought us that array's downside was that they were slow to delete and insert into the middle of the array, because a new array had to be created, and every value had to be copied in, with the correct value added or left out. now, this question implies that is not true, and I have no clue how that could be. Any hints?
cheerios is offline  
Old 01-22-2004, 07:28 PM   #2 (permalink)
kel
WARNING: FLAMMABLE
 
Location: Ask Acetylene
You mean a regular array, as in the C++ declaration
Code:
int x[100];
?

You don't normally delete elements from an array.
I don't know of any data structure that supports deletion in O(1) time.

I mean a hash table can do that if the set of keys is the same size as the potential set of elements. But that isn't much different then deleting from a regular array.
__________________
"It better be funny"

Last edited by kel; 01-22-2004 at 07:34 PM..
kel is offline  
Old 01-22-2004, 07:41 PM   #3 (permalink)
Banned
 
Location: 'bout 2 feet from my iMac
yeah, i mean a regular array. that's why this has me baffled. 'cuz you don't delete out of an array 'cuz it's a waste of time!
cheerios is offline  
Old 01-22-2004, 08:39 PM   #4 (permalink)
beauty in the breakdown
 
Location: Chapel Hill, NC
Ha, there was a CS major explaining this to someone in the library this afternoon. I didnt join in because I dont know much about programming, but IIRC, she said just what you did--you cant really delete them, you have to make a new array minus the element you wish to delete, or, if you can, just set the element equal to null.
__________________
"Good people do not need laws to tell them to act responsibly, while bad people will find a way around the laws."
--Plato
sailor is offline  
Old 01-22-2004, 08:56 PM   #5 (permalink)
kel
WARNING: FLAMMABLE
 
Location: Ask Acetylene
Creating a new array and copying is an O(n) operation so that can't be it. Arrays don't have null values, unless they are arrays of pointers, which isn't specified.

Question = nonsensical.
__________________
"It better be funny"
kel is offline  
Old 01-23-2004, 07:06 AM   #6 (permalink)
Upright
 
Location: MN, USA
Well, I agree that the question doesn't seem to make a lot of sense.

The closest thing I can come to that's a "real" answer (i.e., not just 'set the array element to null') is this:

for(j = i; j < array.length - 1; j++) {
array[j] = array[j + 1];
}
array[array.length] = null;

Although in a worst-case analysis this is in fact O(n), it doesn't depend directly on the length of the array in the same way that copying all the elements does.

Be sure to let us know what the answer is!
hlprmnky is offline  
Old 01-23-2004, 09:08 AM   #7 (permalink)
The GrandDaddy of them all!
 
The_Dude's Avatar
 
Location: Austin, TX
i'm thinking hash tables here.

the actual table being a linked list which keeps track of the previous and the next elements.

if the hash table is organized by element #, then u should be able to go into x element and remove it.

same thing with the b part.
__________________
"Luck is what happens when preparation meets opportunity." - Darrel K Royal
The_Dude is offline  
Old 01-23-2004, 01:12 PM   #8 (permalink)
Banned
 
Location: 'bout 2 feet from my iMac
well, I went in to ask, but our department is going through acredditation this semester and the entire dept is out for meetings the whole day. I am NOT amused. So, your (and my) answer will have to wait until monday.
cheerios is offline  
Old 01-23-2004, 01:51 PM   #9 (permalink)
Wehret Den Anfängen!
 
Location: Ontario, Canada
Code:
void delete_ith(element_type* array, int* array_length, int i) {
  array[i] = array[*array_length-1];
  *array_length = *array_length-1;
}
Shortens the "length" of the array by one, deleting the ith element.

The actual memory allocated to the array might be the same, but 1 less element is used, and the ith element has been deleted/overwritten.

Quote:
b. Delete the ith element of a sorted array (the remaining array has to stay sorted, of course.)
In levels of increasing incorrectness:
1> it can't be done.
2> use markers for "unused value".
3> Implement a hash table instead of an array. Takes practical constant time, theoretical (when you pay attention to the dots on the i's) lg n time.
4> Implement a skip list instead of an array. Takes probabalistic-average lg n time.
__________________
Last edited by JHVH : 10-29-4004 BC at 09:00 PM. Reason: Time for a rest.
Yakk is offline  
Old 02-21-2009, 01:08 AM   #10 (permalink)
42, baby!
 
Dragonlich's Avatar
 
Location: The Netherlands
Quote:
Originally Posted by cheerios View Post
Describe how one can implement each of the following operations on an array so that the time it takes does not depend on the array's size n.
a. Delete the ith element of an array(1<=i<=n).
b. Delete the ith element of a sorted array (the remaining array has to stay sorted, of course.)
If you could solve both problems independently:
a) just set array[i ] to zero/null.
b) use linked lists (pointers). simply drop item i, have item [i-1] point to item [i+1].

To solve both problems at once seems impossible. With a normal array, problem b seems impossible, because sorting an array takes n steps at least. With linked lists, problem a seems impossible, because deleting item i is dependent on the size of n (you have to search for the item).

I got the big olde book of data structures, which tells me that no matter which data structure you use, either a or b depends on the array size n in some way. The most efficient option I saw was a heap (https://users.cs.jmu.edu/bernstdh/we...troduction.php).
Dragonlich is offline  
Old 02-21-2009, 08:05 AM   #11 (permalink)
Darth Papa
 
ratbastid's Avatar
 
Location: Yonder
Really? 5-year-old programming homework solutions?
ratbastid is offline  
Old 02-21-2009, 05:09 PM   #12 (permalink)
Junkie
 
Location: San Francisco
Okay I just rewrote the answer to part (a) then realized that was exactly what Yakk already said... Heh. The null thing is even worse because then you need to rewrite all your code to check for nulls whenever you iterate or dereference an array (and you WILL forget that little nested for loop that causes your program to randomly crash and burn), and you can't have a valid value equal to null in the array.

For the sorted array it's a little trickier because you need to move things around so it's still dependent on the size of the array. I don't believe it's technically possible to do that for any given sorted array without resorting to even worse hackery which kind of defeats the whole purpose of using one. If you really need to do deletions in O(1) time maybe you are using the wrong data structure. I would be really curious to find out the instructor's 'answer' to part (b), but I don't imagine cheerios will be back to tell us.

A hash table supports deletion in O(1) time, but by most definitions a hash table is not an array. There are exceptions such as in PHP where an 'array' is actually implemented as a hash table, but a PHP array is quite different from a traditional low-level array.
__________________
"Prohibition will work great injury to the cause of temperance. It is a species of intemperance within itself, for it goes beyond the bounds of reason in that it attempts to control a man's appetite by legislation, and makes a crime out of things that are not crimes. A Prohibition law strikes a blow at the very principles upon which our government was founded." --Abraham Lincoln

Last edited by n0nsensical; 02-21-2009 at 06:31 PM.. Reason: i am all fucked up!
n0nsensical is offline  
Old 02-21-2009, 06:29 PM   #13 (permalink)
Young Crumudgeon
 
Martian's Avatar
 
Location: Canada
Quote:
Originally Posted by ratbastid View Post
Really? 5-year-old programming homework solutions?
Apparently. For a banned member, no less.
__________________
I wake up in the morning more tired than before I slept
I get through cryin' and I'm sadder than before I wept
I get through thinkin' now, and the thoughts have left my head
I get through speakin' and I can't remember, not a word that I said

- Ben Harper, Show Me A Little Shame
Martian is offline  
Old 02-21-2009, 09:26 PM   #14 (permalink)
Darth Papa
 
ratbastid's Avatar
 
Location: Yonder
Quote:
Originally Posted by n0nsensical View Post
that was exactly what Yakk already said...
...five YEARS ago!!!

I have to admit. I miss cheerios a lot.
ratbastid is offline  
Old 02-21-2009, 09:31 PM   #15 (permalink)
Junkie
 
Location: San Francisco
Quote:
Originally Posted by ratbastid View Post
...five YEARS ago!!!

I have to admit. I miss cheerios a lot.
I remember when she got banned, I couldn't figure it out, hell I still don't know.
__________________
"Prohibition will work great injury to the cause of temperance. It is a species of intemperance within itself, for it goes beyond the bounds of reason in that it attempts to control a man's appetite by legislation, and makes a crime out of things that are not crimes. A Prohibition law strikes a blow at the very principles upon which our government was founded." --Abraham Lincoln
n0nsensical is offline  
 

Tags
array, deleting, elements


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 02:35 AM.

Tilted Forum Project

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Search Engine Optimization by vBSEO 3.6.0 PL2
© 2002-2012 Tilted Forum Project

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 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360