Code must be in C++. Please, work with the code I have provided
(please) and show me where the changes are made, please. Also show
the result on the screen console so I can see the program
implementation. Thank you for all your help.
This is the txt file I am working with "g.txt"
10
0 1
0 2
1 4
2 1
2 5
2 6
3 1
3 2
3 4
3 5
5 4
6 3
7 8
7 9
8 9
graphType example.cpp file below
Write a program that outputs the nodes of a graph in a breath first traversal.
1 #pragma once B//******* 2 ************************** 3 // Author: D.S. Malik 4 5 // class graphType 6 // This class specifies the basic operations to implement a graph. ************************** 7 8 9 #include 10 #include 11 #include 12 #include #include 13 14 #include 15 #include 16 #include 17 using namespace std; 18 19 Eclass graphType 20 { 21 public: 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | }; 77 78 bool graphType::isEmpty() const 79 { 80 return gSize == 0; 81 | } 82 83 Evoid graphType::clearGraph() 84 |{ 8 B: B bool isEmpty() const; //Function to determine whether the graph is empty. //Postcondition: Returns true if the graph is empty; otherwise, returns false. void clearGraph(); //Function to clear graph. //Postcondition: The memory occupied by each vertex // is deallocated. void printGraph() const; //Function to print graph. //Postcondition: The graph is printed. void depthFirst Traversal(); //Function to perform the depth first traversal of //the entire graph. //Postcondition: The vertices of the graph are printed // using the depth first traversal algorithm. void dftAtVertex(size_t vertex); //Function to perform the depth first traversal of //the graph at a node specified by the parameter vertex. //Postcondition: Starting at vertex, the vertices are // printed using the depth first traversal algorithm. void breadthFirst Traversal(); //Function to perform the breadth first traversal of //the entire graph. //Postcondition: The vertices of the graph are printed using the breadth first traversal algorithm. graphType(const vector>& adjacencylists); //Constructor //PostCondition : ? graphType(size_t size = 0); //Constructor //Postcondition: gSize = 0, graph is an array of pointers to linked lists. ~graphType(); //Destructor //The storage occupied by the vertices is deallocated. protected: size_t gSize; //current number of vertices list *graph; //array to create of adjacency lists private: void dft(size_t v, bool visited[]); //Function to perform the depth first traversal of //the graph at a node specified by the parameter vertex. //This function is used by the public member functions //depthFirstTraversal and dftAtVertex. //Postcondition: Starting at vertex, the vertices are // printed using the depth first traversal algorithm. B: B O Á
83 84 85 86 87 88 89 98 91 92 93 94 95 96 97 98 99 100 101 102 103 104 cout << "depth first traversal" << endl; 105 106 create boolean array, visited, of size gSize 107 for each vertex, v, in the graph 108 if v is not visited then 109 start the depth first traversal at v end if 110 111 112 [} //end depthFirst Traversal 113 114 Evoid graphType::dft (size_t v, bool visited[]) 115 { 116 visited[v] = true; 117 cout << " " << v << " "; 118 list::const_iterator graphIt; 119 B for (graphIt = graph[v].begin(); graphIt != graph[v].end(); ++graphIt) 120 { 121 cout << *graphIt << endl;; 122 123 }//end dft 124 125 Evoid graphType::dftAtVertex(size_t vertex) 126 |{\ 127 cout << "depth first traversal" << endl; 128 } // end dftAtVertex 129 130 131 Evoid graphType: :breadthFirst Traversal() 132 { 133 //cout << "breath first traversal" << endl; 134 135 }//end breadthFirst Traversal 136 137 //Constructors 138 EgraphType::graphType(size_t size) 139 { 140 gSize = size; 141 graph = new list[gSize]; 142 | } 143 144 EgraphType::graphType(const vector>& adjacencylists) 145 { 146 gSize = adjacencylists.size(); 147 graph = new list[gSize]; for (size_t i = 0; i < gSize; ++i) 148 149 for (size_t j = 0; j < adjacencylists.size(); ++j) graph[1].push_back(adjacencylists[j]); 150 151 | } 152 //Destructor 153 ElgraphType::~graphType() 154 { 155 clearGraph(); 156 | } Evoid graphType::clearGraph() { for (size_t index = 0; index < gSize; index++) graph[index].clear(); } //end clearGraph Elvoid graphType::printGraph() const { for (size_t index = 0; index < gSize; index++) { cout << index << "->"; for (list::const_iterator graphIt = graph[index].begin(); graphIt != graph[index].end(); ++graphIt) cout << *graphIt << ' '; cout << endl; cout << endl; }//end printGraph Evoid graphType::depthFirst Traversal() { Q 81
1 2 3 4 5 6 7 8 9 10 11 12 13 #HEADAANNNNNNNNN 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 #include #include #include #include #include "graphType.h" using namespace std; int main() { { vector> adj (10); adj [0].push_back(1); adj [0].push_back (3); adj[1].push_back(4); adj[2].push_back(5); adj[5].push_back(7); adj[5].push_back(8); adj[6].push_back(4); adj[6].push_back(7); adj[9].push_back(7); adj[9].push_back(8); graphType g(adj); g.printGraph(); string file_name = "g.txt"; ifstream inps (file_name); unsigned nvertices, from, to; inps >> nvertices; vector> adj(nvertices); while(inps.good()) { inps ›› from >> to; adj[from].push_back(to); inps.close(); graphType g(adj); g.printGraph();
Code must be in C++. Please, work with the code I have provided (please) and show me where the changes are made, please.
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am