Advertisement
Guest User

tretyaya

a guest
May 24th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 9.31 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5. )
  6.  
  7. type Vertex struct {
  8.     i int
  9.     parent *Vertex
  10.     depth int
  11. }
  12.  
  13. /*func DFS(used []int, mass [][]int, ind int, begin int, m int, n int, k *int, help []bool) {
  14.     for i:=0; i<n; i++ {
  15.         used[i] = -1
  16.     }
  17.     /*var eq = false
  18.     for i:=0; i<m; i++ {
  19.         if mass[begin][i] != 0 {
  20.             eq = true
  21.             break
  22.         }
  23.     }
  24.     if !eq {
  25.         begin = 0
  26.     }
  27.     if !help[begin] {
  28.         begin = 0
  29.     }
  30.     VisitVertex(used, mass, ind, begin, m, n, k)
  31.     //}
  32.     //}
  33. }
  34.  
  35. func VisitVertex(used []int, mass [][]int, ind int, begin int, m int, n int, k *int) {
  36.     used[begin] = *k
  37.     //fmt.Println(" i = ", ind)
  38.     (*k)++
  39.     for i:=0; i<m; i++ {
  40.         if used[mass[begin][i]] == -1 {
  41.             //ind++
  42.             VisitVertex(used, mass, ind, mass[begin][i], m, n, k)
  43.         }
  44.     }
  45.  
  46. } */
  47. func DFS(mass [][]int, begin int, m int, n int, Num [][]int, NumNew [][]int, Str [][]string, StrNew[][]string) ([][]int, [][]string, int){
  48.     used := make([]bool, n)
  49.     stack := newStack(n)
  50.     sizeStack := 0
  51.     arr := make([] int, n)
  52.     count := 0
  53.     stack[sizeStack] = begin
  54.     sizeStack++
  55.     for sizeStack > 0 {
  56.         sizeStack--
  57.         v := stack[sizeStack]
  58.         if !used[v] {
  59.             used[v] = true
  60.             fmt.Println("arr[",v,"] = ",used[v])
  61.             arr[v] = count
  62.             fmt.Println("cc arr[",v,"] = ",arr[v])
  63.             count++
  64.         }
  65.         for i := m - 1; i > -1; i-- {
  66.             u := mass[v][i]
  67.             if !used[u] {
  68.                 stack[sizeStack] = u
  69.                 sizeStack++
  70.             }
  71.         }
  72.     }
  73.     for i := 0; i < n; i++ {
  74.         fmt.Println("used[",i,"] = ",arr[i])
  75.     }
  76.     fmt.Println("co = ", count)
  77.     NumNew, StrNew = Change(arr, used,  Num, NumNew, count, m, Str, StrNew, n)
  78.     return NumNew, StrNew, count
  79. }
  80.  
  81. func newStack(n int) []int  {
  82.     stack := make([]int, n*n)
  83.     return stack
  84. }
  85.  
  86. func Search(used []int, f int) (int) {
  87.     for i := range used {
  88.         if used[i] == f {
  89.             return i
  90.         }
  91.     }
  92.     return 0
  93. }
  94.  
  95. func Search1(Qn []int, qn int) (bool) {
  96.     var eq bool
  97.     if Qn[qn] != -1 {
  98.         eq = true
  99.     }
  100.     /*for i:= range Qn {
  101.         if Qn[i] == qn {
  102.             eq = true
  103.         }
  104.     }*/
  105.     return eq
  106. }
  107.  
  108. func Change(used []int, b []bool, mass [][]int, NumNew [][]int, k int, m int, Str [][]string, StrNew [][]string, n int) ([][]int, [][]string) {
  109.     fmt.Println("!k = ",k)
  110.  
  111.     /*for i := 0; i < k; i++ {
  112.         for j := 0; j < m; j++ {
  113.             fmt.Print(mass[i][j], " ")
  114.         }
  115.         fmt.Println()
  116.     }
  117.  
  118.     for i := 0; i < n; i++ {
  119.         if b[i] {
  120.             for j := 0; j < m; j++ {
  121.                 if b[i] {
  122.                     NumNew[used[i]][j] = used[mass[i][j]]
  123.                 }
  124.             }
  125.         }
  126.     }
  127.     fmt.Println("aaaaaaaaaa")
  128.     for i := 0; i < k; i++ {
  129.         for j := 0; j < m; j++ {
  130.             fmt.Print(NumNew[i][j], " ")
  131.         }
  132.         fmt.Println()
  133.     }
  134.     fmt.Println("aaaaaaaaaa")
  135.     for i := 0; i < n; i++ {
  136.         for j := 0; j < m; j++ {
  137.                 if Str[Search(used, i)][j] != "" {
  138.                     StrNew[i][j] = Str[Search(used, i)][j]
  139.                     fmt.Print(Str[Search(used, i)][j], " ")
  140.                     //fmt.Print(StrNew[i][j], " ")
  141.                 }
  142.             }
  143.         fmt.Println()
  144.     }*/
  145.     for i := 0; i < n; i++ {
  146.         if b[i] {
  147.             StrNew[used[i]] = Str[i]
  148.             for j := 0; j < m; j++ {
  149.                 NumNew[used[i]][j] = used[mass[i][j]]
  150.             }
  151.         }
  152.     }
  153.     fmt.Println("aaaaaaaaaa")
  154.     for i := 0; i < k; i++ {
  155.         for j := 0; j < m; j++ {
  156.             fmt.Print(NumNew[i][j], " ")
  157.         }
  158.         fmt.Println()
  159.     }
  160.     fmt.Println("aaaaaaaaaa")
  161.     return NumNew, StrNew
  162. }
  163.  
  164. func Find(x *Vertex) (*Vertex) {
  165.     var root *Vertex
  166.     if (x.parent == x) {
  167.         root = x
  168.         return root
  169.     } else {
  170.         x.parent = Find(x.parent)
  171.         root = x.parent
  172.         return root
  173.     }
  174. }
  175.  
  176. func Union(x *Vertex, y *Vertex) {
  177.     var rootX = Find(x)
  178.     var rootY = Find(y)
  179.     if rootX.depth < rootY.depth {
  180.         rootX.parent = rootY
  181.     } else {
  182.         rootY.parent = rootX
  183.         if rootX.depth == rootY.depth && rootX != rootY {
  184.             rootX.depth = rootX.depth + 1
  185.         }
  186.     }
  187. }
  188.  
  189. func Split(n int, m int, delta [][]int, fi [][]string, pi []int) (int, []int){
  190.     var mn= n
  191.     var eq bool
  192.     Q := make([]Vertex, n)
  193.                                     //по сути как со сплитом1, ток к+1 эквивалентность, вроде как на шаг вглубь
  194.     for q:=0; q < n; q++ {
  195.         Q[q].i = q
  196.         Q[q].parent = &Q[q]
  197.         Q[q].depth = 0
  198.     }
  199.     for q1:=0; q1<n; q1++ {
  200.         for q2:=0; q2<n; q2++ {
  201.             if pi[Q[q1].i] == pi[Q[q2].i] && Find(&Q[q1]) != Find(&Q[q2]) {
  202.                 eq = true
  203.                 for x:=0; x<m; x++ {
  204.                     var w1 = delta[Q[q1].i][x]
  205.                     var w2 = delta[Q[q2].i][x]
  206.                     if pi[w1] != pi[w2] {
  207.                         eq = false
  208.                         break
  209.                     }
  210.                 }
  211.                 if eq {
  212.                     Union(&Q[q1], &Q[q2])
  213.                     mn--
  214.                 }
  215.             }
  216.         }
  217.     }
  218.     for q:=0; q<n; q++ {
  219.         pi[Q[q].i] = Find(&Q[q]).i
  220.         fmt.Println("p!i = ",pi[Q[q].i], " ")
  221.     }
  222.     return mn, pi
  223. }
  224.  
  225. func Split1(n int, m int, NumNew [][]int, StrNew [][]string) (int, []int) {
  226.     var eq bool
  227.     var mn = n           //общее кол-во классов
  228.     pi := make([]int, n) //каждому состоянию ставится в соответствие корень класса, кот оно принадлежит
  229.     Q := make([]Vertex, n)
  230.  
  231.     for q:=0; q < n; q++ {
  232.         Q[q].i = q
  233.         Q[q].parent = &Q[q]
  234.         Q[q].depth = 0
  235.     }
  236.  
  237.     for q1:=0; q1<n; q1++ {
  238.         for q2:=0; q2<n; q2++ {
  239.             if Find(&Q[q1]) != Find(&Q[q2]) { //если переходы по любому входгному сигналу дают одинаковый
  240.                 eq = true                       //сигнал, то надо их объединить
  241.                 for x:=0; x<m; x++ {
  242.                     if StrNew[q1][x] != StrNew[q2][x] {
  243.                         eq = false
  244.                         break
  245.                     }
  246.                 }
  247.                 if eq {
  248.                     Union(&Q[q1], &Q[q2])
  249.                     mn--
  250.                 }
  251.             }
  252.         }
  253.     }
  254.     for q:=0; q<n; q++ {
  255.         pi[Q[q].i] = Find(&Q[q]).i
  256.         fmt.Println("pi = ",pi[Q[q].i], " ")
  257.     }
  258.     fmt.Println("mn = ", mn)
  259.  
  260.     return mn, pi
  261. }
  262.  
  263. func AufenkampHohn(n int, m int, NumNew [][]int, StrNew [][]string) ([][]int, [][]string, []int){
  264.     m_res, pi := Split1(n, m, NumNew, StrNew)
  265.     var m_res1 int
  266.     for {
  267.         m_res1, pi = Split(n, m, NumNew, StrNew, pi) //смотрим глубину эквивалентности
  268.         if m_res == m_res1 { //не может быть больше, чем состояний автомата
  269.             break
  270.         }
  271.         m_res = m_res1
  272.     }
  273.     fmt.Println("pi:")
  274.     for i := 0; i < n; i++ {
  275.         fmt.Print(pi[i], " ")
  276.     }
  277.     fmt.Println()
  278.  
  279.     Qn := make([]int, n)
  280.  
  281.     delta := make([][]int, n) //цифры
  282.     fi := make([][]string, n) //буквы
  283.     for i:=0; i<n; i++ {
  284.         delta[i] = make([]int, m)
  285.         fi[i] = make([]string, m)
  286.     }
  287.     c := 0
  288.     //eq := make([]bool, n)
  289.     for i :=0; i < n ;i++ {
  290.         Qn[i] = -1
  291.     }
  292.     help := make([]bool, n)
  293.     for q:=0; q<n; q++ {
  294.         var qn = pi[q]
  295.         if !Search1(Qn, qn) { //принадлежит ли qn массиву Qn
  296.         //if !eq[qn]{
  297.         // eq[qn] = true
  298.         c++
  299.             Qn[q] = q
  300.             for x:=0; x<m; x++ {
  301.                 help[qn] = true
  302.                 delta[qn][x] = pi[NumNew[q][x]]
  303.                 fmt.Print(delta[qn][x], " ")
  304.                 fi[qn][x] = StrNew[q][x]
  305.             }
  306.             fmt.Println()
  307.         }
  308.     }
  309.     fmt.Println("c = " , c)
  310.     fmt.Println("here")
  311.     for i := 0; i < n; i++ {
  312.         for j := 0; j < m; j++ {
  313.             fmt.Print(delta[i][j], " ")
  314.         }
  315.         fmt.Println()
  316.     }
  317.  
  318.     for i := 0; i < n; i++ {
  319.         for j := 0; j < m; j++ {
  320.             fmt.Print(fi[i][j], " ")
  321.         }
  322.         fmt.Println()
  323.     }
  324.     fmt.Println("here")
  325.     return delta, fi, pi
  326. }
  327.  
  328. func main() {
  329.     var n, m, q, k int
  330.  
  331.     fmt.Scan(&n)
  332.     fmt.Scan(&m)
  333.     fmt.Scan(&q)
  334.     //n = 3; m = 2; q = 1
  335.  
  336.     Num := make([][]int, n)
  337.     Str := make([][]string, n)
  338.     NumNew := make([][]int, n)
  339.     StrNew := make([][]string, n)
  340.     //used := make([]int, n)
  341.     help := make([]int, n)
  342.     for i:=0; i<n; i++ {
  343.         Num[i] = make([]int, m)
  344.         NumNew[i] = make([]int, m)
  345.         Str[i] = make([]string, m)
  346.         StrNew[i] = make([]string, m)
  347.     }
  348.     for i:=0; i<n; i++ {
  349.         for j:=0; j<m; j++ {
  350.             fmt.Scan(&Num[i][j])
  351.         }
  352.     }
  353.     for i:=0; i<n; i++ {
  354.         for j:=0; j<m; j++ {
  355.             fmt.Scan(&Str[i][j])
  356.         }
  357.     }
  358.  
  359.     /*Num[0][0] = 1; Num[0][1] = 0; Num[1][0] = 2; Num[1][1] = 0; Num[2][0] = 2; Num[2][1] = 2
  360.     Str[0][0] = "x"; Str[0][1] = "y"; Str[1][0] = "y"; Str[1][1] = "x"; Str[2][0] = "x"; Str[2][1] = "y"*/
  361.  
  362.     /*for i:=0; i<n; i++ {
  363.         for j:=0; j<m; j++ {
  364.             fmt.Print("Num[",i,"][",j,"] = ", Num[i][j]," ")
  365.         }
  366.         fmt.Println()
  367.     }*/
  368.     Num, Str, help = AufenkampHohn(n, m, Num, Str)
  369.  
  370.     fmt.Println("n = ", n, " m = ", m, " len = ", len(Num))
  371.  
  372.     for i:=0; i<n; i++ {
  373.         for j:=0; j<m; j++ {
  374.             fmt.Print("Num[",i,"][",j,"] = ", Num[i][j]," ") //до канонизации, но уже убрано ненужное
  375.         }
  376.         fmt.Println()
  377.     }
  378. //func DFS(mass [][]int, begin int, m int, n int, Num [][]int, NumNew [][]int, Str [][]string, StrNew[][]string) {
  379.     NumNew, StrNew, k = DFS(Num, help[q], m, n, Num, NumNew, Str, StrNew)
  380.     //q = 0
  381.     //fmt.Print(k, "\n", m, "\n", q, "\n")
  382.  
  383.     /*for i := 0; i < n; i++ {
  384.         fmt.Println("used[",i,"] = ",used[i])
  385.     }*/
  386.  
  387.     //Change(used, Num, NumNew, k, m, Str, StrNew, n) //тут закончена какнонизация
  388.     /*for i := 0; i < n; i++ {
  389.         fmt.Println("used[",i,"] = ",used[i])
  390.     }*/
  391.     //fmt.Println("n = ", n, " m = ", m, " len = ", len(Num))
  392.     n = k
  393.     //все, связанное с выводом
  394.     alphabet := make([]byte, 27)
  395.     q = 0
  396.     for i:=0; i<27; i++ {
  397.         alphabet[i] = (byte) (i+97)
  398.     }
  399.     fmt.Println("digraph {")
  400.     fmt.Println("    rankdir = LR")
  401.     fmt.Println("    dummy [label = \"\" , shape = none]")
  402.     for i:=0; i<n; i++ {
  403.         fmt.Println("    ",i," [shape = circle]")
  404.     }
  405.     fmt.Println("    dummy -> ", q)
  406.     for i:=0; i<n; i++ {
  407.         for j:=0; j<m; j++ {
  408.             //fmt.Println("    ",q," -> ",goMatrix_delta[i][j]," [label = \"",array[j],"(",exitMatrix_fi[i][j],")\"]")
  409.             fmt.Printf("     %d -> %d [label = \"%c(%s)\"]",i,NumNew[i][j],alphabet[j], StrNew[i][j])
  410.             fmt.Println()
  411.         }
  412.         q++
  413.     }
  414.     fmt.Println("}")
  415. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement