1.11M Members

directed graph

 
0
 

So I need help asap. I can't figure out how to modify code from my book to work with my problem.
I'm givin an input file, the first value is the dimension of an n x n matrix. every value after is a weight for each vertice for the matrix. I'm unable to insert edges into an adjacency list properly.
input is as follows:
4
1 -2 3 2
2 2 2 1
2 1 2 1
-3 3 2 0

Initially moves are vertical and horizontal. when you land on negative moves turn to diagonal. goal is to reach 0, while starting at the top left 1.

These are the structs from the book:

struct edgenode{
    edgenode()
        :y(0),weight(0), next(NULL)
    {};
    edgenode(int newWeight)
        :y(0), weight(newWeight), next(NULL)
    {};
    edgenode(int newAdj, int newWeight)
        :y(newAdj),weight(newWeight), next(NULL)
    {};

    int y;
    int weight;
    edgenode* next;

};
struct graph{
    edgenode* edges[MAXV+1]; //adjacency list
    int degree[MAXV+1];      //outdegree of each vertx
    int nvertices;           //number of vertices in graph
    int nedges;              //number of edges in graph
    int directed;            //is graph directed
};

this is the insertion from the book:

void insert_edge(graph* g, int x, int y, bool directed, int w){
    edgenode *p;

    p = new edgenode;

    p->weight = w;
    p->y = y;
    p->next = g->edges[x];

    g->edges[x] = p;
    g->degree[x]++;

    if(directed == false)
        insert_edge(g,y,x, true, w);
    else
        g->nedges++;


}

This is the function that uses the insert:

void read_graph(graph* g, bool directed, ifstream* input){
    int m;
    int x,y;

    initialize_graph(g, directed);

    //source is an input.txt file
    *input >> m;

    g->nvertices = m * m;

    for(int j=1; j <= g->nvertices; j++)
        *input >> weightArray[j];


    for(int i = 1; i <=g->nvertices; i++){
        //this needs to be changed******orignally looked like this
        // cin >> x >> y*********** but i need to use input file values
        //also might need a second for loop to add every edge to each row*******
        x = i;
        y = i;

        insert_edge(g,x,y,directed, weightArray[i]);
    }
}
You
Post:
Start New Discussion
View similar articles that have also been tagged: