Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 11-17-2004, 12:58 AM   #1 (permalink)
toast28
Registered User
 
Join Date: Sep 2004
Posts: 21
toast28 is on a distinguished road
Graphs and matrices

OK, so I have an assignment due about graphs. We are to use the floyd warshall algorithum to find the diameter of each graph. I have some code for the algorithum, but I am getting some strange results. Using the graph info below, when I get to 0-7, instead of taking the 8, it finds the -1 part of the equation and skips the if statement. Anyone know what I need to add to avoid mistakes like this?

I know where the mistake is, but not the logical answer to it. I bolded it so you can find it fast. Any help will be appreciated.

Code:
void map::floyd()
{
	//initalize path matrix
	for(int i=0;i<MAX_NODES;i++)
	{
		for(int j=0;j<MAX_NODES;j++)
		{
			if(adj[i][j]!=-1)
				path[i][j] = j;
			else
				path[i][j] = -1;

		}
	}


	//floyd warshall alg
	for(i=0;i<N;i++)
	{
		for(int j=0;j<N;j++)
		{
			for(int k=0;k<N;k++)
			{
				if((adj[i][j] > adj[i][k] + adj[k][j]) && 
					(path[i][k] !=-1 && path[j][k] !=-1) &&
					(adj[i][k] != -1 && adj[k][j] != -1))
				{
					adj[i][j] = adj[i][k] + adj[k][j];
					//adj[j][i] = adj[i][j];
					path[i][j] = path[i][k];
				}
			}
		}
	}

}
Code:
11   13
0    3     -1     -1     -1      12     10     -1     -1     -1     -1
1   -1     -1     -1      2       7      8     -1     -1      1
2    2     -1     -1     -1      -1     -1      8     -1
3   -1     -1     -1     -1      -1      4     -1
4   -1     -1     -1     -1      -1     -1
5    3     -1     -1     -1      -1
6   -1      5     -1     -1
7    5     -1     -1
8   -1     -1
9   -1
10
toast28 is offline   Reply With Quote
Old 11-17-2004, 01:03 AM   #2 (permalink)
toast28
Registered User
 
Join Date: Sep 2004
Posts: 21
toast28 is on a distinguished road
by "get to 0-7", i mean i=0, j=7, and k would = 1
toast28 is offline   Reply With Quote
Old 11-17-2004, 02:25 AM   #3 (permalink)
toast28
Registered User
 
Join Date: Sep 2004
Posts: 21
toast28 is on a distinguished road
ok, yet again I have figured out my problem and I'm off to find new ones. Seems the next hardest thing is going to be finding what nodes are in what components. Any ideas?
toast28 is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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 06:40 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting