Graph -Adjaceny matrix




Comment hidden because of low score. Click to expand.
0
of 0 vote

For starters Im sure you know that the adjacency matrix is one of the worst ways to represent a graph because of its large memory requirements (n^2).

To determine if a vertex is adjacent to another we need to scan the matrix in the row i and column y, if the value at Matrix[i][j] == 1 then those vertex are adjacent. a common variation to this problem might be in a weighted graph, were, instead of 1 and 0s you have actual numbers representing the cost. that does not seem to be the case here

So the first thing you need to do is to represent the adjacency matrix in memory thats a 2D array.

To dinamycally allocate a 2D array in the following code might do the trick, (please keep in mind Im a CPP programer so the syntax might be a bit off)

int matrixSize = getVertexCount();//assumed to return an int with number of vertices

int** Matrix = new int*[matrixSize];
for (int i=0; i<matrixSize; i++){
  Matrix[i] = new int[matrixSize];
}

Now you have the array, its time to fill it up, please not that new should be replaced with malloc() in the above code to comply with the C ONLY requirement.

Im assuming that your file contains the actual number of vertices and that you have that number stored in a variable already... in case you dont, all you gotta do to get is scan the first line of the file counting the numbers of 'comas' and adding one to it

//always a good idea to get strings by reference, this method reads a line of text
//and fill its corresponding row
void fillMatrixRow(int rowNum, const string &data){
	vector<string> numbers = splitString(data, ',');//assuming we have this somewere
        for(int i=0; i< numbers.size(); i++){
           int n = atoi(numbers[i],10);
           Matrix[rowNumber][i] = n;
        }
}

void fillMatrixFromDisk(const char* fileName){
	fstream f(fileName);
	if ( !f.is_open() ) return;
	int currentRow =0;
	while (!f.eof){
	  std::getline(f, currentLine);
          fillMatrixRow(currentRow, currentLine);
	}
}

Ok so now you have a 2D array filled from the info on th HDD all that is left is to read i and j from the console and ouput the result

int main(){
	int i,j;
	cout << "Value of i:";
        cin >>i;
	cout << "Value of J:";
        cin >>j;

	if (Matrix[i][j] == 1 )
		cout << "Vertex are adjacent";
        else
		cout << "Vertex are NOT adjacent";

return 1;
}

Thats pretty much it, most of the code is C++ all you gotta do is replace cout, with pritnf, and cin with scanf, also the string tokenization used an STL vector that you might need to replace with a buch of calls to strtok().

I hope you get the gist, an apologies for the code it was written straight into the comment box without double checking if the syntax was ok

- Red October 21, 2014 | Flag Reply




Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More