github.com/qiuhoude/go-web@v0.0.0-20220223060959-ab545e78f20d/algorithm/datastructures/graph/dijkstra_test.go (about)

     1  package graph
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  )
     7  
     8  func buildDiGraph() *Digraph {
     9  	g := NewGraph()
    10  	g.AddVertex(1, "莫斯科")
    11  	g.AddVertex(2, "华盛顿")
    12  	g.AddVertex(3, "伦敦")
    13  	g.AddVertex(4, "圣彼得堡")
    14  	g.AddVertex(5, "哥本哈根")
    15  	g.AddVertex(6, "里斯本")
    16  	g.AddVertex(7, "新奥尔良")
    17  	g.AddVertex(8, "阿姆斯特丹")
    18  	g.AddVertex(9, "布鲁塞尔")
    19  	g.AddVertex(10, "巴黎")
    20  	// 添加边
    21  	g.AddDuplexEdge(1, 4, 1)
    22  	g.AddDuplexEdge(1, 5, 1)
    23  	g.AddDuplexEdge(2, 7, 1)
    24  	g.AddDuplexEdge(3, 8, 1)
    25  	g.AddDuplexEdge(2, 6, 1)
    26  	g.AddDuplexEdge(3, 9, 1)
    27  	g.AddDuplexEdge(4, 10, 1)
    28  	g.AddDuplexEdge(5, 10, 1)
    29  	g.AddDuplexEdge(6, 10, 1)
    30  	g.AddDuplexEdge(7, 10, 1)
    31  	g.AddDuplexEdge(8, 10, 1)
    32  	g.AddDuplexEdge(9, 10, 1)
    33  	return g
    34  }
    35  
    36  func TestGraphAdjTab_DijkstraSearch(t *testing.T) {
    37  	g := buildDiGraph()
    38  	path := g.DijkstraSearch(2, 3)
    39  	for _, v := range path {
    40  		fmt.Printf("(%v)%v->", v, g.GetVertexData(v))
    41  	}
    42  	fmt.Println()
    43  }