Public Group

# Weighted Graph using a matrix

This topic is 3671 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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[j] = 1;
}
}
}
public void addEdge(int a, int b, int c)
{
data[a] = c;
data[a] = c;
}

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

1. 1
2. 2
Rutin
16
3. 3
4. 4
5. 5

• 26
• 11
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633709
• Total Posts
3013478
×