Working on a program where the program will read in an adjacency matrix and create a .dot file for GraphViz using that information. All class code is below.
I am only trying to create three files, and all three are being properly created and filled with the header text, but it's not filling in the graph information for some reason. I'm assuming a flaw in the way the CreateEdges function is coded, but I don't see what's wrong with it. Can anyone help?
Code:
// graph.h
#include <iostream>
#include <fstream>
#include "string"
#pragma once
class graph
{
private:
bool adjacency[10][10];
int VertCount;
bool isSymmetrical;
public:
graph();
~graph();
graph(bool adj[][10], int count);
void setSymmetry();
void CreateEdges(std::ofstream &outfile);
void GraphVizOut(std::string filename);
};
// graph.cpp
#include "graph.h"
#include <fstream>
#include "string"
graph::graph()
{
}
graph::~graph()
{
}
graph::graph(bool adj[][10], int count)
{
VertCount = count;
for (int x = 0; x < count; x++)
{
for (int y = 0; y < count; y++)
{
adjacency[x][y] = adj[x][y];
}
}
setSymmetry();
return;
}
void graph::setSymmetry()
{
for (int x = 0; x < VertCount; x++)
{
for (int y = x; y < VertCount; y++)
{
if (adjacency[x][y] != adjacency[y][x])
isSymmetrical = false;
return;
}
}
isSymmetrical = true;
return;
}
void graph::CreateEdges(std::ofstream &outfile)
{
// If matrix is symmetrical, graph is undirected
if (isSymmetrical)
{
for (int x = 0; x < VertCount; x++)
{
for (int y = x; y < VertCount; y++)
{
if (adjacency[x][y])
outfile << x << " " << "--" << y << "\n";
}
}
}
// If matrix is not symmetrical, graph is directed
else
{
for (int x = 0; x < VertCount; x++)
{
for (int y = 0; y < VertCount; y++)
{
if (adjacency[x][y])
outfile << x << "--" << y << "\n";
}
}
}
return;
}
void graph::GraphVizOut(std::string filename)
{
std::ofstream VizOut;
VizOut.open(filename);
VizOut << "// Trey Brumley \n";
VizOut << "// File created by C++ GraphGen Project \n";
VizOut << "graph G { \n";
CreateEdges(VizOut); // This isn't working here, for whatever reason. Everything on either side is, but not this.
VizOut << "} \n";
VizOut.close();
return;
}
// main.cpp
#include <fstream>
#include "graph.h"
#include "string"
using namespace std;
int main()
{
ifstream infile;
infile.open("input.txt");
char c;
int i, count = 0;
string fileName1 = "graph0.dot", fileName2 = "graph1.dot", fileName3 = "graph2.dot";
while (infile>>c)
{
bool adjacencies[10][10];
infile >> i;
// Loops looking for 1s and 0s to "store" in adjacency matrix
for (int x = 0; x < i; x++)
{
for (int y = 0; y < i; y++)
{
infile.get(c);
while (c != '1' && c != '0')
infile.get(c);
if (c == '1')
adjacencies[x][y] = true;
else if (c == '0')
adjacencies[x][y] = false;
}
}
for (int x = 0; x < i; x++)
{
for (int y = 0; y < i; y++)
{
cout << adjacencies[x][y] << " ";
}
cout << endl;
}
cout << endl << endl;
graph G(adjacencies, i);
if (count == 0)
G.GraphVizOut(fileName1);
else if (count == 1)
G.GraphVizOut(fileName2);
else if (count == 2)
G.GraphVizOut(fileName3);
count++;
}
system("pause");
return 0;
}