具有不同输出的相同.txt文件

时间:2017-02-17 06:57:24

标签: c++ arguments codeblocks

使用代码块,我编写了一个显示邻接矩阵,BFS和DFS搜索顺序以及图形是否为二分的代码。

图形作为程序参数通过main作为.txt文件传递。

我有两个相同的.txt文件,这些文件具有不同的名称和完全不同的输出。我完全不知道为什么。

core-site.xml enter image description here 我有这个输出 enter image description here

graph0.txt enter image description here 我有这个输出 enter image description here

除文件名外,文件完全相同。我甚至以完全相同的方式输入它们以确保它们。为什么会这样?

graphNull.txt

Graph.h

#include <iostream>
using namespace std;

#include "Graph.h"

int main(int argc, char *argv[])
{
Graph g;
g.load(argv[1]);

cout << endl << "Adjacency Matrix" << endl;
g.display();

g.displayDFS(0);
g.displayBFS(0);

cout << "Bipartite: ";
if (g.isBipartite())
cout << "TRUE";
else
cout << "FALSE";
cout << endl;



return 0;
}

Graph.cpp

    #ifndef GRAPH_H
#define GRAPH_H

#include "QueueInt.h"
#include "StackInt.h"

class Graph {

  public:
    Graph();        // constructor
    ~Graph();       // destructor

    // Load the graph from a file
    void load(char *filename);

    // Display the adjacency matrix
    void display() const;

    // Display the depth first search from the given vertex
    void displayDFS(int vertex) const;

    // Display the breadth first search from the given vertex
    void displayBFS(int vertex) const;

    // Determine whether the graph is bipartite or not
    bool isBipartite() const;

  private:
    string fName;
    int numVert;
    int** mat;
    int* visited;
    int* dfs;


    void helperDFS(int vertex, int &counter, int* visited, int* dfs) const;




};

#endif

QueueInt.h

    #include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;

#include "Graph.h"
#include "QueueInt.h"
#include "StackInt.h"

// Constructor
Graph::Graph()
{
    fName = "";
    numVert = 0;

}

// Destructor
Graph::~Graph()
{
    delete mat;
    delete visited;
    delete dfs;
}

// Load the graph from a file
void Graph::load(char* filename)
{
    int numStore;
    fName = filename;
    fstream fin;
    fin.open(filename);
    fin >> numStore;
    numVert = numStore;
    mat  = new int *[numVert];
    int a;
    int b;

    for (int i = 0; i < numVert ; i++){
        //matrix is created
       mat[i] = new int[numVert];

       for (int j = 0; j < numVert; j++){
        mat[i][j] = 0;
       }
    }

        while (fin >> a >> b){
            mat[a][b] = 1;
            mat[b][a] = 1;
        }



}

// Display the adjacency matrix
void Graph::display() const
{

    for (int i = 0; i < numVert; i++){
        for (int j = 0; j < numVert; j++){
            cout << mat[i][j] << " ";
        }
    cout << "\n";
    }

}





// Displays the depth first search starting at the given vertex
void Graph::helperDFS(int vertex, int &counter, int* visited, int* dfs) const {
    visited[vertex] = 1;
    dfs[counter] = vertex;
    counter++;

    for (int j = 0; j < numVert; j++){
        if (mat[vertex][j] == 1 && visited[j] != 1){
            helperDFS(j, counter, visited, dfs);
        }

    }
}

void Graph::displayDFS(int vertex) const
{
int counter = 0;
int* visited = new int [numVert];
for(int i = 0; i < numVert; i++){
    visited[i]  = 0;
}

int* dfs = new int [numVert];

helperDFS(vertex, counter, visited, dfs);
cout << "DFS at vertex " << vertex << ": ";
for (int i = 0; i < numVert; i++){
    cout << dfs[i] << " ";
}
}

// Perform breadth first search starting at the given vertex
void Graph::displayBFS(int vertex) const
{
    int v = vertex;
    int vert;
    int bfs[numVert];
    int counter = 0;
    int* visited = new int [numVert];
    QueueInt q;     //empty q
    //create visited array initizalized to false for each vertex
    for (int i = 0; i < numVert; i++){
        visited[i] = 0;
    }
    cout << endl;
    //mark v as visited
    visited[vertex] = 1;
    //add v to queue
    q.enqueue(vertex);
    //process v to array for output
    bfs[counter] = vertex;

    counter ++;

    while(!q.isEmpty()){
     vert = q.dequeue();
     for (int j = 0; j < numVert; j++){

        if (mat[vert][j] == 1 && visited[j]!=1){

            visited[j] = 1;

            q.enqueue(j);
            bfs[counter] = j;
            counter ++;
        }
     }
    }

    cout << "BFS at vertex " << v << ": ";
    for (int i = 0; i < numVert; i++){
        cout << bfs[i] << " ";

    }
    cout << "\n";



}

// Determine whether the graph is bipartite or not
bool Graph::isBipartite() const
{
    bool flag = true;
    int color[numVert];
    //ASSIGN COLOR ELEMENTS TO 0
    for (int i = 0; i < numVert; i++){
        color[i] = 0;
    }
    //ASSIGN CURRENT ELEMENT COLOR 1
    for (int i =0; i < numVert; i++){

    if (color[i] == 0){
        color[i] = 1;

    }
    //ASSIGN ADJACENT ELEMENTS COLOR 2
    for (int j = 0; j < numVert; j++){
        if (mat[i][j] == 1){
            color[j] = 2;
        }
    }
    //FOR ELEMENTS WITH COLOR TWO, CHECK FOR ADJACENCY
    for (int j = 0; j < numVert; j++){
        if (color[j] == 2){
        for (int k = 0; k < numVert; k++){ //will have a self check, but thats not a problem given no self loops
            if (color[k] == 2){
                if (mat[j][k] == 1){
                    flag = false;
                    return flag;
                }
            }
        }
        }
    }
    //RESET COLORS, RUN AGAIN
    for (int h = 0; h < numVert; h++){
        color[h] = 0;
    }
    }
    return flag;

}

QueueInt.cpp

#ifndef QUEUEINT_H
#define QUEUEINT_H

#include <queue>
using namespace std;

class QueueInt
{
 public:

   // Add an integer to the back of the queue.
   void enqueue(int val);

   // Removes and returns an integer from the front of the queue. Aborts the
   // program is the queue is empty.
   int dequeue();

   // Returns but does not remove the integer at the front of the queue. Aborts the
   // program is the queue is empty.
   int front() const;

   // Returns true if the queue is empty.
   bool isEmpty() const;

 private:
   queue<int> queueObj;     // underyling STL queue object
};

#endif // QUEUE_H

0 个答案:

没有答案