使用stack 实现了回溯法, 使用函数的递归调用 + 循环实现了回溯法。
当pop之后··· 又一次push的作用;
for instance dfs
#include <iostream>
#include <fstream>
using namespace std;
#define MAXV 100
typedef struct edgenode {
int y;
int weight;
struct edgenode * next;
} edgenode;
typedef struct {
edgenode * edges[MAXV];
int degeree[MAXV + 1];
int nvertex;
int nedges;
bool directed;
}graph;
void init_graph(graph *g, bool isdirected)
{
int i;
g->nvertex = 0;
g->nedges = 0;
g->directed = isdirected;
for (i = 1; i <= MAXV; ++i) g->degeree[i] = 0;
for (i = 1; i < MAXV; ++i) g->edges[i] = NULL;
}
void insert(graph *g, int x, int y, bool directed)
{
edgenode *p;
p = new edgenode;
p->weight = NULL;
p->y = y;
p->next = g->edges[x];
g->edges[x] = p;
g->degeree[x] ++;
g->nvertex++;
if (directed == false)
{
insert(g, y, x, true);
}
else
{
g->nedges++;// this is only happen in the second recursive call;
}
}
void read_Graph(graph * g, bool directed)
{
int i;
int m;
int x, y;
init_graph(g, directed);
ifstream outf;
/*@@@@*/
outf.open("graph3.txt");
if (!outf)
{
cout << "file is wrong " << endl;
}
while (outf >> x >> y)
{
insert(g, x, y, directed);
}
}
void print_Graph(graph *g)
{
int i;
edgenode *p;
for (i = 1; i <= g->nvertex; ++i)
{
cout << i << ": >>" ;
p = g->edges[i];
while (p!=NULL)
{
cout << " " << p->y;
p = p->next;
}
cout << endl;
}
}
void RunGraph(graph * g)
{
init_graph(g, 1);
read_Graph(g, 1);
print_Graph(g);
}
bool discovered[MAXV] = { 0 };
int time;
int entry_time[MAXV] = { 0 };
int parent[MAXV] = { 0 };
int processed[MAXV] = { 0 };
int exit_time[MAXV] = { 0 };
void dfs(graph *g, int v)
{
edgenode *p; /* temporary pointer */
int y; /* successor vertex */
discovered[v] = true;
time = time + 1;
entry_time[v] = time;
cout << "process_vertex_early(v) =>> "<<v;
p = g->edges[v];
while (p != NULL) {
y = p->y;
if (discovered[y] == false) {
parent[y] = v;
cout << "process_edge(v, y)"<<v << " ->> "<<y;
dfs(g, y);
}
else if ((!processed[y]) || (g->directed))
cout << "process_edge(v, y)" << v << " ->> " << y;
//if (finished) return;
p = p->next;
}
cout << "process_vertex_late(v) " << v << endl;
time = time + 1;
exit_time[v] = time;
processed[v] = true;
}
void my_dfs(graph *g, int v)
{ //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
edgenode *p; /* temporary pointer */
int y; /* successor vertex */
discovered[v] = true;
cout << "process_vertex_early(v) =>> " << v << endl;
p = g->edges[v];
while (p != NULL) {
y = p->y;
if (discovered[y] == false) {
//parent[y] = v;
cout << "process_edge(v, y) " << v << " ->> " << y << endl;
my_dfs(g, y);
}
//else if ((!processed[y]) || (g->directed))
// cout << "process_edge(v, y)" << v << " ->> " << y << endl;
p = p->next;
}
//cout << "process_vertex_late(v) " << v << endl;
processed[v] = true;
}
void my_dfs_3(graph *g, int v)
{
int y;
edgenode *p = g->edges[v];
discovered[v] = true;
cout << "v = " << v << endl;
while (p)
{
y = p->y;
if (discovered[y] == false)
{
cout << "v and y
" << v<<" -->
" << y;
my_dfs_3(g,y);
}
p = p->next;
}
}
int main()
{
graph g;
RunGraph(&g);
my_dfs_3(&g, 1);
cout << "asdfas" << endl;
system("pause");
}