本文整理汇总了C++中Graph::BFS方法的典型用法代码示例。如果您正苦于以下问题:C++ Graph::BFS方法的具体用法?C++ Graph::BFS怎么用?C++ Graph::BFS使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Graph
的用法示例。
在下文中一共展示了Graph::BFS方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
int main()
{
freopen("input.txt", "r", stdin);
cin >> TestCases;
for (int test = 1; test <= TestCases; ++test)
{
int vertices;
cin >> vertices;
cout << "Vertices: " << vertices << endl;
Graph g;
for (int i = 0; i < vertices; i++)
{
int v1, v2;
cin >> v1;
cin >> v2;
cout << "adding " << v1 << "," << v2 << endl;
Vertex *vert1, *vert2;
vert1 = new Vertex(v1);
vert2 = new Vertex(v2);
g.addEdges(vert1, vert2);
}
g.BFS(g.getVertex(2));
}
return 0;
}
示例2: main
int main () {
cout << "test graph begin" << endl;
int *p;
Graph g (5);
g.randomInit (0.6);
cout << g << endl;
cout << "test BFS begin" << endl;
p = g.BFS(1);
for (int i=0; i<5; i++)
cout << p[i] << ", ";
cout << endl;
g.printPath (p, 1, 4);
cout << endl;
g.printPath (p, 1, 2);
cout << endl;
delete[] p;
cout << "test BFS end" << endl;
cout << "test DFS begin" << endl;
p = g.DFS();
for (int i=0; i<5; i++)
cout << p[i] << ", ";
cout << endl;
g.printPath (p, 1, 4);
cout << endl;
g.printPath (p, 1, 2);
cout << endl;
delete[] p;
cout << "test DFS end" << endl;
cout << "test graph end" << endl;
}
示例3: solve
bool solve(vector<pr> &vec,Graph &g){
sort(vec.rbegin(),vec.rend());
for(int i=0;i<vec.size();i++){
int s=vec[i].second;
if(g.BFS(s)) return true;
}
return false;
}
示例4: main
int main(){
Graph y;
y.readFromFile("input2.txt");
y.writeToFile("dfadd");
cout << "Test DFS" << endl;
cout << y.DFS("Philadelphia", "Trenton") << endl;
cout << y.DFS("Trenton", "Boston") << endl;
cout << y.DFS("Philadelphia", "Palo Alto") << endl;
cout << "Test BFS" << endl;
cout << y.BFS("Philadelphia", "Trenton") << endl;
cout << y.BFS("Trenton", "Boston") << endl;
cout << y.BFS("Philadelphia", "Palo Alto") << endl;
cout << "Test Tree property" << endl;
cout << y.tree() << endl << endl;
cout << "Number of Components" << endl;
cout << y.numConnectedComponents() << endl << endl;
cout << "Partionable" << endl;
cout << y.partitionable() << endl << endl;
cout << "Minimum Weight Components" << endl;
y.minWeightComponent("Los Angeles");
y.minWeightComponent("New York");
cout << endl << endl;
cout << "Print Path Close Val" << endl;
y.printPathCloseVal(41.7);
cout << endl << endl;
Graph x;
x.readFromFile("input.txt");
cout << "Test sub-graph" << endl;
cout << y.isSubGraph(x) << endl;
return 0;
}
示例5: main
// Driver program to test methods of graph class
int main()
{
// Create a graph given in the above diagram
Graph<Vertex> g;
g.addEdge(0, 1, 1);
g.addEdge(0, 2, 1);
g.addEdge(1, 2, 1);
g.addEdge(2, 0, 1);
g.addEdge(2, 3, 1);
g.addEdge(3, 3, 1);
cout << "Following is Breadth First Traversal (starting from vertex 2) \n";
g.BFS(2);
cout << "Following is Depth First Traversal (starting from vertex 2) \n";
g.DFS(2);
return 0;
}
示例6: isConnected
bool isConnected()
{
//bool visited[nverts];
for (int i = 0; i <nverts; i++)
visited[i] = false;
BFS(0);
for (int i = 0; i < nverts; i++)
if (visited[i] == false)
return false;
Graph gr = getTranspose();
for(int i = 0; i < nverts; i++)
visited[i] = false;
gr.BFS(0);
for (int i = 0; i < nverts; i++)
if (visited[i] == false)
return false;
return true;
}
示例7: main
int main (int argc, char ** argv) {
// ===== graph init ======
Graph<size_t> g;
Vertex<size_t> & a = g.addVertex(1);
Vertex<size_t> & b = g.addVertex(2);
Vertex<size_t> & c = g.addVertex(3);
Vertex<size_t> & d = g.addVertex(4);
Vertex<size_t> & e = g.addVertex(5);
Vertex<size_t> & f = g.addVertex(6);
Vertex<size_t> & h = g.addVertex(7);
Vertex<size_t> & i = g.addVertex(8);
Vertex<size_t> & j = g.addVertex(9);
g.addEdge(a, b);
g.addEdge(a, d);
g.addEdge(a, e);
g.addEdge(b, c);
g.addEdge(d, c);
g.addEdge(d, e);
g.addEdge(c, f);
g.addEdge(f, h);
g.addEdge(d, i);
g.addEdge(i, j);
// ===== run BFS ======
cout << "bfs" << endl;
g.BFS(a, func);
// ===== run DFS ======
cout << "dfs" << endl;
g.DFS(a, func);
cout << g;
return 0;
}
示例8: main
int main()
{
Graph g = createGraph();
int ch;
while (1) {
cout << "\nEnter a choice:\n";
cout << "1.Make DFS from a edge\n";
cout << "2.Show all edges using DFS\n";
cout << "3.Make BFS from a edge\n";
cout << "4.Show all edges using BFS\n";
cout << "5.Find if there is path between 2 edges\n";
cout << "6.Topological sort\n";
cout << "7.Find shortest unweighted path between two edges\n";
cout << "8.Find shortest weighted path between two edges\n";
cout << "9.Create Kruskal's MST\n";
cout << "Other:Exit\n";
cin >> ch;
switch (ch) {
case 1:
cout << "Enter edge:";
int n;
cin >> n;
if(g.vertices()>n && n>=0){
cout << endl;
g.DFS(n);
cout << endl;
}else{
cout << "Invalid entry\n";
}
break;
case 2:
cout << endl;
g.DFS();
cout << endl;
break;
case 3:
cout << "Enter edge:";
cin >> n;
if(g.vertices()>n && n>=0){
cout << endl;
g.BFS(n);
cout << endl;
}else{
cout << "Invalid entry\n";
}break;
case 4:
cout << endl;
g.BFS();
cout << endl;
break;
case 5:
cout << "Enter source and destination edges\n";
int a,b;
cin >> a >> b;
if(g.vertices()>a && a>=0 && g.vertices()>b && b>=0){
cout << endl;
g.BFSpath(a, b);
cout << endl;
}else{
cout << "Invalid entry\n";
}break;
case 6:
cout << endl;
g.topoSort();
cout << endl;
break;
case 7:
cout << "Enter source and destination edges\n";
int x,y;
cin >> x >> y;
cout << endl;
g.shortPath(x, y);
cout << endl;
break;
case 8:
cout << "Enter source and destination edges\n";
int l,m;
cin >> l >> m;
cout << endl;
g.shortPathWeight(l, m);
cout << endl;
break;
case 9:
{
Graph mst(g.vertices());
int count = 0;
cout << endl;
while (g.edgeHeap->count>0) {
edge e = delMinInHeap(&g.edgeHeap);
if (!mst.IsInMST(e.source(), e.destination())) {
mst.AddInMST(e.source(), e.destination());
mst.addEdge(e.source(), e.destination(),e.length());
cout << e.source() << "\t<->\t\t"<< e.destination() << "\t\t" <<e.length() << endl;
count++;
}
if (count == g.vertices()-1) {
break;
}
}
//.........这里部分代码省略.........