Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for reading (and writing) .graph files #178

Open
ardasener opened this issue Sep 26, 2022 · 3 comments
Open

Add support for reading (and writing) .graph files #178

ardasener opened this issue Sep 26, 2022 · 3 comments
Assignees
Labels
state: inactive Issue is abandoned, but might become relavent at some point type: feature Brand new functionality, features, workflows, endpoints, etc

Comments

@ardasener
Copy link
Contributor

ardasener commented Sep 26, 2022

SparseBase should provide Readers for METIS GRAPH Files. This is quite a flexible format that can contain both vertex and edge weights.

The code snippet below is one I used in another project and reads the file into a CSR format. Note that this code was not of critical importance so it isn't really optimized. It only supports vertex weights. The x variable on the first line should be checked for values other than 10 and the reader should act accordingly. Here are the possible values and their meanings:

%  FMT has the following meanings:
%    0  the graph has no weights (in this case, you can omit FMT);
%    1  the graph has edge weights;
%   10  the graph has vertex weights;
%   11  the graph has both edge and vertex weights.
int new_reader_read_graph(const char* input_file, vtx_type* nvtxs, adj_type** xadj, vtx_type** adj, wgt_type** vwgt, wgt_type** adjwgt){

	vtx_type n;
	adj_type m;

	ifstream ifs(input_file);

	cout << "Reading graph from: " << input_file << endl;

	string line;
	bool first_line = true;
	size_t last_index = 0;
	vtx_type u = 0;
	while(getline(ifs,line)){
		
		if(line[0] == '%'){
			continue;
		}

		stringstream ss(line);

		if(first_line){
			
			int x;
			ss >> n >> m >> x;
			m *= 2;
			cout << "N=" << n << " M=" << m << endl;
			if(x != 10){
				cout << "File must have vertex weights" << endl;
				return 2;
			}
			first_line = false;

			*xadj = new adj_type[n+1];
			(*xadj)[0] = 0;
			*adj = new vtx_type[m];
			*vwgt = new wgt_type[n];
			
		} else {
			wgt_type w;
			ss >> w;
			vtx_type v;
			while(ss >> v){
				(*adj)[last_index++] = v-1;					
			}
			(*xadj)[u+1] = last_index;
			(*vwgt)[u] = w;
			u++;
		}
	}

	cout << "Done reading graph" << endl;

	*nvtxs = n;

	return 1;
}
@ardasener ardasener self-assigned this Sep 26, 2022
@ardasener ardasener added this to the Milestone 3 milestone Sep 26, 2022
@ardasener ardasener added the type: feature Brand new functionality, features, workflows, endpoints, etc label Sep 26, 2022
@AmroAlJundi AmroAlJundi removed this from the Milestone 3 milestone Sep 30, 2022
@ardasener
Copy link
Contributor Author

The main issue right now is that this file format can contain vertex weights which the current Format system won't represent. And since this file format is designed to be used for graphs, it is logical to return Graph objects from the reader. We do have a Graph class but it hasn't been updated in a while. So we should probably delay this until we update that.

@AmroAlJundi
Copy link
Contributor

@ardasener
Copy link
Contributor Author

ardasener commented Nov 18, 2022

Here is a library with a .graph reader: https://github.com/dlasalle/wildriver. Looks like it hasn't been updated in a while and I had trouble with it in another project. So I suggest using it as a reference, not adding it as a dependency.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
state: inactive Issue is abandoned, but might become relavent at some point type: feature Brand new functionality, features, workflows, endpoints, etc
Projects
None yet
Development

No branches or pull requests

2 participants