golang 最短路径实现通过djistra,spfa,递归

{"graph":{"S263":{"J89": 40,"S271": 30},"J89":{"S263": 40,"BDBC1": 170,"BDB1": 100,"BD604": 280,"S267": 120},"S271":{"S263": 30,"BD01": 80},"S315":{"J40": 90,"S316": 10},"J40":{"S315": 90,"J39": 470,"BD716": 110,"GB101": 60},"S316":{"S315": 10,"S313": 10},"S313":{"S316": 10,"S314": 10},"S314":{"S313": 10,"BD506": 20},"BD506":{"S314": 20},"S320":{"J10": -20,"S319": 10},"J10":{"S320": 70,"S103": 40,"BD501": 90},"S319":{"S320": 10,"S318": 10},"S318":{"S319": 10,"BD502": 20},"BD502":{"S318": 20},"S317":{"J12": 150,"BD504": 20},"J12":{"S317": 150,"BD505": 110,"J11": 100},"BD504":{"S317": 20},"J116":{"J92": 10,"BP04": 5,"BP05": 540},"J92":{"J116": 10,"BP03": 210,"BP02": 195,"BP01": 180,"S136": 130,"J91": 60},"BD01":{"S271": 80,"S259": 0,"S269": 0,"S270": 0},"J37":{"GB011": 120,"S13": 140,"BDFL1": 175,"GB012": 120,"J38": 1080},"GB011":{"J37": 120},"J70":{"GB162": 80,"GB161": 80,"J69": 310,"BD512": 110,"J114": 70},"GB162":{"J70": 80},"BP04":{"J116": 5},"BP05":{"J116": 540},"S259":{"BD01": 0},"S269":{"BD01": 0},"S270":{"BD01": 0},"BP03":{"J92": 210},"BP02":{"J92": 195},"BP01":{"J92": 180},"J34":{"J35": 50,"IP711": 160,"BD106": 330,"IP710": 110,"IP709": 70},"J35":{"J34": 50,"J36": 130,"S309": 80},"IP711":{"J34": 160},"BD106":{"J34": 330},"IP710":{"J34": 110},"IP709":{"J34": 70},"J36":{"IP706": 100,"IP707": 130,"IP708": 170,"J35": 130},"IP706":{"J36": 100},"IP707":{"J36": 130},"IP708":{"J36": 170},"J33":{"GB182": 160,"GB181": 160,"S310": 50,"GB171": 110,"GB173": 110,"GB172": 110,"S188": 150},"GB182":{"J33": 160},"GB181":{"J33": 160},"S309":{"J35": 80,"S310": 40},"S310":{"S309": 40,"J33": 50},"GB171":{"J33": 110},"GB173":{"J33": 110},"GB172":{"J33": 110},"S188":{"J33": 150,"J32": 50},"J32":{"S188": 50,"BDIP1": 160,"J30": 90,"S232": 70},"BDIP1":{"J32": 160},"J76":{"GB151": 130,"J74": 240,"S148": 40},"GB151":{"J76": 130},"J58":{"BD417": 90,"J57": 140,"BD509": 460},"BD417":{"J58": 90},"J57":{"J58": 140,"S264": 70,"S111": 170},"BD509":{"J58": 460},"S15":{"J29": 260,"S14": 30},"J29":{"S15": 260,"S80": 50,"S82": 80},"S14":{"S15": 30,"J115": 60},"J115":{"S14": 60,"S49": 100,"GB021": 130},"S49":{"J115": 100,"S44": 140},"GB021":{"J115": 130},"S44":{"S49": 140,"S122": 40},"S122":{"S44": 40,"S123": 30},"S123":{"S122": 30,"J25": 160},"J25":{"S123": 160,"J26": 50,"S303": 100},"J31":{"BDIP2": 80,"BDIP3": 80,"S225": 50},"BDIP2":{"J31": 80},"BDIP3":{"J31": 80},"S225":{"J31": 50,"J30": 50},"J30":{"S225": 50,"S222": 50,"J32": 90},"S102":{"BD721B": 300,"J42": 100},"BD721B":{"S102": 300},"J42":{"S102": 100,"GB061": 120,"GB062": 120,"J43": 420,"S75": 70,"S246": 100},"GB061":{"J42": 120},"GB062":{"J42": 120},"J43":{"J42": 420,"BD903C": 120,"BD903B": 80,"BD903A": 60,"BD903D": 50},"BD903C":{"J43": 120},"BD903B":{"J43": 80},"BD903A":{"J43": 60},"BD903D":{"J43": 50},"S217":{"J45": 40,"S218": 30},"J45":{"S217": 40,"J46": 450,"S21": 50},"S218":{"S217": 30,"J44": 100},"J44":{"S218": 100,"S198": 175,"J48": 200},"J50":{"GB072": 120,"GB071": 120,"BD703": 305,"BD301": 90,"S199": 270},"GB072":{"J50": 120},"GB071":{"J50": 120},"BD703":{"J50": 305},"BD301":{"J50": 90},"S199":{"J50": 270,"S198": 30},"S198":{"S199": 30,"J44": 175},"J48":{"J44": 200,"BD409": 90,"J49": 160},"BD409":{"J48": 90},"J49":{"J48": 160,"BDRL1B": 60,"BDRL1A": 80},"BDRL1B":{"J49": 60},"BDRL1A":{"J49": 80},"BD410":{"J47": 80},"J47":{"BD410": 80,"BD508": 340,"BD410A": 80,"J46": 100},"BD508":{"J47": 340},"BD410A":{"J47": 80},"J46":{"J47": 100,"BD416": 65,"J45": 450},"BD416":{"J46": 65},"S21":{"J45": 50,"S20": 30},"S20":{"S21": 30,"S125": 90},"S125":{"S20": 90,"S124": 30},"S124":{"S125": 30,"J41": 40},"J41":{"S124": 40,"S294": 40,"S75": 110},"S294":{"J41": 40,"S79": 90},"S79":{"S294": 90,"S78": 50},"S78":{"S79": 50,"S80": 40},"S80":{"S78": 40,"J29": 50},"S82":{"J29": 80,"S312": 50},"S312":{"S82": 50,"S222": 50},"S222":{"S312": 50,"J30": 50},"S232":{"J32": 70,"S12": 110},"S12":{"S232": 110,"S13": 30},"S13":{"S12": 30,"J37": 140},"BDFL1":{"J37": 175},"GB012":{"J37": 120},"J38":{"J37": 1080,"BD705": 640,"J39": 240},"BD705":{"J38": 640},"J39":{"J38": 240,"BD414": 320,"J40": 470},"BD414":{"J39": 320},"BD716":{"J40": 110},"GB101":{"J40": 60},"J75":{"J74": 50,"BD516": 70,"BD514": 95,"BD515": 120},"J74":{"J75": 50,"S118": 50,"J76": 240},"BD516":{"J75": 70},"BD514":{"J75": 95},"BD515":{"J75": 120},"GB161":{"J70": 80},"J67":{"BD702": 320,"BD717": 100,"J68": 170},"BD702":{"J67": 320},"BD717":{"J67": 100},"J68":{"J67": 170,"BD510": 80,"J69": 1040},"BD510":{"J68": 80},"J69":{"J68": 1040,"BD412": 210,"J70": 310},"BD412":{"J69": 210},"BD512":{"J70": 110},"J114":{"J70": 70,"J113": 70,"S295": 80},"J72":{"J71": 1300,"BD109": 120,"BD110": 120},"J71":{"J72": 1300,"S106": 50,"S258": 40},"BD109":{"J72": 120},"BD110":{"J72": 120},"BD723":{"J113": 100},"J113":{"BD723": 100,"BD708": 260,"J114": 70},"BD708":{"J113": 260},"S295":{"J114": 80,"S223": 50},"S223":{"S295": 50,"S107": 80},"S107":{"S223": 80,"S106": 30},"S106":{"S107": 30,"J71": 50},"S258":{"J71": 40,"J73": 110},"J73":{"S258": 110,"BD517": 100,"S119": 60},"BD517":{"J73": 100},"S119":{"J73": 60,"S118": 30},"S118":{"S119": 30,"J74": 50},"S148":{"J76": 40,"S149": 30},"S149":{"S148": 30,"J77": 40},"J77":{"S149": 40,"BD513": 80,"J78": 100},"BD513":{"J77": 80},"J78":{"J77": 100,"BDTG1": 80,"BD413": 270,"S214": 60},"BDTG1":{"J78": 80},"BD413":{"J78": 270},"S214":{"J78": 60,"J79": 100},"J79":{"S214": 100,"S94": 180,"J80": 130},"J82":{"BD511": 80,"J81": 870,"BD718": 460},"BD511":{"J82": 80},"J94":{"BDRB1": 90,"J93": 280},"BDRB1":{"J94": 90},"S75":{"J41": 110,"J42": 70},"S246":{"BD721A": 300,"J42": 100},"BD721A":{"S246": 300},"J27":{"BD401": 100,"J28": 90,"BD402": 290},"BD401":{"J27": 100},"J28":{"J27": 90,"BD202A": 80,"BD202": 80,"S220": 110},"BD402":{"J27": 290},"J51":{"BDRR1B": 110,"BDRR1A": 90,"J52": 170},"BDRR1B":{"J51": 110},"BDRR1A":{"J51": 90},"J52":{"J51": 170,"GB092": 460,"J53": 370,"BD411": 80,"GB091": 460},"GB092":{"J52": 460},"J53":{"BD724": 170,"S24": 120,"S108": 120,"J52": 370},"BD724":{"J53": 170},"S24":{"S25": 30,"J53": 120},"S25":{"S24": 30,"S110": 80},"S27":{"J60": 40,"S56": 30},"J60":{"S27": 40,"S46": 40,"S32": 90},"S56":{"S27": 30,"S31": 40},"S31":{"S56": 40,"S30": 30},"S30":{"S31": 30,"S162": 80},"S162":{"S30": 80,"S163": 30},"S163":{"S162": 30,"S266": 200},"S266":{"S163": 200,"S255": 40},"S255":{"S266": 40,"J86": 70},"J86":{"S255": 70,"GB112": 70,"GB111": 70,"GB121": 110,"GB122": 110,"J85": 60},"GB112":{"J86": 70},"GB111":{"J86": 70},"GB121":{"J86": 110},"GB122":{"J86": 110},"J85":{"J86": 60,"GB131": 90,"GB132": 90,"J84": 50},"GB131":{"J85": 90},"GB132":{"J85": 90},"J87":{"GB141": 85,"S261": 80,"S113": 110},"GB141":{"J87": 85},"J93":{"J94": 280,"S36": 150,"S92": 50,"S137": 100},"S36":{"BD607": 180,"J93": 150},"BD607":{"S36": 180},"BD205":{"J111": 120},"J111":{"BD205": 120,"J112": 40},"J105":{"BD801": 100,"BD802": 100,"J106": 60},"BD801":{"J105": 100},"BD802":{"J105": 100},"J106":{"J105": 60,"S304": 60,"S1": 35},"BD208":{"J107": 150},"J107":{"BD208": 150,"J108": 100,"BD201A": 150,"S177": 40,"S305": 90},"BDBC2":{"J108": 350},"J108":{"BDBC2": 350,"J109": 130,"J107": 100},"S71":{"BD603": 140,"J109": 420},"BD603":{"S71": 140},"J109":{"S71": 420,"BDBC4": 110,"J108": 130},"BDBC4":{"J109": 110},"BD201A":{"J107": 150},"S177":{"BD201": 120,"J107": 40},"BD201":{"S177": 120},"S305":{"J107": 90,"S304": 30},"S304":{"S305": 30,"J106": 60},"S1":{"J106": 35,"J110": 40},"J110":{"S1": 40,"S165": 40,"S3": 40},"S165":{"J110": 40,"S164": 40},"S164":{"S165": 40,"J112": 40},"J112":{"S164": 40,"J111": 40,"S115": 80},"S115":{"J112": 80,"S114": 30},"S114":{"S115": 30,"S2": 40},"S2":{"S114": 40,"S265": 40},"S265":{"S2": 40,"S4": 110},"S4":{"S265": 110,"S262": 50},"S262":{"S4": 50,"J90": 50},"J90":{"S262": 50,"BDBC3": 290,"S142": 90,"S143": 70},"S3":{"J110": 40,"BD204": 100},"BD204":{"S3": 100},"BDBC5":{"J91": 120},"J91":{"BDBC5": 120,"J92": 60,"S101": 80},"S90":{"J96": 50,"S51": 120},"J96":{"S90": 50,"S89": 50,"S92": 50},"S51":{"S90": 120,"S207": 820},"S207":{"S51": 820,"S206": 30},"S206":{"S207": 30,"J97": 80},"J97":{"S206": 80,"BD518": 130,"BD712A": 70,"BD712": 70,"S91": 160},"BD518":{"J97": 130},"BD712A":{"J97": 70},"BD712":{"J97": 70},"S91":{"J97": 160,"J98": 50},"J98":{"S91": 50,"J99": 50,"J100": 310},"J99":{"BD711": 110,"BD711A": 110,"J98": 50},"BD711":{"J99": 110},"BD711A":{"J99": 110},"J100":{"BD803": 80,"J98": 310,"BD804": 100},"BD803":{"J100": 80},"BD804":{"J100": 100},"BD105":{"J19": 200},"J19":{"BD105": 200,"S62": 40,"S140": 100},"J07":{"BD203": 100,"S251": 90},"BD203":{"J07": 100},"J104":{"BD713A": 110,"BD713": 110,"J102": 50},"BD713A":{"J104": 110},"BD713":{"J104": 110},"J102":{"J104": 50,"J101": 310,"J103": 80},"J101":{"BD805": 80,"BD806": 100,"J102": 310},"BD805":{"J101": 80},"BD806":{"J101": 100},"J103":{"BD701": 130,"J102": 80,"S209": 60},"BD701":{"J103": 130},"S209":{"J103": 60,"S208": 30},"S208":{"S209": 30,"J95": 40},"J95":{"S208": 40,"BD714": 70,"BD714A": 70,"S89": 50},"BD714":{"J95": 70},"BD714A":{"J95": 70},"S89":{"J95": 50,"J96": 50},"S92":{"J96": 50,"J93": 50},"S137":{"J93": 100,"S136": 30},"S136":{"S137": 30,"J92": 130},"BDBC3":{"J90": 290},"S101":{"J91": 80,"S142": 40},"S142":{"S101": 40,"J90": 90},"S143":{"J90": 70,"J88": 40},"J88":{"S143": 40,"S267": 50,"S61": 40},"BDBC1":{"J89": 170},"BDB1":{"J89": 100},"BD604":{"J89": 280},"S267":{"J89": 120,"J88": 50},"S61":{"J88": 40,"S261": 50},"S261":{"S61": 50,"J87": 80},"S113":{"J87": 110,"S112": 30},"S112":{"S113": 30,"J84": 40},"J84":{"S112": 40,"J85": 50,"S96": 100},"S96":{"J84": 100,"J83": 80},"J83":{"S96": 80,"BD704": 120,"S94": 50},"BD704":{"J83": 120},"S94":{"J83": 50,"J79": 180},"J80":{"J79": 130,"BD507": 110,"J81": 140},"BD507":{"J80": 110},"J81":{"J80": 140,"BD715": 390,"J82": 870},"BD715":{"J81": 390},"BD718":{"J82": 460},"J65":{"BD404": 50,"BD403": 110,"J64": 110},"BD404":{"J65": 50},"BD403":{"J65": 110},"J64":{"J65": 110,"BD710": 70,"S9": 100},"BD710":{"J64": 70},"J66":{"BD605": 150,"BD606": 150,"J62": 200},"BD605":{"J66": 150},"BD606":{"J66": 150},"J62":{"J66": 200,"J63": 230,"S131": 90},"J63":{"BD405": 50,"S9": 60,"J62": 230},"BD405":{"J63": 50},"S9":{"J64": 100,"J63": 60},"S131":{"J62": 90,"S130": 30},"S130":{"S131": 30,"S121": 40},"S121":{"S130": 40,"S120": 30},"S120":{"S121": 30,"J61": 50},"J61":{"S120": 50,"BD601": 100,"S52": 50},"BD601":{"J61": 100},"S52":{"J61": 50,"S46": 30},"S46":{"S52": 30,"J60": 40},"S32":{"J60": 90,"S34": 40},"S34":{"S32": 40,"S35": 50},"S35":{"S34": 50,"J59": 80},"J59":{"S35": 80,"S29": 50,"S33": 160},"S29":{"BD602": 130,"J59": 50},"BD602":{"S29": 130},"S33":{"J59": 160,"S264": 80},"S264":{"S33": 80,"J57": 70},"S111":{"J57": 170,"S110": 30},"S110":{"S111": 30,"S25": 80},"S108":{"S109": 30,"J53": 120},"S109":{"S108": 30,"J54": 40},"BD411":{"J52": 80},"GB091":{"J52": 460},"J56":{"BD904C": 50,"BD904A": 60,"BD904B": 120,"J55": 420},"BD904C":{"J56": 50},"BD904A":{"J56": 60},"BD904B":{"J56": 120},"J55":{"J56": 420,"BD706A": 400,"GB081": 120,"S224": 70},"BD706A":{"J55": 400},"GB081":{"J55": 120},"S224":{"J55": 70,"J54": 110},"J54":{"S224": 110,"S109": 40,"S53": 160},"S53":{"J54": 160,"S306": 40},"S306":{"S53": 40,"J22": 50},"J22":{"S306": 50,"S69": 50,"S11": 80},"J05":{"BD415": 570,"S166": 70,"S181": 50},"BD415":{"J05": 570},"J06":{"BD302": 120,"S185": 40,"S159": 90},"BD302":{"J06": 120},"S251":{"J07": 90,"S186": 160},"S186":{"S251": 160,"S185": 30},"S185":{"S186": 30,"J06": 40},"S159":{"J06": 90,"S166": 30},"S166":{"S159": 30,"J05": 70},"S181":{"J05": 50,"S182": 30},"S182":{"S181": 30,"J04": 990},"J04":{"S182": 990,"GB051": 110,"GB053": 110,"GB052": 110,"GB041": 80,"GB042": 80,"J03": 90},"GB051":{"J04": 110},"GB053":{"J04": 110},"GB052":{"J04": 110},"BD202A":{"J28": 80},"BD202":{"J28": 80},"S220":{"J28": 110,"S219": 30},"S219":{"S220": 30,"J26": 30},"J26":{"BD719": 110,"S219": 30,"J25": 50},"BD719":{"J26": 110},"S303":{"J25": 100,"S302": 30},"S302":{"S303": 30,"S116": 40},"S116":{"S302": 40,"S117": 30},"S117":{"S116": 30,"S45": 40},"S45":{"S117": 40,"S23": 140},"S23":{"S45": 140,"J23": 100},"J23":{"S23": 100,"GB031": 150,"GB032": 150,"S70": 170},"GB031":{"J23": 150},"GB032":{"J23": 150},"S70":{"J23": 170,"S69": 110},"S69":{"S70": 110,"J22": 50},"S11":{"J22": 80,"S10": 30},"S10":{"S11": 30,"S221": 40},"S221":{"S10": 40,"S189": 40},"S189":{"S221": 40,"S191": 30},"S191":{"S189": 30,"S183": 40},"S183":{"S191": 40,"S184": 30},"S184":{"S183": 30,"J02": 40},"J02":{"S184": 40,"S203": 60,"S17": 90,"J01": 180},"S203":{"J02": 60,"J09": 80},"J09":{"S203": 80,"S134": 40,"S160": 100,"S103": 40},"S134":{"J09": 40,"S135": 30},"S135":{"S134": 30,"S202": 30},"S202":{"S135": 30,"S201": 30},"S201":{"S202": 30,"J11": 40},"J11":{"S201": 40,"J12": 100,"S200": 40,"J13": 30},"BD505":{"J12": 110},"S200":{"BD209": 180,"J11": 40},"BD209":{"S200": 180},"J13":{"J11": 30,"BDF13": 90},"BDF13":{"J13": 90},"S62":{"BD706": 190,"J19": 40},"BD706":{"S62": 190},"S140":{"J19": 100,"S138": 30},"S138":{"S140": 30,"J18": 40},"J18":{"S138": 40,"J16": 60},"J16":{"J18": 60,"S247": 50,"J20": 130,"J17": 170,"J14": 40},"J21":{"BD721": 80,"BD720": 110,"BD707": 100,"S247": 90},"BD721":{"J21": 80},"BD720":{"J21": 110},"BD707":{"J21": 100},"S247":{"J16": 50,"J21": 90},"J20":{"BDFB1": 60,"J16": 130},"BDFB1":{"J20": 60},"J15":{"BD503": 100,"J14": 50},"BD503":{"J15": 100},"J17":{"BDFB2": 100,"BDFB3": 100,"J16": 170},"BDFB2":{"J17": 100},"BDFB3":{"J17": 100},"J14":{"J16": 40,"J15": 50,"S250": 80},"S250":{"J14": 80,"S147": 40},"S147":{"S250": 40,"S144": 30},"S144":{"S147": 30,"S235": 50},"S235":{"S144": 50,"S158": 90},"S158":{"S235": 90,"S167": 30},"S167":{"S158": 30,"S161": 40},"S161":{"S167": 40,"S160": 40},"S160":{"S161": 40,"J09": 100},"S103":{"J09": 40,"J10": 40},"BD501":{"J10": 90},"GB041":{"J04": 80},"GB042":{"J04": 80},"J03":{"J04": 90,"BDFR1": 210,"S233": 60},"BDFR1":{"J03": 210},"S233":{"J03": 60,"S16": 100},"S16":{"S233": 100,"S17": 30},"S17":{"S16": 30,"J02": 90},"J01":{"J02": 180,"BDIP5": 140,"BPFB1": 180,"BDFB4": 170,"BDIP4": 140,"BDIP6": 160},"BDIP5":{"J01": 140},"BPFB1":{"J01": 180},"BDFB4":{"J01": 170},"BDIP4":{"J01": 140},"BDIP6":{"J01": 160}}};S320:S315|S320:BD502|S319:S314|S319:BD502|S318:S313|S318:BD502|S317:S316|S317:BD504|S316:BD506|S315:BD506|S314:BD506|S313:BD506|IP711:BDIP3|IP710:BDIP3|BDIP3:IP710|IP708:BDTG1|BD208:BDRB1|BD208:BDRB1|BD208:S164|IP710:BDIP3|S23:BD724|S23:BDFR1|S23:BDRR1B|S23:BDRL1B|S23:BDFL1|S23:BDIP2|BD413:BD402|BD413:BD402|BD415:BD401|BD415:BD401|BD209:S201|BD209:S203|BD209:BD202A|BD209:S202|BD209:S184|BD209:S135|BD209:S183|BD209:S134|BD209:S203|BD209:S167|BD209:S202|BD209:BDFB3|BD209:S203|BD209:BD202A|BD209:S158|BD209:S191|BD209:BDFB3|BD209:S200|BD209:S200|BD209:S201|BD209:S189|BD209:S201|BD209:S200|BD209:S264|BD209:S200|BD209:S202|BD209:S200|BD209:S202|BD202A:S61|BD202A:GB031|BD202A:S220|BD202A:S219|BD202A:S302|BD202A:S303|BD202:S61|BD202:GB031|BD202:S265|BD202:S303|GB021:BD401|BDF13:BD721|BDF13:BD205|BDF13:BD721|BDF13:BD205|GB061:S294|GB061:BD903A|GB062:BD409|GB032:BD703|GB032:BD504|GB032:BD301|GB031:BD719|GB031:BD724|GB173:S188|GB173:IP708|GB172:IP707|GB053:BD203|GB053:S62|GB053:BD720|GB053:BD504|GB051:BD501|GB051:BD501|GB051:BD501|GB052:S203|GB052:S202|GB052:S201|GB181:BDIP1|GB182:BDIP1|BPFB1:BP01|GB042:BDFR1|GB041:BDFR1|GB081:S224|GB162:BD109|GB161:BD717|GB112:BD604|GB112:S71|GB112:BD704|GB112:BD802|GB112:BDBC1|GB111:BDBC1|GB111:BD607|GB111:BD605|GB111:S29|GB121:BD806|GB121:BD804|GB121:BD718|GB121:S9|GB122:BDRB1|GB122:S92|GB122:S91|GB122:BDTG1|GB131:BDRB1|GB141:BD801|BD904C:BD401|BD904C:BD401|GB151:BD517|BD411:BD401|BD411:BD401|GB091:BDRR1A|GB092:BDRR1A|GB072:BDRL1A|GB071:BDRL1A|BD410:BD401|BD410:BD401|BD410A:BD401|BD410A:BD401|GB012:BDFL1|GB011:BDFL1|GB171:IP706|BD414:BD401|BD414:BD401|GB101:BD716|GB101:BD705|GB162:BD109|GB161:BD702|GB161:S295|GB131:S1|GB131:BD208|GB131:S30|GB132:S4|GB132:BD201|GB141:BD801|BD412:BD401|BD412:BD401|BD409:S264|BD409:S217|BD409:S218|BD409:BDFB1|BD903D:BD401|BD903D:BD401|BD903A:BDFB1|BD903A:BD516|BD903A:BD516|BD903A:S294|BD903A:BD01|BD903A:BD516|BD903A:S294|BD903A:S125|BD903A:BD516|BD903A:S124|BD903A:BD01|BP05:BP04|BP03:BDB1|BP02:BDIP5|BD01:S261|BD01:S271|BD01:S33|BD01:S271|BD01:BD601|BD01:BD701|BD01:S223|BD01:S267|BD01:BD605|BD01:S258|BD01:BDIP4|BD01:BDBC1|BD01:S259|BD01:BDIP2|BD01:S262|BD01:S259|BD01:BDFB1|BD01:S200|BD01:S259|BD01:S269|BD01:S27|BD01:S264|BD01:S255|BD01:BD801|BD01:S265|BD01:BD507|BD01:BD801|BD01:S266|BD01:S53|BD01:IP709|BD01:S263|BD01:BDRB1|BD01:S270|BD01:S270|BD01:S61|BD01:BD401|BD01:S270|BD01:S263|BD01:S250|BD01:S270|BD01:S251|BD01:S270|BD01:BD601|BD01:S271|BD01:S271|BD01:S269|BD01:S271|BD01:S269|BD01:BD517|BD01:S271|BD01:S90|BD01:S259|BD01:S271|BD01:S263|BD01:BDB1|BD01:S271|BD01:S263|BD801:BDFL1|BD801:BDFL1|BD801:BDFR1|BD801:BDFR1|BD801:BDRR1A|BD801:BDRR1A|BD801:BDRL1A|BD801:BDRL1A|BDBC5:BD110|BDBC5:BD110|BDBC2:BDFB1|BDBC2:BDFB1|BDBC2:BD110|BDBC2:BDFB1|BDBC2:BDFB1|BDBC2:BD110|BDBC2:BD110|BDBC3:BDFB1|BDBC3:BDFB1|BDBC3:BD110|BDBC3:BDFB1|BDBC3:BDFB1|BDBC3:BD110|BDBC4:BD110|BDBC4:BD110|BD203:S251|BD203:S181|BD203:S166|BD203:S185|BD203:S159|BD203:S182|BD203:S186|S9:BD404|S9:BD405|S9:BD403|S271:BDIP1|S263:BD106|S165:BD201|S165:BD204|S165:BD208|S165:S163|S165:S182|S164:BD201|S164:BD204|S164:S162|S164:S181|S115:BD802|S115:S107|S115:S113|S51:BD714A|S51:BD712A|S51:BD714|S51:BD712|S51:IP711|S232:BDFB1|S232:BDFL1|S232:BDRL1A|S62:BD706|S62:BD706|S134:BD720|S134:BDIP6|S134:S302|S247:BD720|S247:BDIP4|S247:BD706|BD603:IP708|BD603:IP708|BD603:S71|BD603:BD607|BD603:S101|BD603:BD607|BD603:BD602|BD603:BD601|BD603:BD602|BD603:BD601|BD603:BD602|BD603:BD601|BD603:S262|BD603:S71|BD603:IP709|BD603:BD601|BD802:S114|BD802:BDFL1|BD802:S264|BD802:BDFR1|BD802:BDFL1|BD802:BDRL1B|BD802:BDFR1|BD802:S261|BD802:BDRR1B|BD802:BDRL1B|BD802:BDRR1B|BD208:BDRB1|BD208:S266|BD201:S183|BD201:S184|BD201:S142|BD201:S143|BD201:BDFL1|BD201:S1|BD201:S1|BD201:S177|BD201:S177|BD201:S191|BD201:S189|BD201:BD201A|BD201:BD201A|BD201:BD201A|BD201:BD201A|BD201:S304|BD201:S305|BD201:S2|BD201:BDFR1|BD201:S265|BDTG1:IP709|BDTG1:S214|BDTG1:BD723|BDTG1:BD723|BDTG1:S207|BDTG1:S206|BDTG1:S89|BDTG1:S90|BDBC1:S267|BDBC1:BD721A|BDBC1:BD721A|BDBC1:S130|BDBC1:S131|BDBC1:S137|BDBC1:S136|BD604:BD601|BD604:BD601|BD604:BD601|BD604:BD605|BD604:BD605|BD604:S121|BD604:S120|BD704:S113|BD704:S112|BD704:S266|BD704:S235|BD513:BD514|BD513:BD514|BD205:BD721|BD205:BD721|BDBC3:BD110|BDRB1:S305|BDRB1:S304|BDRB1:S56|BDRB1:BDFB1|BDRB1:S189|BDRB1:BDFB1|BDRB1:S191|BDRB1:S143|BDRB1:S220|BDRB1:S219|BDRB1:S89|BDRB1:S2|BDRB1:S142|BD518:BD514|BD518:BD514|BD518:BD514|BD518:BD514|BD607:S262|BD607:BD605|BD607:BD605|BD607:S137|BD607:S136|BD607:S36|BD607:BD602|BD607:S36|BD607:BDFB3|BD607:BD605|BD607:BD601|BD607:BD605|BD713:S214|BD713:S89|BD713:S207|BD713:S206|BD713:S92|BD713A:S56|BD713A:S92|BD714:S221|BD714:S90|BD714:S92|BD714A:S221|BD714A:S89|BD714A:S90|BD714A:S92|BD711:S214|BD711:S89|BD711:S209|BD711:S208|BD711:S91|BD712:S312|BD712:S90|BD712:S91|BD712A:S312|BD712A:S89|BD712A:S90|BD712A:S91|BD805:IP706|BD805:IP710|BD805:IP706|BD803:IP706|BD803:S101|BD803:IP706|BD804:IP710|BD804:IP710|BD804:IP707|BD804:S80|BD804:IP707|BD806:IP710|BD806:IP707|BD806:S80|BD806:IP707|BD806:IP710|BD711A:S91|BD701:S31|BD515:S25|BD515:S24|BD515:S20|BD515:S21|BD515:BDIP2|BD515:BDIP2|BD515:BD501|BD515:BD501|BD515:BD501|BD515:BD501|BD515:S10|BD515:S11|BD515:BDFR1|BD515:BDFR1|BD514:BDFR1|BD514:BDFR1|BD514:BD512|BD514:BD512|BD516:S149|BD516:S148|BD516:S255|BD516:BD501|BD516:BD501|BD516:BD510|BD516:BD510|BD516:BD511|BD516:BD511|BD516:S14|BD516:S15|BD516:BDFL1|BD516:BDFL1|BD516:BDFL1|BD516:BDFL1|BD109:S258|BD109:S258|BD109:S118|BD109:S119|BD110:BDFB1|BD110:BDFB1|BD110:BDFB1|BD110:BDFB1|BD110:BDFB1|BD110:BDFB1|BD110:BDFB1|BD110:BDFB1|BD723:S107|BD723:S106|BD723:S295|BD723:S295|BD723:S223|BD723:BDFB2|BD723:S223|BD723:S214|BD602:S27|BD602:S30|BD602:S29|BD602:S29|BD602:BDFB3|BD602:S56|BD602:BDIP6|BD602:BDIP6|BD602:BDIP4|BD602:BDIP4|BD602:S33|BD602:BDIP6|BD602:BDFB3|BD602:S264|BD602:BD605|BD602:BD106|BD602:S267|BD602:BDIP6|BD708:IP710|BD708:S94|BD601:S163|BD601:S35|BD601:BDFB2|BD601:S34|BD601:S34|BD601:BDFB1|BD601:BDFB1|BD601:BDFB1|BD601:S162|BD601:BD105|BD601:BDIP6|BD601:S52|BD601:S46|BD601:BDFB1|BD601:BD605|BD601:S31|BD601:S32|BD601:S35|BD601:BD402|BD601:BDIP6|BD601:BD105|BD601:BD105|BD601:BD105|BD601:S137|BD601:S136|BD601:S27|BD601:S27|BD601:S30|BD601:S29|BD601:S29|BD601:S56|BD601:BDFB2|BD601:BDFB2|BD601:BDIP6|BD601:BD105|BD601:BD105|BD601:S32|S75:BD721B|S75:BD721A|S75:BDIP2|S246:BD721A|S246:BD721A|S246:S188|S102:BD721B|S102:BD721B|S102:S188|BD721A:BDFB1|BD721A:BDFB1|BD721B:S130|BD721B:BD606|BD721B:S109|BD721B:S158|BD721B:S120|BD721B:S121|BD721B:S131|BD721B:BD606|BD721B:S108|BD721B:S189|BD721B:S191|BD721B:S167|BD721B:BD606|BD402:BD903C|BD401:BD416|BD401:BD416|BD401:BD417|BD401:BD417|BD401:S117|BD401:BDFB1|BD401:BDFB1|BD401:BDIP1|BD401:BDIP1|BD401:BD904B|BD401:S116|BD401:BDFB1|BD401:BDFB1|BD401:BDIP1|BD401:BDIP1|BD401:BD605|BD301:BDIP6|BD301:BDIP6|BD301:S70|BD301:S69|BD703:S261|BD703:S199|BD703:S198|BD508:S20|BD508:S21|BDRL1B:BDFL1|BDRL1B:BDRR1B|BDRL1B:BD502|BDRL1B:S82|BDRL1B:IP711|BDRL1B:S79|BDRL1B:S78|BDRL1B:IP710|BDRL1B:IP709|BDRL1B:IP708|BDRL1B:S44|BDRL1B:S45|BDRL1B:S103|BDRL1A:S21|BDRL1A:S20|BDRR1B:S82|BDRR1B:IP711|BDRR1B:S79|BDRR1B:S78|BDRR1B:IP711|BDRR1B:IP709|BDRR1B:IP708|BDRR1B:S44|BDRR1B:S45|BDRR1B:BDFR1|BDRR1B:S103|BDRR1B:BD502|BDRR1A:S24|BDRR1A:S25|BDRR1A:S233|IP711:BDIP2|IP711:BDFL1|IP711:BDFB4|IP711:BDFL1|IP711:BD710|IP711:BDFL1|IP711:BDFL1|IP711:BDIP2|IP711:BDFL1|IP711:BDFR1|IP711:BDFB4|IP711:BDFL1|IP711:S49|IP711:S82|IP711:BDIP1|IP711:BDFR1|IP711:BDIP6|IP711:BDIP1|IP711:BDIP1|IP711:BDFB4|IP711:BDFR1|IP711:BD710|IP711:BDFR1|IP711:BDFL1|IP711:BDIP2|IP711:BDFL1|IP711:BDFR1|IP711:BD903B|IP711:BDFL1|IP711:BD716|IP710:BDFL1|IP710:BDFL1|IP710:BDFR1|IP710:BDFR1|IP710:BDFL1|IP710:BDFR1|IP710:S101|IP710:BD716|IP710:S78|IP710:BDFL1|IP710:BDFL1|IP710:BDFL1|IP710:BDIP1|IP710:BDIP1|IP710:S79|IP710:BD716|IP710:BDIP3|IP709:BDFB4|IP709:BDIP1|IP709:BDFB4|IP709:BDFB4|IP709:BDIP1|IP709:BD716|IP709:BDFB4|IP709:BDIP1|IP709:BDFR1|IP709:BDFB4|IP709:S255|IP709:BDIP1|IP709:BDIP3|IP709:BDIP1|IP709:BDFL1|IP709:BDIP1|IP709:BDIP1|IP709:BDFL1|IP709:S218|IP709:S33|IP709:S217|IP709:BDIP1|BD903C:S294|BD903B:S79|BD904B:S224|BD904A:S224|BD904A:BDFB1|BD706A:BD706|BD706A:BD706|BD706A:BD706|BD706A:BD706|BD719:S123|BD719:BDFB2|BD719:BDFB2|BD719:BDFB2|BD719:S266|BD719:S306|BD719:S122|BD505:BD721|BD505:BD721|BD505:BD721|BD505:BD721|BD504:BD506|BD504:S161|BD504:S160|BD504:S250|BD504:S235|BDIP2:S15|BDIP2:S13|BDIP2:S188|BDIP2:BD724|BDIP2:S82|BDIP2:S255|BDIP2:S225|BDIP2:S183|BDIP2:S14|BDIP2:S184|BDIP2:S12|BDIP3:BD706|BDIP3:BD721|BDIP3:BD721|BDIP3:BD706|BDIP3:S198|BDIP3:S199|BDIP3:BD720|BDIP3:BD720|BDIP3:BD721|BDIP3:BD721|BDIP3:BD721|BD718:S96|BD718:S94|BD715:S92|BD715:S90|BD717:S94|BD717:S96|BD702:S199|BD702:S198|BD702:S261|BD606:S121|BD606:S120|BD606:S130|BD606:S131|BD605:S267|BD716:S94|BD716:S225|BD716:S82|BD506:BD502|BD506:BD502|BD506:BD502|BD705:S78|BD705:S251|BDIP1:S222|BDIP1:IP707|BDIP1:IP706|BDIP1:IP706|BDIP1:IP708|BDIP1:IP708|BDIP1:IP708|BDIP1:IP708|BD106:S266|BD106:S33|S36:S267|S225:IP706|S15:BDFL1|S14:BDFL1|S49:BDFL1|S49:BDFR1|S44:BDFL1|S44:BDFR1|S44:IP708|S122:S116|S122:S124|S123:S117|S123:S125|S303:S135|S303:S163|S302:BD202|S302:S162|S116:S148|S117:S149|S45:BDFL1|S45:BDFR1|S45:IP708|S219:S52|S219:S159|S220:S46|S220:S166|S130:BDFB1|S130:BDIP4|S131:BDFB1|S131:BDIP4|S96:IP706|S207:S209|S206:S208|S177:S61|S305:S46|S304:S52|S3:BD204|S3:BD204|S3:S61|S4:BD204|S4:BD204|S2:BDFB1|S265:BD204|S114:S106|S114:S112|S94:S222|S214:BDFB3|S148:S119|S149:S118|S118:S106|S119:S107|S13:BD501|S13:BDFL1|S12:BD501|S12:BDFL1|S309:BDFL1|S309:BDFR1|S309:IP707|S310:BDFL1|S310:BDFR1|S310:IP707|S222:IP707|S312:IP707|S312:BDFB3|S312:BDFL1|S82:BDFL1|S82:BDFR1|S80:BDFL1|S80:BDFR1|S80:IP708|S78:BDFL1|S78:BDFR1|S79:BDFL1|S79:BDFR1|S124:S217|S125:S218|S21:BD501|S20:BD501|S209:BDFB2|S208:BDFB2|S89:IP707|S142:BDFB1|S143:BDFB1|S113:S111|S112:S110|S53:BDFL1|S53:BDFR1|S306:BD724|S306:BDFB1|S31:BDFB2|S32:BDFB2|S34:BDFB2|S35:BDFB3|S33:BDFB3|S111:S108|S111:BD724|S110:S109|S110:BD724|S25:BD501|S25:BD509|S24:BD501|S24:BD509|S108:BDFB2|S109:BDFB2|S70:BD302|S70:BDIP4|S69:BD302|S69:BDIP4|S186:S184|S186:BDFB1|S185:S183|S185:BDFB1|S166:S158|S159:S167|S182:BDFB1|S181:BDFB1|S17:BD501|S17:BDIP4|S17:BDFR1|S16:BD501|S16:BDFR1|S16:BDIP4|S233:BDFB1|S233:BDFR1|S11:BDFR1|S11:BDIP4|S10:BDFR1|S10:BDIP4|S221:BDFR1|S221:IP707|S221:BDFB3|S191:BDFB3|S189:BDFB3|S135:BD720|S135:BDIP6|S103:BD502|S103:BDFR1|S160:S144|S160:BDIP4|S161:S147|S161:BDIP4|S158:BDFB3|S167:BDFB3|S235:BDFB1|S144:BD502|S144:S138|S250:BD720|S250:BD706|S250:BD501|S147:BD502|S147:S140|S140:BD706|S140:BD720|S138:BD706|S138:BD720|BD706:BDFB4|IP708:BDFB4|IP708:BDFB4|IP708:BDFB4|IP707:BDFB4|IP707:BDFB3|IP707:BDFB4|IP707:BDFB3|IP707:BDFB4|IP707:BDFB4|BD724:BDFR1|BD302:BDIP4|BD302:BDIP4|BD302:BDIP4|BDFR1:BD502|IP706:BDFB3|IP706:BDFB3|BD503:BD721|BD503:BD721|BD707:BD721|BD707:BD721|BD720:BDFB2|BD720:BDFB2|BD720:BDFB3|BD720:BDFB3|BD720:BDIP4|BD720:BDIP4|BD720:BDIP6|BDFB1:BD501|BDFB1:BD501|BDFB2:BDIP4|BDFB2:BDIP4

给定的测试数据如下,图和起始点通过;号隔开,起始点通过 | 隔开:

处理函数如下:

func parseBody(body string) (string,string){
	params := strings.Split(body, ";")
	graphInfo := params[0]
	evaluateInfo := params[1]
	return graphInfo,evaluateInfo
}

处理函数如下将图解析进入:

type Graphs map[string]map[string]map[string]float64

func (g *Graphs) getChildByKey(key string) map[string]float64 {
	return (*g)["graph"][key]
}

func (g *Graphs) getLengthByKeys(key1, key2 string) float64 {
	return (*g)["graph"][key1][key2]
}


//解析json
func  parseGraph(graphStr string) (Graphs, error) {
	graphMap := make(Graphs)
	err := json.Unmarshal([]byte(graphStr), &graphMap)
	if err != nil {
		fmt.Println(err)
		return graphMap, err
	}
	if _, ok := graphMap["graph"]; !ok {
		return graphMap, errors.New("请传入graph")
	}
	return graphMap, nil
}


将 map 转换为邻接矩阵(此处处理的不是很好,暂时先用的):

func  mapToSlice(graphMap Graphs) ([][]float64, map[string]int, map[int]string, error) {
	var vertex [][]float64
	//顶点对应的下标
	Key2Index := make(map[string]int)
	//下标对应的顶点
	Index2Key := make(map[int]string)

	for k, v := range graphMap["graph"] {
		Key2Index[k] = 0
		for k1 := range v {
			Key2Index[k1] = 0
		}
	}
	i := 0
	//每个节点映射一个下标,由于map的乱序性每次给同样的key给的下标都不一样
	keys := make([]string, 0)
	for k := range Key2Index {
		keys = append(keys, k)
	}
	sort.Strings(keys)
	for _, v := range keys {
		Key2Index[v] = i
		Index2Key[i] = v
		i++
	}
	//初始化分配二维数组
	for i := 0; i < len(Key2Index); i++ {
		tmp := make([]float64, len(Key2Index))
		vertex = append(vertex, tmp)
	}
	//给每个点赋值无穷大
	for i := 0; i < len(Key2Index); i++ {
		for j := 0; j < len(Key2Index); j++ {
			vertex[i][j] = math.MaxFloat64
		}

	}
	for k, v := range graphMap["graph"] {
		for k1, v := range v {
			vertex[Key2Index[k]][Key2Index[k1]] = v
			vertex[Key2Index[k1]][Key2Index[k]] = v
		}

	}
	return vertex, Key2Index, Index2Key, nil
}

狄杰斯特拉算法:

type Dis struct {
	Path    string
	Length  float64
	Visited bool
}

var dis = []Dis{}
var datas = []string{} //此处由于需要并发处理声明了全局变量并且加了锁

//起点,终点,矩阵,矩阵中索引与json中key的对应
func getShortPathByDijkstra(startNode, endNode int,
	vertex [][]float64, Index2Key map[int]string) {

	var TablePathMin float64 //存放dis中,未遍历的最小结点的值
	defer wg.Done()
	//记录结点是否已经找到v0到vx的最小路径
	dis = make([]Dis, len(vertex))
	 获取v0这一行的权值数组
	for i := 0; i < len(vertex); i++ {
		dis[i].Length = vertex[startNode][i]
		dis[i].Path = Index2Key[startNode] + "->" + Index2Key[i]
	}
	dis[startNode].Length = 0
	dis[startNode].Visited = true
	for v := 1; v < len(vertex); v++ {
		TablePathMin = math.MaxFloat64
		//找出dis中,未遍历的最小结点的值
		Vx := 0 //存放dis中,未遍历的最小结点的下标
		for w := 0; w < len(vertex); w++ {
			if !dis[w].Visited && dis[w].Length < TablePathMin {
				Vx = w
				TablePathMin = dis[w].Length
			}
		}
		dis[Vx].Visited = true
		for j := 0; j < len(vertex); j++ {
			if !dis[j].Visited && dis[Vx].Length+vertex[Vx][j] < dis[j].Length {
				dis[j].Length = dis[Vx].Length + vertex[Vx][j
				dis[j].Path = dis[Vx].Path + "->" + Index2Key[j]

			}
		}
	}

	path := c.getDijstraCadPath(dis[endNode], Index2Key[startNode], Index2Key[endNode])
	datas = append(datas, path)
}

SPFA:

type Path struct {
	StartNode string
	EndNode   string
	Path      string
	Value     float64
}
//s起点索引 e终点索引 n矩阵长度 vert矩阵 Index2Key矩阵中索引对应json中的key
func  SPFA(s, e, n int, p *Path, Vert [][]float64, Index2Key map[int]string) []float64 {

	dis := make([]float64, n) //
	vis := make([]bool, n)
	num := make([]int, n)
	path := make([]int, n)
	disMap := make(map[string]float64)
	q := queue.New()
	for i := 0; i < n; i++ {
		dis[i] = math.MaxFloat64
	}
	dis[s] = 0
	vis[s] = true
	num[s] = 1
	q.Add(s)
	p.StartNode = Index2Key[s]
	p.EndNode = Index2Key[e]
	for q.Length() != 0 {
		a := q.Get(0)
		u := a.(int)

		q.Remove()
		vis[u] = false
		for v := 0; v < n; v++ {
			if dis[v] > dis[u]+Vert[u][v] {
				dis[v] = dis[u] + Vert[u][v]
				disMap[Index2Key[v]] = dis[u] + Vert[u][v]
				path[v] = u
				if !vis[v] {
					q.Add(v)
					vis[v] = true
					num[v]++
					if num[v] >= n {
						goto no
					}
				}
			}
		}

	}
	//以上是spfa的主要算法内容以下为业务操作内容
	fmt.Println("开始路径封装")
	goto ok
no:

	p.Path = "存在负权回路"
	p.Value = math.MaxFloat64
	fmt.Println("结束路径封装", p.Path)
	return dis
ok:
	buf := bytes.Buffer{}
	tmpE := e
	buf.WriteString(Index2Key[tmpE])
	for s != tmpE {
		prv := path[tmpE]
		buf.WriteString("<-" + Index2Key[prv])
		if prv == tmpE {
			break
		}
		tmpE = prv
	}
	p.Path = c.reversePath(buf.String())
	p.Value = disMap[Index2Key[e]]
	fmt.Println("结束路径封装", p.Path)
	return dis
}

//起点,终点,路径字符串(用来拼接路径) ,传入一个paths map用来记录所有的路径及其长度最终递归执行完,遍历此map即可
func  getAllPath(startNode, endNode, path string,
	graph Graphs, paths map[string]float64, length float64) (string, float64) {
	NextNodesMap := graph.getChildByKey(startNode)
	if startNode == endNode || len(NextNodesMap) == 0 {
		return path, length
	}

	curPath := path
	curLength := length
	for key := range NextNodesMap {
		//判断key在中间,在开头,在结尾
		if checkNodeIsPath(path, key) {
			continue
		}
		//获取到当前节点的路径
		addKey(&path, key)
		//计算到当前节点的距离
		calculatePath(≤ngth, startNode, key, graph)
		lastPath, lastLength := getAllPath(key, endNode, path, graph, paths, length)
		//当遍历完当前节点的路径后重置path,length为当前path,length
		path = curPath
		length = curLength
		if strings.Contains(lastPath, startNode) && strings.Contains(lastPath, endNode){
			paths[lastPath] = lastLength
		}

	}
	return curPath, curLength
}


func  addKey(s *string, key string) {
	var build strings.Builder
	build.WriteString(*s)
	build.WriteString("->")
	build.WriteString(key)
	*s = build.String()
}

func calculatePath(length *float64, preNode, curNode string, graph Graphs) {
	*length += graph.getLengthByKeys(preNode, curNode)
}


递归全遍历:

全部代码:

/**
 * @Author: xiaoxiao
 * @Description:
 * @File:  CaclService
 * @Version: 1.0.0
 * @Date: 4/11/20 8:06 下午
 */
package service

import (
	"bytes"
	"encoding/json"
	"errors"
	"fmt"
	"github.com/spf13/cast"
	"github.com/xhaoxiong/util/cad"
	"gopkg.in/eapache/queue.v1"
	"math"
	"sort"
	"strings"
	"sync"
)

type CacService struct{}

func NewCacService() *CacService {
	return &CacService{}
}

/*Dijkstra使用*/
type Dis struct {
	Path    string
	Length  float64
	Visited bool
}

var dis = []Dis{}

type Graphs map[string]map[string]map[string]float64

var datas = []string{}
var mutex sync.Mutex

/*SPFA使用*/
type Path struct {
	StartNode string
	EndNode   string
	Path      string
	Value     float64
}

func (g *Graphs) getChildByKey(key string) map[string]float64 {
	return (*g)["graph"][key]
}

func (g *Graphs) getLengthByKeys(key1, key2 string) float64 {
	return (*g)["graph"][key1][key2]
}

/**
Dijkstra算法求解
*/
func (c *CacService) GetAllPathsByDijkstra(KVs string, graph string) string {
	parseGraph, _ := c.parseGraph(graph)
	vertex, m, m2, _ := c.mapToSlice(parseGraph)
	kvs := strings.Split(KVs, "|")
	wg := &sync.WaitGroup{}
	datas = make([]string, len(kvs))
	for _, KV := range kvs {
		kv := strings.Split(KV, ":")
		startNode, endNode := kv[0], kv[1]
		wg.Add(1)
		c.getShortPathByDijkstra(wg, m[startNode], m[endNode], vertex, m2)
	}
	wg.Wait()
	return cad.GetCADSlice2Str(datas...)
}

func (c *CacService) parseGraph(graphStr string) (Graphs, error) {
	graphMap := make(Graphs)
	err := json.Unmarshal([]byte(graphStr), &graphMap)
	if err != nil {
		fmt.Println(err)
		return graphMap, err
	}
	if _, ok := graphMap["graph"]; !ok {
		return graphMap, errors.New("请传入graph")
	}
	return graphMap, nil
}

func (c *CacService) mapToSlice(graphMap Graphs) ([][]float64, map[string]int, map[int]string, error) {
	var vertex [][]float64
	//顶点对应的下标
	Key2Index := make(map[string]int)
	//下标对应的顶点
	Index2Key := make(map[int]string)

	for k, v := range graphMap["graph"] {
		Key2Index[k] = 0
		for k1 := range v {
			Key2Index[k1] = 0
		}
	}
	i := 0
	//每个节点映射一个下标,由于map的乱序性每次给同样的key给的下标都不一样
	keys := make([]string, 0)
	for k := range Key2Index {
		keys = append(keys, k)
	}
	sort.Strings(keys)
	for _, v := range keys {
		Key2Index[v] = i
		Index2Key[i] = v
		i++
	}
	//初始化分配二维数组
	for i := 0; i < len(Key2Index); i++ {
		tmp := make([]float64, len(Key2Index))
		vertex = append(vertex, tmp)
	}
	//给每个点赋值无穷大
	for i := 0; i < len(Key2Index); i++ {
		for j := 0; j < len(Key2Index); j++ {
			vertex[i][j] = math.MaxFloat64
		}

	}
	for k, v := range graphMap["graph"] {
		for k1, v := range v {
			vertex[Key2Index[k]][Key2Index[k1]] = v
			vertex[Key2Index[k1]][Key2Index[k]] = v
		}

	}
	return vertex, Key2Index, Index2Key, nil
}

func (c *CacService) getShortPathByDijkstra(wg *sync.WaitGroup, startNode, endNode int,
	vertex [][]float64, Index2Key map[int]string) {

	var TablePathMin float64 //存放dis中,未遍历的最小结点的值
	defer wg.Done()
	//记录结点是否已经找到v0到vx的最小路径
	dis = make([]Dis, len(vertex))
	 获取v0这一行的权值数组
	for i := 0; i < len(vertex); i++ {
		dis[i].Length = vertex[startNode][i]
		dis[i].Path = Index2Key[startNode] + "->" + Index2Key[i]
	}
	dis[startNode].Length = 0
	dis[startNode].Visited = true
	for v := 1; v < len(vertex); v++ {
		TablePathMin = math.MaxFloat64
		//找出dis中,未遍历的最小结点的值
		Vx := 0 //存放dis中,未遍历的最小结点的下标
		for w := 0; w < len(vertex); w++ {
			if !dis[w].Visited && dis[w].Length < TablePathMin {
				Vx = w
				TablePathMin = dis[w].Length
			}
		}
		dis[Vx].Visited = true
		for j := 0; j < len(vertex); j++ {
			if !dis[j].Visited && dis[Vx].Length+vertex[Vx][j] < dis[j].Length {
				dis[j].Length = dis[Vx].Length + vertex[Vx][j]
				//end := j + 1
				//if j == len(vertex)-1 {
				//	end = j
				//}
				dis[j].Path = dis[Vx].Path + "->" + Index2Key[j]

			}
		}
	}

	path := c.getDijstraCadPath(dis[endNode], Index2Key[startNode], Index2Key[endNode])
	datas = append(datas, path)
	mutex.Unlock()
}

func (c *CacService) getDijstraCadPath(dis Dis,
	startNode, endNode string) string {
	if dis.Length == math.MaxFloat64 {
		minPath := cad.GetCADObject(cad.GetCADString("起点", startNode),
			cad.GetCADString("终点", endNode),
			cad.GetCADSpaceString("路径", getCADNoPath(startNode, endNode, "路径不通")),
			cad.GetCADString("长度", "0"))
		return minPath
	} else {
		minPath := cad.GetCADObject(cad.GetCADString("起点", startNode),
			cad.GetCADString("终点", endNode),
			cad.GetCADSpaceString("路径", getCADPath(dis.Path)),
			cad.GetCADString("长度", cast.ToString(dis.Length)),
		)
		return minPath
	}

}

/**
递归算法全路径遍历,然后找到最短的路径
*/

func (c *CacService) GetAllPathsByRecursive(KVs string, graph string) string {
	parseGraph, _ := c.parseGraph(graph)
	kvs := strings.Split(KVs, "|")
	wg := &sync.WaitGroup{}
	datas = make([]string, len(kvs))
	for _, KV := range kvs {
		kv := strings.Split(KV, ":")
		startNode, endNode := kv[0], kv[1]
		wg.Add(1)
		path, paths, length := startNode, make(map[string]float64), 0
		go func(wg *sync.WaitGroup, startNode, endNode, path string,
			graph Graphs, paths map[string]float64, length float64) {
			c.getMinPath(wg, startNode, endNode, path, graph, paths, length)
		}(wg, startNode, endNode, path, parseGraph, paths, float64(length))
	}
	wg.Wait()
	return cad.GetCADObject(datas...)
}

func (c *CacService) getAllPath(startNode, endNode, path string,
	graph Graphs, paths map[string]float64, length float64) (string, float64) {
	NextNodesMap := graph.getChildByKey(startNode)
	if startNode == endNode || len(NextNodesMap) == 0 {
		return path, length
	}

	curPath := path
	curLength := length
	for key := range NextNodesMap {
		//判断key在中间,在开头,在结尾
		if c.checkNodeIsPath(path, key) {
			continue
		}
		//获取到当前节点的路径
		c.addKey(&path, key)
		//计算到当前节点的距离
		c.calculatePath(≤ngth, startNode, key, graph)
		lastPath, lastLength := c.getAllPath(key, endNode, path, graph, paths, length)
		//当遍历完当前节点的路径后重置path,length为当前path,length
		path = curPath
		length = curLength
		if strings.Contains(lastPath, startNode) && strings.Contains(lastPath, endNode) {
			paths[lastPath] = lastLength
		}

	}
	return curPath, curLength
}

func (c *CacService) checkNodeIsPath(path, node string) bool {
	ps := strings.Split(path, "->")
	m := make(map[string]bool)
	for i := range ps {
		m[ps[i]] = true
	}
	return m[node]
}

func (c *CacService) addKey(s *string, key string) {
	var build strings.Builder
	build.WriteString(*s)
	build.WriteString("->")
	build.WriteString(key)
	*s = build.String()
}

func (c *CacService) calculatePath(length *float64, preNode, curNode string, graph Graphs) {
	*length += graph.getLengthByKeys(preNode, curNode)
}

func (c *CacService) getMinPath(wg *sync.WaitGroup, startNode, endNode, path string,
	graph Graphs, paths map[string]float64, length float64) {
	defer wg.Done()

	c.getAllPath(startNode, endNode, path, graph, paths, length)
	//正常应该有多条最短路径,需求仅仅只需要返回一条
	minPaths := c.getShortPaths(paths, startNode, endNode)
	mutex.Lock()
	datas = append(datas, minPaths)
	mutex.Unlock()
}

func (c *CacService) getShortPaths(paths map[string]float64, startNode, endNode string) string {
	minLen := float64(0)
	minK := ""
	for k, v := range paths {
		if minLen == 0 {
			minLen = v
			minK = k
		}
		if minLen != 0 && v < minLen {
			minLen = v
			minK = k
		}
	}
	//这是存在最短路径
	if len(paths) > 0 {
		minPath := cad.GetCADObject(cad.GetCADString("起点", startNode),
			cad.GetCADString("终点", endNode),
			cad.GetCADSpaceString("路径", getCADPath(minK)),
			cad.GetCADString("长度", cast.ToString(minLen)),
		)
		return minPath
	} else {
		minPath := cad.GetCADObject(cad.GetCADString("起点", startNode),
			cad.GetCADString("终点", endNode),
			cad.GetCADSpaceString("路径", getCADNoPath(startNode, endNode, "路径不通")),
			cad.GetCADString("长度", cast.ToString(minLen)), )
		return minPath
	}

}

/**
SPFA算法
*/

func (c *CacService) GetAllPathBySPFA(KVs string, graph string) string {
	splits := strings.Split(KVs, "|")
	parseGraph, _ := c.parseGraph(graph)
	vert, Key2Index, Index2Key, _ := c.mapToSlice(parseGraph)
	Paths := make([]*Path, 0)
	for v := range splits {
		k := strings.Split(splits[v], ":")
		startNode := Key2Index[k[0]]
		endNode := Key2Index[k[1]]
		path := &Path{}
		c.SPFA(startNode, endNode, len(vert), path, vert, Index2Key)
		Paths = append(Paths, path)
	}
	datas := make([]string, 0)
	for _, p := range Paths {
		datas = append(datas, c.getSPFACADPath(p))
	}

	return cad.GetCADSlice2Str(datas...)
}

func (c *CacService) SPFA(s, e, n int, p *Path, Vert [][]float64, Index2Key map[int]string) []float64 {

	dis := make([]float64, n) //
	vis := make([]bool, n)
	num := make([]int, n)
	path := make([]int, n)
	disMap := make(map[string]float64)
	q := queue.New()
	for i := 0; i < n; i++ {
		dis[i] = math.MaxFloat64
	}
	dis[s] = 0
	vis[s] = true
	num[s] = 1
	q.Add(s)
	p.StartNode = Index2Key[s]
	p.EndNode = Index2Key[e]
	for q.Length() != 0 {
		a := q.Get(0)
		u := a.(int)

		q.Remove()
		vis[u] = false
		for v := 0; v < n; v++ {
			if dis[v] > dis[u]+Vert[u][v] {
				dis[v] = dis[u] + Vert[u][v]
				disMap[Index2Key[v]] = dis[u] + Vert[u][v]
				path[v] = u
				if !vis[v] {
					q.Add(v)
					vis[v] = true
					num[v]++
					if num[v] >= n {
						goto no
					}
				}
			}
		}

	}
	//以上是spfa的主要算法内容以下为业务操作内容
	fmt.Println("开始路径封装")
	goto ok
no:

	p.Path = "存在负权回路"
	p.Value = math.MaxFloat64
	fmt.Println("结束路径封装", p.Path)
	return dis
ok:
	buf := bytes.Buffer{}
	tmpE := e
	buf.WriteString(Index2Key[tmpE])
	for s != tmpE {
		prv := path[tmpE]
		buf.WriteString("<-" + Index2Key[prv])
		if prv == tmpE {
			break
		}
		tmpE = prv
	}
	p.Path = c.reversePath(buf.String())
	p.Value = disMap[Index2Key[e]]
	fmt.Println("结束路径封装", p.Path)
	return dis
}

func (c *CacService) reversePath(path string) string {
	buf := bytes.Buffer{}
	pathSlice := strings.Split(path, "<-")
	for i, j := 0, len(pathSlice)-1; i < j; i, j = i+1, j-1 {
		pathSlice[i], pathSlice[j] = pathSlice[j], pathSlice[i]
	}
	node := ""
	for i := 0; i < len(pathSlice); i++ {
		if i != len(pathSlice)-1 {
			node = pathSlice[i] + "->"
		} else {
			node = pathSlice[i]
		}
		buf.WriteString(node)
	}
	return buf.String()
}

func (c *CacService) getSPFACADPath(p *Path) string {
	if p.Value == math.MaxFloat64 {
		minPath := cad.GetCADObject(cad.GetCADString("起点", p.StartNode),
			cad.GetCADString("终点", p.EndNode),
			cad.GetCADSpaceString("路径", getCADNoPath(p.StartNode, p.EndNode, p.Path)),
			cad.GetCADString("长度", "0"))
		return minPath
	} else {
		minPath := cad.GetCADObject(cad.GetCADString("起点", p.StartNode),
			cad.GetCADString("终点", p.EndNode),
			cad.GetCADSpaceString("路径", getCADPath(p.Path)),
			cad.GetCADString("长度", cast.ToString(p.Value)),
		)
		return minPath
	}
}

func getCADPath(path string) string {
	var build strings.Builder
	nodes := strings.Split(path, "->")
	for index, v := range nodes {
		build.WriteString(`"`)
		build.WriteString(v)
		if index == len(nodes)-1 {
			build.WriteString(`"`)
		} else {
			build.WriteString(`" `)
		}

	}
	return build.String()
}

func getCADNoPath(startNode, endNode, path string) string {
	var build strings.Builder
	build.WriteString(` "`)
	build.WriteString(startNode)
	build.WriteString(`"`)

	build.WriteString(` "`)
	build.WriteString(path)
	build.WriteString(`"`)

	build.WriteString(` "`)
	build.WriteString(endNode)
	build.WriteString(`"`)

	return build.String()
}

你可能感兴趣的:(golang,golang,算法,最短路径)