#include <iostream> #include <fstream> #include <vector> #include <sstream> #include <stdlib.h> using namespace std; // Vertex class class Vertex { public: string label; int number; int degree; Vertex() { label = ""; number = -1; degree = 0; } Vertex(string l, int x) { label = l; number = x; degree = 0; } void print() { cout << number << ": " << label << " (deg = " << degree << ")" << endl; } }; // Edge class class Edge { public: int src, dst, w; Edge() { src = dst = -1; w = 0; } Edge(int s, int d, int weight) { src = s; dst = d; w = weight; } void print() { cout << "(" << src << ", " << dst << ", w=" << w << ")" << endl; } }; class Graph { private: public: int n; // # of vertices vector<Vertex> V; vector<Edge> E; Graph(const char *filename) { ifstream in(filename); if (! in.good()) { cerr << "Unable to open file " << filename << endl; exit(0); } char line[1000]; // assuming first line has the # vertices in >> n; in.getline(line, 999); // consume the \n character // read in the vertex labels for(int i=0; i < n; i++) { if (! in.getline(line, 999)) { cerr << "Unable to finish reading file..." << endl; exit(0); } // store the vertex label Vertex v(line, i); V.push_back(v); } // read in the edges while (in.getline(line, 999)) { int s, d, w; stringstream line_s(line, ios_base::in); line_s >> s >> d >> w; Edge e(s, d, w); E.push_back(e); } in.close(); compute_degrees(); } void print() { cout << "n = " << n << endl; for(int i=0; i < V.size(); i++) V[i].print(); for(int i=0; i < E.size(); i++) E[i].print(); } void compute_degrees() { for(int i=0; i < V.size(); i++) { V[i].degree = 0; } for(int i=0; i < E.size(); i++) { int j = E[i].src; V[j].degree ++ ; j = E[i].dst; V[j].degree ++ ; } } // BFS - perform BFS from s void BFS(int s) { } // DFS }; int main(int argc, char * argv[]) { if (argc < 2) { cerr << "Usage: ./graph1 graph_text_file.txt" << endl; exit(0); } Graph G(argv[1]); G.print(); return 0; }