graph in go

package main

import (

type Graph map[string]map[string]bool

func (g Graph) addEdge(node1, node2 string) {
    if g[node1] == nil {
        g[node1] = make(map[string]bool)
    g[node1][node2] = true

    if g[node2] == nil {
        g[node2] = make(map[string]bool)
    g[node2][node1] = true

func (g Graph) removeEdge(node1, node2 string) {
    delete(g[node1], node2)
    delete(g[node2], node1)

func (g Graph) hasEdge(node1, node2 string) bool {
    return g[node1][node2]

func main() {
    graph := make(Graph)
    graph.addEdge("A", "B")
    graph.addEdge("B", "C")
    graph.addEdge("C", "D")
    graph.addEdge("D", "A")

    fmt.Println("Graph:", graph)
    fmt.Println("Does edge A-B exist?", graph.hasEdge("A", "B"))

    graph.removeEdge("A", "B")
    fmt.Println("After removing edge A-B, Graph:", graph)
    fmt.Println("Does edge A-B exist now?", graph.hasEdge("A", "B"))

This Go code defines a Graph type using a map of strings to maps of strings to booleans to represent an undirected graph. Methods to add edges between nodes (addEdge), remove edges (removeEdge), and check if an edge exists between nodes (hasEdge) are implemented.

In main, a graph is created and edges "A-B," "B-C," "C-D," and "D-A" are added. It demonstrates adding, removing, and checking for edges within the graph.