tags:

views:

428

answers:

2

Hi, How do I create a C++ weighted Graph where each vertex in the graph has a weight (some integer value)?

You can download my graph project here (RapidShare):

Here is the function to create a graph from graph data stored in a text file:

void GraphType::createGraph()
{
    ifstream infile;
    char fileName[50];

    int index;
    int vertex;
    int adjacentVertex;

    if(gSize != 0)
        clearGraph();

    cout << "Enter input file name: ";
    cin >> fileName;
    cout << endl;

    infile.open(fileName);

    if(!infile)
    {
            cout << "Cannot open input file." << endl;
            return;
    }

    infile >> gSize;

    graph = new UnorderedLinkList[gSize];

    for(index = 0; index < gSize; index++)
    {
            infile >> vertex;
            infile >> adjacentVertex;

            while(adjacentVertex != -999)
            {
                graph[ vertex ].insertLast(adjacentVertex);
                infile >> adjacentVertex;
            }
    }
    infile.close();
}

And here is the Graph data (number of vertices = 10, vertex 0 to 9 and adjacent vertices) input from text file "Network2.txt":

10

0 1 2 9 -999

1 0 2 -999

2 0 1 9 8 3 -999

3 2 8 5 -999

4 3 8 6 5 -999

5 4 6 7 -999

6 4 7 8 -999

7 8 6 5 -999

8 9 2 3 4 6 7 -999

9 0 2 8 -999

My question is, how do i assign a unique value or weight to vertices 0 to 9? Any assistance will be really appreciated. Thanks in advance!

+1  A: 

In your adjacency list, instead of having it just store the indexes of the adjacent node, have it store a struct that contains the indexes of the adjacent node, and a value for the edge connecting those nodes.

Nali4Freedom
Thank you Nali4Freedom for replying.Your answer sounds very interesting. However, after some thought, I am still not sure how to implement your it into my project. Tell me if I am far off from your solution with this code below:struct Vertex{ int numVertices; int vertex; int adjacentVertices[]; int weight; };int main(){ Vertex v0; v0.vertex = 0; v0.adjacentVertices[0] = 1; v0.adjacentVertices[1] = 2; v0.adjacentVertices[2] = 9; v0.weight = 19; Vertex v1; //... system ("pause"); }
01010011
That code seems like you're giving weights to verticies, not edges between verticies. I was thinking this: http://pastebin.com/m1f8ca859 which is a much simpler change to your code. That way each edge has it's own weight. I just set all the weights to 1 in that example, you would probably want to get them from the input file.
Nali4Freedom
+3  A: 

The Boost Graph Library (BGL) offers type MutablePropertyGraph, within which each edge and vertex can store a weight as a property. See the example here, which builds up a directed graph with weighted edges.

seh
Thanks very much Seh for reminding me that I need to start learning and using the Boost Library. I am downloading it as I write this and can't wait to try out the MutablePropertyGraph.Unfortunately, for this class exercise, I have to create the graph and all of its functions from scratch! But thanks for the very useful link!
01010011