Last active
September 27, 2019 08:46
-
-
Save alissonbrunosa/c0593712ff737c3496758b2d13e7a14c to your computer and use it in GitHub Desktop.
Implementation of BFS algorithm in Go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package bfs | |
import "fmt" | |
type City struct { | |
Name string | |
Population uint32 | |
neighbors []Node | |
} | |
func (c *City) Neighbors() []Node { | |
return c.neighbors | |
} | |
func (c *City) AddNeighbor(neighbor Node) { | |
c.neighbors = append(c.neighbors, neighbor) | |
} | |
func (c *City) String() string { | |
return fmt.Sprintf("Name: %s, Population: %d", c.Name, c.Population) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package bfs | |
type Node interface { | |
Neighbors() []Node | |
AddNeighbor(neighbor Node) | |
} | |
type Graph struct { | |
root Node | |
} | |
func (g *Graph) Root(root Node) { | |
g.root = root | |
} | |
type queue struct { | |
items []Node | |
} | |
func (q *queue) push(node Node) { | |
q.items = append(q.items, node) | |
} | |
func (q *queue) shift() Node { | |
node := q.items[0] | |
q.items = q.items[1:] | |
return node | |
} | |
func (q queue) isEmpty() bool { | |
return len(q.items) == 0 | |
} | |
// Unweighted graph | |
// Using BFS to get the shortest path | |
func (g *Graph) BFS(dest Node) int { | |
var q queue | |
q.push(g.root) | |
distances := make(map[Node]int) | |
distances[g.root] = 0 | |
for !q.isEmpty() { | |
n := q.shift() | |
for _, neighbor := range n.Neighbors() { | |
if _, ok := distances[neighbor]; !ok { | |
distances[neighbor] = distances[n] + 1 | |
if neighbor == dest { | |
break | |
} | |
q.push(neighbor) | |
} | |
} | |
} | |
return distances[dest] | |
} | |
func Init() *Graph { | |
// Create cities | |
frankfurt := City{Name: "Frankfurt", Population: 736414} | |
mannheim := City{Name: "Mannheim", Population: 305780} | |
wurzburg := City{Name: "Würzburg", Population: 124873} | |
karlsruhe := City{Name: "Karlsruhe", Population: 307755} | |
kassel := City{Name: "Kassel", Population: 197984} | |
nurnberg := City{Name: "Nürnberg", Population: 509975} | |
erfurt := City{Name: "Erfurt", Population: 210118} | |
munchen := City{Name: "München", Population: 1450000} | |
augsburg := City{Name: "Augsburg", Population: 286374} | |
stuttgart := City{Name: "Stuttgart", Population: 628032} | |
// Add neighbors for Frankfurt | |
frankfurt.AddNeighbor(mannheim) | |
frankfurt.AddNeighbor(wurzburg) | |
frankfurt.AddNeighbor(kassel) | |
// Add neighbors for mannheim | |
mannheim.AddNeighbor(karlsruhe) | |
mannheim.AddNeighbor(frankfurt) | |
// Add neighbors for Würzburg | |
wurzburg.AddNeighbor(nurnberg) | |
wurzburg.AddNeighbor(erfurt) | |
wurzburg.AddNeighbor(frankfurt) | |
// Add neighbors for Kassel | |
kassel.AddNeighbor(munchen) | |
kassel.AddNeighbor(frankfurt) | |
// Add neighbors for Karlsruhe | |
karlsruhe.AddNeighbor(mannheim) | |
karlsruhe.AddNeighbor(augsburg) | |
// Add neighbors for Nürnberg | |
nurnberg.AddNeighbor(wurzburg) | |
nurnberg.AddNeighbor(stuttgart) | |
nurnberg.AddNeighbor(munchen) | |
// Add neighbors for Erfurt | |
erfurt.AddNeighbor(wurzburg) | |
// Add neighbors for München | |
munchen.AddNeighbor(kassel) | |
munchen.AddNeighbor(augsburg) | |
munchen.AddNeighbor(nurnberg) | |
// Add neighbors for Augsburg | |
augsburg.AddNeighbor(karlsruhe) | |
augsburg.AddNeighbor(munchen) | |
// Add neighbors for Stuttgart | |
stuttgart.AddNeighbor(nurnberg) | |
graph := &Graph{} | |
// Set Root | |
graph.Root(frankfurt) | |
return graph | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment