# Golang program to implement graph data structure

In Go programming language, a graph is a type of data structure made up of a limited number of nodes (also known as vertices) and a set of connecting edges. Relationships between several entities can be depicted on a graph. It can be represented by employing different data structures, such as an adjacency matrix or an adjacency list. The particular use case and the needs of the application will determine the data structure to be used. A graph can also be implemented in Go by using a library or package like go-graph. We will use two methods here to implement graph data structure.

## Method 1: Using Adjacency Matrix

The graph is displayed in this implementation as an AdMatrix field in the Graph struct. The value N, which stands for the number of nodes in the graph, determines the size of the matrix. The matrix's true values are used to represent the graph's edges.

### Algorithm

• Step 1 − Create a package main and declare fmt(format package) package in the program where main produces executable codes and fmt helps in formatting input and output.

• Step 2 − To store the graph as a 2D matrix, define a Graph struct with an AdMatrix field.

• Step 3 − In the next step, to represent the graph, create an instance of the Graph struct.

• Step 4 − By setting the value of a cell in the AdMatrix to true, you can connect graph nodes. This is an edge in the graph between two nodes.

• Step 5 − For each edge in the graph, perform step 3 again.

• Step 6 − By iterating through the AdMatrix and printing the values, create the graph.

• Step 7 − Using an adjacency matrix, this technique creates a basic representation of a network. It can be expanded to include more features like adding and deleting nodes or edges, determining the shortest path between two nodes, and more.

### Example

In the following example, we are going to use Adjacency matrix to implement graph data structure in the Go programming language

package main
import (
"fmt"
)

const n = 4

// Graph represents a graph using an adjacency matrix
type Graph struct {
}

func main() {
// Create graph
graph := Graph{}

// Connect nodes

// Print graph
fmt.Println("The graph is printed as follows using adjacency matrix:")
for i := 0; i < n; i++ {
for j := 0; j < n; j++ {
}
fmt.Println()
}
}


### Output

The graph is printed as follows using adjacency matrix:
false true true false
false false true false
false false false false
false false false false


## Method 2: Using Node Struct

In this example we will use node struct to implement Graph data structure. The outcome will be a graph printed on the console. Let’s see through the code and algorithm to understand the concept.

### Syntax

func append(slice, element_1, element_2…, element_N) []T


The append function is used to add values to an array slice. It takes number of arguments. The first argument is the array to which we wish to add the values followed by the values to add. The function then returns the final slice of array containing all the values.

### Algorithm

• Step 1 − Create a package main and declare fmt(format package) package in the program where main produces executable codes and fmt helps in formatting input and output.

• Step 2 − To represent a node in the graph, create a struct called Node that has two fields: a value field to hold the node's value, and an edges field to hold a slice of pointers to nodes that are nearby.

• Step 3 − To add a new edge to the node, create the method Add_Edge for the Node struct.

• Step 4 − In the next step, to represent the nodes in the graph, create instances of the Node struct.

• Step 5 − To add edges between the nodes, use the Add_Edge function.

• Step 6 − By iterating through the nodes and printing the values of the nodes they are related to; you can display the graph.

• Step 7 − Each node in the implementation stores a list of its neighbors in an adjacency list representation of the graph. The Add_Edge method adds a new node to the current node's edges slice, forming a directed edge between the two nodes.

• Step 8 − The slice of node pointers is transformed into a slice of node values for display using the getNodeValues method.

### Example

In this example we will use node struct to execute the program.

package main
import (
"fmt"
)

// Node represents a node in the graph.
type Node struct {
value int
edges []*Node
}

func (n *Node) Add_Edge(node *Node) {
n.edges = append(n.edges, node)
}

func main() {
// Create nodes.
n1 := &Node{value: 10}
n2 := &Node{value: 20}
n3 := &Node{value: 30}
n4 := &Node{value: 40}
n5 := &Node{value: 50}

// Display the graph.
fmt.Println("The Graph is represented as:")
fmt.Printf("Node %d -> %v\n", n1.value, getNodeValues(n1.edges))
fmt.Printf("Node %d -> %v\n", n2.value, getNodeValues(n2.edges))
fmt.Printf("Node %d -> %v\n", n3.value, getNodeValues(n3.edges))
fmt.Printf("Node %d -> %v\n", n4.value, getNodeValues(n4.edges))
fmt.Printf("Node %d -> %v\n", n5.value, getNodeValues(n5.edges))
}

// returns a slice of node values.
func getNodeValues(nodes []*Node) []int {
var values []int
for _, node := range nodes {
values = append(values, node.value)
}
return values
}


### Output

The Graph is represented as:
Node 10 -> [20 30]
Node 20 -> [40 50]
Node 30 -> 
Node 40 -> []
Node 50 -> []


## Conclusion

We executed the program of implementing the graph data structure using two examples. In the first example we used adjacency matrix to implement graph data structure and in the second example we used node struct.