Weighted Graph using a matrix

Started by
1 comment, last by dwahler 15 years, 4 months ago
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.addEdge( a , b );
      gl.addEdge( a, b );
      wgm.addEdge(a, b, c );
    }]

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.
Advertisement
I would appreciate any help i could get
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.

This topic is closed to new replies.

Advertisement