Advertisement
rinab333

CSCI 340 assignment9.cc

Jul 16th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.79 KB | None | 0 0
  1. //terina
  2. //section 2
  3. //due
  4. #include <iostream>
  5. #include <fstream>
  6. #include <vector>
  7. #include <list>
  8. #include <iomanip>
  9. #include <string>
  10. #include "assignment9.h"
  11. #include <cstring>
  12. using namespace std;
  13.  
  14. /***************************************************************
  15. Function: graph
  16.  
  17. 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
  18. Arguments:  1. filename: input file that it reads from
  19.        
  20. Returns:   nothing
  21. ***************************************************************/
  22. graph :: graph ( const char* filename )
  23. {
  24.     ifstream infile;// (filename);
  25.     infile.open(filename);
  26.     string tline;
  27. infile >> size;
  28. labels = vector <char>(size);
  29.  labels [ 0 ] = 'A';
  30.  
  31.     for ( int i = 1; i < size; i++ )
  32.     labels [ i ] = labels [ i - 1 ] + 1;
  33. //puts a b c in label vector
  34. vector<vector<int> > matrix(size, vector<int>(size));
  35. infile>> tline;
  36. infile>> tline;
  37. infile>> tline;
  38. infile>> tline;
  39. infile>> tline;
  40. infile>> tline;
  41. infile>> tline;
  42. infile>> tline;
  43. string line;
  44.     for(int i = 0; i < size; i++)
  45.     {
  46. //infile.ignore() ignore the first char
  47. infile >> tline;
  48.             for(int j = 0; j < size; j++)
  49.             {
  50.        
  51.             infile >> matrix[i][j];
  52.                 if(i == j)
  53.                     matrix[i][j] = 0;
  54.                 if(matrix[i][j] == 1)
  55.                     adj_list[i].push_back(j);
  56. //cout << adj_list[i];
  57.  
  58.         }
  59.  
  60. }  
  61. infile.close();
  62. }
  63. /***************************************************************
  64. Function:   ~ graph
  65.  
  66. Use:    it’s a desconstructor
  67. Arguments:  none
  68.        
  69. Returns:   nothing
  70. ***************************************************************/
  71.  
  72. graph::~graph()
  73. {
  74. }
  75. /***************************************************************
  76. Function: dept_first (int v)
  77.  
  78. Use:      to traverse a graph invokes depth first search traverse the graph use the depth first traversal
  79. Arguments:  1. V: index value
  80.        
  81. Returns:   nothing
  82. ***************************************************************/
  83.  
  84. void graph::depth_first(int u ) const
  85. {
  86. /************
  87.     static int oneNode = -1;
  88.         static bool* visit = NULL;
  89.                 if(oneNode == -1)
  90.                 {
  91.                         oneNode = u;
  92.                        
  93.                         visit = new bool(size);
  94.                         // make all elements visited  false
  95.                                 for(int i = 0; i < size; i++)
  96.                                 {
  97.                                         visit[i] = false;
  98.                                 }
  99.                 }
  100.         cout << labels[u];
  101.         visit[u] = true;    // node that was visited
  102.         for(int j = 0; j != size; j++)
  103.                 {
  104.             for( char blah = 'A'; j!=size; j++)
  105.             for ( int i = 1; i < size; i++ )
  106.          
  107.             if( visit[j] != true)
  108.                                 {
  109.                                      cout << "->";
  110.                                         // call depth_first() is argument
  111.                                         depth_first(j);
  112.                                 }
  113.                  }
  114.                  if(u == oneNode)
  115.                  {
  116.                     cout << endl << endl;
  117.                         oneNode = -1;
  118.                         delete[] visit;
  119.                     visit = NULL;
  120.                  }
  121. *************/
  122. //this code is good commented it out because it didnt compile because i couldnt get it top ignore the char
  123. }
  124. /***************************************************************
  125. Function: traverse ()
  126.  
  127. Use:      to travers a graph invokes depth first search
  128. Arguments:  none
  129.        
  130. Returns:   nothing
  131. ***************************************************************/
  132.  
  133. void graph :: traverse ( ) const
  134. {
  135. cout <<"------- travere of graph ------";
  136.  
  137. for(int i = 0; i < size; i++)
  138. {
  139.     depth_first(i);
  140. }
  141. //traverse the graph
  142.  
  143. cout << "--------- end of traverse -------";
  144. }
  145. int graph::get_size() const
  146. {
  147. return size;
  148. }
  149.  
  150. /***************************************************************
  151. Function: print ()
  152.  
  153. Use:  prints the adjacency list    
  154. Arguments:  none
  155.        
  156. Returns:   nothing
  157. ***************************************************************/
  158.  
  159. void graph :: print ( ) const
  160. {
  161. cout << "Number of vertices in the graph: ";
  162. cout <<//get_size();
  163. size;
  164. cout << "\n -------- graph -------\n";
  165. for(int i = 0; i < size; i++)
  166. {
  167.     cout<< labels[i];
  168.     cout<<": \n";
  169. //for (char x :  adj_list[i])
  170. //cout<<x;
  171.  
  172. //cout << adj_list[i];
  173.  
  174. }
  175.  
  176. cout<< "\n------- end of graph ------\n";
  177. }
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187. #define ASSIGNMENT9_TEST
  188. #ifdef  ASSIGNMENT9_TEST
  189.  
  190. int main(int argc, char** argv) {
  191.     if ( argc < 2 ) {
  192.         cerr << "args: input-file-name\n";
  193.         return 1;
  194.     }
  195.    
  196.     graph g(argv[1]);
  197.  
  198.     g.print();
  199.    
  200. //    g.traverse();
  201.  
  202.     return 0;
  203. }
  204.  
  205. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement