當前位置:首頁 » 課程大全 » 課程設計圖的遍歷演示

課程設計圖的遍歷演示

發布時間: 2021-02-17 22:45:32

1. C語言編寫程序實現圖的遍歷操作

樓主你好,下面是源程序!

/*/////////////////////////////////////////////////////////////*/
/* 圖的深度優先遍歷 */
/*/////////////////////////////////////////////////////////////*/
#include <stdlib.h>
#include <stdio.h>
struct node /* 圖頂點結構定義 */
{
int vertex; /* 頂點數據信息 */
struct node *nextnode; /* 指下一頂點的指標 */
};
typedef struct node *graph; /* 圖形的結構新型態 */
struct node head[9]; /* 圖形頂點數組 */
int visited[9]; /* 遍歷標記數組 */

/********************根據已有的信息建立鄰接表********************/
void creategraph(int node[20][2],int num)/*num指的是圖的邊數*/
{
graph newnode; /*指向新節點的指針定義*/
graph ptr;
int from; /* 邊的起點 */
int to; /* 邊的終點 */
int i;
for ( i = 0; i < num; i++ ) /* 讀取邊線信息,插入鄰接表*/
{
from = node[i][0]; /* 邊線的起點 */
to = node[i][1]; /* 邊線的終點 */

/* 建立新頂點 */
newnode = ( graph ) malloc(sizeof(struct node));
newnode->vertex = to; /* 建立頂點內容 */
newnode->nextnode = NULL; /* 設定指標初值 */
ptr = &(head[from]); /* 頂點位置 */
while ( ptr->nextnode != NULL ) /* 遍歷至鏈表尾 */
ptr = ptr->nextnode; /* 下一個頂點 */
ptr->nextnode = newnode; /* 插入節點 */
}
}

/********************** 圖的深度優先搜尋法********************/
void dfs(int current)
{
graph ptr;
visited[current] = 1; /* 記錄已遍歷過 */
printf("vertex[%d]\n",current); /* 輸出遍歷頂點值 */
ptr = head[current].nextnode; /* 頂點位置 */
while ( ptr != NULL ) /* 遍歷至鏈表尾 */
{
if ( visited[ptr->vertex] == 0 ) /* 如過沒遍歷過 */
dfs(ptr->vertex); /* 遞回遍歷呼叫 */
ptr = ptr->nextnode; /* 下一個頂點 */
}
}

/****************************** 主程序******************************/
void main()
{
graph ptr;
int node[20][2] = { {1, 2}, {2, 1}, /* 邊線數組 */
{1, 3}, {3, 1},
{1, 4}, {4, 1},
{2, 5}, {5, 2},
{2, 6}, {6, 2},
{3, 7}, {7, 3},
{4, 7}, {4, 4},
{5, 8}, {8, 5},
{6, 7}, {7, 6},
{7, 8}, {8, 7} };
int i;
clrscr();
for ( i = 1; i <= 8; i++ ) /* 頂點數組初始化 */
{
head[i].vertex = i; /* 設定頂點值 */
head[i].nextnode = NULL; /* 指針為空 */
visited[i] = 0; /* 設定遍歷初始標志 */
}
creategraph(node,20); /* 建立鄰接表 */
printf("Content of the gragh's ADlist is:\n");
for ( i = 1; i <= 8; i++ )
{
printf("vertex%d ->",head[i].vertex); /* 頂點值 */
ptr = head[i].nextnode; /* 頂點位置 */
while ( ptr != NULL ) /* 遍歷至鏈表尾 */
{
printf(" %d ",ptr->vertex); /* 印出頂點內容 */
ptr = ptr->nextnode; /* 下一個頂點 */
}
printf("\n"); /* 換行 */
}
printf("\nThe end of the dfs are:\n");
dfs(1); /* 列印輸出遍歷過程 */
printf("\n"); /* 換行 */
puts(" Press any key to quit...");
getch();
}


/*//////////////////////////////////////////*/
/* 圖形的廣度優先搜尋法 */
/* ///////////////////////////////////////*/
#include <stdlib.h>
#include <stdio.h>
#define MAXQUEUE 10 /* 隊列的最大容量 */
struct node /* 圖的頂點結構定義 */
{
int vertex;
struct node *nextnode;
};
typedef struct node *graph; /* 圖的結構指針 */
struct node head[9]; /* 圖的頂點數組 */
int visited[9]; /* 遍歷標記數組 */
int queue[MAXQUEUE]; /* 定義序列數組 */
int front = -1; /* 序列前端 */
int rear = -1; /* 序列後端 */

/***********************二維數組向鄰接表的轉化****************************/
void creategraph(int node[20][2],int num)
{
graph newnode; /* 頂點指針 */
graph ptr;
int from; /* 邊起點 */
int to; /* 邊終點 */
int i;
for ( i = 0; i < num; i++ ) /* 第i條邊的信息處理 */
{
from = node[i][0]; /* 邊的起點 */
to = node[i][1]; /* 邊的終點 */
/* 建立新頂點 */
newnode = ( graph ) malloc(sizeof(struct node));
newnode->vertex = to; /* 頂點內容 */
newnode->nextnode = NULL; /* 設定指針初值 */
ptr = &(head[from]); /* 頂點位置 */
while ( ptr->nextnode != NULL ) /* 遍歷至鏈表尾 */
ptr = ptr->nextnode; /* 下一個頂點 */
ptr->nextnode = newnode; /* 插入第i個節點的鏈表尾部 */
}
}

/************************ 數值入隊列************************************/
int enqueue(int value)
{
if ( rear >= MAXQUEUE ) /* 檢查佇列是否全滿 */
return -1; /* 無法存入 */
rear++; /* 後端指標往前移 */
queue[rear] = value; /* 存入佇列 */
}

/************************* 數值出隊列*********************************/
int dequeue()
{
if ( front == rear ) /* 隊列是否為空 */
return -1; /* 為空,無法取出 */
front++; /* 前端指標往前移 */
return queue[front]; /* 從隊列中取出信息 */
}

/*********************** 圖形的廣度優先遍歷************************/
void bfs(int current)
{
graph ptr;
/* 處理第一個頂點 */
enqueue(current); /* 將頂點存入隊列 */
visited[current] = 1; /* 已遍歷過記錄標志置疑1*/
printf(" Vertex[%d]\n",current); /* 列印輸出遍歷頂點值 */
while ( front != rear ) /* 隊列是否為空 */
{
current = dequeue(); /* 將頂點從隊列列取出 */
ptr = head[current].nextnode; /* 頂點位置 */
while ( ptr != NULL ) /* 遍歷至鏈表尾 */
{
if ( visited[ptr->vertex] == 0 ) /*頂點沒有遍歷過*/
{
enqueue(ptr->vertex); /* 獎定點放入隊列 */
visited[ptr->vertex] = 1; /* 置遍歷標記為1 */
printf(" Vertex[%d]\n",ptr->vertex);/* 印出遍歷頂點值 */
}
ptr = ptr->nextnode; /* 下一個頂點 */
}
}
}

/*********************** 主程序 ************************************/
/*********************************************************************/
void main()
{
graph ptr;
int node[20][2] = { {1, 2}, {2, 1}, /* 邊信息數組 */
{6, 3}, {3, 6},
{2, 4}, {4, 2},
{1, 5}, {5, 1},
{3, 7}, {7, 3},
{1, 7}, {7, 1},
{4, 8}, {8, 4},
{5, 8}, {8, 5},
{2, 8}, {8, 2},
{7, 8}, {8, 7} };
int i;
clrscr();
puts("This is an example of Width Preferred Traverse of Gragh.\n");
for ( i = 1; i <= 8; i++ ) /*頂點結構數組初始化*/
{
head[i].vertex = i;
head[i].nextnode = NULL;
visited[i] = 0;
}
creategraph(node,20); /* 圖信息轉換,鄰接表的建立 */
printf("The content of the graph's allist is:\n");
for ( i = 1; i <= 8; i++ )
{
printf(" vertex%d =>",head[i].vertex); /* 頂點值 */
ptr = head[i].nextnode; /* 頂點位置 */
while ( ptr != NULL ) /* 遍歷至鏈表尾 */
{
printf(" %d ",ptr->vertex); /* 列印輸出頂點內容 */
ptr = ptr->nextnode; /* 下一個頂點 */
}
printf("\n"); /* 換行 */
}
printf("The contents of BFS are:\n");
bfs(1); /* 列印輸出遍歷過程 */
printf("\n"); /* 換行 */
puts(" Press any key to quit...");
getch();
}


2. 數據結構課程設計:圖的遍歷

圖及其應用http://blog.163.com/yhss_214/blog/static/10376979200711773346563

3. 數據結構課程設計題目,圖的建立以及遍歷。

//圖的遍歷演算法程序

//圖的遍歷是指按某條搜索路徑訪問圖中每個結點,使得每個結點均被訪問一次,而且僅被訪問一次。圖的遍歷有深度遍歷演算法和廣度遍歷演算法,程序如下:
#include <iostream>
//#include <malloc.h>
#define INFINITY 32767
#define MAX_VEX 20 //最大頂點個數
#define QUEUE_SIZE (MAX_VEX+1) //隊列長度
using namespace std;
bool *visited; //訪問標志數組
//圖的鄰接矩陣存儲結構
typedef struct{
char *vexs; //頂點向量
int arcs[MAX_VEX][MAX_VEX]; //鄰接矩陣
int vexnum,arcnum; //圖的當前頂點數和弧數
}Graph;
//隊列類
class Queue{
public:
void InitQueue(){
base=(int *)malloc(QUEUE_SIZE*sizeof(int));
front=rear=0;
}
void EnQueue(int e){
base[rear]=e;
rear=(rear+1)%QUEUE_SIZE;
}
void DeQueue(int &e){
e=base[front];
front=(front+1)%QUEUE_SIZE;
}
public:
int *base;
int front;
int rear;
};
//圖G中查找元素c的位置
int Locate(Graph G,char c){
for(int i=0;i<G.vexnum;i++)
if(G.vexs[i]==c) return i;
return -1;
}
//創建無向網
void CreateUDN(Graph &G){
int i,j,w,s1,s2;
char a,b,temp;
printf("輸入頂點數和弧數:");
scanf("%d%d",&G.vexnum,&G.arcnum);
temp=getchar(); //接收回車
G.vexs=(char *)malloc(G.vexnum*sizeof(char)); //分配頂點數目
printf("輸入%d個頂點.\n",G.vexnum);
for(i=0;i<G.vexnum;i++){ //初始化頂點
printf("輸入頂點%d:",i);
scanf("%c",&G.vexs[i]);
temp=getchar(); //接收回車
}
for(i=0;i<G.vexnum;i++) //初始化鄰接矩陣
for(j=0;j<G.vexnum;j++)
G.arcs[i][j]=INFINITY;
printf("輸入%d條弧.\n",G.arcnum);
for(i=0;i<G.arcnum;i++){ //初始化弧
printf("輸入弧%d:",i);
scanf("%c %c %d",&a,&b,&w); //輸入一條邊依附的頂點和權值
temp=getchar(); //接收回車
s1=Locate(G,a);
s2=Locate(G,b);
G.arcs[s1][s2]=G.arcs[s2][s1]=w;
}
}
//圖G中頂點k的第一個鄰接頂點
int FirstVex(Graph G,int k){
if(k>=0 && k<G.vexnum){ //k合理
for(int i=0;i<G.vexnum;i++)
if(G.arcs[k][i]!=INFINITY) return i;
}
return -1;
}
//圖G中頂點i的第j個鄰接頂點的下一個鄰接頂點
int NextVex(Graph G,int i,int j){
if(i>=0 && i<G.vexnum && j>=0 && j<G.vexnum){ //i,j合理
for(int k=j+1;k<G.vexnum;k++)
if(G.arcs[i][k]!=INFINITY) return k;
}
return -1;
}
//深度優先遍歷
void DFS(Graph G,int k){
int i;
if(k==-1){ //第一次執行DFS時,k為-1
for(i=0;i<G.vexnum;i++)
if(!visited[i]) DFS(G,i); //對尚未訪問的頂點調用DFS
}
else{
visited[k]=true;
printf("%c ",G.vexs[k]); //訪問第k個頂點
for(i=FirstVex(G,k);i>=0;i=NextVex(G,k,i))
if(!visited[i]) DFS(G,i); //對k的尚未訪問的鄰接頂點i遞歸調用DFS
}
}
//廣度優先遍歷
void BFS(Graph G){
int k;
Queue Q; //輔助隊列Q
Q.InitQueue();
for(int i=0;i<G.vexnum;i++)
if(!visited[i]){ //i尚未訪問
visited[i]=true;
printf("%c ",G.vexs[i]);
Q.EnQueue(i); //i入列
while(Q.front!=Q.rear){
Q.DeQueue(k); //隊頭元素出列並置為k
for(int w=FirstVex(G,k);w>=0;w=NextVex(G,k,w))
if(!visited[w]){ //w為k的尚未訪問的鄰接頂點
visited[w]=true;
printf("%c ",G.vexs[w]);
Q.EnQueue(w);
}
}
}
}

//主函數
void main(){
int i;
Graph G;
CreateUDN(G);
visited=(bool *)malloc(G.vexnum*sizeof(bool));
printf("\n廣度優先遍歷: ");
for(i=0;i<G.vexnum;i++)
visited[i]=false;
DFS(G,-1);
printf("\n深度優先遍歷: ");
for(i=0;i<G.vexnum;i++)
visited[i]=false;
BFS(G);
printf("\n程序結束.\n");
}
輸出結果為(紅色為鍵盤輸入的數據,權值都置為1):
輸入頂點數和弧數:8 9
輸入8個頂點.
輸入頂點0:a
輸入頂點1:b
輸入頂點2:c
輸入頂點3:d
輸入頂點4:e
輸入頂點5:f
輸入頂點6:g
輸入頂點7:h
輸入9條弧.
輸入弧0:a b 1
輸入弧1:b d 1
輸入弧2:b e 1
輸入弧3:d h 1
輸入弧4:e h 1
輸入弧5:a c 1
輸入弧6:c f 1
輸入弧7:c g 1
輸入弧8:f g 1
廣度優先遍歷: a b d h e c f g
深度優先遍歷: a b c d e f g h
程序結束.

4. 數據結構課程設計圖遍歷的演示

{d[i][j]=d[i][k]+d[k][j]; p[i][j]=k; }
} cout<<"最短路徑數組P[i][j]如下:\n"; for(i=1;i<=D.vexnum;i++) {
for(j=1;j<=D.vexnum;j++) cout<<p[i][j]<<" "; cout<<endl; }
cout<<"各對頂點最短路徑長度為\n"; for(i=1;i<=D.vexnum;i++) for(j=1;j<=D.vexnum;j++) {if(i!=j)
cout<<D.vexs[i]<<"-->"<<D.vexs[j]<<":"<<d[i][j]; cout<<endl; } }
//CirQueue.h(隊列的定義及基本操作)
struct Queue {
int save[20];

int tou,wei; }; //initial
void InitQueue(Queue *q) { int i;
for(i=0;i<20;i++) q->save[i]=0; q->tou=0; q->wei=0; }
//EnQueue
void EnQueue(Queue *q,int n) {
q->save[q->wei]=n; q->wei++; }
//DeQueue
void DeQueue(Queue *q,int &n) {
n=q->save[q->tou]; q->tou++;

} //Empty
int QueueEmpty(Queue *q) {
if(q->tou==q->wei) return 0; else return 1; }
//Sqstack.h(棧的定義及基本操作)
#define STACK_INIT_SIZE 999 typedef int ElemType; typedef struct /*定義棧類型*/ {
ElemType *base; ElemType *top; int stacksize; int length; }SqStack;
void InitStack(SqStack &S) /*初始化棧*/ {
S.base = (ElemType *)malloc(STACK_INIT_SIZE * sizeof(ElemType));

if(!S.base) exit(-1); S.top=S.base;
S.stacksize=STACK_INIT_SIZE; S.length=0; }
int StackEmpty(SqStack S) /*判斷棧空否*/ {
if(S.top==S.base) return 1; else return 0; }
void Push(SqStack &S,ElemType e) /*把數據壓入棧*/ {
if(S.top - S.base>=S.stacksize) {
S.base=(ElemType *) realloc(S.base, (S.stacksize + 10) * sizeof(ElemType)); if(!S.base)exit(-1); S.top=S.base+S.stacksize; S.stacksize +=10; }

*(S.top++)=e; ++S.length; }
int Pop(SqStack &S,ElemType &e)/*刪除棧頂元素*/ {
if(S.top==S.base) return 0; e=*--S.top; --S.length; return 1;

5. 數據結構圖的遍歷,要求用圖形演示遍歷過程,求大神!!!

建議還是找本數據結構課本研究,網路很難發圖的

6. 圖的遍歷的演示

深度優先遍歷的遞歸演算法 (1)深度優先遍歷演算法 int visited[MaxVertexNum]; //訪問標志向量是全局版量void DFSTraverse(ALGraph *G)//DFSTraverse(2)鄰接表權表示的深度優先搜索演算法void DFS(ALGraph *G,int i)}//DFS#define MaxVertexNum 5#define m 5#define NULL 0typedef struct nodeJD;typedef struct tnodeTD;typedef structALGRAPH;void DFS(ALGRAPH *G,int i);void creat(ALGRAPH *G)}}int visited[MaxVertexNum];void DFSTraverse(ALGRAPH *G)/*DFSTraverse */

熱點內容
武漢大學學生會輔導員寄語 發布:2021-03-16 21:44:16 瀏覽:612
七年級學生作文輔導學案 發布:2021-03-16 21:42:09 瀏覽:1
不屑弟高考成績 發布:2021-03-16 21:40:59 瀏覽:754
大學畢業證會有成績單 發布:2021-03-16 21:40:07 瀏覽:756
2017信陽學院輔導員招聘名單 發布:2021-03-16 21:40:02 瀏覽:800
查詢重慶2018中考成績查詢 發布:2021-03-16 21:39:58 瀏覽:21
結業考試成績怎麼查詢 發布:2021-03-16 21:28:40 瀏覽:679
14中醫醫師資格筆試考試成績查分 發布:2021-03-16 21:28:39 瀏覽:655
名著賞析課程標准 發布:2021-03-16 21:27:57 瀏覽:881
北京大學商業領袖高端培訓課程 發布:2021-03-16 21:27:41 瀏覽:919