fork(1) download
  1. graph = {}
  2. results = {}
  3. firstline = true
  4. matrix = {}
  5.  
  6. --read the input file and create the necessary structure
  7. for line in io.lines() do
  8. if firstline then
  9. _, _, total = string.find(line, "(%d+)")
  10. total = tonumber(total)
  11. for i = 1, total do
  12. matrix[i] = {}
  13. for k = 1, total do
  14. matrix[i][k] = 0
  15. end
  16. end
  17. firstline = false
  18. else
  19. local _, _, x, y = string.find(line, "(%d+)%s(%d+)")
  20. x = tonumber(x)
  21. y = tonumber(y)
  22. table.insert(graph, {x, y})
  23. matrix[x][y] = 1
  24. matrix[y][x] = 1
  25. end
  26. end
  27.  
  28. --fill the results table
  29. for i = 1, total do
  30. results[i] = 0
  31. end
  32.  
  33. --count items
  34. for _, v in ipairs(graph) do
  35. results[v[1]] = results[v[1]] + 1
  36. results[v[2]] = results[v[2]] + 1
  37. end
  38.  
  39. --print results
  40. for i, v in ipairs(results) do
  41. print("Node " .. i .. " has a degree of " .. v)
  42. end
  43.  
  44. --print adjacency matrix
  45.  
  46. for i, v in ipairs(matrix) do
  47. local row = ""
  48. for _, k in ipairs(matrix[i]) do
  49. row = row .. k .. " "
  50. end
  51. print(row)
  52. end
  53.  
Success #stdin #stdout 0s 2840KB
stdin
16
1 2
1 3
2 3
1 4
3 4
1 5
2 5
1 6
2 6
3 6
3 7
5 7
6 7
3 8
4 8
6 8
7 8
2 9
5 9
6 9
2 10
9 10
6 11
7 11
8 11
9 11
10 11
1 12
6 12
7 12
8 12
11 12
6 13
7 13
9 13
10 13
11 13
5 14
8 14
12 14
13 14
1 15
2 15
5 15
9 15
10 15
11 15
12 15
13 15
1 16
2 16
5 16
6 16
11 16
12 16
13 16
14 16
15 16
stdout
Node 1 has a degree of 8
Node 2 has a degree of 8
Node 3 has a degree of 6
Node 4 has a degree of 3
Node 5 has a degree of 7
Node 6 has a degree of 10
Node 7 has a degree of 7
Node 8 has a degree of 7
Node 9 has a degree of 7
Node 10 has a degree of 5
Node 11 has a degree of 9
Node 12 has a degree of 8
Node 13 has a degree of 8
Node 14 has a degree of 5
Node 15 has a degree of 9
Node 16 has a degree of 9
0 1 1 1 1 1 0 0 0 0 0 1 0 0 1 1 
1 0 1 0 1 1 0 0 1 1 0 0 0 0 1 1 
1 1 0 1 0 1 1 1 0 0 0 0 0 0 0 0 
1 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 
1 1 0 0 0 0 1 0 1 0 0 0 0 1 1 1 
1 1 1 0 0 0 1 1 1 0 1 1 1 0 0 1 
0 0 1 0 1 1 0 1 0 0 1 1 1 0 0 0 
0 0 1 1 0 1 1 0 0 0 1 1 0 1 0 0 
0 1 0 0 1 1 0 0 0 1 1 0 1 0 1 0 
0 1 0 0 0 0 0 0 1 0 1 0 1 0 1 0 
0 0 0 0 0 1 1 1 1 1 0 1 1 0 1 1 
1 0 0 0 0 1 1 1 0 0 1 0 0 1 1 1 
0 0 0 0 0 1 1 0 1 1 1 0 0 1 1 1 
0 0 0 0 1 0 0 1 0 0 0 1 1 0 0 1 
1 1 0 0 1 0 0 0 1 1 1 1 1 0 0 1 
1 1 0 0 1 1 0 0 0 0 1 1 1 1 1 0