我有一个图形着色程序,它将2D数组作为邻接矩阵,并根据该邻接矩阵对图形进行着色。此文件为Color.cpp
。我有另一个程序创建这个邻接矩阵并将其放在一个文本文件中,这个程序AdjacencyMatrix.cpp
。
当我最初创建着色程序时,我将邻接矩阵硬编码到2D数组graph[V][V]
中,其中V是顶点数。
这是两个独立的程序,我只是将AdjacencyMatrix.cpp
创建的邻接矩阵复制并粘贴到Color.cpp
中。
因为我的程序已经在其中创建了一个带有邻接矩阵的文件,所以我想让我的着色程序读取该文件。我知道最佳实践是将它们组合在一起,并将存储在数组中的矩阵传递到一个有凝聚力的程序中的着色程序中。我认为在这一点上阅读文件是我最简单的解决方案,但如果更有经验的用户可以告诉我如何将矩阵存储在数据结构中并将其传递给着色程序,我欢迎它。
AdjacencyMatrix.cpp
该程序接收一个文件,该文件是作为相邻对列出的整数字符串,并从中创建一个邻接矩阵,然后将其存储在一个文件中。文件“24cell_skeleton.txt”是它读入的文件,“out.txt”是它打印矩阵的文件。
#include <string>
#include <sstream>
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
const int cellSize = 24;
bool graph[cellSize][cellSize];
int prevNum = -1;
int currNum = -1;
bool markMatrix(int x, int y) {
graph[x][y] = true;
graph[y][x] = true;
/*std:cout << "adding pair: " << y << "," << x << "\n";*/
return true;
}
int main()
{
ifstream in("C:\\Users\\Austin\\Documents\\adjMatrix\\24cell_skeleton.txt");
ofstream out;
out.open("C:\\Users\\Austin\\Documents\\adjMatrix\\output.txt", ios::out);
ios::out;
string line, field;
vector< vector<string> > array; // the 2D array
vector<string> v; // array of values for one line only
while (getline(in, line)) // get next line in file
{
v.clear();
stringstream ss(line);
while (getline(ss, field, ',')) // break line into comma delimitted fields
{
v.push_back(field); // add each field to the 1D array
}
array.push_back(v); // add the 1D array to the 2D array
}
// print out what was read in
bool firstIter = true;
for (size_t i = 0; i<array.size(); ++i)
{
for (size_t j = 0; j<array[i].size(); ++j)
{
string curr = array[i][j]; // (separate fields by |)
prevNum = currNum;
currNum = stoi(curr);
if (j != 0 && firstIter) { //if its not the first iteration . . . or the last?
/*out << "adding pair: " << prevNum << "," << currNum << "\n";*/
markMatrix(prevNum, currNum);
}
}
firstIter = false;
}
in.close();
//std::cout << "{";
for (int i = 1; i <= cellSize; ++i)
{
std::cout << "{";
out << "{";
for (int j = 1; j <= cellSize; ++j)
{
//std::cout << "{";
std::cout << graph[i][j] << ' ';
std::cout << ", ";
out << graph[i][j] << ' ';
out << ", ";
//std::cout << "}";
}
out << "},";
std::cout << "},";
std::cout << std::endl;
}
//std::cout << "}";
out.close();
//cout << graph[534][42];
system("pause");
return 0;
}
Color.cpp
此文件为给定邻接矩阵的图形着色。就像我之前提到的那样,矩阵被硬编码到其中(从先前程序创建的文件中复制和粘贴)。这是我想要阅读文件而不是自己复制和粘贴的地方。
#include <string>
#include <sstream>
#include <iostream>
#include <vector>
#include <fstream>
#include<stdio.h>
#include<cstdio>
// Number of vertices in the graph
#define V 24
using namespace std;
//this function was added in the update to attempt reading file and storing into a 2d matrix
int createMatrix(int myArray[V][V])
{
ifstream in("C:\\Users\\Austin\\Documents\\adjMatrix\\output.txt");
for (int i = 0; i < V; i++)
{
for (int j = 0; j < V; j++)
{
in >> myArray[i][j];
}
}
return myArray[V][V];
}
//end of updated code added
void printSolution(int color[]);
/* A utility function to check if the current color assignment
is safe for vertex v */
bool isSafe(int v, bool graph[V][V], int color[], int c)
{
for (int i = 0; i < V; i++)
if (graph[v][i] && c == color[i])
return false;
return true;
}
/* A recursive utility function to solve m coloring problem */
bool graphColoringUtil(bool graph[V][V], int m, int color[], int v)
{
/* base case: If all vertices are assigned a color then
return true */
if (v == V)
return true;
/* Consider this vertex v and try different colors */
for (int c = 1; c <= m; c++)
{
/* Check if assignment of color c to v is fine*/
if (isSafe(v, graph, color, c))
{
color[v] = c;
/* recur to assign colors to rest of the vertices */
if (graphColoringUtil(graph, m, color, v + 1) == true)
return true;
/* If assigning color c doesn't lead to a solution
then remove it */
color[v] = 0;
}
}
/* If no color can be assigned to this vertex then return false */
return false;
}
bool graphColoring(bool graph[V][V], int m)
{
// Initialize all color values as 0. This initialization is needed
// correct functioning of isSafe()
int *color = new int[V];
for (int i = 0; i < V; i++)
color[i] = 0;
// Call graphColoringUtil() for vertex 0
if (graphColoringUtil(graph, m, color, 0) == false)
{
std::cout << "Solution does not exist";
return false;
}
// Print the solution
printSolution(color);
return true;
}
/* A utility function to print solution */
void printSolution(int color[])
{
std::cout << "Solution Exists:"
" Following are the assigned colors \n";
for (int i = 0; i < V; i++)
std::cout << color[i];
std::cout << "\n";
}
// driver program to test above function
int main()
{
/* Create following graph and test whether it is 3 colorable
(3)---(2)
| / |
| / |
| / |
(0)---(1)
*/
bool graph[V][V] = {
{ 0 , 1 , 0 , 1 , 1 , 0 , 0 , 1 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 1 , 1 , 0 , 1 , 1 , 0 , 0 , },
{ 1 , 1 , 1 , 0 , 0 , 1 , 1 , 0 , 0 , 1 , 1 , 0 , 0 , 0 , 0 , 0 , 1 , 1 , 1 , 0 , 0 , 1 , 0 , 0 , },
{ 0 , 1 , 1 , 1 , 0 , 0 , 1 , 1 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 1 , 1 , 1 , 1 , 0 , 0 , 0 , 1 , },
{ 1 , 0 , 1 , 0 , 0 , 0 , 0 , 1 , 1 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 1 , 1 , 0 , 1 , 1 , 1 , 0 , 0 , },
{ 1 , 0 , 0 , 0 , 1 , 1 , 0 , 1 , 0 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 1 , 0 , 1 , 0 , 1 , 1 , 1 , 1 , },
{ 0 , 1 , 0 , 0 , 1 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 0 , 0 , 1 , 0 , 1 , 1 , 0 , 1 , 1 , 0 , },
{ 0 , 1 , 1 , 0 , 0 , 1 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 0 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, 0 , 0 , 0 , 0 , },
{ 1 , 1 , 1 , 1 , 0 , 0 , 0 , 0 , 1 , 1 , 1 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , },
{ 1 , 1 , 1 , 0 , 1 , 1 , 1 , 0 , 0 , 1 , 1 , 0 , 0 , 1 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , },
{ 0 , 0 , 1 , 1 , 0 , 1 , 1 , 1 , 1 , 0 , 1 , 1 , 0 , 0 , 1 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , },
{ 1 , 0 , 0 , 1 , 1 , 0 , 0 , 1 , 1 , 0 , 1 , 1 , 1 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , },
{ 1 , 1 , 0 , 1 , 1 , 1 , 0 , 0 , 1 , 1 , 1 , 0 , 1 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , },
{ 0 , 0 , 0 , 0 , 1 , 1 , 1 , 1 , 1 , 0 , 0 , 1 , 1 , 1 , 1 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , },
{ 0 , 0 , 1 , 0 , 1 , 0 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , },
};
int m = 5; // Number of colors
graphColoring(graph, m);
//return 0;
system("pause");
}
有没有办法从文件中获取此矩阵而不是将其硬编码到程序中?括号和逗号很容易在文件中添加或删除。当硬编码到着色程序中时它们是必需的,但如果我能从文件中读取,我认为它们不是必需的。
为了使这一点尽可能清晰,您在Color.cpp
中看到24x24矩阵,我希望该部分是从AdjacencyMatrix.cpp
创建的out.txt中读取的矩阵。
修改/更新
到目前为止,我在color.cpp
int createMatrix(int myArray[V][V])
{
ifstream in("C:\\Users\\Austin\\Documents\\adjMatrix\\output.txt");
for (int i = 0; i < V; i++)
{
for (int j = 0; j < V; j++)
{
in >> myArray[i][j];
}
}
return myArray[V][V];
}
我知道有些事情在这里,但如果我能让这个工作,我计划使用像
这样的东西bool graph[V][V] =
{
myArray[V][V]
};
main()
中的。
截至目前,VisualStudio表示myArray
是一个未声明的标识符,在main()
中未定义。
答案 0 :(得分:1)
您的createMatrix
功能几乎就在那里,但return myArray[V][V];
不是您想要做的。这就是说返回一个超出数组边界的int
(大小V的数组有效0到V-1。Undefined Behaviour不良将导致数组的界限被破坏。
幸运的是,你不必返回一个蠢货。 myArray
是指向createMatrix
被调用的数组的指针(请参阅What is array decaying?),因此in >> myArray[i][j];
正在将文件直接读入数组。
通过一些检查确保读数成功:
bool createMatrix(int myArray[V][V])
{
ifstream in("C:\\Users\\Austin\\Documents\\adjMatrix\\output.txt");
if (in) // if the file is open and readable
{
for (int i = 0; i < V; i++)
{
for (int j = 0; j < V; j++)
{
if (!(in >> myArray[i][j]))
{ // failed to read a value from the file. Bad file
// may want to log the error here so you know why it failed
return false;
}
}
}
}
else
{ // couldn't open the file
// may want to log the error here so you know why it failed
return false;
}
return true; // read everything we wanted to read. all done.
}
然后在main
int main()
{
int graph[V][V];
if (createMatrix(graph))
{ // read the file successfully
int m = 5; // Number of colors
graphColoring(graph, m);
}
else
{ // did not read the file successfully
// display failure message
}
system("pause");
}