Skip to main content
Code Review

Return to Question

Commonmark migration
Source Link

I implemented DFS using recursion in Go. When I dfs the graph, I am able to get the path of traversal. But I'm unsure what else could I add to this DFS in order to make the search better.

Also, Is there any benefit from using a stack instead of traditional recursion? I know that recursion uses stack as underlying data structure. I'm also setting the capacity of list for every vertex.

#dfs.go

dfs.go

package main
import (
 "fmt"
)
type Vertex struct {
 visited bool
 value string
 neighbours []*Vertex
}
func (v *Vertex) connect(vertex *Vertex) {
 v.neighbours = append(v.neighbours, vertex)
}
type Graph struct{}
func (g *Graph) dfs(vertex *Vertex) {
 if vertex.visited != true {
 vertex.visited = true
 fmt.Println(vertex)
 if len(vertex.neighbours) != 0 {
 for _, v := range vertex.neighbours {
 g.dfs(v)
 }
 } else {
 return
 }
 }
}
func (g *Graph) disconnected(vertices ...*Vertex){
 for _, v := range vertices{
 g.dfs(v)
 }
}
func main() {
 v1 := Vertex{false, "A", make([]*Vertex, 0, 5)}
 v2 := Vertex{false, "B", make([]*Vertex, 0, 5)}
 v3 := Vertex{false, "C", make([]*Vertex, 0, 5)}
 v4 := Vertex{false, "D", make([]*Vertex, 0, 5)}
 v5 := Vertex{false, "E", make([]*Vertex, 0, 5)}
 g := Graph{}
 v1.connect(&v2)
 v2.connect(&v4)
 v2.connect(&v5)
 v3.connect(&v4)
 v3.connect(&v5)
 g.dfs(&v1)
}

I implemented DFS using recursion in Go. When I dfs the graph, I am able to get the path of traversal. But I'm unsure what else could I add to this DFS in order to make the search better.

Also, Is there any benefit from using a stack instead of traditional recursion? I know that recursion uses stack as underlying data structure. I'm also setting the capacity of list for every vertex.

#dfs.go

package main
import (
 "fmt"
)
type Vertex struct {
 visited bool
 value string
 neighbours []*Vertex
}
func (v *Vertex) connect(vertex *Vertex) {
 v.neighbours = append(v.neighbours, vertex)
}
type Graph struct{}
func (g *Graph) dfs(vertex *Vertex) {
 if vertex.visited != true {
 vertex.visited = true
 fmt.Println(vertex)
 if len(vertex.neighbours) != 0 {
 for _, v := range vertex.neighbours {
 g.dfs(v)
 }
 } else {
 return
 }
 }
}
func (g *Graph) disconnected(vertices ...*Vertex){
 for _, v := range vertices{
 g.dfs(v)
 }
}
func main() {
 v1 := Vertex{false, "A", make([]*Vertex, 0, 5)}
 v2 := Vertex{false, "B", make([]*Vertex, 0, 5)}
 v3 := Vertex{false, "C", make([]*Vertex, 0, 5)}
 v4 := Vertex{false, "D", make([]*Vertex, 0, 5)}
 v5 := Vertex{false, "E", make([]*Vertex, 0, 5)}
 g := Graph{}
 v1.connect(&v2)
 v2.connect(&v4)
 v2.connect(&v5)
 v3.connect(&v4)
 v3.connect(&v5)
 g.dfs(&v1)
}

I implemented DFS using recursion in Go. When I dfs the graph, I am able to get the path of traversal. But I'm unsure what else could I add to this DFS in order to make the search better.

Also, Is there any benefit from using a stack instead of traditional recursion? I know that recursion uses stack as underlying data structure. I'm also setting the capacity of list for every vertex.

dfs.go

package main
import (
 "fmt"
)
type Vertex struct {
 visited bool
 value string
 neighbours []*Vertex
}
func (v *Vertex) connect(vertex *Vertex) {
 v.neighbours = append(v.neighbours, vertex)
}
type Graph struct{}
func (g *Graph) dfs(vertex *Vertex) {
 if vertex.visited != true {
 vertex.visited = true
 fmt.Println(vertex)
 if len(vertex.neighbours) != 0 {
 for _, v := range vertex.neighbours {
 g.dfs(v)
 }
 } else {
 return
 }
 }
}
func (g *Graph) disconnected(vertices ...*Vertex){
 for _, v := range vertices{
 g.dfs(v)
 }
}
func main() {
 v1 := Vertex{false, "A", make([]*Vertex, 0, 5)}
 v2 := Vertex{false, "B", make([]*Vertex, 0, 5)}
 v3 := Vertex{false, "C", make([]*Vertex, 0, 5)}
 v4 := Vertex{false, "D", make([]*Vertex, 0, 5)}
 v5 := Vertex{false, "E", make([]*Vertex, 0, 5)}
 g := Graph{}
 v1.connect(&v2)
 v2.connect(&v4)
 v2.connect(&v5)
 v3.connect(&v4)
 v3.connect(&v5)
 g.dfs(&v1)
}
deleted 7 characters in body; edited tags
Source Link
200_success
  • 145.6k
  • 22
  • 190
  • 479

I tried implementingimplemented DFS using recursion in Go. When I dfs the graph, I am able to get the path of traversal. But I'm unsure what else could I add to this DFS in order to make the search better.

Also, Is there any benefit from using a stack instead of traditional recursion? I know that recursion uses stack as underlying data structure. I'm also setting the capacity of list for every vertex.

#dfs.go

package main
import (
 "fmt"
)
type Vertex struct {
 visited bool
 value string
 neighbours []*Vertex
}
func (v *Vertex) connect(vertex *Vertex) {
 v.neighbours = append(v.neighbours, vertex)
}
type Graph struct{}
func (g *Graph) dfs(vertex *Vertex) {
 if vertex.visited != true {
 vertex.visited = true
 fmt.Println(vertex)
 if len(vertex.neighbours) != 0 {
 for _, v := range vertex.neighbours {
 g.dfs(v)
 }
 } else {
 return
 }
 }
}
func (g *Graph) disconnected(vertices ...*Vertex){
 for _, v := range vertices{
 g.dfs(v)
 }
}
func main() {
 v1 := Vertex{false, "A", make([]*Vertex, 0, 5)}
 v2 := Vertex{false, "B", make([]*Vertex, 0, 5)}
 v3 := Vertex{false, "C", make([]*Vertex, 0, 5)}
 v4 := Vertex{false, "D", make([]*Vertex, 0, 5)}
 v5 := Vertex{false, "E", make([]*Vertex, 0, 5)}
 g := Graph{}
 v1.connect(&v2)
 v2.connect(&v4)
 v2.connect(&v5)
 v3.connect(&v4)
 v3.connect(&v5)
 g.dfs(&v1)
}

I tried implementing DFS using recursion in Go. When I dfs the graph, I am able to get the path of traversal. But I'm unsure what else could I add to this DFS in order to make the search better.

Also, Is there any benefit from using a stack instead of traditional recursion? I know that recursion uses stack as underlying data structure. I'm also setting the capacity of list for every vertex.

#dfs.go

package main
import (
 "fmt"
)
type Vertex struct {
 visited bool
 value string
 neighbours []*Vertex
}
func (v *Vertex) connect(vertex *Vertex) {
 v.neighbours = append(v.neighbours, vertex)
}
type Graph struct{}
func (g *Graph) dfs(vertex *Vertex) {
 if vertex.visited != true {
 vertex.visited = true
 fmt.Println(vertex)
 if len(vertex.neighbours) != 0 {
 for _, v := range vertex.neighbours {
 g.dfs(v)
 }
 } else {
 return
 }
 }
}
func (g *Graph) disconnected(vertices ...*Vertex){
 for _, v := range vertices{
 g.dfs(v)
 }
}
func main() {
 v1 := Vertex{false, "A", make([]*Vertex, 0, 5)}
 v2 := Vertex{false, "B", make([]*Vertex, 0, 5)}
 v3 := Vertex{false, "C", make([]*Vertex, 0, 5)}
 v4 := Vertex{false, "D", make([]*Vertex, 0, 5)}
 v5 := Vertex{false, "E", make([]*Vertex, 0, 5)}
 g := Graph{}
 v1.connect(&v2)
 v2.connect(&v4)
 v2.connect(&v5)
 v3.connect(&v4)
 v3.connect(&v5)
 g.dfs(&v1)
}

I implemented DFS using recursion in Go. When I dfs the graph, I am able to get the path of traversal. But I'm unsure what else could I add to this DFS in order to make the search better.

Also, Is there any benefit from using a stack instead of traditional recursion? I know that recursion uses stack as underlying data structure. I'm also setting the capacity of list for every vertex.

#dfs.go

package main
import (
 "fmt"
)
type Vertex struct {
 visited bool
 value string
 neighbours []*Vertex
}
func (v *Vertex) connect(vertex *Vertex) {
 v.neighbours = append(v.neighbours, vertex)
}
type Graph struct{}
func (g *Graph) dfs(vertex *Vertex) {
 if vertex.visited != true {
 vertex.visited = true
 fmt.Println(vertex)
 if len(vertex.neighbours) != 0 {
 for _, v := range vertex.neighbours {
 g.dfs(v)
 }
 } else {
 return
 }
 }
}
func (g *Graph) disconnected(vertices ...*Vertex){
 for _, v := range vertices{
 g.dfs(v)
 }
}
func main() {
 v1 := Vertex{false, "A", make([]*Vertex, 0, 5)}
 v2 := Vertex{false, "B", make([]*Vertex, 0, 5)}
 v3 := Vertex{false, "C", make([]*Vertex, 0, 5)}
 v4 := Vertex{false, "D", make([]*Vertex, 0, 5)}
 v5 := Vertex{false, "E", make([]*Vertex, 0, 5)}
 g := Graph{}
 v1.connect(&v2)
 v2.connect(&v4)
 v2.connect(&v5)
 v3.connect(&v4)
 v3.connect(&v5)
 g.dfs(&v1)
}
Bumped by Community user
Tweeted twitter.com/StackCodeReview/status/960110125364244480
deleted 22 characters in body
Source Link
SKSV
  • 175
  • 7

I tried implementing DFS using recursion in Go. When I dfs the graph, I am able to get the path of traversal. But I'm unsure what else could I add to this DFS in order to make the search better.

Also, Is there any benefit from using a stack instead of traditional recursion? I know that recursion uses stack as underlying data structure. I'm also setting the capacity of list for every vertex.

#dfs.go

package main
import (
 "fmt"
)
type Vertex struct {
 visited bool
 value string
 neighbours []*Vertex
}
func (v *Vertex) connect(vertex *Vertex) {
 v.neighbours = append(v.neighbours, vertex)
}
type Graph struct{}
func (g *Graph) dfs(vertex *Vertex) {
 if vertex.visited != true {
 vertex.visited = true
 fmt.Println(vertex)
 if len(vertex.neighbours) != 0 {
 for _, v := range vertex.neighbours {
 g.dfs(v)
 }
 } else {
 return
 }
 }
}
func (g *Graph) disconnected(vertices ...*Vertex){
 for _, v := range vertices{
 g.dfs(v)
 }
}
func main() {
 v1 := Vertex{false, "A", make([]*Vertex, 0, 5)}
 v2 := Vertex{false, "B", make([]*Vertex, 0, 5)}
 v3 := Vertex{false, "C", make([]*Vertex, 0, 5)}
 v4 := Vertex{false, "D", make([]*Vertex, 0, 5)}
 v5 := Vertex{false, "E", make([]*Vertex, 0, 5)}
 g := Graph{}
 v1.connect(&v2)
 v1.connect(&v3)
 v2.connect(&v4)
 v2.connect(&v5)
 v3.connect(&v4)
 v3.connect(&v5)
 g.dfs(&v1)
}

I tried implementing DFS using recursion in Go. When I dfs the graph, I am able to get the path of traversal. But I'm unsure what else could I add to this DFS in order to make the search better.

Also, Is there any benefit from using a stack instead of traditional recursion? I know that recursion uses stack as underlying data structure. I'm also setting the capacity of list for every vertex.

#dfs.go

package main
import (
 "fmt"
)
type Vertex struct {
 visited bool
 value string
 neighbours []*Vertex
}
func (v *Vertex) connect(vertex *Vertex) {
 v.neighbours = append(v.neighbours, vertex)
}
type Graph struct{}
func (g *Graph) dfs(vertex *Vertex) {
 if vertex.visited != true {
 vertex.visited = true
 fmt.Println(vertex)
 if len(vertex.neighbours) != 0 {
 for _, v := range vertex.neighbours {
 g.dfs(v)
 }
 } else {
 return
 }
 }
}
func (g *Graph) disconnected(vertices ...*Vertex){
 for _, v := range vertices{
 g.dfs(v)
 }
}
func main() {
 v1 := Vertex{false, "A", make([]*Vertex, 0, 5)}
 v2 := Vertex{false, "B", make([]*Vertex, 0, 5)}
 v3 := Vertex{false, "C", make([]*Vertex, 0, 5)}
 v4 := Vertex{false, "D", make([]*Vertex, 0, 5)}
 v5 := Vertex{false, "E", make([]*Vertex, 0, 5)}
 g := Graph{}
 v1.connect(&v2)
 v1.connect(&v3)
 v2.connect(&v4)
 v2.connect(&v5)
 v3.connect(&v4)
 v3.connect(&v5)
 g.dfs(&v1)
}

I tried implementing DFS using recursion in Go. When I dfs the graph, I am able to get the path of traversal. But I'm unsure what else could I add to this DFS in order to make the search better.

Also, Is there any benefit from using a stack instead of traditional recursion? I know that recursion uses stack as underlying data structure. I'm also setting the capacity of list for every vertex.

#dfs.go

package main
import (
 "fmt"
)
type Vertex struct {
 visited bool
 value string
 neighbours []*Vertex
}
func (v *Vertex) connect(vertex *Vertex) {
 v.neighbours = append(v.neighbours, vertex)
}
type Graph struct{}
func (g *Graph) dfs(vertex *Vertex) {
 if vertex.visited != true {
 vertex.visited = true
 fmt.Println(vertex)
 if len(vertex.neighbours) != 0 {
 for _, v := range vertex.neighbours {
 g.dfs(v)
 }
 } else {
 return
 }
 }
}
func (g *Graph) disconnected(vertices ...*Vertex){
 for _, v := range vertices{
 g.dfs(v)
 }
}
func main() {
 v1 := Vertex{false, "A", make([]*Vertex, 0, 5)}
 v2 := Vertex{false, "B", make([]*Vertex, 0, 5)}
 v3 := Vertex{false, "C", make([]*Vertex, 0, 5)}
 v4 := Vertex{false, "D", make([]*Vertex, 0, 5)}
 v5 := Vertex{false, "E", make([]*Vertex, 0, 5)}
 g := Graph{}
 v1.connect(&v2)
 v2.connect(&v4)
 v2.connect(&v5)
 v3.connect(&v4)
 v3.connect(&v5)
 g.dfs(&v1)
}
edited title
Link
Heslacher
  • 50.9k
  • 5
  • 83
  • 177
Loading
added disconnected method
Source Link
SKSV
  • 175
  • 7
Loading
added 4 characters in body
Source Link
Loading
deleted 58 characters in body
Source Link
SKSV
  • 175
  • 7
Loading
Source Link
SKSV
  • 175
  • 7
Loading
lang-golang

AltStyle によって変換されたページ (->オリジナル) /