- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Golang program to find all paths in a graph
In this article we are going to learn how to use DFS Algorithm and breath-first searh method in golang to find all the paths in the a graph. In a graph, the nodes are depicted by the entities while the edges depict the relation between those entities. Finding all paths in a graph is a common task in graph theory and can be useful in a variety of applications.
Algorithm
Step 1 − First, we need to import the fmt package.
Step 2 − Then create different structs and functions and define properties to them.
Step 3 − Now, create the main() function. Inside the main() initialize the nodes and assign value to them.
Step 4 − Further, create edges from these nodes by passing them as arguments in the graph struct.
Step 5 − Now, call the AllPaths() function by passing the required nodes as arguments to the function and store the result obtained in a variable.
Step 6 − Print the result on the screen by using fmt.Println() function.
Example 1
In this Example we will write a go language program to find all the paths in a graph by using the depth-first search. The Depth-First Search (DFS) Algorithm is a classic Algorithm used to traverse and search graphs.
package main import "fmt" type Node struct { name string } type Edge struct { source *Node dest *Node } type Graph struct { nodes []*Node edges []*Edge } func (g *Graph) AddNode(n *Node) { g.nodes = append(g.nodes, n) } func (g *Graph) AddEdge(s *Node, d *Node) { e := &Edge{source: s, dest: d} g.edges = append(g.edges, e) } func (g *Graph) DFS(s *Node, d *Node, visited map[*Node]bool, path []string, paths *[][]string) { visited[s] = true path = append(path, s.name) if s == d { *paths = append(*paths, path) } else { for _, e := range g.edges { if e.source == s && !visited[e.dest] { g.DFS(e.dest, d, visited, path, paths) } } } delete(visited, s) path = path[:len(path)-1] } func (g *Graph) AllPaths(s *Node, d *Node) [][]string { visited := make(map[*Node]bool) paths := [][]string{} path := []string{} g.DFS(s, d, visited, path, &paths) return paths } func main() { a := &Node{name: "A"} b := &Node{name: "B"} c := &Node{name: "C"} d := &Node{name: "D"} g := &Graph{} g.AddNode(a) g.AddNode(b) g.AddNode(c) g.AddNode(d) g.AddEdge(a, b) g.AddEdge(b, c) g.AddEdge(a, c) g.AddEdge(c, d) paths := g.AllPaths(a, d) fmt.Println("The required paths in a graph are:", paths) }
Output
The required paths in a graph are: [[A B C D] [A C D]]
Example 2
In this Example we will write a go language program to find all the paths in a graph by using the breadth first search method.
package main import "fmt" type Node struct { name string } type Edge struct { source *Node dest *Node } type Graph struct { nodes []*Node edges []*Edge } func (g *Graph) AddNode(n *Node) { g.nodes = append(g.nodes, n) } func (g *Graph) AddEdge(s *Node, d *Node) { e := &Edge{source: s, dest: d} g.edges = append(g.edges, e) } func (g *Graph) BFS(s *Node, d *Node) [][]string { visited := make(map[*Node]bool) queue := [][]*Node{{s}} paths := [][]string{} for len(queue) > 0 { path := queue[0] queue = queue[1:] node := path[len(path)-1] if node == d { var pathStr []string for _, n := range path { pathStr = append(pathStr, n.name) } paths = append(paths, pathStr) } for _, e := range g.edges { if e.source == node && !visited[e.dest] { newPath := append(path, e.dest) queue = append(queue, newPath) visited[e.dest] = true } } } return paths } func main() { a := &Node{name: "A"} b := &Node{name: "B"} c := &Node{name: "C"} d := &Node{name: "D"} g := &Graph{} g.AddNode(a) g.AddNode(b) g.AddNode(c) g.AddNode(d) g.AddEdge(a, b) g.AddEdge(b, c) g.AddEdge(a, c) g.AddEdge(c, d) paths := g.BFS(a, d) fmt.Println("The required paths are:", paths) }
Output
The required paths are: [[A C D]]
Conclusion
We have successfully compiled and executed a go language program to find all paths in a graph along with Examples. this is used in applications like network routing, social network analysis, and recommendation systems. We have shown two methods viz Breadth-First Search and Depth-First Search to implement this result.