1
1
package graph
2
2
3
3
import (
4
- "github.com/youngzhu/algs4-go/graphs"
5
4
"github.com/youngzhu/algs4-go/fund"
5
+ "github.com/youngzhu/algs4-go/graphs"
6
6
)
7
7
8
8
// Breadth-first search.
9
9
// Depth-first search finds some path from a source vertex s to a target vertex v.
10
- // We are often interested in finding the shortest such path (one with a minimal
10
+ // We are often interested in finding the shortest such path (one with a minimal
11
11
// number of edges). Breadth-first search is a classic method based on this goal.
12
- // To find a shortest path from s to v, we start at s and check for v among all
12
+ // To find a shortest path from s to v, we start at s and check for v among all
13
13
// the vertices that we can reach by following one edge, then we check for v among
14
14
// all the vertices that we can reach from s by following two edges, and so forth.
15
15
@@ -20,14 +20,14 @@ import (
20
20
// 2. Put onto the queue all unmarked vertices that are adjacent to v and mark them
21
21
22
22
type BreadthFirstPaths struct {
23
- graph Graph
24
- source int // source vertex
23
+ graph Graph
24
+ source int // source vertex
25
25
marked []bool // marked[v]: is there an s-v path?
26
- edgeTo []int // edgeTo[v]: previous edge on shortest s-v path
27
- distTo []int // distTo[v]: number of edges on shortest s-v path
26
+ edgeTo []int // edgeTo[v]: previous edge on shortest s-v path
27
+ distTo []int // distTo[v]: number of edges on shortest s-v path
28
28
}
29
29
30
- // Computes the shortest path between the source vertex (s)
30
+ // Computes the shortest path between the source vertex (s)
31
31
// and every other vertex in graph g
32
32
func NewBreadthFirstPaths (g Graph , s int ) BreadthFirstPaths {
33
33
g .validateVertex (s )
@@ -57,7 +57,8 @@ func (p BreadthFirstPaths) bfs() {
57
57
for ! queue .IsEmpty () {
58
58
v := queue .Dequeue ().(int )
59
59
60
- for _ , w := range p .graph .Adj (v ) {
60
+ for _ , ww := range p .graph .Adj (v ) {
61
+ w := ww .(int )
61
62
if ! p .marked [w ] {
62
63
p .marked [w ] = true
63
64
p .edgeTo [w ] = v
@@ -103,4 +104,4 @@ func (p BreadthFirstPaths) PathTo(v int) []int {
103
104
}
104
105
105
106
return path
106
- }
107
+ }
0 commit comments