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