Identical .txt files w/ different outputs - c++

Using codeblocks, I have written a code that displays an adjacency matrix, BFS and DFS search order, and whether or not graph is bipartite.
The graph is passed as a program argument through main as a .txt file.
I have two identical .txt files with different names and drastically different outputs. I have absolutely no idea why.
for graph0.txt
I have this output
For graphNull.txt
I have this output
With the exception of the file name, the files are identical. I even typed them the exact same way just to make sure. Why is this happening?
#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.h
#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
Graph.cpp
#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.h
#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
QueueInt.cpp
#include <iostream>
#include <cstdlib>
using namespace std;
#include "QueueInt.h"
// Add an integer to the back of the queue.
void QueueInt::enqueue(int val)
{
queueObj.push(val);
}
// Removes and returns an integer from the front of the queue. Aborts the
// program is the queue is empty.
int QueueInt::dequeue()
{
int val = front();
queueObj.pop();
return val;
}
// Returns but does not remove the integer at the front of the queue. Aborts the
// program is the queue is empty.
int QueueInt::front() const
{
if (isEmpty()) {
cerr << "Tried to access empty queue --> aborting program" << endl;
exit(-1);
}
return queueObj.front();
}
// Returns true if the queue is empty.
bool QueueInt::isEmpty() const
{
return queueObj.empty();
}

Related

How do i make my function in my player class for game of hearts work without being a pointer?

I was assigned to make a game of hearts where there is an array of 4 people and each people has a hand of 13 cards. Currently, when dealing trying to deal cards to each person deck, I am only able to accomplish this using pointers. However, this has posed problems in getting other functions like sort and such to work. I know that it's possible to do it without pointers I'm just not sure how and was wondering if anybody could assist.
In the psudocode, my teacher gave us, the hand[13] array which holds the cards is not a pointer, but I made it a pointer in order to points the cards to it.
#include <iostream>
#include <iomanip>
#include "Card.h"
#include "Player.h"
#include <cstdlib>
#include <random>
using namespace std;
//Prototype
void shuffleDeck(Card deck1[])
{
int ranNum;
Card temp;
int count = 0;
for(int i = 0; i < 52; i++)
{
count++;
ranNum = (rand() % 52);
temp = deck1[ranNum];
deck1[ranNum] = deck1[i];
deck1[i] = temp;
}
}
int main()
{
//Declarations
int nextPlayer;
int firstPlayer;
Card deck2[4][13];
Suit temp;
Card deck1[52];
int count = 0;
int check = 0;
int round = 0;
int points = 0;
int numCards = 0;
//Part 1: The set-up
//Create player array of [4] players
Player player[4] = {Player("Me"),
Player("Elton John"),
Player("Snoop Dog"),
Player("Lady Gaga")
};
//create and initialize deck;
for (int r = 0; r < 4; r++)
{
for (int c = 0; c < 13; c++)
{
deck2[r][c].setNumber(c + 1);
temp = ((Suit)r);
deck2[r][c].setSuit(temp);
deck2[r][c].setDescription();
}
}
//Convert to one dimensional array
for(int i = 0; i < 4; i++)
{
for(int j = 0; j < 13; j++)
{
deck1[count] = deck2[i][j];
count++;
}
}
shuffleDeck(deck1); //call shuffle deck function
//Deal deck to players // have add card in player class that sticks a card in players deck
/*
for(int i = 0; i < 52; i++)
{
int playerIndex = i % 4;
player[playerIndex].addCard(&deck1[i]); //HERE IS WHERE I CALL IT IN THE MAIN
}
//Sort each players hand
for (int i = 0; i < 4; i++)
{
player[i].sort();
}
//Find player who goes first (the person holding the 2 of clubs) //firstPlayer
for(int j = 0; j < 4; j ++)
{
for(int i = 0; i < 13; i++)
{
if(player[j].getCard(i) == 20)
{
firstPlayer = j;
}
}
}
*/
Card openCard[4]; //keeps track of all played cards
// //Part 2: The play
for(int i = 0 ; i < 13; i++) //gonna play 13 rounds, same thing 13 times
{ numCards = 0;
round++;
// Print points of players
cout << "\nPlayer Round " << round << " Total\n";
cout << "------- -------- -----\n";
cout << "\nMe ";
cout << "\nSnoop Dogg ";
cout << "\nLady Gaga ";
cout << "\nElton John ";
// Print hand
cout << "\n\nMy Hand: \n" << "-------------\n\n";
for(int i = 0; i < 13; i++)
{
// cout << player[0].getCard(i) << "\n";
numCards++;
check++;
cout << numCards << ".) " << deck1[i].getDescription() << "\n";
}
for (int j = 0; j < 3; j++)
{
nextPlayer = (firstPlayer + j) % 4;
//Play a card
//Compare card to other card
// openCard[nextPlayer] = player[nextPlayer].playCard(deck1[i], firstPlayer);
//inside playCard() remove card from hand after it's played
// hand[cardPlayed] = null //null means played already// (I think this is another method of doing it)
}
//end for j
//count points per player
//firstPlayer = winner of round //implement this in
// if(firstPlayer) //something like this
// {
// points++;
// }
//add points to first player
}
//end for i
return 0;
}
//PLAYER CLASS
#ifndef PLAYER_H
#define PLAYER_H
#include "Card.h"
#include <iostream>
#include <iomanip>
using namespace std;
class Player
{
//Member variables
private:
string name;
public:
static int count;
Card *hand[13]; //TRYING TO MAKE THIS NOT A POINTER
int number;
bool played;
//Constructors
Player()
{
name = "";
}
Player(string n)
{
name = n;
}
string getName()
{
return name;
}
int getCard(int index)
{
return hand[index]->getNumber();
}
void setName(string n)
{
name = n;
}
// void addCard(Card *card) //THIS IS WHERE CARDS ARE DELT (HOW DO I
MAKE IT NOT A POINTER?)
// {
// if (number < 13)
// {
// hand[number] = card;
// number++;
// }
// }
// void sort()
// {
// for (int i = 1; i < 13; i++)
// {
// Card *temp;
// bool swap = false;
// int size = 52;
// do
// {
// swap = false;
// for (int count = 0; count < (size - 1); count++)
// {
// if (hand[count + 1] < hand[count])
// {
// temp = hand[count];
// hand[count] = hand[count + 1];
// hand[count + 1] = temp;
// swap = true;
// }
// }
// }
// while (swap == true);
// }
// }
// Card playCard(Suit lead, bool leader)
// {
// int cardPlayed;
// hand[cardPlayed] = -1; //null means played already// (I think this
is another method of doing it)
// }
};
#endif

c++ - Segmentation fault for class function of vector of custom class

I am using following code to run kmeans algorithm on Iris flower dataset- https://github.com/marcoscastro/kmeans/blob/master/kmeans.cpp
I have modified the above code to read input from files. Below is my code -
#include <iostream>
#include <vector>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include <algorithm>
#include <fstream>
using namespace std;
class Point
{
private:
int id_point, id_cluster;
vector<double> values;
int total_values;
string name;
public:
Point(int id_point, vector<double>& values, string name = "")
{
this->id_point = id_point;
total_values = values.size();
for(int i = 0; i < total_values; i++)
this->values.push_back(values[i]);
this->name = name;
this->id_cluster = -1;
}
int getID()
{
return id_point;
}
void setCluster(int id_cluster)
{
this->id_cluster = id_cluster;
}
int getCluster()
{
return id_cluster;
}
double getValue(int index)
{
return values[index];
}
int getTotalValues()
{
return total_values;
}
void addValue(double value)
{
values.push_back(value);
}
string getName()
{
return name;
}
};
class Cluster
{
private:
int id_cluster;
vector<double> central_values;
vector<Point> points;
public:
Cluster(int id_cluster, Point point)
{
this->id_cluster = id_cluster;
int total_values = point.getTotalValues();
for(int i = 0; i < total_values; i++)
central_values.push_back(point.getValue(i));
points.push_back(point);
}
void addPoint(Point point)
{
points.push_back(point);
}
bool removePoint(int id_point)
{
int total_points = points.size();
for(int i = 0; i < total_points; i++)
{
if(points[i].getID() == id_point)
{
points.erase(points.begin() + i);
return true;
}
}
return false;
}
double getCentralValue(int index)
{
return central_values[index];
}
void setCentralValue(int index, double value)
{
central_values[index] = value;
}
Point getPoint(int index)
{
return points[index];
}
int getTotalPoints()
{
return points.size();
}
int getID()
{
return id_cluster;
}
};
class KMeans
{
private:
int K; // number of clusters
int total_values, total_points, max_iterations;
vector<Cluster> clusters;
// return ID of nearest center (uses euclidean distance)
int getIDNearestCenter(Point point)
{
double sum = 0.0, min_dist;
int id_cluster_center = 0;
for(int i = 0; i < total_values; i++)
{
sum += pow(clusters[0].getCentralValue(i) -
point.getValue(i), 2.0);
}
min_dist = sqrt(sum);
for(int i = 1; i < K; i++)
{
double dist;
sum = 0.0;
for(int j = 0; j < total_values; j++)
{
sum += pow(clusters[i].getCentralValue(j) -
point.getValue(j), 2.0);
}
dist = sqrt(sum);
if(dist < min_dist)
{
min_dist = dist;
id_cluster_center = i;
}
}
return id_cluster_center;
}
public:
KMeans(int K, int total_points, int total_values, int max_iterations)
{
this->K = K;
this->total_points = total_points;
this->total_values = total_values;
this->max_iterations = max_iterations;
}
void run(vector<Point> & points)
{
if(K > total_points)
return;
vector<int> prohibited_indexes;
printf("Inside run \n");
// choose K distinct values for the centers of the clusters
printf(" K distinct cluster\n");
for(int i = 0; i < K; i++)
{
while(true)
{
int index_point = rand() % total_points;
if(find(prohibited_indexes.begin(), prohibited_indexes.end(),
index_point) == prohibited_indexes.end())
{
printf("i= %d\n",i);
prohibited_indexes.push_back(index_point);
points[index_point].setCluster(i);
Cluster cluster(i, points[index_point]);
clusters.push_back(cluster);
break;
}
}
}
int iter = 1;
printf(" Each point to nearest cluster\n");
while(true)
{
bool done = true;
// associates each point to the nearest center
for(int i = 0; i < total_points; i++)
{
int id_old_cluster = points[i].getCluster();
int id_nearest_center = getIDNearestCenter(points[i]);
if(id_old_cluster != id_nearest_center)
{
if(id_old_cluster != -1)
clusters[id_old_cluster].removePoint(points[i].getID());
points[i].setCluster(id_nearest_center);
clusters[id_nearest_center].addPoint(points[i]);
done = false;
}
}
// recalculating the center of each cluster
for(int i = 0; i < K; i++)
{
for(int j = 0; j < total_values; j++)
{
int total_points_cluster = clusters[i].getTotalPoints();
double sum = 0.0;
if(total_points_cluster > 0)
{
for(int p = 0; p < total_points_cluster; p++)
sum += clusters[i].getPoint(p).getValue(j);
clusters[i].setCentralValue(j, sum / total_points_cluster);
}
}
}
if(done == true || iter >= max_iterations)
{
cout << "Break in iteration " << iter << "\n\n";
break;
}
iter++;
}
// shows elements of clusters
for(int i = 0; i < K; i++)
{
int total_points_cluster = clusters[i].getTotalPoints();
cout << "Cluster " << clusters[i].getID() + 1 << endl;
for(int j = 0; j < total_points_cluster; j++)
{
cout << "Point " << clusters[i].getPoint(j).getID() + 1 << ": ";
for(int p = 0; p < total_values; p++)
cout << clusters[i].getPoint(j).getValue(p) << " ";
string point_name = clusters[i].getPoint(j).getName();
if(point_name != "")
cout << "- " << point_name;
cout << endl;
}
cout << "Cluster values: ";
for(int j = 0; j < total_values; j++)
cout << clusters[i].getCentralValue(j) << " ";
cout << "\n\n";
}
}
};
int main(int argc, char *argv[])
{
srand(time(NULL));
int total_points, total_values, K, max_iterations, has_name;
ifstream inFile("datafile.txt");
if (!inFile) {
cerr << "Unable to open file datafile.txt";
exit(1); // call system to stop
}
inFile >> total_points >> total_values >> K >> max_iterations >> has_name;
cout << "Details- \n";
vector<Point> points;
string point_name,str;
int i=0;
while(inFile.eof())
{
string temp;
vector<double> values;
for(int j = 0; j < total_values; j++)
{
double value;
inFile >> value;
values.push_back(value);
}
if(has_name)
{
inFile >> point_name;
Point p(i, values, point_name);
points.push_back(p);
i++;
}
else
{
inFile >> temp;
Point p(i, values);
points.push_back(p);
i++;
}
}
inFile.close();
KMeans kmeans(K, total_points, total_values, max_iterations);
kmeans.run(points);
return 0;
}
Output of code is -
Details-
15043100000Inside run
K distinct cluster i= 0
Segmentation fault
When I run it in gdb, the error shown is -
Program received signal SIGSEGV, Segmentation fault.
0x0000000000401db6 in Point::setCluster (this=0x540, id_cluster=0)
at kmeans.cpp:41
41 this->id_cluster = id_cluster;
I am stuck at this as I cannot find the cause for this segmentation fault.
My dataset file looks like -
150 4 3 10000 1
5.1,3.5,1.4,0.2,Iris-setosa
4.9,3.0,1.4,0.2,Iris-setosa
4.7,3.2,1.3,0.2,Iris-setosa
. . .
7.0,3.2,4.7,1.4,Iris-versicolor
6.4,3.2,4.5,1.5,Iris-versicolor
6.9,3.1,4.9,1.5,Iris-versicolor
5.5,2.3,4.0,1.3,Iris-versicolor
6.5,2.8,4.6,1.5,Iris-versicolor
. . .
in KMeans::run(vector<Point>&) you call points[index_point].setCluster(i); without any guarantee that index_point is within bounds.
index_point is determined by int index_point = rand() % total_points;, and total_points is retrieved from the input file "datafile.txt" which could be anything. It certainly does not have to match points.size(), but it should. Make sure it does, or just use points.size() instead.
A bit offtopic, but using rand() and only using modulo is almost always wrong. If you use C++11 or newer, please consider using std::uniform_int_distribution.
points[index_point].setCluster(i); could be accessing the vector out of bounds. The code you quoted actually always sets a number of total_points in the vector points before calling run, while your modified code just reads until end of file and has no guarantees that the number of total points passed to the constructor of KMeans matches the value of entries in points. Either fix your file I/O or fix the logic of bounds checking.

how many nodes in graph are reachable?

I want to print the number of nodes which are reachable from a particular node. I read graph and stored in adjacency list and performed bfs.i had tried the following code.it works with certain graph.can you traced out what's wrong with this?
#include <vector>
#include <iostream>
#include <list>
#include<queue>
using namespace std;
int BFS(int s)
{
const int V=100;
int r=0;
vector<list<int> > a(V);
int visited[V]={0};
queue<int> Q;
visited[s]=1;
Q.push(s);
++r;
while(!Q.empty())
{
int x=Q.front();
Q.pop(); // pop here. we have x now
++r;
vector<list<int> >::iterator it1=a.begin()+x;
list<int> it2=*it1;
list<int>::iterator iter=it2.begin();
while(iter!=it2.end())
{
if(visited[*iter]==0)
{
visited[*iter]=1;
Q.push(*iter);
}
++iter;
}
visited[x]=2; // set visited here.
}
return r;
}
void printAsGrid(int V)
{
// Create a local 2D projection grid
int size = V.size();
double *grid = new double[size*size];
memset(grid, 0.0, size*size*sizeof(double));
// Get the edge connection and weight
int index;
for (index = 0; index < size; index++) {
list<Edge>::const_iterator eit;
for (eit = V[index].edges.begin();
eit != V[index].edges.end(); eit++) {
int to = (*eit).to;
double w = (*eit).weight;
// record weight in the projection grid
grid[(index*size)+to] = w;
}
}
// print header
out << " |";
for (index = 0; index < size; index++)
out << " " << index;
out << endl;
out << "---+";
for (index = 0; index < size; index++)
out << "-----";
out << endl;
// print content
out.setf(ios::fixed);
out.setf(ios::showpoint);
for (index = 0; index < size; index++) {
out << " " << index << " |";
for (int j = 0; j < size; ++j)
out << setw(5) << setprecision(1) << grid[(index*size)+j];
out << endl;
}
// delete grid before exit
delete [] grid;
}
int main()
{
int s;
int V,total_neighbors, id, weight;
//number of vertices
cout<<"enter the no.of vertices:";
cin>>V;
vector< list< int > > graph(V + 1);
for(int i= 0; i<V;i++) {
cout<<"Enter no.of neighbours of"<<i<<":";
cin>>total_neighbors;
cout<<"Enter the neighbours of"<<i<<":";
for(int j = 0; j <total_neighbors; j++) {
cin>>id;
graph[i].push_back(id);
}
}
vector<list<int> >::iterator i;
int c=0;
for (vector<std::list<int> >::iterator i=graph.begin(); i !=graph.end(); ++i){
cout<<"vertices connected to node "<<c <<" are ";
//cout<<*i;
std::list<int> li = *i;
for(std::list<int>::iterator iter = li.begin(); iter!= li.end(); ++iter){
cout<<*iter<<" ";
}
cout<<endl;
c++;
}
int f;
cin>>f;
s=BFS(f);
cout<<s<<" ";
return 0;
}
adjacencyList 0 -> 1 -> 2
adjacencyList 1 -> 2 -> 4
adjacencyList 2 -> 4
adjacencyList 3 -> 5
adjacencyList 4
adjacencyList 5 -> 3
returns 2 but actual answer is 3
You are doing r++ two times. You can do it only when you are pushing the node or when just popping the node. Otherwise for the source node, it will count twice. Also initialise r to 0. Also initialise the visited array manually to be on safe side.
Also you have taken vector<list<int> >::iterator it1=a.begin()+x; whereas a is empty. vector<list<int> > a(V); just initialises the vector of list but doesn't put values inside it. And hence the list which you are trying to traverse is empty. You are getting 2 because you are making r++ two times . One when source is inserted and other when source is removed which gives you 2.
See this code :
#include <vector>
#include <iostream>
#include <list>
#include<queue>
using namespace std;
int BFS(vector<list<int> > graph, int s)
{
const int V=100;
int r=0;
int visited[V]={0};
for(int i = 0; i < V; i++) visited[i] = 0;
queue<int> Q;
visited[s]=1;
Q.push(s);
while(!Q.empty())
{
int x=Q.front();
cout << x << endl;
Q.pop(); // pop here. we have x now
++r;
vector<list<int> >::iterator it1=graph.begin()+x;
list<int> it2=*it1;
list<int>::iterator iter=it2.begin();
while(iter!=it2.end())
{
if(visited[*iter]==0)
{
visited[*iter]=1;
Q.push(*iter);
}
++iter;
}
visited[x]=2; // set visited here.
}
return r;
}
int main()
{
int s;
int V,total_neighbors, id, weight;
//number of vertices
cout<<"enter the no.of vertices:";
cin>>V;
vector< list< int > > graph(V + 1);
for(int i= 0; i<V;i++) {
cout<<"Enter no.of neighbours of"<<i<<":";
cin>>total_neighbors;
cout<<"Enter the neighbours of"<<i<<":";
for(int j = 0; j <total_neighbors; j++) {
cin>>id;
graph[i].push_back(id);
}
}
vector<list<int> >::iterator i;
int c=0;
for (vector<std::list<int> >::iterator i=graph.begin(); i !=graph.end(); ++i){
cout<<"vertices connected to node "<<c <<" are ";
//cout<<*i;
std::list<int> li = *i;
for(std::list<int>::iterator iter = li.begin(); iter!= li.end(); ++iter){
cout<<*iter<<" ";
}
cout<<endl;
c++;
}
int f;
cin>>f;
s=BFS(graph,f);
cout<<s<<" ";
return 0;
}

Directed Acyclical Graph Crash

So I am having trouble with my code and cant figure out exactly what the problem is. I am supposed to take in a text file, where the first line shows the number of vertices, and the second line shows the number of edges.From this text file I am trying to take in the values shown then build a graph and determine whether or not it contains a cycle. Then display nodes which have no incoming edges.
t1.txt
3
2
1,2,
2,3
I am not sure exactly where I am going wrong here.
t2.txt
5
5
3,4,
4,5,
2,3,
1,3,
3,5
//
#include <iostream>
#include <fstream>
#include <sstream>
#include<string>
#include<list>
#include <limits.h>
#include <stack>
#include<stdlib.h>
#include <cstdlib>
using namespace std;
class Graph
{
int V; // No. of vertices
list<int> *adj; // Pointer to an array containing adjacency lists
bool isCyclicUtil(int v, bool visited[], bool *rs); // used by isCyclic()
public:
Graph(int V); // Constructor
void addEdge(int v, int w); // to add an edge to graph
bool isCyclic(); // returns true if there is a cycle in this graph
};
Graph::Graph(int V)
{
this->V = V;
adj = new list<int>[V];
}
void Graph::addEdge(int v, int w)
{
adj[v].push_back(w); // Add w to v’s list.
}
bool Graph::isCyclicUtil(int v, bool visited[], bool *recStack)
{
if(visited[v] == false)
{
// Mark the current node as visited and part of recursion stack
visited[v] = true;
recStack[v] = true;
// Recur for all the vertices adjacent to this vertex
list<int>::iterator i;
for(i = adj[v].begin(); i != adj[v].end(); ++i)
{
if ( !visited[*i] && isCyclicUtil(*i, visited, recStack) )
return true;
else if (recStack[*i])
return true;
}
}
recStack[v] = false; // remove the vertex from recursion stack
return false;
}
// Returns true if the graph contains a cycle, else false.
bool Graph::isCyclic()
{
// Mark all the vertices as not visited and not part of recursion
// stack
bool *visited = new bool[V];
bool *recStack = new bool[V];
for(int i = 0; i < V; i++)
{
visited[i] = false;
recStack[i] = false;
}
// Call the recursive helper function to detect cycle in different
// DFS trees
for(int i = 0; i < V; i++)
if (isCyclicUtil(i, visited, recStack))
return true;
return false;
}
int main()
{
float n ,n2;
string filename,line;
cout << "Enter filename: ";
cin >> filename;
fstream file;
file.open(filename.c_str());
file >> n;
file >> n2;
int num = n;
cout << num << " ";
int num2 = n2;
cout << num2 << " ";
string matrix[num2][2];
//int ** mat = new int*[num2];
int mat[num2][2];
Graph g(num);
while(!file.eof())
{
for(int i = 0; i < num2; i++)
{
//mat[i] = new int[num];
for(int j = 0; j < 2; j++)
{
if(getline(file,line,','))
{
matrix[i][j] = line; // read file into string matrix
istringstream(line) >> mat[i][j]; //convert to int and store in dynamic 2d array
}
}
}
}
cout << endl << "TEST " << mat[0][0] << " " << mat[0][1] << " " << mat[1][0]<< " " << mat[1][1];
for(int i = 0; i <num2-1; i++)
{
int temp1,temp2;
temp1 = mat[i][0];
temp2 = mat[i][1];
g.addEdge(temp1,temp2);
}
if(g.isCyclic())//Here is where the program crashes for input t2.txt
cout << "Graph is not DAG";
else
cout << "Graph is DAG";
return 0;
}

Adding counter to bubblesort function

I need to calculate the total number of swapping process and sorting running time for my bubble sort function. For running time, I was successful. But for the total number of swapping processes, I couldn't really understand what to do. I thought of initializing "count" and then tried to call it into the main function. That was a failure.
This if my bubble sort function:
void bubbleSort(T patient[], int size)
{
bool noChange = true; // stop when a pass causes no change
for(int i = size; i > 0; i--)
{
noChange = true;
for(int j = 1; j < i; j++)
{
if(patient[j] < patient[j - 1])
{
swap(patient[j], patient[j-1]);
count = count + 1;
noChange = false;
} // end if
} // end for(j)
if (noChange)
return; // sorted--no need to continue
} // end for(i)
}
"count" seems to show no value when called into the main function. Any tips on what I should try so that I could get the total number of swapping process in this?
EDIT 3:
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <time.h>
#include <string>
#include <ctime>
using namespace std;
const int SIZE = 5;
template <class T>
void printArray(T ar[], int sz);
template <class T>
int bubbleSort(T ar[], int sz);
//////////////////////////////////////////////////////////////////////////
// Main Function Implementation
//////////////////////////////////////////////////////////////////////////
int main() {
int numOfData = 50000;
string line, temp;
ofstream resultFile;
string patient[numOfData];
ifstream dataFile("shufflePatient.txt");
int times,i,count;
cout << "Program to shuffle data" << endl << endl;
cout << "This program will calculate swapping processes and running time.";
/*Storing data*/
cout << "Reading data in process.." << endl;
if (dataFile.is_open()) {
i=-1;
while (dataFile.good()) {
getline (dataFile, line);
if (i>=0) patient[i] = line;
i++;
}
dataFile.close();
}
double start_s=clock();
bubbleSort(patient,SIZE);
double stop_s=clock();
cout << "time: " << (stop_s-start_s)/double(CLOCKS_PER_SEC) << endl;
count = bubbleSort(patient,SIZE) ;
cout << "swapping process : " << count ;
cin.get(); // hold window open
/*Writing to file*/
cout << "Writing to file.." << endl;
resultFile.open ("test.txt");
for (int i=0 ; i<numOfData ; i++) {
resultFile << patient[i] << "\n";
}
resultFile.close();
system("pause");
return 0;
}
//----------------------------------------------------------------------------
// prints array of size size
//----------------------------------------------------------------------------
template <class T>
void printArray(T patient[], int size)
{
for(int i = 0; i < size; i++)
cout << patient[i] << " ";
cout << endl;
}
//----------------------------------------------------------------------------
// sorts array of size size by Bubble Sort method
//----------------------------------------------------------------------------
template <class T>
int bubbleSort(T patient[], int size) //returning an int
{
int count = 0; //initializing count
bool noChange = true;
for(int i = size; i > 0; i--)
{
noChange = true;
for(int j = 1; j < i; j++)
{
if(patient[j] < patient[j - 1])
{
swap(patient[j], patient[j-1]);
count = count + 1;
noChange = false;
}
}
if (noChange)
return count; // returning count
}
return count; // returning count
}
This is my updated code. Count value returns to 0. I don't know if the code I've used is right or wrong (where i call the return value of count). Any thoughts?
PS
Also, after changing my functions from void to int, for some reason my code stops sorting the data alphabetically when its written into the "text" file. Whats up with this?
You are not using the return value from the function, why don't you make the function return int - and return the count of number of swaps:
int bubbleSort(T patient[], int size) //returning an int
{
int count = 0; //initializing count
bool noChange = true;
for(int i = size; i > 0; i--)
{
noChange = true;
for(int j = 1; j < i; j++)
{
if(patient[j] < patient[j - 1])
{
swap(patient[j], patient[j-1]);
count = count + 1;
noChange = false;
}
}
if (noChange)
return count; // returning count
}
return count; // returning count
}
PS
The problem in the original code could be where you declared or initialized count (which is not shown in the code snap).
Also, using local variable is usually a better practice than using global ones.