Skip to content

Commit 0c3403a

Browse files
committed
func Adj() []int to Adj() fund.Iterator
1 parent 2dafec4 commit 0c3403a

6 files changed

+92
-61
lines changed

graphs/graph/breadth_first_paths.go

+11-10
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
package graph
22

33
import (
4-
"github.com/youngzhu/algs4-go/graphs"
54
"github.com/youngzhu/algs4-go/fund"
5+
"github.com/youngzhu/algs4-go/graphs"
66
)
77

88
// Breadth-first search.
99
// 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
1111
// 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
1313
// the vertices that we can reach by following one edge, then we check for v among
1414
// all the vertices that we can reach from s by following two edges, and so forth.
1515

@@ -20,14 +20,14 @@ import (
2020
// 2. Put onto the queue all unmarked vertices that are adjacent to v and mark them
2121

2222
type BreadthFirstPaths struct {
23-
graph Graph
24-
source int // source vertex
23+
graph Graph
24+
source int // source vertex
2525
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
2828
}
2929

30-
// Computes the shortest path between the source vertex (s)
30+
// Computes the shortest path between the source vertex (s)
3131
// and every other vertex in graph g
3232
func NewBreadthFirstPaths(g Graph, s int) BreadthFirstPaths {
3333
g.validateVertex(s)
@@ -57,7 +57,8 @@ func (p BreadthFirstPaths) bfs() {
5757
for !queue.IsEmpty() {
5858
v := queue.Dequeue().(int)
5959

60-
for _, w := range p.graph.Adj(v) {
60+
for _, ww := range p.graph.Adj(v) {
61+
w := ww.(int)
6162
if !p.marked[w] {
6263
p.marked[w] = true
6364
p.edgeTo[w] = v
@@ -103,4 +104,4 @@ func (p BreadthFirstPaths) PathTo(v int) []int {
103104
}
104105

105106
return path
106-
}
107+
}

graphs/graph/connected_components.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ package graph
22

33
// Another client of DFS, find the connected components of a graph.
44
type ConnectedComponents struct {
5-
graph Graph
5+
graph Graph
66
marked []bool // makred[v]: has vertex v been marked?
7-
id []int // id[v]: id of connected component containing v
8-
size []int // size[id]: number of vertices in given component
9-
count int // number of connected components
7+
id []int // id[v]: id of connected component containing v
8+
size []int // size[id]: number of vertices in given component
9+
count int // number of connected components
1010
}
1111

1212
func NewConnectedComponents(g Graph) ConnectedComponents {
@@ -30,7 +30,8 @@ func (cc ConnectedComponents) dfs(v int) {
3030
cc.marked[v] = true
3131
cc.id[v] = cc.count
3232
cc.size[cc.count]++
33-
for _, w := range cc.graph.Adj(v) {
33+
for _, ww := range cc.graph.Adj(v) {
34+
w := ww.(int)
3435
if !cc.marked[w] {
3536
cc.dfs(w)
3637
}

graphs/graph/depth_first_paths.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@ package graph
22

33
import "github.com/youngzhu/algs4-go/fund"
44

5-
// We remember the edge v-w that takes us to each vertex w for the first time
5+
// We remember the edge v-w that takes us to each vertex w for the first time
66
// by setting edge[w] to v. In other words, v-w is the last edge on the known
77
// path from s to w. The result of the search is a tree rooted at the source;
88
// edgeTo[] is a parent-link representation of that tree.
99

1010
type DepthFirstPaths struct {
11-
graph Graph
12-
source int // source vertex
11+
graph Graph
12+
source int // source vertex
1313
marked []bool // marked[v]: is there an s-v path?
14-
edgeTo []int // edgeTo[v]: last edge on s-v path
14+
edgeTo []int // edgeTo[v]: last edge on s-v path
1515
}
1616

1717
// Computes a path between the source vertex (s) and every other vertex in graph g
@@ -31,7 +31,8 @@ func NewDepthFirstPaths(g Graph, s int) DepthFirstPaths {
3131
func (p DepthFirstPaths) dfs(g Graph, v int) {
3232
p.marked[v] = true
3333

34-
for _, w := range g.Adj(v) {
34+
for _, ww := range g.Adj(v) {
35+
w := ww.(int)
3536
if !p.marked[w] {
3637
p.edgeTo[w] = v
3738
p.dfs(g, w)
@@ -67,4 +68,4 @@ func (p DepthFirstPaths) PathTo(v int) []int {
6768
}
6869

6970
return path
70-
}
71+
}

graphs/graph/depth_first_search.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ package graph
99

1010
type DepthFirstSearch struct {
1111
marked []bool // marked[v]: is there an s-v path?
12-
count int // number of vertices connected to s
12+
count int // number of vertices connected to s
1313
}
1414

1515
// Computes the vertices in graph (g) that are connected to
@@ -29,7 +29,8 @@ func (d DepthFirstSearch) dfs(g Graph, s int) {
2929
d.count++
3030
d.marked[s] = true
3131

32-
for _, w := range g.Adj(s) {
32+
for _, ww := range g.Adj(s) {
33+
w := ww.(int)
3334
if !d.marked[w] {
3435
d.dfs(g, w)
3536
}
@@ -44,4 +45,4 @@ func (d DepthFirstSearch) Marked(v int) bool {
4445
// Returns the number of vertices connected to the source vertex (s)
4546
func (d DepthFirstSearch) Count() int {
4647
return d.count
47-
}
48+
}

graphs/graph/example_test.go

+15-15
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ package graph_test
33
import (
44
"fmt"
55

6+
"github.com/youngzhu/algs4-go/fund"
67
"github.com/youngzhu/algs4-go/graphs/graph"
78
"github.com/youngzhu/algs4-go/util"
8-
"github.com/youngzhu/algs4-go/fund"
99
)
1010

1111
var (
1212
tinyGraph *graph.Graph
13-
tinyCG *graph.Graph
13+
tinyCG *graph.Graph
1414
)
1515

1616
func dataInit() {
@@ -95,7 +95,7 @@ func ExampleDepthFirstSearch() {
9595
// NOT connected
9696
// 9 10 11 12
9797
// NOT connected
98-
98+
9999
}
100100

101101
func dfs(g graph.Graph, s int) {
@@ -117,7 +117,7 @@ func dfs(g graph.Graph, s int) {
117117
}
118118

119119
func ExampleDepthFirstPaths() {
120-
120+
121121
if tinyCG == nil {
122122
dataInit()
123123
}
@@ -142,14 +142,14 @@ func ExampleDepthFirstPaths() {
142142
}
143143
}
144144

145-
// Output:
145+
// Output:
146146
// 0 to 0: 0
147147
// 0 to 1: 0-2-1
148148
// 0 to 2: 0-2
149149
// 0 to 3: 0-2-3
150150
// 0 to 4: 0-2-3-4
151151
// 0 to 5: 0-2-3-5
152-
152+
153153
}
154154

155155
func ExampleBreadthFirstPaths() {
@@ -177,14 +177,14 @@ func ExampleBreadthFirstPaths() {
177177
}
178178
}
179179

180-
// Output:
180+
// Output:
181181
// 0 to 0 (0): 0
182182
// 0 to 1 (1): 0-1
183183
// 0 to 2 (1): 0-2
184184
// 0 to 3 (2): 0-2-3
185185
// 0 to 4 (2): 0-2-4
186186
// 0 to 5 (1): 0-5
187-
187+
188188
}
189189

190190
func ExampleConnectedComponents() {
@@ -216,7 +216,7 @@ func ExampleConnectedComponents() {
216216
fmt.Printf("\n")
217217
}
218218

219-
// Output:
219+
// Output:
220220
// 3 components
221221
// 0 1 2 3 4 5 6
222222
// 7 8
@@ -232,11 +232,11 @@ func ExampleSymbolGraph_routes() {
232232

233233
// Output:
234234
// JFK
235-
// ORD
236-
// ATL
237-
// MCO
235+
// ORD
236+
// ATL
237+
// MCO
238238
// KFC
239-
// input not contain 'KFC'
239+
// input not contain 'KFC'
240240
}
241241

242242
func ExampleSymbolGraph_movies() {
@@ -254,9 +254,9 @@ func symbolGraph(sg graph.SymbolGraph, input string) {
254254
s := sg.Index(input)
255255
g := sg.Graph()
256256
for _, v := range g.Adj(s) {
257-
fmt.Printf(" %v\n", sg.Name(v))
257+
fmt.Printf(" %v\n", sg.Name(v.(int)))
258258
}
259259
} else {
260260
fmt.Printf(" input not contain '%v'\n", input)
261261
}
262-
}
262+
}

0 commit comments

Comments
 (0)