1. 程式人生 > >圖論知識小結2-DFS的陣列模擬實現

圖論知識小結2-DFS的陣列模擬實現

#include <bits/stdc++.h>
using namespace std;
const int MAX_EDGE = 10000;
const int MAX_VERTICES = 100;
struct Edge{
	int len, v, last;
} edge[MAX_EDGE];
bool vst[MAX_VERTICES];
int latest_edge_of_u[MAX_EDGE], eid;
void init(){
	memset(latest_edge_of_u, -1, latest_edge_of_u);
	for(int i = 0 ; i < MAX_VERTICES ; i++){
		vst[i] = false; //開始預設沒有訪問過 
	}
	eid = 0;
}
void insert(int w, int u, int v){
	if(w){  //權值不為0,該邊存在 
		edge[u].v = v;
		edge[u].len = w;
		edge[u].last = latest_edge_of_u[u];
		latest_edge_of_u[u] = eid++;
	}
}
void dfs(int start){
	vst[start] = true;
	previsit(start);  //前序訪問該結點 
	for(eid = latest_edge_of_u[start] ; eid != -1 ; eid = edge[eid].last){
		if(!vst[edge[eid].v]){
			dfs(edge[eid].v);
		}
	}
	postvisit(start);  //回溯訪問該結點 
} 	
int main(){
	init();
    // 
	return 0;
}