im looking at creating an adjacency matrix built with rows r1, and columns c1, with a float value.

i have managed to read the the values from a text file, set out like:
r1,c1,float1
r2,c2,float2
etc...

i am having trouble converting these values into an adjacency matrix.

thanks

Dani AI

Generated

described the common CSV layout (row,col,weight). Several good directions are already suggested: (use a matrix class), (dynamic 2‑D container), and (watch assumptions about indices). The approach below ties those together and shows a robust, practical pattern that avoids knowing the matrix size up front and handles common pitfalls.

Do a two-pass read: first scan the file to discover the minimum/maximum row and column indices, then allocate a matrix of the exact size and fill it on a second pass. Tracking min as well as max makes the code tolerant of files that use 0-based or 1-based indexing (or even non-zero starts). Choose an explicit sentinel for “no edge” (0.0, NaN, or numeric_limits<float>::infinity(), depending on algorithm needs). Example pattern:

// First pass: discover min/max indices
int minR = INT_MAX, minC = INT_MAX, maxR = INT_MIN, maxC = INT_MIN;
// parse each line into ints r,c and float w and update min/max

// Allocate: rows = maxR - minR + 1; cols = maxC - minC + 1
// Matrix M(rows, vector<float>(cols, NO_EDGE));

// Second pass: parse again and store at M[r - minR][c - minC] = w;
// If graph is undirected, also set M[c - minC][r - minR] = w
// For duplicates decide policy: overwrite, keep min, or accumulate.

Corner cases and troubleshooting: confirm whether indices in the file are 0- or 1-based (min/max trick handles both); trim whitespace and ignore blank/comment lines; handle malformed lines with try/catch around stoi/stof; beware Windows CR at line ends; pick a storage strategy for very sparse graphs (adjacency list or hash map instead of an N x N matrix). For weighted graphs, decide whether absent edges are 0 or +INF depending on algorithms used (Dijkstra/Floyd require different sentinels).

This strategy addresses the allocation issue warned about, uses a dynamic container as suggested, and avoids the fragile “line number as index” assumption mentioned by .

Recommended Answers

All 5 Replies

Do you have a matrix class? If not, I'd recommend vnl_matrix from VNL (part of VXL). You can then just do:

yourMatrix(r1,c1) = float1

Dave

Do you have a matrix class? If not, I'd recommend vnl_matrix from VNL (part of VXL). You can then just do:

yourMatrix(r1,c1) = float1

Dave

we haven't studied that yet, so id say no.

Well to make an adjacency matrix, you'll definitely need a matrix! You could use a 2D array: . The problem with this is that you'll have to know the size of the matrix before you start, which is not always the case.

Dave

If your file structure is consistent to the sample you provided, you could also try:
(In pseudo-code)

{ // Inside your file-reading loop
  Matrix[num][num] = floatnum; // where num = line number
}

Or you could use a class to represent your matrix, as daviddoria suggested, which would be easier to manage, on the whole :)

You don't necessarily need a matrix class. All you need is arrays. Better yet, you
should use std::vector < std::vector<float> >, as a 2d array.
Adjacency matrix are fairly easy to implement with the cost of O(row^2) of space.
What exactly are you having trouble with, in respect to Adjacency matrix.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.