图采用邻接表法,Java中
DFS:先深后广,采用堆栈和递归两种数据结构,
BFS:先广后深:采用队列的数据结构: Java的代码:
package Graph; //对零接链表的模拟,下一个节点只能表示兄弟节点 class Node{ int Data; Node next; public Node(int Data) { this.Data=Data; this.next=null; } } public class GraphLink { //表头和链接节点 public Node first; public Node last; public static int run[]=new int[9]; //数据结构进行BFS,使用队列 public final static int MAXSIZE=10; static int[] queue=new int[MAXSIZE]; static int front=-1; static int rear=-1;//指向最后一个位置 public static void enqueue(int value) { if(rear<MAXSIZE) { rear++; queue[rear]=value; }else { return; } } public static int dequeue() { if(front==rear) return -1; front++;//front 从-1开始 return queue[front]; } public boolean isEmpty() { return first==null; } public void print() { Node current=first; while(current!=null) { System.out.println("["+current.Data+"]"); current=current.next; } } public void insert(int X) { Node newNode=new Node(X); if(this.isEmpty()) { first=newNode; last=newNode; } else { last.next=newNode; last=newNode; } } public static void BFS(int current,GraphLink graph[]) { Node tmpNode; enqueue(current); System.out.println("["+current+"]"); //类似于层次遍历一样 while(front!=rear)//当相等于表示队列是空的 { current=dequeue(); tmpNode=graph[current].first; run[current]=1; while(tmpNode!=null) { if(run[tmpNode.Data]==0) { enqueue(tmpNode.Data); run[tmpNode.Data]=1; System.out.println("["+tmpNode.Data+"]"); } tmpNode=tmpNode.next; } } } public static void DFS(int current,GraphLink graph[]) {//先深后广,堆栈实现的可以用递归 run[current]=1; System.out.println("["+current+"]"); Node T=graph[current].first; while(T!=null) { if(run[T.Data]==0) { DFS(T.Data,graph); } T=T.next; } } public static void main(String[] args) { // TODO Auto-generated method stub int DataNum; int Data[][]={{1,2},{2,1},{1,3},{3,1},{2,4},{4,2},{2,5},{5,2},{3,6},{6,3},{3,7} ,{7,3},{4,5},{5,4},{6,7},{7,6},{5,8},{8,5},{6,8},{8,6}}; GraphLink Graph[]=new GraphLink[9];//申请的是一个数组图链表数组 int i,j; for(i=1;i<9;i++) { run[i]=0; //表头节点,只有5个顶点 Graph[i]=new GraphLink(); System.out.println("图形顶点"+i+"内容:"); for(j=0;j<20;j++) { if(Data[j][0]==i) { Graph[i].insert(Data[j][1]); } } Graph[i].print(); } System.out.println("DFS"); DFS(1,Graph); for(i=0;i<9;i++) { run[i]=0; } System.out.println("BFS"); BFS(1,Graph); } }
1 图形顶点1内容: 2 [2] 3 [3] 4 图形顶点2内容: 5 [1] 6 [4] 7 [5] 8 图形顶点3内容: 9 [1] 10 [6] 11 [7] 12 图形顶点4内容: 13 [2] 14 [5] 15 图形顶点5内容: 16 [2] 17 [4] 18 [8] 19 图形顶点6内容: 20 [3] 21 [7] 22 [8] 23 图形顶点7内容: 24 [3] 25 [6] 26 图形顶点8内容: 27 [5] 28 [6] 29 DFS 30 [1] 31 [2] 32 [4] 33 [5] 34 [8] 35 [6] 36 [3] 37 [7] 38 BFS 39 [1] 40 [2] 41 [3] 42 [4] 43 [5] 44 [6] 45 [7] 46 [8]
使用C++语言:
#include<iostream> #include<queue> using namespace std; #define MAXSIZE 20 //采用邻接表方式建立图,为了节省空间 //最终要的东西有领接链表 //typedef struct AdjNode PtrToAdjVNode; typedef struct AdjNode{ //相邻顶点位置信息 int weight; int AdjV;//相邻顶点信息; AdjNode*next; }PtrToAdjVNode; //如果在这里*PtrToAdjNode *ptr=new Ptr这里表示指针的指针,不行的 typedef struct Vnode{ //表头信息 PtrToAdjVNode *FirstEdge; //每个表头函数子节点数 int DataNum; }AdjList[MAXSIZE]; int *Flag=new int[20]; typedef struct GNode{ int Vem; int Edge; //如果是邻接矩阵MG AdjList LG;//一个邻接表的表头,存取每个顶点,下一位存取与之相邻的顶点 }LGraph; void InsertEdge(LGraph *G,int Current,int NextVer) { PtrToAdjVNode *newNode=new PtrToAdjVNode; PtrToAdjVNode *tmp; (newNode)->AdjV=NextVer;(newNode)->weight=NULL;(newNode)->next=NULL; if((G)->LG[Current].FirstEdge==NULL) { (G->LG[Current].FirstEdge)=newNode; } else { tmp=G->LG[Current].FirstEdge; while(tmp->next!=NULL) { tmp=tmp->next; } tmp->next=newNode; } } LGraph CreateGraph(LGraph *G) { int VerterNum; cin>>VerterNum; (G)->Vem=VerterNum; (G)->Edge=0; //清空表头中内容 int V; for(V=1;V<=VerterNum;V++) G->LG[V].FirstEdge=NULL; //通过插值建立邻接表 int NextVer=1,CurrentVer;//负数表示1输入表示截止 for(V=1;V<=VerterNum;V++) { cin>>CurrentVer; while(true) { cin>>NextVer; if(NextVer<0) break; InsertEdge(G,CurrentVer,NextVer); } } return *G; } void DFS(int X,LGraph *G) { cout<<"["<<X<<"]"<<endl; Flag[X]=1; PtrToAdjVNode *tmp=G->LG[X].FirstEdge; while( tmp!=NULL) { if(Flag[tmp->AdjV]==0)//此顶点位置没有被遍历 DFS(tmp->AdjV,G); tmp=tmp->next; } } void BFS(int X,LGraph *G) { queue<int >Q; Q.push(X);PtrToAdjVNode *tmp; Flag[X]=1; while(!Q.empty()) { cout<<Q.front()<<" "; tmp=G->LG[Q.front()].FirstEdge; while(tmp!=NULL) { if(Flag[tmp->AdjV]==0) { Q.push(tmp->AdjV); Flag[tmp->AdjV]=1; } tmp=tmp->next; } Q.pop(); } } int main() { LGraph *Graph=new LGraph; CreateGraph(Graph); memset(Flag,0,sizeof(int)*20); DFS(1,Graph); memset(Flag,0,sizeof(int)*20); BFS(1,Graph); return 0; }