/* Graph read from file, and represnted as adjacency list.
To implement DFS and BFS on the graph
*/
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
// Each vertex has an integer id.
typedef vector>> adjlist; // Pair: (head vertex, edge weight)
adjlist makeGraph(ifstream& ifs);
void printGraph(const adjlist& alist);
vector BFS(const adjlist& alist, int source); // Return vertices in BFS order
vector DFS(const adjlist& alist, int source); // Return vertices in DFS order
void DFSHelper(const adjlist& alist, vector& dfslist, vector& visited, int source);
void printQ(queue qcopy);
// DFS - returns list of nodes in DFS order starting from source vertex
vector DFS(const adjlist& alist, int source) {
// Your code here
}
void DFSHelper(const adjlist& alist, vector& dfslist, vector& visited, int source) {
// Your code here
}
// BFS - returns list of nodes in BFS order starting from source vertex
vector BFS(const adjlist& alist, int source) {
// Your code here
}
// Reads a csv graph from file and returns an adjacency list
adjlist makeGraph(ifstream& ifs) {
int vh, vt, wt;
string line;
unordered_multimap> elist;
set vlist;
while (getline(ifs, line)) {
stringstream ss(line);
ss >> vt;
if (ss.peek() == ',')
ss.ignore();
ss >> vh;
if (ss.peek() == ',')
ss.ignore();
ss >> wt;
elist.emplace(vt, make_pair(vh, wt));
vlist.insert(vt);
vlist.insert(vh);
}
adjlist res(vlist.size()); // Preallocate vector
for (auto& ele : elist) {
res.at(ele.first).push_back(make_pair(ele.second.first, ele.second.second));
}
return res;
}
// Prints the adjacency list (only vertices, not edge weights)
void printGraph(const adjlist& alist) {
int i = 0;
for (auto& v : alist) {
cout << i++ << ": ";
for (auto& av : v) {
cout << av.first << " ";
}
cout << endl;
}
}
// Prints queue for debugging
void printQ(queue qcopy) {
while (!qcopy.empty()) {
cout << qcopy.front();
qcopy.pop();
}
cout << endl;
}
int main() {
ifstream ifs("sample_edges.txt");
adjlist alist = makeGraph(ifs);
printGraph(alist);
vector dfslist = DFS(alist, 0);
for (auto& ele : dfslist) // Prints 0 2 4 5 1 3
cout << ele << " ";
cout << endl;
vector bfslist = BFS(alist, 0);
for (auto& ele : bfslist) // Prints 0 2 1 4 3 5
cout << ele << " ";
cout << endl;
}
// sampletxt
//0, 1, 1
//0, 2, 1
//1, 3, 1
//2, 4, 1
//3, 4, 1
//3, 5, 1
//4, 5, 1
//0, 1, 1
//0, 2, 1
//1, 3, 1
//2, 4, 1
//3, 4, 1
In this assignment you will implement the BFS and DFS graph traversal algorithms as a part of the code below (graph.cpp). graph.cpp sample edges.txt Here's the test - graph test.cpp The code reads in a graph from the sample_edges.txt file (to be put in the same directory as your code), and returns an adjacency list. The adjacency list is a vector, where each element is in turn a vector of (adjacent vertex, edge weight) pairs. Typedef is used to enhance readability. You are to use the adjacency list returned to implement the BFS and DFS algorithms. A header for the helper function is also included to implement DFS recursively. A utility function that prints the queue is provided to help you with the BFS implementation. Please do not modify any code provided including the test. Upload your implementation of DFS and BFS in graph.cpp