Question
I need help with BFS traversal creating for main function : Here is the code that i have been working with: Graph.h : #ifndef GRAPH_H
I need help with BFS traversal creating for main function :
Here is the code that i have been working with:
Graph.h :
#ifndef GRAPH_H #define GRAPH_H
#include
class Graph { public: class Edge; class Vertex; typedef std::list
Graph(){}
class Edge { private: Decorator key; bool directed; public: std::pair
Edge() {}
void set(std::string _status, Object* yn) { key.set(_status, yn); }
Object* get(std::string prop) { return key.get(prop); }
Vertex* opposite(Vertex &v) { if (!directed) { if (&v == pair.first) return pair.second; else return pair.first; } else return NULL; }
Vertex* origin() { if (directed) return pair.first; else return NULL; }
Vertex* dest() { if (directed) return pair.second; else return NULL; }
bool isDirected() { return directed; }
void setIsDirected(bool value) { directed = value; }
bool isVisited() { return directed; } };
class Vertex { private: Decorator key; EdgeList edges;
public: Vertex() {}
EdgeList* getEdges() { return &edges; }
void set(std::string _status, Object* yn) { key.set(_status, yn); }
Object* get(std::string prop) { return key.get(prop); }
void insertEdge(Edge& e) { edges.push_back(&e); }
EdgeList* incidentEdges() { return getEdges(); } };
private: VertexList m_Vertices; EdgeList m_Edges;
public: VertexList* vertices() { return &m_Vertices; }
EdgeList* edges() { return &m_Edges; }
void insertVertex(Vertex& a) { m_Vertices.push_back(&a); }
void insertEdge(Vertex& origin, Vertex& end, Edge& edge) { edge.setIsDirected(false); edge.pair.first = &origin; edge.pair.second = &end;
origin.insertEdge(edge); end.insertEdge(edge);
m_Edges.push_back(&edge); }
void insertDirectedEdge(Vertex& origin, Vertex& end, Edge& edge) { edge.setIsDirected(true); edge.pair.first = &origin; edge.pair.second = &end;
origin.insertEdge(edge); end.insertEdge(edge);
m_Edges.push_back(&edge); }
void eraseVertex(Vertex& v) { }
void eraseEdge(Edge& e) { } };
#endif
BFS.h :
#ifndef BFS_H #define BFS_H
#include #include
template
protected: const G& graph; // the graph Vertex start; // start vertex Object *yes, *no; // decorator values
public: BFS(const G& g); void initialize(); void bfsTraversal(Vertex& v);
virtual void startVisit(Vertex& v) { std::string n = v.get("node")->stringValue(); std::cout "
virtual void traverseDiscovery(Edge& e, Vertex& from) { std::string edge = e.get("edge")->stringValue(); std::string vertex = from.get("node")->stringValue(); //std::cout stringValue(); std::string vertex = from.get("node")->stringValue(); //std::cout stringValue(); //std::cout
template
template
template
visit(v); vQueue.push(&v);
while (!vQueue.empty()) { Vertex* current = vQueue.front(); startVisit(*current); EdgeList* incident = (*current).incidentEdges(); EdgeItor pe = (*incident).begin(); while (pe != (*incident).end()) { Edge* e = *pe++; if (!isVisited(*e)) { Vertex* w = NULL; if ((*e).isDirected() && current == (*e).origin()) w = e->dest(); else w = (*e).opposite(*current);
if (w != NULL && !isVisited(*w)) { visit(*w); traverseDiscovery(*e, *w); vQueue.push(w); } } } vQueue.pop(); finishVisit(*current); } }
#endif
Here is the example of what i need to create for main function, anyone like to help me ? thank you
(a) F (c) (e) Figure 1 BFS Traversal Example (b) (d) (f
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started