55 lines
1.1 KiB
Go
55 lines
1.1 KiB
Go
|
|
package diagram
|
||
|
|
|
||
|
|
import (
|
||
|
|
"fmt"
|
||
|
|
)
|
||
|
|
|
||
|
|
// Graph represents a topological structure using an adjacency list
|
||
|
|
type Graph struct {
|
||
|
|
vertices map[string][]string
|
||
|
|
}
|
||
|
|
|
||
|
|
// NewGraph creates a new graph
|
||
|
|
// TODO 修改结构为map[uuid][]orm.Topologic
|
||
|
|
func NewGraph() *Graph {
|
||
|
|
return &Graph{
|
||
|
|
vertices: make(map[string][]string),
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// AddVertex adds a vertex to the graph
|
||
|
|
func (g *Graph) AddVertex(vertex string) {
|
||
|
|
if _, exists := g.vertices[vertex]; !exists {
|
||
|
|
g.vertices[vertex] = []string{}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// AddEdge adds an edge between two vertices
|
||
|
|
func (g *Graph) AddEdge(from, to string) {
|
||
|
|
g.AddVertex(from)
|
||
|
|
g.AddVertex(to)
|
||
|
|
g.vertices[from] = append(g.vertices[from], to)
|
||
|
|
}
|
||
|
|
|
||
|
|
// PrintGraph prints the graph in adjacency list format
|
||
|
|
func (g *Graph) PrintGraph() {
|
||
|
|
for vertex, edges := range g.vertices {
|
||
|
|
fmt.Printf("%s -> %v\n", vertex, edges)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func main() {
|
||
|
|
graph := NewGraph()
|
||
|
|
|
||
|
|
// Add vertices and edges
|
||
|
|
graph.AddEdge("A", "B")
|
||
|
|
graph.AddEdge("A", "C")
|
||
|
|
graph.AddEdge("B", "D")
|
||
|
|
graph.AddEdge("C", "D")
|
||
|
|
graph.AddEdge("C", "E")
|
||
|
|
graph.AddEdge("E", "F")
|
||
|
|
|
||
|
|
// Print the graph
|
||
|
|
graph.PrintGraph()
|
||
|
|
}
|