diff options
author | elioat <elioat@tilde.institute> | 2024-08-10 09:43:04 -0400 |
---|---|---|
committer | elioat <elioat@tilde.institute> | 2024-08-10 09:43:04 -0400 |
commit | 391793456f87ccba1a8e1775e1695ced9594ae33 (patch) | |
tree | 1489212616096116e2c16b60e3de4387ff10025b | |
parent | f67aecd5015ffd45701981bf5c2c95836f47efe7 (diff) | |
download | tour-391793456f87ccba1a8e1775e1695ced9594ae33.tar.gz |
*
-rw-r--r-- | go/graph.go | 43 | ||||
-rw-r--r-- | go/mermaid-graph.go | 62 |
2 files changed, 105 insertions, 0 deletions
diff --git a/go/graph.go b/go/graph.go new file mode 100644 index 0000000..6c306c5 --- /dev/null +++ b/go/graph.go @@ -0,0 +1,43 @@ +package main + +import ( + "fmt" + "math/rand" + "time" +) + +type Graph struct { + adjacencyList map[int][]int +} + +func NewGraph() *Graph { + return &Graph{adjacencyList: make(map[int][]int)} +} + +func (g *Graph) addEdge(node1, node2 int) { + g.adjacencyList[node1] = append(g.adjacencyList[node1], node2) + g.adjacencyList[node2] = append(g.adjacencyList[node2], node1) +} + +func generateConnectedGraph(n int) *Graph { + rand.Seed(time.Now().UnixNano()) + graph := NewGraph() + + // Start with node 0 + for i := 1; i < n; i++ { + // Connect the new node i to a random existing node + existingNode := rand.Intn(i) + graph.addEdge(i, existingNode) + } + + return graph +} + +func main() { + graph := generateConnectedGraph(100) + + // Print the adjacency list to show the graph + for node, edges := range graph.adjacencyList { + fmt.Printf("Node %d: %v\n", node, edges) + } +} diff --git a/go/mermaid-graph.go b/go/mermaid-graph.go new file mode 100644 index 0000000..d40bf9e --- /dev/null +++ b/go/mermaid-graph.go @@ -0,0 +1,62 @@ +package main + +import ( + "fmt" + "math/rand" + "strings" + "time" +) + +type Graph struct { + adjacencyList map[int][]int +} + +func NewGraph() *Graph { + return &Graph{adjacencyList: make(map[int][]int)} +} + +func (g *Graph) addEdge(node1, node2 int) { + g.adjacencyList[node1] = append(g.adjacencyList[node1], node2) + g.adjacencyList[node2] = append(g.adjacencyList[node2], node1) +} + +func generateConnectedGraph(n int) *Graph { + rand.Seed(time.Now().UnixNano()) + graph := NewGraph() + + // Start with node 0 + for i := 1; i < n; i++ { + // Connect the new node i to a random existing node + existingNode := rand.Intn(i) + graph.addEdge(i, existingNode) + } + + return graph +} + +func generateMermaid(graph *Graph) string { + var sb strings.Builder + + sb.WriteString("graph TD;\n") + + visited := make(map[string]bool) + + for node, edges := range graph.adjacencyList { + for _, edge := range edges { + // Ensure each edge is only printed once + if !visited[fmt.Sprintf("%d-%d", node, edge)] && !visited[fmt.Sprintf("%d-%d", edge, node)] { + sb.WriteString(fmt.Sprintf(" %d --> %d;\n", node, edge)) + visited[fmt.Sprintf("%d-%d", node, edge)] = true + } + } + } + + return sb.String() +} + +func main() { + graph := generateConnectedGraph(100) + mermaid := generateMermaid(graph) + + fmt.Println(mermaid) +} |