图的遍历 浙大mooc

Posted accomplishment

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了图的遍历 浙大mooc相关的知识,希望对你有一定的参考价值。

  1 #include<stdio.h>
  2 #include<stdlib.h>
  3 
  4 #define OK 1
  5 #define NO 0
  6 #define TRUE 1
  7 #define FALSE 0
  8 #define ERROR -1 
  9 
 10 #define MaxVerTexNum 100
 11 #define INFINITY 65535
 12 
 13 
 14 
 15 
 16 typedef int ElementType;
 17 typedef int Status;
 18 typedef int WeightType;
 19 typedef int Vertex;
 20 typedef char DataType;
 21 
 22 typedef int Position;
 23 typedef struct QNode{
 24     ElementType *Data;
 25     Position Front, Rear;
 26     int MaxSize;  
 27 }QNode,*Queue;
 28 
 29 
 30 
 31 Status Visited[MaxVerTexNum];
 32 
 33 typedef  struct  ENode{
 34     Vertex V1,V2;     //有向边<v1,V2>
 35     WeightType Weight;//权重
 36 }ENode,*PtrToENode ;
 37 
 38 typedef PtrToENode Edge;
 39 
 40 Status IsFull( Queue Q );
 41 
 42 typedef struct GNode{
 43     int Nv;//顶点数
 44     int Ne;//边数
 45     WeightType G[MaxVerTexNum][MaxVerTexNum];//邻接矩阵
 46     DataType Data[MaxVerTexNum];//save data of the Vertex;
 47 }GNode,*PtrToGNode;
 48 
 49 typedef PtrToGNode MGraph;
 50 
 51 ElementType DeleteQ( Queue Q );
 52 
 53 Queue CreateQueue( int MaxSize );
 54 
 55 Status IsEmpty( Queue Q );
 56 
 57 Status AddQ( Queue Q, ElementType X );
 58 
 59 MGraph CreateGraph(int VertexNum);//Create a Graph with VertexNum Vertex but without an ege;
 60 
 61 void InsertEdge(MGraph Graph,Edge E);
 62 
 63 MGraph BuildGraph();
 64 
 65  void Visit(Vertex V);
 66 
 67 Status IsEdge(MGraph Graph,Vertex V,Vertex W);//检查<V,W>是不是图中的一条边,即W是不是V的邻接点
 68 
 69 //void DFS()
 70 
 71 void BFS(MGraph Graph,Vertex S);
 72 
 73 int main(int argc,char** argv)
 74 {
 75     MGraph Graph;
 76     Graph=BuildGraph();
 77     BFS(Graph,1);
 78 
 79     return 0;
 80 }
 81 
 82 MGraph CreateGraph(int VertexNum){
 83     Vertex V,W;
 84     MGraph Graph;
 85     Graph=(MGraph)malloc(sizeof(GNode));//form a Graph
 86     Graph->Nv=VertexNum;
 87     Graph->Ne=0;//
 88     for(V=0;V<Graph->Nv;V++)
 89         for(W=0;W<Graph->Nv;W++)
 90             Graph->G[V][W]=INFINITY;
 91         return Graph;
 92 }
 93 
 94 void InsertEdge(MGraph Graph,Edge E){
 95     Graph->G[E->V1][E->V2]=E->Weight;
 96     //若是无向图还要插入
 97     Graph->G[E->V2][E->V1]=E->Weight;
 98 }
 99 
100 MGraph BuildGraph(){
101     MGraph Graph;
102     Edge E;
103     Vertex V;
104     int Nv,i;
105     printf("输入结点的个数\n");
106     scanf("%d",&Nv);//the number of vertex
107     Graph=CreateGraph(Nv);//initate graph with Nv vertexs  !!return 回去要赋值给Graph;
108     //getchar();
109     printf("输入弧的个数\n");
110     scanf("%d",&(Graph->Ne));//read the number of ege
111     if(Graph->Ne!=0)
112     {
113         E=(Edge)malloc(sizeof(ENode));
114         for(i=0;i<Graph->Ne;i++){
115             // getchar();
116             printf("输入弧的信息V1 V2 Weight\n");
117             scanf("%d %d %d",&(E->V1),&(E->V2),&(E->Weight));
118             InsertEdge(Graph,E);
119         }
120     }
121     // for(V=0;V<Graph->Nv;V++)
122     //     scanf("%c",&(Graph->Data[V]));
123     return Graph;
124 
125 }
126 
127 void Visit(Vertex V){
128     printf("正在访问顶点%d\n",V);
129 }
130 
131 Status IsEdge(MGraph Graph,Vertex V,Vertex W){
132     return Graph->G[V][W]<INFINITY?TRUE:FALSE;
133 }
134 
135 //void (*Visit)(Vertex)
136 void BFS(MGraph Graph,Vertex S){
137     Queue Q;
138     Vertex V,W;
139     int i;
140     int MaxSize=100;
141     Q=CreateQueue(MaxSize);
142     for(i=0;i<Graph->Nv;i++)
143         Visited[i]=FALSE;
144     Visit(S);
145     Visited[S]=TRUE;
146     AddQ(Q,S);
147     while(!IsEmpty(Q))
148     {
149         V=DeleteQ(Q);
150         for(W=0;W<Graph->Nv;W++)
151             if(!Visited[W]&&IsEdge(Graph,V,W)){
152                 Visit(W);
153                 Visited[W]=TRUE;
154                 AddQ(Q,W);
155             }
156     }
157 }
158 
159 Queue CreateQueue( int MaxSize )
160 {
161     Queue Q = (Queue)malloc(sizeof(struct QNode));
162     Q->Data = (ElementType *)malloc(MaxSize * sizeof(ElementType));
163     Q->Front = Q->Rear = 0;
164     Q->MaxSize = MaxSize;
165     return Q;
166 }
167  
168 Status AddQ( Queue Q, ElementType X )
169 {
170     if ( IsFull(Q) ) {
171         printf("队列满");
172         return FALSE;
173     }
174     else {
175         Q->Rear = (Q->Rear+1)%Q->MaxSize;
176         Q->Data[Q->Rear] = X;
177         return FALSE;
178     }
179 }
180 ElementType DeleteQ( Queue Q )
181 {
182     if ( IsEmpty(Q) ) { 
183         printf("队列空");
184         return ERROR;
185     }
186     else  {
187         Q->Front =(Q->Front+1)%Q->MaxSize;
188         return  Q->Data[Q->Front];
189     }
190 }
191 
192 Status IsEmpty( Queue Q )
193 {
194     return (Q->Front == Q->Rear);
195 }
196 Status IsFull( Queue Q )
197 {
198     return ((Q->Rear+1)%Q->MaxSize == Q->Front);
199 }

 

以上是关于图的遍历 浙大mooc的主要内容,如果未能解决你的问题,请参考以下文章

浙大Python程序设计(MOOC)习题答案

中国大学MOOC-JAVA学习(浙大翁恺)—— 信号报告

念整数——mooc《零基础学Java语言》-(浙大翁凯)第四周编程题

中国大学MOOC-C程序设计(浙大翁恺)—— 单词长度

中国大学MOOC-C程序设计(浙大翁恺)—— 时间换算

中国大学MOOC-C程序设计(浙大翁恺)—— 素数和