Skip to content

Commit

Permalink
test bfs shortest path
Browse files Browse the repository at this point in the history
  • Loading branch information
arnauddri committed Jan 14, 2015
1 parent 5352dd8 commit 67ebf06
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
11 changes: 4 additions & 7 deletions bfs_shortest_path.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package graph

import (
"fmt"
)
import ()

func bfsShortestPath(g *Graph, start VertexId) {
func bfsShortestPath(g *Graph, start VertexId) (dist map[VertexId]int) {
queue := []VertexId{start}
visited := make(map[VertexId]bool)
dist := make(map[VertexId]int)
dist = make(map[VertexId]int)
var next []VertexId

for len(queue) > 0 {
Expand All @@ -27,6 +25,5 @@ func bfsShortestPath(g *Graph, start VertexId) {
}
queue = next
}
fmt.Println(g)
fmt.Println(dist)
return
}
22 changes: 21 additions & 1 deletion graph_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,30 @@ func TestBfs(t *testing.T) {
h.AddEdge(VertexId(i), VertexId(i+1))
}

bfsShortestPath(h, VertexId(1))
bfs(h, VertexId(2))
}

func TestBfsShortestPath(t *testing.T) {
h := NewGraph()

for i := 0; i < 10; i++ {
v := VertexId(i)
h.AddVertex(v)
}

for i := 0; i < 9; i++ {
h.AddEdge(VertexId(i), VertexId(i+1))
}

distance := bfsShortestPath(h, VertexId(0))

for i := 0; i < len(distance); i++ {
if distance[VertexId(i)] != i {
t.Error()
}
}
}

func TestDfs(t *testing.T) {
h := NewGraph()

Expand Down

0 comments on commit 67ebf06

Please sign in to comment.