Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 #include "Object.h" #include "Decorator.h" #include

class Graph { public: class Edge; class Vertex; typedef std::list VertexList; typedef std::list EdgeList; typedef std::list::const_iterator VertexItor; typedef std::list::const_iterator EdgeItor;

Graph(){}

class Edge { private: Decorator key; bool directed; public: std::pair 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 #include #include #include "Object.h" #include "Graph.h"

template class BFS { // generic DFS protected: // local types typedef typename G::Vertex Vertex; // vertex position typedef typename G::Edge Edge; // edge position typedef typename std::queue VertexQueue; typedef typename std::list VertexList; typedef typename std::list EdgeList; typedef typename std::list::const_iterator VertexItor; typedef typename std::list::const_iterator EdgeItor;

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 // constructor BFS::BFS(const G& g) : graph(g), yes(new Object), no(new Object) {}

template // initialize a new DFS void BFS::initialize() { VertexList verts = graph.vertices(); for (VertexItor pv = verts.begin(); pv != verts.end(); ++pv) unvisit(*pv); // mark vertices unvisited EdgeList edges = graph.edges(); for (EdgeItor pe = edges.begin(); pe != edges.end(); ++pe) unvisit(*pe); // mark edges unvisited }

template void BFS::bfsTraversal(Vertex& v) { VertexQueue vQueue;

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

image text in transcribed

(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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Reliability Engineering Designing And Operating Resilient Database Systems

Authors: Laine Campbell, Charity Majors

1st Edition

978-1491925942

Students also viewed these Databases questions

Question

What does the start( ) method defined by Thread do?

Answered: 1 week ago