0

I have to read graph inputs (all at once) (an example below):

6 8 //no of vertices, edges
0 1 2 //connectivity from vertex 0 to vertex 1, with weight 2
0 2 1
0 4 2
1 2 4
2 3 5
3 4 5
4 1 2
4 5 5

What is the best way to read inputs dynamically. I have to read all the above content at once. I need to read it dynamically since the number of edges and vertices can change, with a maximun of 10000. The following doesn't work:

int *twod_array; 
int N,M; //no of vertices, edges
scanf("%d %d", &N, &M);
twod_array = (int *)malloc(sizeof(int)*N*M); //where N = no of rows, M = no of cols
for(i=0; i < N; i++) {
  for(j=0; j < M; j++) {
scanf("%d",&twod_array[i*M +j]);
  }
}
for(i=0; i < N; i++) {
  for(j=0; j < M; j++) {
    if(twod_array[i*M +j] == "\0") {
twod_array[i*M +j] = 0;
    }
  }
}

Also, is this the best way for graphs in C/C++ or is it better to use struct, since traversals will be done.

10
  • 2
    The following doen't work because (at least) 1. &N&M is invalid. it should be &N,&M 2. 2d_array is invalid as identifier since it cannot start with a number. 3. where N = no of rows, M = no of cols makes no sense as code. make it a comment. 4. N won't affect the number of rows nor cols. 5. M is number of rows, not cols. 6. the asssignment 2d_array[i*M +j] = "\0" make no sence. it should be comparation to '\0' or just nothing. Commented Nov 1, 2015 at 7:32
  • @MikeCAT - made the necessary changes. Still doesn't work as needed. Commented Nov 1, 2015 at 7:35
  • 2
    Also if(twod_array[i*M +j] = "\0") dosent compare...should be ==. Commented Nov 1, 2015 at 7:35
  • @amdixon - I am currently using C Commented Nov 1, 2015 at 7:36
  • 1
    @SachinS 1. The proglem of no. of rows and cols isn't corrected. 2. The comparation between integer and the pointer twod_array[i*M +j] == "\0" makes no sense. What do you want to do by it? Commented Nov 1, 2015 at 7:38

2 Answers 2

1

As far as loading that data in goes, there are lots of ways. One would be to make a connectivity struct, and dynamically allocate an array of those based on the number of edges value in the first line of the data file:

#include <stdio.h>
#include <stdlib.h>

struct connectivity {
    int source;
    int sink;
    int weight;
};

int main() {
    int num_verts = 0;
    int num_edges = 0;
    struct connectivity *edges = NULL;
    int i = 0;

    scanf("%d %d\n", &num_verts, &num_edges);

    edges = malloc(sizeof (struct connectivity) * num_edges);

    for (i = 0; i < num_edges; i++) {
        scanf("%d %d %d\n", &(edges[i].source), 
                            &(edges[i].sink),
                            &(edges[i].weight));
    }

    // use edges here

    free(edges);
}

Also, please use more legible variable names!

Sign up to request clarification or add additional context in comments.

2 Comments

Nitpick, but how about malloc(sizeof(*edges) * num_edges) to please purists like myself? :P
Not when fed to the sizeof operator. Instead, it'll give you the size of the type edges is pointing to.
1

From my experience, using an Edge struct will be very feasible to handle graph problems.

struct Edge{
    int to;
    int weight;
    Edge(int t, int w):to(t), weight(w){};
};
std::vector<vector<Edge> > graph;
int N,M; //no of vertices, edges
scanf("%d %d", &N,&M);
graph.resize(N);
int u, v, w;
for(int i = 0; i < M; i ++){
    scanf("%d %d %d", &u, &v, &w);
    graph[u].push_back(Edge(v, w));
}

1 Comment

This looks like C++, and the question is tagged C. Then again OP mentions "C/C++" in their question, so...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.