Tilted Forum Project Discussion Community  

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


 
 
LinkBack Thread Tools
Old 09-27-2005, 08:04 PM   #1 (permalink)
Lover - Protector - Teacher
 
Jinn's Avatar
 
Location: Seattle, WA
[Java/C++]Minimum Spanning Trees..

So I'm enrolled in -- by far -- the hardest class I've ever taken for my Comp Sci major (as of yet). It's a Design/Analysis class for Algorithms...

Has anyone implemented the Kruskal, Prim, or Dijkstra Algorithms for Minimum Spaninng Trees in Java? I'm having a very difficult time implementing these alogrithms because of the poor explanation by my professor. Searching around on google, I can't get anything more than the poor psuedo-code my instructor provided.

Quote:
T := {}
for i := 1 to n -1 {
do
(x,y) := getNextedge(G) using greedy selection mechanism
while (not cycle(x,y,G)
addEdge(x, y, T);
}

// assume G is a connected graph with N nodes
That's ALL the information I was provided, and was asked to implement it. That said, I'm not looking for a complete solution, although that wouldn't be refused.

I've implemented it with a java class Edge which is aware of its weight and its connecting nodes. It entirely works, except the "cycle()" method. I'm not sure how to make the program aware of a cycle? Each edge has no knowledge of the next edge to be added other than its verticies and weight.

AAAAAAH .. any help is appreciated, as it is approaching one week late as I've tried to implement it different ways..
__________________
"I'm typing on a computer of science, which is being sent by science wires to a little science server where you can access it. I'm not typing on a computer of philosophy or religion or whatever other thing you think can be used to understand the universe because they're a poor substitute in the role of understanding the universe which exists independent from ourselves." - Willravel
Jinn is offline  
Old 09-27-2005, 08:36 PM   #2 (permalink)
Free Mars!
 
feelgood's Avatar
 
Location: I dunno, there's white people around me saying "eh" all the time
That looks like pascel coding. The := sign gave it away.

Go to java.sun.com and punch in the search key Dijkstra and you'll get quite a few results relating to that particular algorithms
__________________
Looking out the window, that's an act of war. Staring at my shoes, that's an act of war. Committing an act of war? Oh you better believe that's an act of war
feelgood is offline  
Old 09-27-2005, 08:44 PM   #3 (permalink)
Lover - Protector - Teacher
 
Jinn's Avatar
 
Location: Seattle, WA
I'm not getting anything related to the alogrithms on java.sun.com
__________________
"I'm typing on a computer of science, which is being sent by science wires to a little science server where you can access it. I'm not typing on a computer of philosophy or religion or whatever other thing you think can be used to understand the universe because they're a poor substitute in the role of understanding the universe which exists independent from ourselves." - Willravel
Jinn is offline  
Old 09-27-2005, 09:07 PM   #4 (permalink)
Free Mars!
 
feelgood's Avatar
 
Location: I dunno, there's white people around me saying "eh" all the time
Sorry, forgot to mention to look within the forums. Sun Microsystems doesn't offer alot of article on that but there is a lot of threads on that.

Dijkstra Search Results
Kruskal Search Results
__________________
Looking out the window, that's an act of war. Staring at my shoes, that's an act of war. Committing an act of war? Oh you better believe that's an act of war
feelgood is offline  
Old 09-27-2005, 09:19 PM   #5 (permalink)
Dreams In Digital
 
SiNai's Avatar
 
Location: Iowa
MMMmmmmmmm...

We went over these briefly in my Algorithms class last semester. I dug up the online notes, and they skim Dijkstra's-

http://www.cns.uni.edu/~wallingf/tea...session27.html

Here's something I found for Kruskal's and Prim's:

http://www.people.vcu.edu/~gasmerom/MAT131/mst.html

I must admit.. I've forgotten these already.... But I hope that helps just a little!
__________________
I can't seem to remember now
What it was like- to live life, before you.. symbiont
SiNai is offline  
Old 09-27-2005, 11:02 PM   #6 (permalink)
Lover - Protector - Teacher
 
Jinn's Avatar
 
Location: Seattle, WA
Still stuck -- god I hate programming.. if I dont sleep for the next 7 hours until work, I might get somewhere.. eyes burning already.
__________________
"I'm typing on a computer of science, which is being sent by science wires to a little science server where you can access it. I'm not typing on a computer of philosophy or religion or whatever other thing you think can be used to understand the universe because they're a poor substitute in the role of understanding the universe which exists independent from ourselves." - Willravel
Jinn is offline  
Old 09-28-2005, 04:19 AM   #7 (permalink)
Junkie
 
nukeu666's Avatar
 
Location: India
i have that same subject this semester...the prof sucks(as do most of them)
we had to make an implimentation of krustuls algo during the last lab class
here it is in C
good luck understanding it...all i know it worked..eureka
Code:
#include<stdio.h>
int allconn(int con[][10],int no)
{
  int q,w;
  for(q=0;q<no;q++)
    for(w=0;w<no;w++)
      if(con[q][w]==0)
        return 0;
  return 1;
}
int moresameweight(int min,int flag[][10],int weight[][10],int no,int *q1,int *w1)
{
  int q,w;
  for(q=0;q<no;q++)
    for(w=0;w<no;w++)
      if(min==weight[q][w]&&flag[q][w]==0)
        {
          *q1=q;*w1=w;
          return 1;
        }
  return 0;
}
int main()
{
  int noofno,weight[10][10],flag[10][10],con[10][10];
  int next=0,q1,w1,q=1,w=1,e,minfound=100;
  printf("Number of nodes:");
  scanf("%d",&noofno);
  for(q=0;q<noofno;q++)
    for(w=0;w<noofno;w++)
      {weight[q][w]=50;flag[q][w]=0;con[q][w]=0;con[q][q]=1;}
  printf("Enter node1,node2,weight (0 0 0 to end):\n");
  while(!(q==0&&w==0))
    {
      scanf("%d %d %d",&q,&w,&e);
      weight[q-1][w-1]=e;weight[w-1][q-1]=e;
    }
while(allconn(con,noofno)==0)
{
  if(minfound==100&&next==0)
    {
      next=1;
      for(q=0;q<noofno;q++)
        for(w=0;w<noofno;w++)
          if(minfound>weight[q][w])
            {minfound=weight[q][w];q1=q;w1=w;}
      flag[q1][w1]=1;flag[w1][q1]=1;con[q1][w1]=1;con[w1][q1]=1;
      printf("new con:%d %d\n",q1+1,w1+1);
    }
  if(minfound>49&&next==1)
    {printf("over\n");
      exit(0);
    }
  while(moresameweight(minfound,flag,weight,noofno,&q1,&w1)==0)
    {
      minfound++;
    }
  flag[q1][w1]=1;flag[w1][q1]=1;
  con[q1][w1]=1;con[w1][q1]=1;
  printf("new con:%d %d\n",q1+1,w1+1);
  for(q=0;q<noofno;q++)
    for(w=0;w<noofno;w++)
      for(e=0;e<noofno;e++)
        {
          if(con[q][w]==1&&con[w][e]==1)
            {
              con[q][e]=1;con[e][q]=1;
            }
        }
}
 return 0;
}
__________________
Why did the Comp. Engineer get X-mas and Halloween mixed up?
Because Oct(31) == Dec(25)
nukeu666 is offline  
 

Tags
java or c, minimum, spanning, trees


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 05:28 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