Advertisement
Guest User

Untitled

a guest
May 20th, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 4.34 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5. )
  6.  
  7. type Graph struct {
  8.     states       int
  9.     InputAlph    string
  10.     mark         string
  11.     newStates    int
  12.     newInputAlph string
  13.     parent string
  14.     depth int
  15. }
  16.  
  17. type Vert struct {
  18.     s    int
  19.     mark string
  20. }
  21.  
  22. func main() {
  23.  
  24.     var n, m, q int
  25.     //ind := 0
  26.     fmt.Scan(&n)
  27.     fmt.Scan(&m)
  28.     fmt.Scan(&q)
  29.  
  30.     arr1 := make([][] Graph, n, n)
  31.  
  32.     for i := 0; i < n; i++ {
  33.         arr1[i] = make([] Graph, m)
  34.     }
  35.  
  36.     for i := 0; i < n; i++ {
  37.         for j := 0; j < m; j++ {
  38.             fmt.Scan(&arr1[i][j].states)
  39.             //arr1[i][j].mark = "white"
  40.         }
  41.     }
  42.  
  43.     for i := 0; i < n; i++ {
  44.         for j := 0; j < m; j++ {
  45.             fmt.Scan(&arr1[i][j].InputAlph)
  46.         }
  47.     }
  48.     AufenkampHohn(arr1, n, m, q)
  49.     //DFS(arr1, n, m, q)
  50. }
  51.  
  52.  
  53. //2
  54. func print(arr1[][] Graph, n, m, q int) {
  55.  
  56.     fmt.Printf("digraph {\n")
  57.     fmt.Printf("    rankdir = LR\n")
  58.     fmt.Printf("    dummy [label = \"\", shape = none]\n")
  59.     for i := 0; i < n; i++ {
  60.         fmt.Printf("    %d ", i)
  61.         fmt.Printf("[shape = circle]\n")
  62.     }
  63.     fmt.Printf("    dummy -> 0\n")
  64.     //fmt.Printf("%d\n", q)
  65.     for i := 0; i < n; i++ {
  66.         for j := 0; j < m; j++ {
  67.             fmt.Printf("    %d -> %d [label = \"%c(%s)\"]\n", i, arr1[i][j].newStates, 97+j, arr1[i][j].newInputAlph)
  68.         }
  69.     }
  70.     fmt.Printf("}")
  71. }
  72. //
  73. func NewMatrix(arr1[][] Graph, arr[] Vert, newN, n, m, q int)  {
  74.     for i := 0; i < n; i++ {
  75.         for j := 0; j < m; j++ {
  76.             arr1[arr[i].s][j].newStates = arr[arr1[i][j].states].s
  77.             arr1[arr[i].s][j].newInputAlph = arr1[i][j].InputAlph
  78.         }
  79.     }
  80.     print(arr1, newN, m, q)
  81.  
  82. }
  83.  
  84. func DFS(arr1[][] Graph, n, m, q int) {
  85.     stack := newStack(n)
  86.     sizeStack := 0
  87.     arr := make([] Vert, n)
  88.     count := 0
  89.     stack[sizeStack] = q
  90.     sizeStack++
  91.     for sizeStack > 0 {
  92.         sizeStack--
  93.         v := stack[sizeStack]
  94.         if arr[v].mark != "black" {
  95.             arr[v].mark = "black"
  96.             arr[v].s = count
  97.             count++
  98.         }
  99.         for i := m - 1; i > -1; i-- {
  100.             u := arr1[v][i].states
  101.             if arr[u].mark != "black" {
  102.                 stack[sizeStack] = u
  103.                 sizeStack++
  104.             }
  105.         }
  106.     }
  107.     NewMatrix(arr1, arr, count, n, m, q)
  108. }
  109.  
  110. func newStack(n int) []int  {
  111.     stack := make([]int, n*n)
  112.     return stack
  113. }
  114.  
  115. type State struct {
  116.     order  int
  117.     depth  int
  118.     parent *State
  119. }
  120.  
  121. func newState(order int) *State {
  122.     s := &State{order: order, depth: 0}
  123.     s.parent = s
  124.     return s
  125. }
  126.  
  127. func find(v int, arr[] int) int {
  128.     if arr[v] == v {
  129.         return v
  130.     }
  131.     arr[v] = find(arr[v], arr)
  132.     return arr[v]
  133.  
  134. }
  135.  
  136. func unite(x, y int, arr[] int)  {
  137.     xx := find(x, arr)
  138.     yy := find(y, arr)
  139.     if xx == yy {
  140.         return
  141.     }
  142.     if xx != yy {
  143.         xx, yy = yy, xx
  144.     }
  145.     arr[xx] = yy
  146. }
  147.  
  148. func Split1(arr1[][] Graph, n, m int) (int ,[]int) {
  149.     var eq bool
  150.     M := n
  151.     pi := make([]int, n)
  152.     for i := 0; i < n; i++ {
  153.         pi[i] = i
  154.     }
  155.     for q1 := 0; q1 < n; q1++ {
  156.         for q2 := q1+1; q2 < n; q2++{
  157.             if find(q1, pi) != find(q2, pi){
  158.                 eq = true
  159.                 for x := 0; x < m; x++{
  160.                     if arr1[q1][x].InputAlph != arr1[q2][x].InputAlph {
  161.                         eq = false
  162.                         break
  163.                     }
  164.                 }
  165.                 if eq {
  166.                     unite(q1, q2, pi)
  167.                     M--
  168.                 }
  169.             }
  170.         }
  171.     }
  172.     for q := 0; q < n; q++ {
  173.         pi[q] = find(q, pi)
  174.     }
  175.     return M, pi
  176. }
  177.  
  178. func Split(arr1[][] Graph, pi[] int, n, m int) (int, []int) {
  179.     var eq bool
  180.     M := n
  181.     newPi := make([]int, n)
  182.     for i := 0; i < n; i++ {
  183.         newPi[i] = i
  184.     }
  185.     for q1 := 0; q1 < n; q1++ {
  186.         for q2 := q1 + 1; q2 < n; q2++ {
  187.             if (pi[q1] == pi[q2]) && (find(q1, newPi) != find(q2, newPi)) {
  188.                 eq = true
  189.                 for x := 0; x < m; x++ {
  190.                     w1 := arr1[q1][x].states
  191.                     w2 := arr1[q2][x].states
  192.                     if pi[w1] != pi[w2] {
  193.                         eq = false
  194.                         break
  195.                     }
  196.                 }
  197.                 if eq {
  198.                     unite(q1, q2, newPi)
  199.                     M--
  200.                 }
  201.             }
  202.         }
  203.     }
  204.     for q := 0; q < n; q++ {
  205.         pi[q] = find(q, newPi)
  206.     }
  207.     return M, pi
  208. }
  209.  
  210. func AufenkampHohn(arr1[][] Graph, n, m, qq int)  {
  211.  
  212.     M, pi := Split1(arr1, n, m)
  213.     var (
  214.         newM int
  215.         //newPi[] int
  216.         )
  217.     for  {
  218.         newM, pi = Split(arr1, pi, n, m)
  219.         if M == newM {
  220.             break
  221.         }
  222.         M = newM
  223.     }
  224.     /*count := 0
  225.     for i := 0; i < n; i++ {
  226.         if pi[i] < count {
  227.             continue
  228.         }
  229.         pi[i] = count
  230.         count++
  231.     }*/
  232.     arr := make([][] Graph, n, n)
  233.  
  234.     for i := 0; i < n; i++ {
  235.         arr[i] = make([] Graph, m)
  236.     }
  237.  
  238.     b := make([] bool, n)
  239.     for q := 0; q < n; q++ {
  240.         if newQ := pi[q]; !b[q] {
  241.             b[newQ] = true
  242.             for x := 0; x < m; x++ {
  243.                 arr[newQ][x].states = pi[arr1[q][x].states]
  244.                 arr[newQ][x].InputAlph = arr1[q][x].InputAlph
  245.             }
  246.         }
  247.     }
  248.     DFS(arr, M, m, pi[qq])
  249. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement