Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //terina
- //section 2
- //due
- #include <iostream>
- #include <fstream>
- #include <vector>
- #include <list>
- #include <iomanip>
- #include <string>
- #include "assignment9.h"
- #include <cstring>
- using namespace std;
- /***************************************************************
- Function: graph
- Use: it’s a constructor it reads from the input file and it builds the graph with adjacency list and sets the size along with the labels
- Arguments: 1. filename: input file that it reads from
- Returns: nothing
- ***************************************************************/
- graph :: graph ( const char* filename )
- {
- ifstream infile;// (filename);
- infile.open(filename);
- string tline;
- infile >> size;
- labels = vector <char>(size);
- labels [ 0 ] = 'A';
- for ( int i = 1; i < size; i++ )
- labels [ i ] = labels [ i - 1 ] + 1;
- //puts a b c in label vector
- vector<vector<int> > matrix(size, vector<int>(size));
- infile>> tline;
- infile>> tline;
- infile>> tline;
- infile>> tline;
- infile>> tline;
- infile>> tline;
- infile>> tline;
- infile>> tline;
- string line;
- for(int i = 0; i < size; i++)
- {
- //infile.ignore() ignore the first char
- infile >> tline;
- for(int j = 0; j < size; j++)
- {
- infile >> matrix[i][j];
- if(i == j)
- matrix[i][j] = 0;
- if(matrix[i][j] == 1)
- adj_list[i].push_back(j);
- //cout << adj_list[i];
- }
- }
- infile.close();
- }
- /***************************************************************
- Function: ~ graph
- Use: it’s a desconstructor
- Arguments: none
- Returns: nothing
- ***************************************************************/
- graph::~graph()
- {
- }
- /***************************************************************
- Function: dept_first (int v)
- Use: to traverse a graph invokes depth first search traverse the graph use the depth first traversal
- Arguments: 1. V: index value
- Returns: nothing
- ***************************************************************/
- void graph::depth_first(int u ) const
- {
- /************
- static int oneNode = -1;
- static bool* visit = NULL;
- if(oneNode == -1)
- {
- oneNode = u;
- visit = new bool(size);
- // make all elements visited false
- for(int i = 0; i < size; i++)
- {
- visit[i] = false;
- }
- }
- cout << labels[u];
- visit[u] = true; // node that was visited
- for(int j = 0; j != size; j++)
- {
- for( char blah = 'A'; j!=size; j++)
- for ( int i = 1; i < size; i++ )
- if( visit[j] != true)
- {
- cout << "->";
- // call depth_first() is argument
- depth_first(j);
- }
- }
- if(u == oneNode)
- {
- cout << endl << endl;
- oneNode = -1;
- delete[] visit;
- visit = NULL;
- }
- *************/
- //this code is good commented it out because it didnt compile because i couldnt get it top ignore the char
- }
- /***************************************************************
- Function: traverse ()
- Use: to travers a graph invokes depth first search
- Arguments: none
- Returns: nothing
- ***************************************************************/
- void graph :: traverse ( ) const
- {
- cout <<"------- travere of graph ------";
- for(int i = 0; i < size; i++)
- {
- depth_first(i);
- }
- //traverse the graph
- cout << "--------- end of traverse -------";
- }
- int graph::get_size() const
- {
- return size;
- }
- /***************************************************************
- Function: print ()
- Use: prints the adjacency list
- Arguments: none
- Returns: nothing
- ***************************************************************/
- void graph :: print ( ) const
- {
- cout << "Number of vertices in the graph: ";
- cout <<//get_size();
- size;
- cout << "\n -------- graph -------\n";
- for(int i = 0; i < size; i++)
- {
- cout<< labels[i];
- cout<<": \n";
- //for (char x : adj_list[i])
- //cout<<x;
- //cout << adj_list[i];
- }
- cout<< "\n------- end of graph ------\n";
- }
- #define ASSIGNMENT9_TEST
- #ifdef ASSIGNMENT9_TEST
- int main(int argc, char** argv) {
- if ( argc < 2 ) {
- cerr << "args: input-file-name\n";
- return 1;
- }
- graph g(argv[1]);
- g.print();
- // g.traverse();
- return 0;
- }
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement