Golang program to check a graph is bipartite using DFS


The Depth-First Search (DFS) Algorithm is a classic Algorithm used to traverse and search graphs. In this article we are going to learn how to develop golang program where we can check if a graph is bipartie or not using DFS . Here we will use two different approaches to implement the result.

Syntax

func len(v Type) int

The len() function is used to get the length of a any parameter. It takes one parameter as the data type variable whose length we wish to find and returns the integer value which is the length of the variable.

Algorithm

  • First, we need to import the fmt package. then create a isBipare() function. Inside the function initialize a Boolean array called visited having size n to keep track of visited vertices, and an integer array called colors of size n to store the colors of vertices.

  • Next create a DFS function to traverse the graph. This function takes two arguments i.e the current node and the color of that node.

  • In this function traverse all neighbors of the current node. If a neighbor is not visited, call the DFS function recursively with the neighbor as the current node and the opposite color (1-color) as the input color.

  • If the recursive call returns false, then the graph is not bipartite, so return false.

  • Traverse all unvisited vertices in the graph. For each unvisited vertex, call the DFS function with that vertex as the current node and color 0 or 1.

  • Now, start the main() function. Inside this function initialize vertices of a graph and print them on the screen.

  • Further call the isBipartite() function by passing the graph as argument to the function and store the result in a variable.

  • Print the result obtained in the variable on the screen by using fmt.Println().

Example 1

In this Example we will write a go language program to check whether a graph is bipartite or not by using 2-colouring method. In 2 coloring method we assign two colors to the vertices of the graph, and then we traverse the graph using DFS and color each vertex with the opposite color of its parent vertex.

package main

import "fmt"

func isBipartite(graph [][]int) bool {
   n := len(graph)
   colors := make([]int, n)
   visited := make([]bool, n)

   var dfs func(int, int) bool
   dfs = func(node int, color int) bool {
      visited[node] = true
      colors[node] = color

      for _, neighbor := range graph[node] {
         if !visited[neighbor] {
            if !dfs(neighbor, 1-color) {
               return false
            }
         } else if colors[neighbor] == colors[node] {
            return false
         }
      }
      return true
   }
   for i := 0; i < n; i++ {
      if !visited[i] {
         if !dfs(i, 0) {
            return false
         }
      }
   }
   return true
}

func main() {
   graph1 := [][]int{{1, 3}, {0, 2}, {1, 3}, {0, 2}}
   fmt.Println("The given graph vertices are:", graph1)
   var result bool = isBipartite(graph1)
   if result {
      fmt.Println("The given graph is bipartite")
   } else {
      fmt.Println("The given graph is not bipartite")
   }
   fmt.Println()
   graph2 := [][]int{{1, 2, 3}, {0, 2}, {1, 3}, {0, 2}}
   fmt.Println("The given graph vertices are:", graph2)
   result = isBipartite(graph2)
   if result {
      fmt.Println("The given graph is bipartite")
   } else {
      fmt.Println("The given graph is not bipartite")
   }
}

Output

The given graph vertices are: [[1 3] [0 2] [1 3] [0 2]]
The given graph is bipartite

The given graph vertices are: [[1 2 3] [0 2] [1 3] [0 2]]
The given graph is not bipartite

Example 2

In this method, instead of using two colors, we assign a boolean value to each vertex to indicate whether it belongs to one part of the bipartite graph or the other.

package main

import "fmt"

func isBipartite(graph [][]int) bool {
   n := len(graph)

   colors := make([]int, n)
   for i := range colors {
      colors[i] = -1
   }

   for i := 0; i < n; i++ {
      if colors[i] == -1 {
         if !colorGraph(graph, colors, i, 0) {
            return false
         }
      }
   }

   return true
}

func colorGraph(graph [][]int, colors []int, curr int, color int) bool {
   colors[curr] = color

   for _, neighbor := range graph[curr] {
      if colors[neighbor] == -1 {
         if !colorGraph(graph, colors, neighbor, 1-color) {
            return false
         }
      } else if colors[neighbor] == color {
         return false
      }
   }
   return true
}

func main() {
   graph1 := [][]int{{1, 3}, {0, 2}, {1, 3}, {0, 2}}
   fmt.Println("The given graph vertices are:", graph1)
   var result bool = isBipartite(graph1)
   if result {
      fmt.Println("The given graph is bipartite")
   } else {
      fmt.Println("The given graph is not bipartite")
   }
   fmt.Println()
   graph2 := [][]int{{1, 2, 3}, {0, 2}, {1, 3}, {0, 2}}
   fmt.Println("The given graph vertices are:", graph2)
   result = isBipartite(graph2)
   if result {
      fmt.Println("The given graph is bipartite")
   } else {
      fmt.Println("The given graph is not bipartite")
   }
}

Output

The given graph vertices are: [[1 3] [0 2] [1 3] [0 2]]
The given graph is bipartite

The given graph vertices are: [[1 2 3] [0 2] [1 3] [0 2]]
The given graph is not bipartite

Conclusion

We have successfully compiled and executed a go language program to check if a graph is bipartite or not along with Examples. Here we have used two Examples. In the first Example we are using 2-coloring method while in the second program we are using the boolean coloring approach to implement the result.

Updated on: 05-Apr-2023

121 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements