Refactor
:my
=>
'code'
Codes
Refactorings
Popular
Best
Submit
Spam
Account
Logout
Login
JavaScript doesn't seem to be activated, expect things to be ugly and sloppy!
Learn How to Create Your Own Programming Language
createyourproglang.com
Recent
How to get accepted in Fileice (200% Working) 22/2012
Premium Account
FILE HOSTS PREMIUM ACCOUNT
ALL FILE HOST PREMIUM ACCOUNTS
Zynga Slingo Trainer v5.12
iTunes Gift Card Generator V3.1 2012
Diablo 3 GOLD Coins FREE
Working PS3 Jailbreak 3.65 And 3.66
ExtaBit Premium Accounts and Cookies
Steam Wallet Hack - Money Adder & Hack v3
Popular
XBOX POINTS GENERATOR - MICROSOFT POINTS GENERATOR v1.2012
11 may 2012 premium uploading accounts 100% working
Free Microsoft Points
Free Microsoft Points - Microsoft Points Generator - Xbox Live Codes 2012
Car Town Free Blue Points Hack
Free CarTown Blue Points Generator and CarTown Templates
Better way to get content via jQuery $.get()
Free Microsoft Points
Simple Days Purger
Sharecash Downloader Bypass Surveys New 05/2012
Pastable version of
Breadth First Search (Textbook Example)
<pre class='prettyprint' language='c++'>#include <iostream> #include <limits> #include <map> #include <list> #include <queue> using namespace std; const int INFINITY = INT_MAX; enum Color { WHITE, GRAY, BLACK }; class Vertex { public: Vertex(); Vertex(char id); ~Vertex(); char Vertex::getId(); void Vertex::setId(char arg); bool operator <(const Vertex& rhs) const { return id < rhs.id; } private: char id; }; Vertex::Vertex(): id('0') {} Vertex::Vertex(char id): id(id) {} Vertex::~Vertex() { } char Vertex::getId() { return id; } void Vertex::setId(char arg) { id = arg; } int main() { //created the vertices Vertex r('r'); Vertex s('s'); Vertex t('t'); Vertex u('u'); Vertex v('v'); Vertex w('w'); Vertex x('x'); Vertex y('y'); //use maps for keeping track of color, distance and parents map< Vertex , Color> color; color[r] = WHITE; color[s] = WHITE; color[t] = WHITE; color[u] = WHITE; color[v] = WHITE; color[w] = WHITE; color[x] = WHITE; color[y] = WHITE; map< Vertex , int> distance; distance[r] = INFINITY; distance[s] = INFINITY; distance[t] = INFINITY; distance[u] = INFINITY; distance[v] = INFINITY; distance[w] = INFINITY; distance[x] = INFINITY; distance[y] = INFINITY; map< Vertex , Vertex*> parent; parent[r] = NULL; parent[s] = NULL; parent[t] = NULL; parent[u] = NULL; parent[v] = NULL; parent[w] = NULL; parent[x] = NULL; parent[y] = NULL; //create the adj lists list<Vertex> adj_r; list<Vertex> adj_s; list<Vertex> adj_t; list<Vertex> adj_u; list<Vertex> adj_v; list<Vertex> adj_w; list<Vertex> adj_x; list<Vertex> adj_y; adj_r.push_back(s); adj_r.push_back(v); adj_s.push_back(r); adj_s.push_back(w); adj_t.push_back(w); adj_t.push_back(x); adj_t.push_back(u); adj_u.push_back(t); adj_u.push_back(x); adj_u.push_back(y); adj_v.push_back(r); adj_w.push_back(s); adj_w.push_back(t); adj_w.push_back(x); adj_x.push_back(w); adj_x.push_back(t); adj_x.push_back(y); adj_x.push_back(u); adj_y.push_back(x); adj_y.push_back(u); //map vertices to corresponding lists map< Vertex , list<Vertex> > adj; adj[r] = adj_r; adj[s] = adj_s; adj[t] = adj_t; adj[u] = adj_u; adj[v] = adj_v; adj[w] = adj_w; adj[x] = adj_x; adj[y] = adj_y; //other objects queue<Vertex> q; Vertex currentVertex; Vertex currentDecescendant; //start algorithm color[s] = GRAY; distance[s] = 0; parent[s] = NULL; q.push(s); //in while loop while( !q.empty() ) { //dequeue currentVertex = q.front(); q.pop(); //for each currentDecescendant in adj[currentVertex] list<Vertex>::iterator it; for(it = adj[currentVertex].begin(); it != adj[currentVertex].end(); it++) { currentDecescendant.setId( it->getId() ); if( color[currentDecescendant]==WHITE ) { color[currentDecescendant] = GRAY; distance[currentDecescendant] = distance[currentVertex] + 1; parent[currentDecescendant] = &currentVertex; q.push(currentDecescendant); } } color[currentVertex] = BLACK; } cout << "distance[r]: " << distance[r] <<endl; cout << "distance[s]: " << distance[s] <<endl; cout << "distance[t]: " << distance[t] <<endl; cout << "distance[u]: " << distance[u] <<endl; cout << "distance[v]: " << distance[v] <<endl; cout << "distance[w]: " << distance[w] <<endl; cout << "distance[x]: " << distance[x] <<endl; cout << "distance[y]: " << distance[y] <<endl; return 0; }</pre> <a href="http://www.refactormycode.com/codes/957-breadth-first-search-textbook-example" style="color:#fff" title="As seen on RefactorMyCode.com"><img alt="Small_logo" src="http://www.refactormycode.com/images/small_logo.gif" style="border:0" /></a>