本文整理汇总了C++中Graph::DFS方法的典型用法代码示例。如果您正苦于以下问题:C++ Graph::DFS方法的具体用法?C++ Graph::DFS怎么用?C++ Graph::DFS使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Graph
的用法示例。
在下文中一共展示了Graph::DFS方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: 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;
}
示例2: main
int main(void)
{
int n, m;
scanf("%d %d", &m, &n);
Graph * graph = new Graph(n);
int a, b, w;
for (int i = 0; i < m; i++)
{
scanf("%d %d %d", &a, &b, &w);
graph->vertex_arr[a]->AddEdge(new Edge(graph->vertex_arr[a], graph->vertex_arr[b], w));
}
int minimum_weight = 999999;
total_weight = 0;
for (int i = 0; i < graph->vertex_arr[1]->edgesize; i++)
{
flag = moveflag = 0;
minimum = 999999;
graph->DFS(graph->vertex_arr[1]);
graph->vertex_arr[1]->Check_Edge(i);
for (int j = 1; j <= n; j++) graph->vertex_arr[j]->visit = NonVisited;
}
printf("%d\n", total_weight);
return 0;
}
示例3: SSC
void Graph::SSC(int S)
{
bool *visited = new bool[V];
memset(visited, 0, V);
stack<int> Stack;
// DFS(like Topological Sort) and populate stack
for(int v=0; v<V; v++)
if( !visited[v])
DFSwithStack(v, visited, Stack);
//Reverse Direction of all edges
Graph G = getTranspose();
memset(visited, 0, V);
while ( !Stack.empty())
{
// Pop a vertex from stack
int v = Stack.top();
Stack.pop();
// Do DFS which prints strongly connected components
if ( !visited[v])
G.DFS(v, visited);
cout << endl;
}
}
示例4: main
int main()
{
Graph g;
while(!cin.eof())
{
string tmp;
string arg1, arg2, arg3;
string::iterator i;
getline(cin, tmp);
if(tmp.empty())
continue;
i = tmp.begin();
while(*i != ' ')
{
arg1.push_back(*i);
++i;
}
++i;
while(*i != ' ' && i != tmp.end() )
{
arg2.push_back(*i);
++i;
}
if(i != tmp.end())
{
++i;
while(i != tmp.end() )
{
arg3.push_back(*i);
++i;
}
g.addEdge(stoi(arg1), stoi(arg2), stod(arg3));
continue;
}
else
{
g.addVertex(stod(arg1), stod(arg2));
}
}
//g.printGraph();
g.DFS();
return 0;
}
示例5: 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;
}
示例6: 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;
}
示例7: main
int main()
{
// Create a graph given in the above diagram
int v,e,e1,e2;
freopen("in","r",stdin);
scanf("%d %d",&v,&e);
cout<<v<<" "<<e<<endl;
for(int i=0; i<e; i++)
{
scanf("%d %d",&e1,&e2);
cout<<e1<<" "<<e2<<endl;
g1.addEdge(e1, e2);
g2.addEdge(e1, e2);
g2.addEdge(e2, e1);
}
g1.DFS(1,v);
cout<<"\nfinal answer "<<ans<<endl;
return 0;
}
示例8: 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;
}
示例9: main
int main( int argc, char ** argv ) {
Graph G;
G.read(argv[1]);
G.DFS();
G.DFS2();
};
示例10: 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;
}
}
//.........这里部分代码省略.........