-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Todo: testing Signed-off-by: unknown <saq10002@iteb-219.ad.engr.uconn.edu>
- Loading branch information
unknown
committed
Sep 23, 2014
1 parent
b902e11
commit f75b407
Showing
2 changed files
with
200 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
#pragma once | ||
#include "common.h" | ||
#include "ds.graph.h" | ||
#include "ds.doublylinkedlist.h" | ||
using namespace std; | ||
|
||
namespace yasi{ | ||
namespace alg{ | ||
|
||
namespace graph{ | ||
|
||
|
||
template<class G> // graph | ||
class BFS{ | ||
public: | ||
typedef typename G::VertexType VertexType; | ||
typedef typename G::EdgeType EdgeType; | ||
typedef typename G::VertexIterator VertexIterator; | ||
typedef typename G::EdgeIterator EdgeIterator; | ||
typedef typename G::VertexList VertexList; | ||
protected: | ||
enum { | ||
VERTEX_UNDISCOVERED = 0, | ||
VERTEX_DISCOVERED, | ||
VERTEX_VISITING, | ||
VERTEX_VISITED | ||
}; | ||
const G& g; | ||
// the BFS ordering of vertices | ||
int* pBfsParent; | ||
void clear(){ | ||
|
||
} | ||
public: | ||
BFS(const G& g):g(g){ | ||
pBfsParent = new int[g.numVertices()]; | ||
} | ||
virtual ~BFS(){ | ||
DELETE_ARR_SAFE(pBfsParent); | ||
} | ||
/////////////////////////////////////////////////// | ||
// override thesse function in child classes | ||
// for your own BSF visit action | ||
virtual void visitVertexBeforeSniffingNeighbors(VertexType* v){ | ||
// do nothing | ||
} | ||
virtual void visitVertexAfterSniffingNeighbors(VertexType* v){ | ||
// do nothing | ||
} | ||
virtual void visitEdge(EdgeType* e){ | ||
// do nothing | ||
} | ||
////////////////////////////////////////////////// | ||
|
||
// perform bfs | ||
// the BFS tree is saved in BFS::pVertexSequence | ||
// the vertex id's must be in the range [0..n-1] | ||
void search(const G& g, const VertexType* src) const{ | ||
// init | ||
int n = g.numVertices(); | ||
const int srcId = src->id; | ||
DoublyLinkedList<VertexType*> q; | ||
|
||
int* vertexStatus = new int[n]; | ||
for (int i = 0; i < n; i++){ | ||
if (i == srcId) continue; | ||
vertexStatus[i] = VERTEX_UNDISCOVERED; | ||
// parent of each vertex in BFS tree is initially NULL | ||
pBfsParent[i] = -1; | ||
} | ||
|
||
// the source vertex | ||
vertexStatus[srcId] = VERTEX_DISCOVERED; | ||
pBfsParent[srcId] = -1; | ||
q.pushBack(src); | ||
|
||
// the traversal | ||
while (!q.empty()){ | ||
VertexType* currentVertex = q.popFront(); | ||
int currentId = currentVertex->id; | ||
|
||
// visit currentVertex | ||
vertexStatus[currentId] = VERTEX_VISITING; | ||
visitVertexBeforeSniffingNeighbors(currentVertex); | ||
|
||
for (EdgeIterator i = v->pOutEdges->begin(); i != v->pOutEdges->end(); i++){ | ||
EdgeType* e = *i; | ||
// process this edge | ||
visitEdge(e); | ||
|
||
VertexType* neighborVertex = e->end; | ||
int neighborId = neighborVertex->id; | ||
if (vertexStatus[neighborId] == VERTEX_UNDISCOVERED){ | ||
// schedule a visit to the neighbor | ||
vertexStatus[neighborid] == VERTEX_DISCOVERED; | ||
pBfsParent[neighborVertex] = currentVertex; | ||
q.pushBack(neighborVertex); | ||
} | ||
|
||
} | ||
|
||
// done visiting current vertex | ||
visitVertexAfterSniffingNeighbors(currentVertex); | ||
vertexStatus[currentId] = VERTEX_VISITED; | ||
|
||
} | ||
|
||
// cleanup | ||
DELETE_SAFE_ARR(vertexVisited); | ||
} | ||
|
||
}; | ||
|
||
} // namespace graph | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters