graph.go 638 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package main
  2. type (
  3. AdjacencyMatrix map[int]map[int]int
  4. Graph struct {
  5. X map[int]Vector
  6. E []map[int]Vector
  7. L map[int]int
  8. A AdjacencyMatrix
  9. }
  10. )
  11. func (A AdjacencyMatrix) Modify(u, v, w int) {
  12. if A[u] == nil {
  13. A[u] = make(map[int]int)
  14. }
  15. A[u][v] = w
  16. }
  17. func MakeGraph() Graph {
  18. G := Graph{
  19. make(map[int]Vector),
  20. make([]map[int]Vector, 3),
  21. make(map[int]int),
  22. make(AdjacencyMatrix),
  23. }
  24. for i := 0; i < 3; i++ {
  25. G.E[i] = make(map[int]Vector)
  26. }
  27. return G
  28. }
  29. func (G Graph) AddNode(u int, V Vector, l int) {
  30. G.X[u] = V
  31. G.L[u] = l
  32. }
  33. func (G Graph) AddEdge(u, v int) {
  34. G.A.Modify(u, v, 1)
  35. G.A.Modify(v, u, -1)
  36. }