# Weighted Graph using a matrix

## Recommended Posts

I'm having a small problem implementing my weighted graph. I have a class called Graphs, and from there i created a class for a graph using a matrix, a graph using a linked list, and now im creating a graph that is weighted. I'll put what i have for my weighted class below. Basically what im having trouble with is i dont seem to be getting random numbers for my "weights". In my main method i have this, where c is the weight being randomized.
[/Random rand = new Random( 37 );
for( i = 0; i < E; i++ )
{
a = rand.nextInt( V );
b = rand.nextInt( V );
c = rand.nextInt( V );
}]

gm and gl are for the other classes so just disregard those.  the wgm.addEdge is my method for adding an edge, a and b are setting the vertices and those work fine.  Here is the rest of my code for the weighted class, any suggestions would be great.

[/class WGraphM extends Graphs
{
int data[][];
int V;

public WGraphM()
{
data = new int [10][10];
}
public WGraphM(int v)
{
V = v;
data = new int [V][V];
for(int i = 0; i < V; i++)
{
for( int j = 0; j < V; j++)
{
data[i][j] = 1;
}
}
}
public void addEdge(int a, int b, int c)
{
data[a][b] = c;
data[b][a] = c;
}

public void deleteEdge(int a, int b)
{
data[a][b] = 0;
data[b][a] = 0;
}
public int adjacent( int a, int b)
{
return data[a][b];
}
public void display()
{
for( int i = 0; i < V; i++)
{
System.out.print(i + ": ");
for(int j = 0; j < V; j++)
{
if(data[i][j] > 0)
{
System.out.print(j + " ");
}
}
System.out.println();
}
}
}]

also trying, in my display method, to display the weight with each edge.  Any help will be greatly appriciated.


##### Share on other sites
I would appreciate any help i could get

##### Share on other sites
What output are you getting, and what output do you expect?

If you're saying the output is the same every time you run the program, then that's to be expected; you're giving the RNG the same seed each time.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628342
• Total Posts
2982177

• 9
• 24
• 9
• 9
• 13