그림의 깊이를 우선적으로 훑어보기 - 인접표 구현

2225 단어
여기에 인접표로 그림의 깊이를 우선적으로 훑어보고 귀속으로 실현한다.
#include<iostream>
using namespace std;
#define VERTEXNUM 5// 
struct  edgenode  
{  
    int to;  
    int weight; //  
    edgenode *next;  
};  
struct vnode  
{  
    int from;  
    edgenode *first;  
};
void createGraph(vnode *adjilist, int start, int end,int weight);
void displayGraph(vnode *adjilist,int nodeNum);
void DFT(vnode *adjilist,int* vertexStatusArr,int nodeNum);
void DFTcore(vnode *adjilist,int i,int* vertexStatusArr);

int main(void){
		
		// 
	    vnode adjilist[VERTEXNUM];
		int nodeNum=VERTEXNUM;
		for(int i=0;i<nodeNum;i++)
			adjilist[i].first=NULL;
		int vertexStatusArr[VERTEXNUM]={0};
		cout<<vertexStatusArr[4]<<endl;
        createGraph(adjilist,0,3,0);
        createGraph(adjilist,0,4,0);
        createGraph(adjilist,3,1,0);
        createGraph(adjilist,3,2,0);
        createGraph(adjilist,4,1,0);

        printf("after create:
"); displayGraph(adjilist,nodeNum); // DFT(adjilist,vertexStatusArr,nodeNum); system("pause"); return 0; } // , void createGraph(vnode *adjilist, int start, int end,int weight) { adjilist[start].from=start; edgenode *p=new edgenode; p->to=end; p->weight=weight; p->next=adjilist[start].first; adjilist[start].first=p; } // void displayGraph(vnode *adjilist,int nodeNum) { int i,j; edgenode *p; for(i=0;i<nodeNum;i++) { p=adjilist[i].first; while(p) { cout<<'('<<adjilist[i].from<<','<<p->to<<')'<<endl; p=p->next; } } } // void DFT(vnode *adjilist, int* vertexStatusArr,int nodeNum) { printf("start BFT graph:
"); int i; for(i=0;i<nodeNum;i++){ DFTcore(adjilist,i,vertexStatusArr); } printf("
"); } void DFTcore(vnode *adjilist,int i,int* vertexStatusArr) { if(vertexStatusArr[i] == 1) return; printf("%d ",i); vertexStatusArr[i] = 1; edgenode *p; for(p=adjilist[i].first;p;p=p->next) { DFTcore(adjilist, p->to, vertexStatusArr); } }

시간 복잡도는 O(V+E)입니다.

좋은 웹페이지 즐겨찾기