zoukankan      html  css  js  c++  java
  • 数据结构与算法李春葆系列之

    //实现图的BFS,DFS 
    #include<iostream>
    #include<stdlib.h>
    #include<algorithm>
    #define MAXV 6 
    #define INF 32767
    using namespace std;
    typedef  char ElemType;
    typedef struct
    {
    	ElemType data[MAXV];
    	int front,rear;
    }SqQueue;
    
    typedef struct
    {
    	int no;
    }VertexType;
    typedef struct
    {
    	int edges[MAXV][MAXV];
    	int n,e;
    	VertexType vexs[MAXV];
    }MatGraph;
    typedef struct ANode
    {
    	int adjvex;
    	struct ANode *nextarc;
    	int weight;
    }ArcNode;
    typedef struct Vnode
    {
    	ArcNode *firstarc;
    }VNode;
    
    typedef struct
    {
    	VNode adjlist[MAXV];
    	int n,e;
    }AdjGraph;
    void InitQueue(SqQueue * &q)//初始化环形队列 
    {
    	q=(SqQueue *)malloc(sizeof(SqQueue));
    	q->front=q->rear=0;
    }
    bool QueueEmpty(SqQueue * q)//环形队列判空 
    {
    	return(q->front==q->rear);
    }
    bool enQueue(SqQueue *&q,ElemType e)//入队 
    {
    	if((q->rear+1)%MAXV==q->front)
    	{
    		return false;
    	}
    	q->rear=(q->rear+1)%MAXV;
    	q->data[q->rear]=e;
    }
    
    
    bool deQueue(SqQueue *&q, int &e)//出队 
    {
    	if(q->front==q->rear)
    	{
    		return false;
    	}
    	q->front=(q->front+1)%MAXV;
    	printf("%c
    ",q->data[q->front]);
    	e=q->data[q->front];
    	return true;
    }
    void CreatAdj(AdjGraph *&G,int A[MAXV][MAXV],int n,int e)
    {
    	int i,j;
    	ArcNode *p;
    	G=(AdjGraph *)malloc(sizeof(AdjGraph));
    	for(i=0;i<n;i++)
    	{
    		G->adjlist[i].firstarc=NULL;
    	}
    	for(i=0;i<n;i++)
    	{
    		for(j=n-1;j>=0;j--)
    		{
    			if(A[i][j]!=0&&A[i][j]!=INF)
    			{
    				p=(ArcNode *)malloc(sizeof(ArcNode));
    				p->adjvex=j;
    				p->weight=A[i][j];
    				p->nextarc=G->adjlist[i].firstarc;
    				G->adjlist[i].firstarc=p;
    			}
    		}
    	}
    	G->n=n;
    	G->e=e;
    }
    void DispAdj(AdjGraph *G)
    {
        int i;
        ArcNode *p;
        for(i = 0; i < G->n; i++)
        {
            p = G->adjlist[i].firstarc;
            if(p)  printf("%3d:", i);
            while(p)
            {
                printf("%3d->", p->adjvex);
                p = p->nextarc;
            }
            printf("
    ");
        }
    }
    void DestroyAdj(AdjGraph *&G)
    {
    	int i;
    	ArcNode *pre,*p;
    	for(i=0;i<G->n;i++)
    	{
    		pre=G->adjlist[i].firstarc;
    		if(pre!=NULL)
    		{
    			p=pre->nextarc;
    			while(p!=NULL)
    			{
    				free(pre);
    				pre=p;p=p->nextarc;
    			}
    			free(pre);
    		}
    	}
    	free(G);
    }
    int visited[MAXV]={0};
    void DFS(AdjGraph *G,int v)
    {
    	ArcNode *p;
    	visited[v]=1;
    	printf("%d",v);
    	p=G->adjlist[v].firstarc;
    	while(p!=NULL)
    	{
    		if(visited[p->adjvex]==0)
    		{
    			DFS(G,p->adjvex);
    		}
    		p=p->nextarc;
    	}
    }
    
    
    void DFS1(AdjGraph *G, int v)
    {
        ArcNode *p;
        ArcNode *St[MAXV];
        int top = -1, i, w;
        for(i = 0; i < G->n; i++)
            visited[i] = 0;
        printf("%3d", v);
        visited[v] = 1;
        top++;
        St[top] = G->adjlist[v].firstarc;
        while(top > -1)
        {
            p = St[top];
            top--;
            while(p)
            {
                w = p->adjvex;
                if(visited[w] == 0)
                {
                    printf("%3d", w);
                    visited[w] = 1;
                    top++;
                    St[top] = G->adjlist[w].firstarc;
                    break;
                }
                p = p->nextarc;
            }
        }
        printf("
    ");
    }
    void BFS(AdjGraph *G,int  v)
    {
    	int w,i;
    	ArcNode *p;
    	SqQueue *qu;
    	InitQueue(qu);
    	int visited[MAXV];
    	for(i=0;i<G->n;i++)
    	{
    		visited[i]=0;
    	}
    	printf("%2d",v);
    	visited[v]=1;
    	enQueue(qu,v);
    	while(!QueueEmpty(qu))
    	{
    		deQueue(qu,w);
    		p=G->adjlist[w].firstarc;
    		while(p!=NULL)
    		{
    			if(visited[p->adjvex]==0)
    			{
    				printf("%2d",p->adjvex);
    				visited[p->adjvex]=1;
    				enQueue(qu,p->adjvex);
    			}
    			p=p->nextarc;
    		}
    	}
    }
    int main()
    {   
     	int A[MAXV][MAXV] =
        {
            {  0, 5,  INF, 7, INF, INF},
            {INF, 0,  4, INF, INF, INF},
            {  8, INF, 0, INF, INF, 9 },
            {INF, INF, 5, 0, INF, 6   },
            {INF, INF, INF, 5, 0,  INF},
            {  3, INF, INF, INF, 1, 0 }
        };
    	 AdjGraph *p;
    	 int n=6;
    	 int e=10;
    	 CreatAdj(p,A,n,e);
    	 cout<<"DFS 递归"<<endl; 
    	 DFS(p,0);
    	 printf("
    ");
    	 cout<<"DFS 非递归"<<endl;
    	 DFS1(p, 0) ;
    	 cout<<"BFS"<<endl; 
    	 BFS(p,0);
    	 DestroyAdj(p); 
    	 
    } 
    
  • 相关阅读:
    Android布局控件
    XAMPP里tomcat启动报错:Make sure you have Java JDK or JRE installed and the required ports are free
    poj 北京大学 2014研究生推免上机考试(校内)
    2014北大计算机学科保研直博夏令营上机poj考试
    《C++Primer》第四版学习笔记--持续更新中
    poj1986:Distance Queries
    poj2533:最长上升子序列
    poj1062:昂贵的聘礼
    黑书贪心例题之钓鱼 poj1042:Gone Fishing
    转:最小没出现的整数
  • 原文地址:https://www.cnblogs.com/AmosAlbert/p/12832251.html
Copyright © 2011-2022 走看看