[輸入]
圖的頂點(diǎn)個(gè)數(shù)N,圖中頂點(diǎn)之間的關(guān)系及起點(diǎn)A和終點(diǎn)B
[輸出]
若A到B無(wú)路徑,則輸出“There is no path” 否則輸出A到B路徑上個(gè)頂點(diǎn)
[存儲(chǔ)結(jié)構(gòu)]
圖采用鄰接矩陣的方式存儲(chǔ)。
[算法的基本思想]
采用廣度優(yōu)先搜索的方法,從頂點(diǎn)A開始,依次訪問(wèn)與A鄰接的頂點(diǎn)VA1,VA2,...,VAK, 訪問(wèn)遍之后,若沒有訪問(wèn)B,則繼續(xù)訪問(wèn)與VA1鄰接的頂點(diǎn)VA11,VA12,...,VA1M,再訪問(wèn)與VA2鄰接頂點(diǎn)...,如此下去,直至找到B,最先到達(dá)B點(diǎn)的路徑,一定是邊數(shù)最少的路徑。實(shí)現(xiàn)時(shí)采用隊(duì)列記錄被訪問(wèn)過(guò)的頂點(diǎn)。每次訪問(wèn)與隊(duì)頭頂點(diǎn)相鄰接的頂點(diǎn),然后將隊(duì)頭頂點(diǎn)從隊(duì)列中刪去。若隊(duì)空,則說(shuō)明到不存在通路。在訪問(wèn)頂點(diǎn)過(guò)程中,每次把當(dāng)前頂點(diǎn)的序號(hào)作為與其鄰接的未訪問(wèn)的頂點(diǎn)的前驅(qū)頂點(diǎn)記錄下來(lái),以便輸出時(shí)回溯。
#include<stdio.h>
int number //隊(duì)列類型
typedef struct{
int q[20]
the calculator s usage!
after you have inputed 2 operators,choose + - * / function!
But the only situation I did t deal with is that
when you choos + fuction ,and the operaters signs is like this
-A+B,just turn it to B-A!
圖的深度遍歷,輸出結(jié)果為(紅色為鍵盤輸入的數(shù)據(jù),權(quán)值都置為1):
輸入頂點(diǎn)數(shù)和弧數(shù):8 9
輸入8個(gè)頂點(diǎn).
輸入頂點(diǎn)0:a
輸入頂點(diǎn)1:b
輸入頂點(diǎn)2:c
輸入頂點(diǎn)3:d
輸入頂點(diǎn)4:e
輸入頂點(diǎn)5:f
輸入頂點(diǎn)6:g
輸入頂點(diǎn)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
深度優(yōu)先遍歷: a b d h e c f g
程序結(jié)束.