File tree Expand file tree Collapse file tree 1 file changed +50
-0
lines changed Expand file tree Collapse file tree 1 file changed +50
-0
lines changed Original file line number Diff line number Diff line change 11package main
22
33import (
4+ "container/list"
45 "fmt"
56)
67
@@ -23,6 +24,11 @@ func main() {
2324 }
2425 initGG (& gg )
2526 fmt .Println (gg .vexs )
27+ fBFS (& gg )
28+ fDFS (& gg )
29+
30+ //listgg := list.New()
31+
2632 PrintG (gg , len (vexs ))
2733}
2834
@@ -56,6 +62,50 @@ func initGG(gg *Graph) {
5662
5763 gg .matrix [4 ][1 ] = 4
5864 gg .matrix [4 ][3 ] = 1
65+
66+ gg .edgnum = 12 / 2
67+ }
68+
69+ //深度遍历
70+ func DFS (gg * Graph , visit * []bool , i int ) {
71+
72+ fmt .Println (gg .vexs [i ])
73+ for j := 0 ; j < gg .vexnum ; j ++ {
74+ if gg.matrix [i ][j ] != MAX_VALUE && ! (* visit )[j ] {
75+ (* visit )[j ] = true
76+ DFS (gg , visit , j )
77+ }
78+ }
79+ }
80+
81+ func fDFS (gg * Graph ) {
82+ visit := make ([]bool , 10 , 10 )
83+ fmt .Println (visit )
84+ visit [0 ] = true
85+ DFS (gg , & visit , 0 )
86+ }
87+
88+ //广度遍历
89+ func fBFS (gg * Graph ) {
90+ listq := list .New ()
91+ visit := make ([]bool , 10 , 10 )
92+
93+ //first push
94+ visit [0 ] = true
95+ listq .PushBack (0 )
96+
97+ for listq .Len () > 0 {
98+ index := listq .Front ()
99+
100+ fmt .Println (gg .vexs [index .Value .(int )])
101+ for i := 0 ; i < gg .vexnum ; i ++ {
102+ if ! visit [i ] && gg .matrix [index .Value .(int )][i ] != MAX_VALUE {
103+ visit [i ] = true
104+ listq .PushBack (i )
105+ }
106+ }
107+ listq .Remove (index )
108+ }
59109}
60110
61111func prim () {
You can’t perform that action at this time.
0 commit comments