Jump to content
  • Advertisement
Sign in to follow this  
duHOSSval

Weighted Graph using a matrix

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

If you intended to correct an error in the post then please contact us.

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.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.

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!