Facebook
From Diminutive Octupus, 7 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 244
  1. package main
  2. import ("fmt"
  3.         "math/rand"
  4.         )
  5. func goTriangle(c chan int, v, n int,A [][] bool){
  6. tr :=0
  7. for i :=0;i<n;i++{if A[v][i]{
  8. for k :=0;k<n;k++{if A[i][k] && A[v][k]{tr++;
  9. //fmt.Println(v,i,k)
  10. }}
  11. }
  12. }
  13. c <- tr
  14. }      
  15. func CountTriangles(n int,A [][] bool) (t int) {
  16. c :=make(chan int)
  17. for i :=0;i < n; i++ {go goTriangle(c,i,n,A)}
  18. t = 0
  19. for i :=0;i < n;i++ {
  20. t += <-c
  21. }
  22. close(c)
  23. return t/3
  24. }
  25. func losuj(n int,A [][] bool) {
  26. A[n-1][n-1]=false
  27. for i := 0; i < n-1; i++ { A[i][i]=false;
  28. for j :=i+1; j < n; j++ {
  29.  x := (rand.Float64() <= 0.5);
  30. //fmt.Printf("%d ",x);
  31. A[i][j] =x; A[j][i]=x
  32. }
  33. }
  34. }
  35.  
  36. func pokaz(n int,A [][] bool) {
  37. for i := 0; i < n; i++ {
  38. for j := 0; j < n; j++ {
  39. fmt.Printf("%d ",A[i][j]);
  40. }
  41. fmt.Println()
  42. }
  43. }
  44. func main() {
  45. YSize :=5
  46. XSize :=5
  47. picture := make([][]bool, YSize)
  48. for i := range picture {
  49.         picture[i] = make([]bool, XSize)
  50. }
  51. losuj(XSize,picture)
  52. pokaz(XSize,picture)
  53.         fmt.Println("Ilosc trójkątów w grafie: ",CountTriangles(XSize,picture))
  54. }